二叉树

[cpp]  view plain copy
  1. #include <iostream.h>   
  2. #include <iomanip.h>   
  3. #include <strstrea.h>   
  4. #include <stdlib.h>   
  5. #include <stdio.h>   
  6. //定义ElemType为char   
  7. typedef char ElemType;   
  8. //单链表中结点的类型   
  9. typedef struct BiTNode  
  10. {   
  11.     ElemType data;    // 值域   
  12.     struct BiTNode *Lchild;  // 左孩子域   
  13.     struct BiTNode *Rchild; // 右孩子域   
  14. }BiTNode, *BiTree;   
  15. //初始化二叉树   
  16. void InitBiTree(BiTree & BT);   
  17. //根据输入的二叉树的先根遍历序列字符串建立对应的二叉树存储结构   
  18. void CreateBiTree(BiTree & BT );   
  19. //判断二叉树是否为空   
  20. bool BiTreeEmpty(BiTree BT);   
  21. //按某种遍历次序输出二叉树中的所有结点   
  22. void PreOrder(BiTree BT);   
  23. //中序   
  24. void InOrder(BiTree BT);   
  25. //后序   
  26. void PostOrder(BiTree BT);   
  27. //层次void LeverOrder(BiTree BT);   
  28. //求二叉树的深度   
  29. int BiTreeDepth(BiTree BT);   
  30. //求二叉树中所有结点数   
  31. void BiTreeCount(BiTree BT);   
  32. //求二叉树中所有叶子结点数   
  33. void BiTreeLeafCount(BiTree BT);   
  34. //按照二叉树的一种表示方法输出整个二叉树   
  35. void PrintBiTree(BiTree BT);   
  36. //清除二叉树,使之变为一棵空树   
  37. void ClearBiTree(BiTree & BT);   
  38. int count=0,Leafcount=0;   
  39. int m,n;   
  40.   
  41.   
  42. void main()   
  43. {   
  44.     BiTree  bt;   
  45.     char flag='y';   
  46.     while(flag=='y')   
  47.     {   
  48.         //初始化二叉树   
  49.         InitBiTree(bt);   
  50.         cout <<"请输入字符' '作为空结点的二叉树先根表示字符串:" <<endl;   
  51.         //建立以bt作为树根指针的二叉树链接存储结构   
  52.         CreateBiTree(bt);   
  53.         //以广义表形式输出二叉树   
  54.         cout <<"广义表:";   
  55.         PrintBiTree(bt);   
  56.         cout <<endl;   
  57.         //先序遍历二叉树bt   
  58.         cout <<"先序:";   
  59.         PreOrder(bt) ;   
  60.         cout <<endl;   
  61.         //中序遍历二叉树bt   
  62.         cout <<"中序:";   
  63.         InOrder(bt); cout <<endl;   
  64.         //后序遍历二叉树bt   
  65.         cout <<"后序:";   
  66.         PostOrder(bt); cout <<endl;   
  67.         /*按层遍历二叉树bt  
  68.         cout <<"按层:";  
  69.         LeverOrder(bt); cout <<endl;*/   
  70.         //求二叉树bt的深度   
  71.         m=BiTreeDepth(bt);   
  72.         printf("该二叉树有%d层:",m);   
  73.         //求二叉树bt中所有结点数   
  74.         cout <<"二叉树中所有结点数为:";   
  75.         BiTreeCount(bt);   
  76.         cout <<count <<endl;   
  77.         //求二叉树中所有叶子结点数   
  78.         cout <<"二叉树中所有叶子结点数为:";   
  79.         BiTreeCount(bt);cout <<Leafcount <<endl;   
  80.         //清除二叉树,使之变为一棵空树   
  81.         ClearBiTree(bt);   
  82.         cout <<"二叉树已经清除!" <<endl;   
  83.           
  84.         cout <<"要否继续实验(y/n)?" <<endl;   
  85.         cin >>flag;   
  86.         if (flag!='y')   
  87.             cout <<"再见!" <<endl;      
  88.     }   
  89. }   
  90.   
  91.   
  92. //根据输入的二叉树的先根遍历序列字符串建立对应的二叉树存储结构   
  93. void CreateBiTree(BiTree &bt)   
  94. {   
  95.     char ch;   
  96.     cin>>ch;   
  97.     ch=getchar();   
  98.     if(ch==' ')   
  99.         bt=NULL;   
  100.     else   
  101.     {   
  102.         bt=new BiTNode;   
  103.         bt->data=ch;   
  104.         CreateBiTree(bt->Lchild);   
  105.         CreateBiTree(bt->Rchild);   
  106.     }   
  107. }   
  108.   
  109.   
  110. //初始化   
  111. void InitBiTree(BiTree &bt)   
  112. {   
  113.     bt=NULL;   
  114. }   
  115.   
  116. //判断二叉树是否为空   
  117. bool BiTreeEmpty(BiTree bt)   
  118. {   
  119.     if(bt!=NULL)   
  120.         return true;   
  121.     else   
  122.         return false;   
  123. }   
  124.   
  125.   
  126. //按某种遍历次序输出二叉树中的所有结点   
  127. void TraverseBiTree(BiTree bt)   
  128. {   
  129.     if(!bt)   
  130.     {   
  131.         printf("%3c",bt->data);   
  132.         TraverseBiTree(bt->Lchild);   
  133.         TraverseBiTree(bt->Rchild);   
  134.     }   
  135. }   
  136.   
  137. //求二叉树的深度   
  138. int BiTreeDepth(BiTree bt)   
  139. {   
  140.     int hl=0,hr=0,max=0;   
  141.     if(bt=!NULL)   
  142.     {   
  143.         hl=BiTreeDepth(bt->Lchild);   
  144.         hr=BiTreeDepth(bt->Rchild);   
  145.         max=hl>hr?hl:hr;   
  146.         return(max+1);   
  147.     }   
  148.     else   
  149.         return(0);   
  150. }   
  151.   
  152.   
  153. //求二叉树中所有结点数   
  154. void BiTreeCount(BiTree bt)   
  155. {   
  156.     if(bt)   
  157.         count++;   
  158.     BiTreeCount(bt->Lchild);   
  159.     BiTreeCount(bt->Rchild);   
  160. }   
  161.   
  162.   
  163. //求二叉树中所有叶子结点数   
  164. void BiTreeLeafCount(BiTree bt)   
  165. {   
  166.     if(bt==NULL)   
  167.         return;   
  168.     else if(bt->Lchild==NULL&&bt->Rchild==NULL)   
  169.         Leafcount++;   
  170.     else if(!(bt->Lchild==NULL&&bt->Rchild==NULL))   
  171.     {   
  172.         BiTreeLeafCount(bt->Lchild);   
  173.         BiTreeLeafCount(bt->Rchild);   
  174.     }   
  175.       
  176. }    
  177. //按照二叉树的一种表示方法输出整个二叉树   
  178. void PrintBiTree(BiTree bt)   
  179. {   
  180.     if(bt!=NULL)   
  181.     {   
  182.         printf("%3c",bt->data);   
  183.         PrintBiTree(bt->Lchild);   
  184.         PrintBiTree(bt->Rchild);   
  185.     }   
  186. }   
  187.   
  188.   
  189. //清除二叉树,使之变为一棵空树   
  190. void ClearBiTree(BiTree &BT)   
  191. {   
  192.     if(BT)   
  193.     {   
  194.         ClearBiTree( BT->Lchild );   
  195.         ClearBiTree( BT->Rchild );   
  196.         delete(BT);   
  197.         BT=NULL;   
  198.     }   
  199. }   
  200.   
  201.   
  202. //按先序输出   
  203. void PreOrder(BiTree bt)   
  204. {   
  205.     if(bt!=NULL)   
  206.     {   
  207.         printf("%3c",bt->data);   
  208.         PreOrder(bt->Lchild);   
  209.         PreOrder(bt->Rchild);   
  210.     }   
  211. }   
  212.   
  213.   
  214. //中序遍历   
  215. void InOrder(BiTree bt)   
  216. {   
  217.     if(bt!=NULL)   
  218.     {   
  219.         InOrder(bt->Lchild);   
  220.         printf("%3c",bt->data);   
  221.         InOrder(bt->Rchild);   
  222.     }   
  223. }   
  224.   
  225.   
  226. //后序   
  227. void PostOrder(BiTree bt)   
  228. {   
  229.     if(bt!=NULL)   
  230.     {   
  231.         PostOrder(bt->Lchild);   
  232.         PostOrder(bt->Rchild);   
  233.         printf("%3c",bt->data);   
  234.     }   
  235. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值