二叉树的各类操作

  1. ********************************************************/  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <malloc.h>  
  5. /********************************************************/  
  6. #define OK      1  
  7. #define ERROR   0  
  8. #define TRUE    1  
  9. #define FALSE   0  
  10.   
  11. typedef char    ElemType;           //数据类型  
  12. typedef int     Status;             //返回值类型  
  13. /********************************************************/  
  14. typedef struct BiTNode{  
  15.     ElemType    data;                   //数据域  
  16.     struct BiTNode  *lChild, *rChlid;   //左右子树域  
  17. }BiTNode, *BiTree;  
  18. /********************************************************/  
  19. //先序创建二叉树  
  20. Status CreateBiTree(BiTree *T)  
  21. {  
  22.     ElemType ch;  
  23.     ElemType temp;  
  24.   
  25.     scanf("%c", &ch);  
  26.     temp = getchar();  
  27.   
  28.     if (' ' == ch)  
  29.         *T = NULL;  
  30.     else  
  31.     {  
  32.         *T = (BiTree)malloc(sizeof(BiTNode));  
  33.         if (!(*T))  
  34.             exit(-1);  
  35.   
  36.         (*T)->data = ch;  
  37.         printf("输入%c的左子节点:", ch);  
  38.         CreateBiTree(&(*T)->lChild);  
  39.         printf("输入%c的右子节点:", ch);  
  40.         CreateBiTree(&(*T)->rChlid);  
  41.     }  
  42.   
  43.     return OK;  
  44. }  
  45. /********************************************************/  
  46. //先序遍历二叉树  
  47. void TraverseBiTree(BiTree T)  
  48. {  
  49.     if (NULL == T)  
  50.         return ;  
  51.   
  52.     printf("%c ", T->data);  
  53.     TraverseBiTree(T->lChild);  
  54.     TraverseBiTree(T->rChlid);  
  55. }  
  56. /********************************************************/  
  57. //二叉树的输出 嵌套括号表示法  
  58. void DispBiTree(BiTree T)  
  59. {  
  60.     if (NULL != T)  
  61.     {  
  62.         printf("%c", T->data);  
  63.         if (T->lChild!=NULL || T->rChlid!=NULL)  
  64.         {  
  65.             printf("(");  
  66.             DispBiTree(T->lChild);  
  67.             if (NULL != T->rChlid)  
  68.                 printf(",");  
  69.             DispBiTree(T->rChlid);  
  70.             printf(")");  
  71.         }  
  72.     }  
  73. }  
  74. /*********************************************************/  
  75. //二叉树的输出 凹入表表示法 输出无法分辨左右子数  
  76. //写的不好 第一次调用时必须给一个参数i  用来控制空格的数目  
  77. void DispBiTree_into(BiTree T, int i)  
  78. {  
  79.     int k = i;  
  80.     if (NULL != T)  
  81.     {  
  82.         putchar('\n');  
  83.         while (k)  
  84.         {  
  85.             putchar(' ');  
  86.             --k;  
  87.         }  
  88.         printf(" %c", T->data);  
  89.         DispBiTree_into(T->lChild, i+1);  
  90.         DispBiTree_into(T->rChlid, i+1);  
  91.     }  
  92. }  
  93. /********************************************************/  
  94. //查找结点 如果找到就返回指向该结点的指针 否则返回NULL  
  95. BiTree locate(BiTree T, ElemType e)  
  96. {  
  97.     BiTree p;  
  98.     if (NULL == T)  
  99.         return NULL;   
  100.     else  
  101.     {  
  102.         if (e == T->data)  
  103.             return T;  
  104.         else  
  105.             p = locate(T->lChild, e);  
  106.         if (p)  
  107.             return p;  
  108.         else  
  109.             return locate(T->rChlid, e);  
  110.     }  
  111. }  
  112. /********************************************************/  
  113. //统计树中结点的个数  
  114. int numofnode(BiTree T)  
  115. {  
  116.     if (NULL == T)  
  117.         return 0;  
  118.     else  
  119.         return (numofnode(T->lChild) + numofnode(T->rChlid) + 1);  
  120. }  
  121. /********************************************************/  
  122. //判断二叉树是否等价  
  123. Status isequal(BiTree T1, BiTree T2)  
  124. {  
  125.     int t = 0;  
  126.   
  127.     if (NULL==T1 && NULL==T2)  
  128.         t = 1;  
  129.     else  
  130.     {  
  131.         if (T1!=NULL && T2!=NULL)       //如果两棵树都不为空  
  132.             if (T1->data == T2->data)     //根节点相同  
  133.                 if ( isequal(T1->lChild, T2->lChild) )    //如果左子树相同 就继续比较右子树    
  134.                     t = isequal(T1->rChlid, T2->rChlid);  
  135.     }  
  136.     return t;  
  137. }  
  138. /********************************************************/  
  139. //求二叉树的高度  
  140. int depth(BiTree T)  
  141. {  
  142.     int h, lh, rh;  
  143.   
  144.     if (NULL == T)  
  145.         h = 0;  
  146.     else  
  147.     {  
  148.         lh = depth(T->lChild);  
  149.         rh = depth(T->rChlid);  
  150.         if (lh >= rh)  
  151.             h = lh+1;  
  152.         else  
  153.             h = rh+1;  
  154.     }  
  155.     return h;  
  156. }  
  157. /********************************************************/  
  158. int main(void)  
  159. {  
  160.     BiTree T;  
  161.     BiTree p;  
  162.     int i=0;  
  163.     CreateBiTree(&T);  
  164.     TraverseBiTree(T);  
  165.     DispBiTree(T);  
  166.     DispBiTree_into(T, i);    
  167.     p = locate(T, 'c');  
  168.     printf("\n\n%c\n", p->data);  
  169.       
  170.     i = numofnode(T);   
  171.     printf("%d", i);  
  172.     return 0;  
  173. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值