#include <stdio.h>
typedef struct BCTreeNode //
{
int Value; //
struct BCTreeNode *left_child; //
struct BCTreeNode *right_brother; //
} *BCTree,BcTree;
int ReverseBCTree(BCTree T){
if(T==NULL)
return 0;
if(T->left_child==NULL) //no child,go to brother node
{
ReverseBCTree(T->right_brother);
return 0;
}
BCTree pr,prnext,prprev;
pr=T->left_child; //head of list
prprev=NULL;
prnext=pr->right_brother;
while(prnext){ //reverse all the right_brother list
pr->right_brother=prprev; //reverse current node to prev list node
//forward one step
prprev=pr;
pr=prnext;
prnext=prnext->right_brother;
}
pr->right_brother=prprev;
T->left_child=pr; //after reverse T ->left point to head of list ;
ReverseBCTree(T->left_child);
ReverseBCTree(T->right_brother);
return 0;
}
int createBCTree(BCTree *T){
int data=0;
BCTree t;
printf("input:");
scanf("%d",&data);
if(data==100){
t=0;
return 0;
}
t=(BCTree)malloc(sizeof(struct BCTreeNode));
t->Value=data;
t->left_child=NULL;
t->right_brother=NULL;
*T=t;
createBCTree(&(t->left_child));
createBCTree(&(t->right_brother));
return 0;
}
int printBCTree(BCTree T){
if(T==0)
return 0;
printf("output:");
printf(" %d ",T->Value);
printBCTree(T->left_child);
printBCTree(T->right_brother);
return 0;
}
int main(int argc, char *argv[]) {
BCTree T=0;
if(createBCTree(&T)){
printf("error");
}
printf("createBCTree over\n");
printBCTree(T);
printf("\nprintBCTree over\n");
ReverseBCTree(T);
printf("\nReverseBCTreeover\n");
printBCTree(T);
printf("\nprintBCTree over\n");
return 0;
}
树的镜像(反转)
最新推荐文章于 2021-03-08 21:22:30 发布