• 手撕二叉树


    前序遍历构建二叉树

    二叉树的销毁

    二叉树的结点个数

    二叉树叶子节点个数

    二叉树第k层节点个数

    二叉树查找值为x的节点

    二叉树前序遍历 

    二叉树中序遍历

    二叉树后序遍历

    二叉树的层序遍历

    判断二叉树是否是完全二叉树

    完整代码

    test.c

    1. #define _CRT_SECURE_NO_WARNINGS 1
    2. #include
    3. #include
    4. #include
    5. typedef int BTDataType;
    6. typedef struct BinaryTreeNode
    7. {
    8. struct BinaryTreeNode* left;
    9. struct BinaryTreeNode* right;
    10. BTDataType val;
    11. }BTNode;
    12. #include "Queue.h"
    13. // 通过前序遍历的数组"ABD##E#H##CF##G##"构建二叉树
    14. BTNode* BinaryTreeCreate(BTDataType* a, int* pi)
    15. {
    16. if (a[*pi] == -1)
    17. {
    18. ++(*pi);
    19. return NULL;
    20. }
    21. BTNode* root = (BTNode*)malloc(sizeof(BTNode));
    22. root->val = a[*pi];
    23. ++(*pi);
    24. root->left = BinaryTreeCreate(a, pi);
    25. root->right = BinaryTreeCreate(a, pi);
    26. return root;
    27. }
    28. //二叉树销毁
    29. void BinaryTreeDestory(BTNode** root)
    30. {
    31. if (*root == NULL)
    32. {
    33. return;
    34. }
    35. BinaryTreeDestory(&(*root)->left);
    36. BinaryTreeDestory(&(*root)->right);
    37. free(*root);
    38. *root = NULL;
    39. }
    40. // 二叉树节点个数
    41. int BinaryTreeSize(BTNode* root)
    42. {
    43. if (root == NULL)
    44. {
    45. return 0;
    46. }
    47. return BinaryTreeSize(root->left) + BinaryTreeSize(root->right) + 1;
    48. }
    49. // 二叉树叶子节点个数
    50. int BinaryTreeLeafSize(BTNode* root)
    51. {
    52. if (root == NULL)
    53. {
    54. return 0;
    55. }
    56. if (root->left == NULL && root->right == NULL)
    57. {
    58. return 1;
    59. }
    60. return BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(root->right);
    61. }
    62. // 二叉树第k层节点个数
    63. int BinaryTreeLevelKSize(BTNode* root, int k)
    64. {
    65. if (root == NULL)
    66. {
    67. return 0;
    68. }
    69. if (k == 1)
    70. {
    71. return 1;
    72. }
    73. return BinaryTreeLevelKSize(root->left, k-1) + BinaryTreeLevelKSize(root->right, k-1);
    74. }
    75. // 二叉树前序遍历
    76. void BinaryTreePrevOrder(BTNode* root)
    77. {
    78. if (root == NULL)
    79. {
    80. return;
    81. }
    82. printf("%d ", root->val);
    83. BinaryTreePrevOrder(root->left);
    84. BinaryTreePrevOrder(root->right);
    85. }
    86. // 二叉树查找值为x的节点
    87. BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
    88. {
    89. if (root == NULL)
    90. {
    91. return NULL;
    92. }
    93. if (root->val == x)
    94. {
    95. return root;
    96. }
    97. BTNode* ret = NULL;
    98. ret = BinaryTreeFind(root->left, x);
    99. if (ret)
    100. {
    101. return ret;
    102. }
    103. ret = BinaryTreeFind(root->right, x);
    104. if (ret)
    105. {
    106. return ret;
    107. }
    108. return NULL;
    109. }
    110. // 二叉树中序遍历
    111. void BinaryTreeInOrder(BTNode* root)
    112. {
    113. if (root == NULL)
    114. {
    115. return;
    116. }
    117. BinaryTreeInOrder(root->left);
    118. printf("%d ", root->val);
    119. BinaryTreeInOrder(root->right);
    120. }
    121. // 二叉树后序遍历
    122. void BinaryTreePostOrder(BTNode* root)
    123. {
    124. if (root == NULL)
    125. {
    126. return;
    127. }
    128. BinaryTreePostOrder(root->left);
    129. BinaryTreePostOrder(root->right);
    130. printf("%d ", root->val);
    131. }
    132. // 层序遍历
    133. void BinaryTreeLevelOrder(BTNode* root)
    134. {
    135. Que q;
    136. QueueInit(&q);
    137. if (root)
    138. {
    139. QueuePush(&q, root);
    140. }
    141. while (!QueueEmpty(&q))
    142. {
    143. BTNode* front = QueueFront(&q);
    144. if (front->left)
    145. {
    146. QueuePush(&q, front->left);
    147. }
    148. if (front->right)
    149. {
    150. QueuePush(&q, front->right);
    151. }
    152. QueuePop(&q);
    153. printf("%d ", front->val);
    154. }
    155. QueueDestroy(&q);
    156. }
    157. // 判断二叉树是否是完全二叉树
    158. bool BinaryTreeComplete(BTNode* root)
    159. {
    160. Que q;
    161. QueueInit(&q);
    162. if (root)
    163. {
    164. QueuePush(&q, root);
    165. }
    166. while (!QueueEmpty(&q))
    167. {
    168. BTNode* front = QueueFront(&q);
    169. if (front == NULL)
    170. {
    171. break;
    172. }
    173. QueuePush(&q, front->left);
    174. QueuePush(&q, front->right);
    175. QueuePop(&q);
    176. }
    177. while (!QueueEmpty(&q))
    178. {
    179. BTNode* front = QueueFront(&q);
    180. QueuePop(&q);
    181. if (front != NULL)
    182. {
    183. QueueDestroy(&q);
    184. return false;
    185. }
    186. }
    187. QueueDestroy(&q);
    188. return true;
    189. }
    190. int main()
    191. {
    192. int a[] = { 1,3,5,-1,-1,2,-1,-1,1,-1,1,-1,-1 };
    193. int j = 0;
    194. BTNode* root=BinaryTreeCreate(a, &j);
    195. bool x = BinaryTreeComplete(root);
    196. printf("%d", x);
    197. return 0;
    198. }

    Queue.h

    1. #pragma once
    2. #include
    3. #include
    4. #include
    5. #include
    6. typedef struct BinaryTreeNode* QDataType;
    7. typedef struct QueueNode
    8. {
    9. struct QueueNode* next;
    10. QDataType data;
    11. }QNode;
    12. typedef struct Queue
    13. {
    14. QNode* head;
    15. QNode* tail;
    16. int size;
    17. }Que;
    18. void QueueInit(Que* pq);
    19. void QueueDestroy(Que* pq);
    20. void QueuePush(Que* pq, QDataType x);
    21. void QueuePop(Que* pq);
    22. QDataType QueueFront(Que* pq);
    23. QDataType QueueBack(Que* pq);
    24. bool QueueEmpty(Que* pq);
    25. int QueueSize(Que* pq);

    Queue.c

    1. #define _CRT_SECURE_NO_WARNINGS 1
    2. #include "Queue.h"
    3. void QueueInit(Que* pq)
    4. {
    5. assert(pq);
    6. pq->head = pq->tail = NULL;
    7. pq->size = 0;
    8. }
    9. void QueueDestroy(Que* pq)
    10. {
    11. assert(pq);
    12. QNode* cur = pq->head;
    13. while (cur)
    14. {
    15. QNode* next = cur->next;
    16. free(cur);
    17. cur = next;
    18. }
    19. pq->head = pq->tail = NULL;
    20. pq->size = 0;
    21. }
    22. void QueuePush(Que* pq, QDataType x)
    23. {
    24. assert(pq);
    25. QNode* newnode = (QNode*)malloc(sizeof(QNode));
    26. if (newnode == NULL)
    27. {
    28. perror("malloc fail");
    29. exit(-1);
    30. }
    31. newnode->data = x;
    32. newnode->next = NULL;
    33. if (pq->tail == NULL)
    34. {
    35. pq->head = pq->tail = newnode;
    36. }
    37. else
    38. {
    39. pq->tail->next = newnode;
    40. pq->tail = newnode;
    41. }
    42. pq->size++;
    43. }
    44. void QueuePop(Que* pq)
    45. {
    46. assert(pq);
    47. assert(!QueueEmpty(pq));
    48. if (pq->head->next == NULL)
    49. {
    50. free(pq->head);
    51. pq->head = pq->tail = NULL;
    52. }
    53. else
    54. {
    55. QNode* next = pq->head->next;
    56. free(pq->head);
    57. pq->head = next;
    58. }
    59. pq->size--;
    60. }
    61. QDataType QueueFront(Que* pq)
    62. {
    63. assert(pq);
    64. assert(!QueueEmpty(pq));
    65. return pq->head->data;
    66. }
    67. QDataType QueueBack(Que* pq)
    68. {
    69. assert(pq);
    70. assert(!QueueEmpty(pq));
    71. return pq->tail->data;
    72. }
    73. bool QueueEmpty(Que* pq)
    74. {
    75. assert(pq);
    76. return pq->head == NULL;
    77. }
    78. int QueueSize(Que* pq)
    79. {
    80. assert(pq);
    81. return pq->size;
    82. }

  • 相关阅读:
    【Python机器学习】零基础掌握AffinityPropagation聚类
    PyTorch深度学习原理与实现
    机器视觉方案工程师,价值远不止于此
    python爬虫设计实验
    【2022版】基于矩阵分解的PCA 白化&ZCA白化
    优先队列---堆
    ArrayBlockingQueue
    BLE抓包调试信息分析
    OpenLDAP基本概念、部署讲解以及和zabbix的对接实验(基于OpenEuler和CentOS-Stream 9)
    c++模板
  • 原文地址:https://blog.csdn.net/m0_61381297/article/details/133107449