树的镜像(反转)

[html]  view plain copy
  1. <pre name="code" class="cpp">#include <stdio.h>  
  2.   
  3.   
  4.  typedef struct BCTreeNode //  
  5.        {  
  6.            int                 Value; //  
  7.            struct BCTreeNode  *left_child;  //  
  8.            struct BCTreeNode  *right_brother; //  
  9.        } *BCTree,BcTree;  
  10.   
  11.   
  12.  int ReverseBCTree(BCTree T){  
  13.   
  14.   
  15.         if(T==NULL)  
  16.             return 0;  
  17.         if(T->left_child==NULL)      //no child,go to brother node  
  18.         {  
  19.             ReverseBCTree(T->right_brother);  
  20.             return 0;  
  21.         }  
  22.         BCTree pr,prnext,prprev;  
  23.         pr=T->left_child;            //head of list  
  24.         prprev=NULL;  
  25.         prnext=pr->right_brother;  
  26.         while(prnext){                //reverse all the right_brother list  
  27.             pr->right_brother=prprev;  //reverse current node to prev list node  
  28.             //forward one step  
  29.             prprev=pr;  
  30.             pr=prnext;  
  31.             prnext=prnext->right_brother;  
  32.         }  
  33.         pr->right_brother=prprev;  
  34.         T->left_child=pr;    //after reverse T ->left point to head of list ;  
  35.         ReverseBCTree(T->left_child);  
  36.         ReverseBCTree(T->right_brother);  
  37.         return 0;  
  38.  }  
  39.   
  40.   
  41. int createBCTree(BCTree *T){  
  42.         int data=0;  
  43.         BCTree t;  
  44.         printf("input:");  
  45.         scanf("%d",&data);  
  46.         if(data==100){  
  47.             t=0;  
  48.             return 0;  
  49.         }  
  50.         t=(BCTree)malloc(sizeof(struct BCTreeNode));  
  51.         t->Value=data;  
  52.         t->left_child=NULL;  
  53.         t->right_brother=NULL;  
  54.         *T=t;  
  55.         createBCTree(&(t->left_child));  
  56.         createBCTree(&(t->right_brother));  
  57.         return 0;  
  58.   }  
  59.   
  60.   
  61. int  printBCTree(BCTree T){  
  62.         if(T==0)  
  63.             return 0;  
  64.         printf("output:");  
  65.         printf(" %d ",T->Value);  
  66.         printBCTree(T->left_child);  
  67.         printBCTree(T->right_brother);  
  68.         return 0;  
  69.   }  
  70.   
  71.   
  72. int main(int argc, char *argv[]) {  
  73.         BCTree T=0;  
  74.         if(createBCTree(&T)){  
  75.             printf("error");  
  76.         }  
  77.         printf("createBCTree over\n");  
  78.         printBCTree(T);  
  79.         printf("\nprintBCTree over\n");  
  80.         ReverseBCTree(T);  
  81.         printf("\nReverseBCTreeover\n");  
  82.         printBCTree(T);  
  83.         printf("\nprintBCTree over\n");  
  84.         return 0;  
  85.  }  
  86. </pre><br>  
  87. <br>  
  88. <pre></pre>  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值