[关闭]
@Archger 2016-07-10T15:06:58.000000Z 字数 6091 阅读 790

二叉树遍历&构建

ACM 二叉树


二叉树的遍历分为三类:前序遍历、中序遍历和后序遍历。

photo

(1)前序遍历

先访问根节点,再遍历左子树,最后遍历右子树;并且在遍历左右子树时,仍需先遍历左子树,然后访问根节点,最后遍历右子树。上图的前序遍历如下。
photo

(2)中序遍历

先遍历左子树、然后访问根节点,最后遍历右子树;并且在遍历左右子树的时候。仍然是先遍历左子树,然后访问根节点,最后遍历右子树。前图的中序遍历如下。

photo

(3)后序遍历

先遍历左子树,然后遍历右子树,最后访问根节点;同样,在遍历左右子树的时候同样要先遍历左子树,然后遍历右子树,最后访问根节点。前图后序遍历结果如下。

photo

(4)层序遍历

用BFS每一层的向下遍历。


代码实现

二叉树的构建、查找、删除

  1. #include<iostream>
  2. #include<string>
  3. #include<string.h>
  4. #include<algorithm>
  5. #include<iomanip>
  6. #include<cstdio>
  7. #include<queue>
  8. #include<stack>
  9. using namespace std;
  10. struct node {
  11. int val;
  12. node *lch, *rch;
  13. };
  14. //插入数值
  15. node *insert(node *p, int x)
  16. {
  17. if (p == NULL)
  18. {
  19. node *q = new node;
  20. q->val = x;
  21. q->lch = q->rch = NULL;
  22. return q;
  23. }
  24. else
  25. {
  26. if (x < p->val) p->lch = insert(p->lch, x);
  27. else p->rch = insert(p->rch, x);
  28. return p;
  29. }
  30. }
  31. //查找数值
  32. bool find(node *p, int x)
  33. {
  34. if (p == NULL) return false;
  35. else if (x == p->val) return true;
  36. else if (x < p->val) return find(p->lch, x);
  37. else return find(p->rch, x);
  38. }
  39. //删除数值
  40. node *remove(node *p, int x)
  41. {
  42. if (p == NULL) return NULL;
  43. else if (x < p->val) p->lch = remove(p->lch, x);
  44. else if (x > p->val) p->rch = remove(p->rch, x);
  45. else if (p->lch == NULL)
  46. {
  47. node *q = p->rch;
  48. delete p;
  49. return q;
  50. }
  51. else if (p->lch->rch == NULL)
  52. {
  53. node *q = p->lch;
  54. q->rch = p->rch;
  55. delete p;
  56. return q;
  57. }
  58. else
  59. {
  60. node *q;
  61. for (q = p->rch; q->rch->rch != NULL; q = q->rch);
  62. node *r = q->rch;
  63. q->rch = r->lch;
  64. r->lch = p->lch;
  65. r->rch = p->rch;
  66. delete p;
  67. return r;
  68. }
  69. return p; //没有找到p点
  70. }
  71. int main()
  72. {
  73. node *head = NULL;
  74. int n;
  75. cout << "1: insert 2: find 3: remove\n";
  76. while (cin >> n)
  77. {
  78. int x;
  79. if (n == 1)
  80. {
  81. cin >> x;
  82. head = insert(head, x);
  83. }
  84. else if (n == 2)
  85. {
  86. cin >> x;
  87. if (find(head, x))
  88. {
  89. cout << "Find it!\n";
  90. }
  91. else
  92. {
  93. cout << "Not find!\n";
  94. }
  95. }
  96. else
  97. {
  98. cin >> x;
  99. head = remove(head, x);
  100. }
  101. }
  102. }

非递归遍历

先序遍历

  1. void PreOrder2(node *p)
  2. {
  3. stack<node*> stack;
  4. while (p || stack.size())
  5. {
  6. if (p)
  7. {
  8. stack.push(p);
  9. cout << p->val<<" ";
  10. p = p->lch;
  11. }
  12. else
  13. {
  14. p = stack.top();
  15. stack.pop();
  16. p = p->rch;
  17. }
  18. }
  19. }

中序遍历

  1. void InOrder2(node *p)
  2. {
  3. stack<node*> stack;
  4. while (p || stack.size())
  5. {
  6. if (p)
  7. {
  8. stack.push(p);
  9. p = p->lch;
  10. }
  11. else
  12. {
  13. p = stack.top();
  14. stack.pop();
  15. cout << p->val << " ";
  16. p = p->rch;
  17. }
  18. }
  19. }

后序遍历

  1. void PostOrder2(node *p)
  2. {
  3. stack<node*> stack;
  4. while (p || stack.size())
  5. {
  6. while (p)
  7. {
  8. p->left = 1;
  9. p = p->lch;
  10. stack.push(p);
  11. }
  12. while ((stack.top()->left) && (stack.top()->right) && stack.size())
  13. {
  14. p = stack.top();
  15. stack.pop();
  16. cout << p->val << " ";
  17. }
  18. if (stack.size())
  19. {
  20. p->right = 1;
  21. p = p->rch;
  22. }
  23. }
  24. }

递归遍历

  1. void view(node *p)
  2. {
  3. if (p)
  4. {
  5. cout << p->val << " ";
  6. }
  7. }

先序遍历

  1. void PreOrder(node *p)
  2. {
  3. if (p)
  4. {
  5. view(p);
  6. PreOrder(p->lch);
  7. PreOrder(p->rch);
  8. }
  9. }

中序遍历

  1. void InOrder(node *p)
  2. {
  3. if (p)
  4. {
  5. InOrder(p->lch);
  6. view(p);
  7. InOrder(p->rch);
  8. }
  9. }

后序遍历

  1. void PostOrder(node *p)
  2. {
  3. if (p)
  4. {
  5. PostOrder(p->lch);
  6. PostOrder(p->rch);
  7. view(p);
  8. }
  9. }

层序遍历

  1. void LevelOrder(node *p)
  2. {
  3. queue<node*> queue;
  4. queue.push(p);
  5. while (queue.size())
  6. {
  7. p = queue.front();
  8. queue.pop();
  9. cout << p->val << " ";
  10. if (p->lch) queue.push(p->lch);
  11. if (p->rch) queue.push(p->rch);
  12. }
  13. }

整体代码测试

  1. #include<iostream>
  2. #include<string>
  3. #include<string.h>
  4. #include<algorithm>
  5. #include<iomanip>
  6. #include<cstdio>
  7. #include<queue>
  8. #include<stack>
  9. using namespace std;
  10. struct node {
  11. int val;
  12. node *lch, *rch;
  13. bool left, right;
  14. };
  15. //插入数值
  16. node *insert(node *p, int x)
  17. {
  18. if (p == NULL)
  19. {
  20. node *q = new node;
  21. q->val = x;
  22. q->lch = q->rch = NULL;
  23. return q;
  24. }
  25. else
  26. {
  27. if (x < p->val) p->lch = insert(p->lch, x);
  28. else p->rch = insert(p->rch, x);
  29. return p;
  30. }
  31. }
  32. //查找数值
  33. bool find(node *p, int x)
  34. {
  35. if (p == NULL) return false;
  36. else if (x == p->val) return true;
  37. else if (x < p->val) return find(p->lch, x);
  38. else return find(p->rch, x);
  39. }
  40. //删除数值
  41. node *remove(node *p, int x)
  42. {
  43. if (p == NULL) return NULL;
  44. else if (x < p->val) p->lch = remove(p->lch, x);
  45. else if (x > p->val) p->rch = remove(p->rch, x);
  46. else if (p->lch == NULL)
  47. {
  48. node *q = p->rch;
  49. delete p;
  50. return q;
  51. }
  52. else if (p->lch->rch == NULL)
  53. {
  54. node *q = p->lch;
  55. q->rch = p->rch;
  56. delete p;
  57. return q;
  58. }
  59. else
  60. {
  61. node *q;
  62. for (q = p->rch; q->rch->rch != NULL; q = q->rch);
  63. node *r = q->rch;
  64. q->rch = r->lch;
  65. r->lch = p->lch;
  66. r->rch = p->rch;
  67. delete p;
  68. return r;
  69. }
  70. return p; //没有找到p点
  71. }
  72. void view(node *p)
  73. {
  74. if (p)
  75. {
  76. cout << p->val << " ";
  77. }
  78. }
  79. void PreOrder(node *p)
  80. {
  81. if (p)
  82. {
  83. view(p);
  84. PreOrder(p->lch);
  85. PreOrder(p->rch);
  86. }
  87. }
  88. void InOrder(node *p)
  89. {
  90. if (p)
  91. {
  92. InOrder(p->lch);
  93. view(p);
  94. InOrder(p->rch);
  95. }
  96. }
  97. void PostOrder(node *p)
  98. {
  99. if (p)
  100. {
  101. PostOrder(p->lch);
  102. PostOrder(p->rch);
  103. view(p);
  104. }
  105. }
  106. void PreOrder2(node *p)
  107. {
  108. stack<node*> stack;
  109. while (p || stack.size())
  110. {
  111. if (p)
  112. {
  113. stack.push(p);
  114. cout << p->val<<" ";
  115. p = p->lch;
  116. }
  117. else
  118. {
  119. p = stack.top();
  120. stack.pop();
  121. p = p->rch;
  122. }
  123. }
  124. }
  125. void InOrder2(node *p)
  126. {
  127. stack<node*> stack;
  128. while (p || stack.size())
  129. {
  130. if (p)
  131. {
  132. stack.push(p);
  133. p = p->lch;
  134. }
  135. else
  136. {
  137. p = stack.top();
  138. stack.pop();
  139. cout << p->val << " ";
  140. p = p->rch;
  141. }
  142. }
  143. }
  144. void PostOrder2(node *p)
  145. {
  146. stack<node*> stack;
  147. while (p || stack.size())
  148. {
  149. while (p)
  150. {
  151. p->left = 1;
  152. p = p->lch;
  153. stack.push(p);
  154. }
  155. while ((stack.top()->left) && (stack.top()->right) && stack.size())
  156. {
  157. p = stack.top();
  158. stack.pop();
  159. cout << p->val << " ";
  160. }
  161. if (stack.size())
  162. {
  163. p->right = 1;
  164. p = p->rch;
  165. }
  166. }
  167. }
  168. void LevelOrder(node *p)
  169. {
  170. queue<node*> queue;
  171. queue.push(p);
  172. while (queue.size())
  173. {
  174. p = queue.front();
  175. queue.pop();
  176. cout << p->val << " ";
  177. if (p->lch) queue.push(p->lch);
  178. if (p->rch) queue.push(p->rch);
  179. }
  180. }
  181. int main()
  182. {
  183. node *head = NULL;
  184. int n;
  185. cout << "1: insert 2: find 3: remove\n";
  186. cout << "4: PreOrder 5: InOrder 6: PostOrder\n";
  187. cout << "7: PreOrder2 8: InOrder2 9: PostOrder2\n";
  188. cout << "10: LevelOrder\n";
  189. while (cin >> n)
  190. {
  191. int x;
  192. if (n == 1)
  193. {
  194. cin >> x;
  195. head = insert(head, x);
  196. }
  197. else if (n == 2)
  198. {
  199. cin >> x;
  200. if (find(head, x))
  201. {
  202. cout << "Find it!\n";
  203. }
  204. else
  205. {
  206. cout << "Not find!\n";
  207. }
  208. }
  209. else if (n == 3)
  210. {
  211. cin >> x;
  212. head = remove(head, x);
  213. }
  214. else if (n == 4)
  215. {
  216. PreOrder(head);
  217. cout << endl;
  218. }
  219. else if (n == 5)
  220. {
  221. InOrder(head);
  222. cout << endl;
  223. }
  224. else if (n == 6)
  225. {
  226. PostOrder(head);
  227. cout << endl;
  228. }
  229. else if (n == 7)
  230. {
  231. PreOrder2(head);
  232. cout << endl;
  233. }
  234. else if (n == 8)
  235. {
  236. InOrder2(head);
  237. cout << endl;
  238. }
  239. else if (n == 9)
  240. {
  241. PostOrder(head);
  242. cout << endl;
  243. }
  244. else if (n == 10)
  245. {
  246. LevelOrder(head);
  247. cout << endl;
  248. }
  249. }
  250. }

其他详解

CSDN中关于二叉树遍历详解


练习


PAT 1020. Tree Traversals (25)
题解:

  1. #include<iostream>
  2. #include<string>
  3. #include<string.h>
  4. #include<algorithm>
  5. #include<iomanip>
  6. #include<cstdio>
  7. #include<queue>
  8. #include<stack>
  9. using namespace std;
  10. struct node
  11. {
  12. long long val;
  13. node *lch, *rch;
  14. };
  15. node *head = NULL;
  16. long long n, post[31], in[31], num;
  17. bool book[31];
  18. node* solve(int x, int y, node* p)
  19. {
  20. if (x > y||x==0||y==0) return NULL;
  21. p = new node;
  22. p->val = post[y];
  23. p->lch = NULL;
  24. p->rch = NULL;
  25. int left = 0, right = 0, s = 0, t = 0, cur = 0;
  26. for (int i = 1; i <= n; i++)
  27. {
  28. if (in[i] == post[y])
  29. {
  30. book[i] = 1;
  31. s = i;
  32. cur = i;
  33. t = i;
  34. break;
  35. }
  36. }
  37. while (t - 1 >= 1 && !book[t - 1]) t--;
  38. while (s + 1 <= n && !book[s + 1]) s++;
  39. left = cur - t;
  40. right = s - cur;
  41. p->lch = solve(x, x + left - 1, p->lch);
  42. p->rch = solve(y - right, y - 1, p->rch);
  43. return p;
  44. }
  45. void LevelOrder(node *p)
  46. {
  47. queue<node*> queue;
  48. queue.push(p);
  49. while (queue.size())
  50. {
  51. p = queue.front();
  52. queue.pop();
  53. cout << p->val;
  54. num++;
  55. if (num != n) cout << " ";
  56. if (p->lch) queue.push(p->lch);
  57. if (p->rch) queue.push(p->rch);
  58. }
  59. }
  60. int main()
  61. {
  62. while (cin >> n)
  63. {
  64. num = 0;
  65. memset(book, 0, sizeof(book));
  66. if (n <= 0) return 0;
  67. for (int i = 1; i <= n; i++)
  68. {
  69. cin >> post[i];
  70. }
  71. for (int i = 1; i <= n; i++)
  72. {
  73. cin >> in[i];
  74. }
  75. head = solve(1, n, head);
  76. LevelOrder(head);
  77. cout << endl;
  78. }
  79. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注