二叉树的建立与遍历详解 菜鸟都能看懂的教程

树形结构要多利用递归来求解,递归的关键就是想清楚所有的基准情形,然后扩展到一般情况,写代码的时候最好把基准情况放在前面,把一般情况放在后面!

定义二叉树结构体:

 
  1. typedef struct BinaryTreeNode

  2. {

  3. TelemType data;

  4. struct BinaryTreeNode *Left;

  5. struct BinaryTreeNode *Right;

  6. }Node;

 

创建二叉树:

 

 
  1. Node* createBinaryTree()

  2. {

  3. Node *p;

  4. TelemType ch;

  5. cin>>ch;

  6. if(ch == 0) //如果到了叶子节点,接下来的左、右子树分别赋值为0

  7. {

  8. p = NULL;

  9. }

  10. else

  11. {

  12. p = (Node*)malloc(sizeof(Node));

  13. p->data = ch;

  14. p->Left = createBinaryTree(); //递归创建左子树

  15. p->Right = createBinaryTree(); //递归创建右子树

  16. }

  17. return p;

  18. }

 

注意:创建二叉树顺序为先中心节点,然后左子树,然后右子树,到了叶子节点后要把它的左右子树分别赋值为0
            比如二叉树为:     1

 

                                        2                         

           我们输入应为:1 2 0 0 0回车

先序遍历:

 

 
  1. void preOrderTraverse(Node* root)

  2. {

  3. if( root )

  4. {

  5. cout<<root->data<<' ';

  6. preOrderTraverse(root->Left);

  7. preOrderTraverse(root->Right);

  8. }

  9. }

 

 

中序遍历:

 

 
  1. void inOrderTraverse(Node* root)

  2. {

  3. if( root )

  4. {

  5. inOrderTraverse(root->Left);

  6. cout<<root->data<<' ';

  7. inOrderTraverse(root->Right);

  8. }

  9. }

 

后序遍历:

 

 
  1. void lastOrderTraverse(Node* root)

  2. {

  3. if( root )

  4. {

  5. lastOrderTraverse(root->Left);

  6. lastOrderTraverse(root->Right);

  7. cout<<root->data<<' ';

  8. }

  9. }

 


二叉树节点总数目:

 
  1. int Nodenum(Node* root)

  2. {

  3. if(root == NULL)

  4. {

  5. return 0;

  6. }

  7. else

  8. {

  9. return 1+Nodenum(root->Left)+Nodenum(root->Right);

  10.  
  11. }

  12. }


二叉树深度:

 
  1. int DepthOfTree(Node* root)

  2. {

  3. if(root)

  4. {

  5. return DepthOfTree(root->Left)>DepthOfTree(root->Right)?DepthOfTree(root->Left)+1:DepthOfTree(root->Right)+1;

  6. }

  7. if( root == NULL )

  8. {

  9. return 0;

  10. }

  11. }


二叉树叶子节点数:

 
  1. int Leafnum(Node* root)

  2. {

  3. if(!root)

  4. {

  5. return 0;

  6. }

  7. else if( (root->Left == NULL) && (root->Right == NULL) )

  8. {

  9. return 1;

  10. }

  11. else

  12. {

  13. return (Leafnum(root->Left) + Leafnum(root->Right)) ;

  14. }

  15. }


下面是整个程序的代码:

 

 
  1. #include <iostream>

  2. #include<cstdio>

  3. #include<cstdlib>

  4.  
  5. using namespace std;

  6.  
  7. typedef int TelemType;

  8.  
  9. typedef struct BinaryTreeNode

  10. {

  11. TelemType data;

  12. struct BinaryTreeNode *Left;

  13. struct BinaryTreeNode *Right;

  14. }Node;

  15.  
  16.  
  17. //创建二叉树,顺序依次为中间节点->左子树->右子树

  18. Node* createBinaryTree()

  19. {

  20. Node *p;

  21. TelemType ch;

  22. cin>>ch;

  23. if(ch == 0) //如果到了叶子节点,接下来的左、右子树分别赋值为0

  24. {

  25. p = NULL;

  26. }

  27. else

  28. {

  29. p = (Node*)malloc(sizeof(Node));

  30. p->data = ch;

  31. p->Left = createBinaryTree(); //递归创建左子树

  32. p->Right = createBinaryTree(); //递归创建右子树

  33. }

  34. return p;

  35. }

  36.  
  37. //先序遍历

  38. void preOrderTraverse(Node* root)

  39. {

  40. if( root )

  41. {

  42. cout<<root->data<<' ';

  43. preOrderTraverse(root->Left);

  44. preOrderTraverse(root->Right);

  45. }

  46. }

  47.  
  48. //中序遍历

  49. void inOrderTraverse(Node* root)

  50. {

  51. if( root )

  52. {

  53. inOrderTraverse(root->Left);

  54. cout<<root->data<<' ';

  55. inOrderTraverse(root->Right);

  56. }

  57. }

  58.  
  59. //后序遍历

  60. void lastOrderTraverse(Node* root)

  61. {

  62. if( root )

  63. {

  64. lastOrderTraverse(root->Left);

  65. lastOrderTraverse(root->Right);

  66. cout<<root->data<<' ';

  67. }

  68. }

  69.  
  70. //二叉树节点总数目

  71. int Nodenum(Node* root)

  72. {

  73. if(root == NULL)

  74. {

  75. return 0;

  76. }

  77. else

  78. {

  79. return 1+Nodenum(root->Left)+Nodenum(root->Right);

  80.  
  81. }

  82. }

  83.  
  84. //二叉树的深度

  85. int DepthOfTree(Node* root)

  86. {

  87. if(root)

  88. {

  89. return DepthOfTree(root->Left)>DepthOfTree(root->Right)?DepthOfTree(root->Left)+1:DepthOfTree(root->Right)+1;

  90. }

  91. if( root == NULL )

  92. {

  93. return 0;

  94. }

  95. }

  96.  
  97. //二叉树叶子节点数

  98. int Leafnum(Node* root)

  99. {

  100. if(!root)

  101. {

  102. return 0;

  103. }

  104. else if( (root->Left == NULL) && (root->Right == NULL) )

  105. {

  106. return 1;

  107. }

  108. else

  109. {

  110. return (Leafnum(root->Left) + Leafnum(root->Right)) ;

  111. }

  112. }

  113.  
  114.  
  115. int main()

  116. {

  117. Node *root = NULL;

  118. root = createBinaryTree();

  119. printf("二叉树建立成功");

  120. cout<<endl;

  121.  
  122. cout<<"二叉树总节点数为:"<<Nodenum(root)<<endl;

  123.  
  124. cout<<"二叉树深度为:"<<DepthOfTree(root)<<endl;

  125.  
  126. cout<<"二叉树叶子节点数为:"<<Leafnum(root)<<endl;

  127.  
  128. cout<<"前序遍历结果:"<<endl;

  129. preOrderTraverse(root);

  130. cout<<endl;

  131.  
  132. cout<<"中序遍历结果:"<<endl;

  133. inOrderTraverse(root);

  134. cout<<endl;

  135.  
  136. cout<<"后序遍历结果:"<<endl;

  137. lastOrderTraverse(root);

  138. cout<<endl;

  139.  
  140. return 0;

  141. }


分别输入两个二叉树来验证结果:

 

 

第一个二叉树为:

                                                                                                  
第二个二叉树为:

                                                                                                          

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值