当前位置:   article > 正文

[手撕数据结构] 二叉树及其基本操作_二叉树的基本操作数据结构

二叉树的基本操作数据结构

话不多说 直接上代码 

复制可直接运行

  1. #include<math.h>
  2. #include<stdlib.h>
  3. #include<string>
  4. #include<stdbool.h>
  5. #include<iostream>
  6. #include<algorithm>
  7. using namespace std;
  8. typedef struct TreeNode
  9. {
  10. char data;
  11. struct TreeNode* left;
  12. struct TreeNode* right;
  13. }Tree;
  14. int EmptyTreeNULL(Tree* root)
  15. {
  16. if (root->left == NULL && root->right == NULL)
  17. {
  18. return 1;
  19. }
  20. else
  21. return 0;
  22. }
  23. Tree* creatTeNode(char x)//创建结点
  24. {
  25. Tree* newNode = (Tree*)malloc(sizeof(Tree));
  26. if (newNode == NULL)
  27. {
  28. cout << "创建结点失败" << endl;
  29. free(newNode);
  30. return 0;
  31. }
  32. newNode->data = x;
  33. newNode->left = NULL;
  34. newNode->right = NULL;
  35. return newNode;
  36. }
  37. void Preorder_Traversal(Tree* root)//先序遍历 递归
  38. {
  39. if (root == NULL)
  40. {
  41. return;
  42. }
  43. cout << root->data << " ";
  44. Preorder_Traversal(root->left);
  45. Preorder_Traversal(root->right);
  46. }
  47. void Inorder_Traversal(Tree* root)//中序遍历 递归
  48. {
  49. if (root == NULL)
  50. {
  51. return;
  52. }
  53. Inorder_Traversal(root->left);
  54. cout << root->data << " ";
  55. Inorder_Traversal(root->right);
  56. }
  57. void Postorder_Traversal(Tree* root)//后续遍历 递归
  58. {
  59. if (root == NULL)
  60. {
  61. return;
  62. }
  63. Postorder_Traversal(root->left);
  64. Postorder_Traversal(root->right);
  65. cout << root->data << " ";
  66. }
  67. void Preorder_Traversal_UseStack(Tree* root)//先序遍历 迭代
  68. {
  69. Tree** stack = (Tree**)malloc(sizeof(Tree) * 100);
  70. if (stack == NULL)
  71. {
  72. cout << "储存栈建立失败" << endl;
  73. free(stack);
  74. return;
  75. }
  76. int top = 0;
  77. while (root != NULL || top != 0)
  78. {
  79. while (root != NULL)
  80. {
  81. cout << root->data << " ";
  82. stack[top++] = root;
  83. root = root->left;
  84. }
  85. root = stack[--top];
  86. root = root->right;
  87. }
  88. free(stack);
  89. cout << endl;
  90. }
  91. void Inorder_Traversal_UseStack(Tree* root)//中序遍历 迭代
  92. {
  93. Tree** stack = (Tree**)malloc(sizeof(Tree) * 100);
  94. if (stack == NULL)
  95. {
  96. cout << "储存栈建立失败" << endl;
  97. free(stack);
  98. return;
  99. }
  100. int top = 0;
  101. while (root != NULL || top != 0)
  102. {
  103. while (root != NULL)
  104. {
  105. stack[top++] = root;
  106. root = root->left;
  107. }
  108. root = stack[--top];
  109. cout << root->data << " ";
  110. root = root->right;
  111. }
  112. free(stack);
  113. cout << endl;
  114. }
  115. void Postorder_Traversal_UseStack(Tree* root)//后序遍历 迭代
  116. {
  117. Tree** stack = (Tree**)malloc(sizeof(Tree) * 100);
  118. if (stack == NULL)
  119. {
  120. cout << "储存栈建立失败" << endl;
  121. free(stack);
  122. return;
  123. }
  124. Tree* visitNode = root;//保存上一个访问(打印)过的结点
  125. int top = 0;
  126. while (root != NULL || top != 0)
  127. {
  128. while (root != NULL)
  129. {
  130. stack[top++] = root;
  131. root = root->left;//先访问左子树
  132. }
  133. root = stack[top - 1];//保存上一个结点
  134. if (root->right != NULL && root->right != visitNode)//如果最左边的叶子结点的上一个结点有右子树且没有被访问
  135. {
  136. root = root->right;//访问右子树
  137. }
  138. else//如果没有右子树或者已经被访问则输出
  139. {
  140. cout << root->data << " ";
  141. visitNode = root;
  142. root = NULL;
  143. top--;//出栈
  144. }
  145. }
  146. //如果复习时候忘记流程 上B站look https://www.bilibili.com/video/BV18i4y1Z7am/?spm_id_from=333.788.recommend_more_video.0
  147. }
  148. void Sequence_Traversal(Tree* root)//层序遍历
  149. {
  150. Tree* Queue[100];
  151. int head = 0;
  152. int tail = 0;
  153. Queue[tail++] = root;
  154. while (tail != head)
  155. {
  156. if (root->left != NULL)
  157. Queue[tail++] = root->left;
  158. if (root->right != NULL)
  159. Queue[tail++] = root->right;
  160. cout << root->data << " ";
  161. root = Queue[++head];
  162. }
  163. cout << endl;
  164. }
  165. void Print_Leaf(Tree* root)//输出叶子结点
  166. {
  167. cout << "叶子结点:";
  168. if (root == NULL)
  169. {
  170. cout << "空树" << endl;
  171. return;
  172. }
  173. Tree** stack = (Tree**)malloc(sizeof(Tree) * 100);
  174. if (stack == NULL)
  175. {
  176. cout << "储存栈建立失败" << endl;
  177. free(stack);
  178. return;
  179. }
  180. int top = 0;
  181. while (root != NULL || top != 0)
  182. {
  183. while (root != NULL)
  184. {
  185. if (root->left == NULL && root->right == NULL)
  186. {
  187. cout << root->data << " ";
  188. }
  189. stack[top++] = root;
  190. root = root->left;
  191. }
  192. root = stack[--top];
  193. root = root->right;
  194. }
  195. free(stack);
  196. cout << endl;
  197. }
  198. int Get_Tree_high(Tree* root)//获取二叉树高度
  199. {
  200. if (root == NULL)
  201. return 0;
  202. return max(1 + Get_Tree_high(root->left), 1 + Get_Tree_high(root->right));
  203. }
  204. void InsertTree(Tree* parents, Tree* left, Tree* right)//插入
  205. {
  206. parents->left = left;
  207. parents->right = right;
  208. }
  209. int main()
  210. {
  211. Tree* A = creatTeNode('A');
  212. Tree* B = creatTeNode('B');
  213. Tree* C = creatTeNode('C');
  214. Tree* D = creatTeNode('D');
  215. Tree* E = creatTeNode('E');
  216. Tree* F = creatTeNode('F');
  217. Tree* G = creatTeNode('G');
  218. InsertTree(A, B, C);
  219. InsertTree(B, D, E);
  220. InsertTree(C, F, G);
  221. cout << "先序遍历" << endl;
  222. Preorder_Traversal(A);
  223. cout << endl;
  224. Preorder_Traversal_UseStack(A);
  225. cout << "中序遍历" << endl;
  226. Inorder_Traversal(A);
  227. cout << endl;
  228. Inorder_Traversal_UseStack(A);
  229. cout << "后序遍历" << endl;
  230. Postorder_Traversal(A);
  231. cout << endl;
  232. Postorder_Traversal_UseStack(A);
  233. cout << endl;
  234. cout << "层序遍历" << endl;
  235. Sequence_Traversal(A);
  236. Print_Leaf(A);
  237. cout << "二叉树高度:" << Get_Tree_high(A)<<endl;
  238. system("pause");
  239. return 0;
  240. }

                                                          效果如下

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/999666
推荐阅读
相关标签
  

闽ICP备14008679号