当前位置:   article > 正文

二叉树的前序、中序、后序遍历(C语言)_二叉树的先序,中序,后序遍历代码

二叉树的先序,中序,后序遍历代码

二叉树的深度遍历分为前序、中序、后序遍历,它们的区别在于打印的先后顺序,我们可以将二叉树分为一些子树,root代表子树的根节点,left代表子树的左节点,right代表子树的右节点,整体就是他们打印的顺序;例如[root] [left] [right] 就是从二叉树的根节点开始,打印root,往左子树遍历,打印节点数据,当左子树为空时,往右子树遍历、打印;

前序遍历

 整体为 [root] [left] [right]。

root是根节点,left是左树,right是右树。

前序遍历步骤:

1、先遍历根节点(root),打印数据

2、访问下一个左节点(left),重复1步骤

3、左节点为NULL时,返回上一层递归,访问右节点(right),重复1步骤

4、结束遍历

图解 

 图中数字为遍历顺序;

遍历顺序: A B D E C F G

前序代码:

  1. void preorder(struct Node* root)
  2. {
  3. if (root == NULL)
  4. {
  5. return;
  6. }
  7. printf("%d ", root->data); // 先打印数据
  8. preorder(root->left); //遍历左节点
  9. preorder(root->right); //遍历右节点
  10. }

 

中序遍历

整体为 [left] [root] [right]

中序遍历步骤:

1、先向左子树找到最后一个左节点,打印数据

2、返回上一层递归,打印数据

3、检查该层递归的节点是否有右节点

4、有则向下递归,重复上面操作

5、遍历结束

图解:

 

图中数字为遍历顺序;

遍历顺序: D B E A F C G

中序代码:

  1. void inorder(struct Node* root)
  2. {
  3. if (root == NULL)
  4. {
  5. return;
  6. }
  7. inorder(root->left);
  8. printf("%d ", root->data);
  9. inorder(root->right);
  10. }

后序遍历

整体为[left] [right] [root]

后序遍历步骤:

1、找到最后一个左节点,打印

2、返回上一层递归,检查是否有右节点,有则打印

3、打印子树的根节点,返回上一层递归,重复以上步骤

4、结束遍历

图解:

图中数字为遍历顺序;

遍历顺序: D E B F G C A

后序代码:

  1. void postorder(struct Node* root)
  2. {
  3. if (root == NULL)
  4. {
  5. return;
  6. }
  7. postorder(root->left);
  8. postorder(root->right);
  9. printf("%d ", root->data);
  10. }

完整代码:

  1. struct Node {
  2. int data;
  3. struct Node* left;
  4. struct Node* right;
  5. };
  6. struct Node* newspot(int n)
  7. {
  8. struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
  9. temp->data = n;
  10. temp->left = NULL;
  11. temp->right = NULL;
  12. return temp;
  13. }
  14. struct Node* Insert(struct Node* root, int x)
  15. {
  16. if (root == NULL)
  17. {
  18. root = newspot(x);
  19. }
  20. else if (root->data >= x)
  21. {
  22. root->left = Insert(root->left, x);
  23. }
  24. else
  25. {
  26. root->right = Insert(root->right, x);
  27. }
  28. return root;
  29. }
  30. //前序遍历二叉树
  31. void preorder(struct Node* root)
  32. {
  33. if (root == NULL)
  34. {
  35. return;
  36. }
  37. printf("%d ", root->data);
  38. preorder(root->left);
  39. preorder(root->right);
  40. }
  41. //中序遍历二叉树
  42. void inorder(struct Node* root)
  43. {
  44. if (root == NULL)
  45. {
  46. return;
  47. }
  48. inorder(root->left);
  49. printf("%d ", root->data);
  50. inorder(root->right);
  51. }
  52. //后序遍历二叉树
  53. void postorder(struct Node* root)
  54. {
  55. if (root == NULL)
  56. {
  57. return;
  58. }
  59. postorder(root->left);
  60. postorder(root->right);
  61. printf("%d ", root->data);
  62. }
  63. int main()
  64. {
  65. struct Node* root = NULL;
  66. root = Insert(root,8);
  67. root = Insert(root,9);
  68. root = Insert(root,6);
  69. root = Insert(root,3);
  70. root = Insert(root,10);
  71. root = Insert(root,7);
  72. root = Insert(root, 12);
  73. preorder(root);
  74. printf("\n");
  75. inorder(root);
  76. printf("\n");
  77. postorder(root);
  78. return 0;
  79. }

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

闽ICP备14008679号