zoukankan      html  css  js  c++  java
  • 二叉树。。。。

    复习下二叉树,创建二叉树,分别以先序,中序,后续三种遍历访问二叉树,输出二叉树的叶子节点及叶子节点的个数,并输出二叉树的高度

    [cpp] view plain copy
     
    1. #include<iostream>  
    2. #include<cstdio>  
    3. #include<cstring>  
    4. #include<cstdlib>  
    5. using namespace std;  
    6. typedef struct BiTNode  
    7. {     
    8.     char data;  
    9.     struct BiTNode *lchild,*rchild;  
    10. }BiTNode,*BiTree;  
    11. int Max(int x,int y)  
    12. {  
    13.     return x>y?x:y;  
    14. }  
    15. void Create(BiTree &T)  //先序建一颗二叉树  
    16. {  
    17.     char ch;  
    18.     scanf("%c",&ch);  
    19.     if(ch=='#')  
    20.     T=NULL;  
    21.     else  
    22.     {  
    23.         T=(BiTNode *)malloc(sizeof(BiTNode));  
    24.         T->data=ch;  
    25.         Create(T->lchild);  
    26.         Create(T->rchild);  
    27.     }  
    28. }  
    29. void Preorder(BiTree &root)  //先序遍历打印二叉树  
    30. {  
    31.     if(root!=NULL)  
    32.     {  
    33.         printf("%c ",root->data);  
    34.         Preorder(root->lchild);  
    35.         Preorder(root->rchild);  
    36.     }  
    37. }  
    38. void Inorder(BiTree &root)  //中序遍历打印二叉树  
    39. {  
    40.     if(root!=NULL)  
    41.     {  
    42.         Inorder(root->lchild);  
    43.         printf("%c ",root->data);  
    44.         Inorder(root->rchild);  
    45.     }  
    46. }  
    47. void Postorder(BiTree &root)  //后续遍历打印二叉树  
    48. {  
    49.     if(root!=NULL)  
    50.     {  
    51.         Postorder(root->lchild);  
    52.         Postorder(root->rchild);  
    53.         printf("%c ",root->data);  
    54.     }  
    55. }  
    56. void Preorderleaf(BiTree &root) //先序遍历输出叶子节点  
    57. {  
    58.     if(root!=NULL)  
    59.     {  
    60.         if(root->lchild==NULL&&root->rchild==NULL)  
    61.         printf("%c ",root->data);  
    62.         Preorderleaf(root->lchild);  
    63.         Preorderleaf(root->rchild);  
    64.     }  
    65. }  
    66. int LeafCount(BiTree &root)  //统计叶子节点的个数  
    67. {  
    68.     int leaf;  
    69.     if(root==NULL)  
    70.     leaf=0;  
    71.     else if(root->lchild==NULL&&root->rchild==NULL)  
    72.     leaf=1;  
    73.     else   
    74.     leaf=LeafCount(root->lchild)+LeafCount(root->rchild);  
    75.     return leaf;  
    76. }  
    77. int PostTreeDepth(BiTree &root)  //统计树的高度  
    78. {  
    79.     int hl,hr,max;  
    80.     if(root!=NULL)  
    81.     {  
    82.         hl=PostTreeDepth(root->lchild);  
    83.         hr=PostTreeDepth(root->rchild);  
    84.         max=Max(hl,hr);  
    85.         return max+1;  
    86.     }  
    87.     else   
    88.     return 0;  
    89. }  
    90. void dowork()  
    91. {  
    92.     BiTree cam;  
    93.     Create(cam);  
    94.     Preorder(cam);  
    95.     printf(" ");  
    96.     Inorder(cam);  
    97.     printf(" ");  
    98.     Postorder(cam);  
    99.     printf(" ");  
    100.     printf("叶子节点:");  
    101.     Preorderleaf(cam);  
    102.     printf(" ");  
    103.     printf("叶子节点的个数为:%d ",LeafCount(cam));  
    104.     printf("树的深度为:%d ",PostTreeDepth(cam));  
    105. }  
    106. int main()  
    107. {  
    108.     dowork();  
    109.     return 0;  
    110. }  


     
    1
  • 相关阅读:
    跨页传值另一种方法
    运行nodejs的blog程序遇见问题
    nodejs和mongodb实践
    mongodb数据库实践笔记
    两次分页显示内容——先少后多显示
    Java进阶4表达式中的陷阱
    Java进阶3. 内存回收机制
    Java进阶1. Synchronized 关键字
    Java复习9网路编程
    Java复习8.多线程
  • 原文地址:https://www.cnblogs.com/myhome-1/p/5267591.html
Copyright © 2011-2022 走看看