二叉树的遍历学习心得includeXincludetypedefintetype;typedefstructbitnode/*树结点结构*/{etypedata;structbitnode*lch,*rch;}bitnode;/*函数原形声明*/bitnode*creat_bt1;bitnode*creat_bt2;voidpreorder(bitnode*p);voidinorder(bitnode*p);voidpostorder(bitnode*p);voidnumb1(bitnode*p);voidnumb2(bitnode*p);voidnumb3(bitnode*p);bitnode*t;intn,n0,n1,n2;/*主函数*/main{charch;intk;do{printf("\n\n\n");printf("\n\n1.建立二叉树方法1");printf("\n\n2.建立二叉树方法2");printf("\n\n3.前序递归遍历二叉树");printf("\n\n4.中序递归遍历二叉树");printf("\n\n5.后序递归遍历二叉树");printf("\n\n6.前序计算树中结点个数");printf("\n\n7.中序计算树中结点个数");printf("\n\n8.后序计算树中结点个数");printf("\n\n9.结束程序运行");printf("\n======================================");printf("\n第1页共6页请输入您的选择(1-9)");scanf("%d",k);switch(k){case1:t=creat_bt1;break;/*调用性质5建立二叉树算法*/case2:t=creat_bt2;break;/*调用递归建立二叉树算法*/case3:{preorder(t);/*调用前序遍历*/printf("\n\n打回车键,继续。");ch=getch;}break;case4:{inorder(t);/*调用中序遍历*/printf("\n\n打回车键,继续。");ch=getch;}break;case5:{postorder(t);/*调用后序遍历*/printf("\n\n打回车键,继续。");ch=getch;}break;case6:{n=0;n0=0;n1=0;n2=0;/*全局变量置0*/numb1(t);printf("\n二叉树结点总数n=%d",n);printf("\n二叉树叶子结点数n0=%d",n0);printf("\n度为1的结点数n1=%d",n1);printf("\n第2页共6页度为2的结点数n2=%d",n2);printf("\n\n打回车键,继续。");ch=getch;}break;case7:{n=0;n0=0;n1=0;n2=0;/*全局变量置0*/numb2(t);printf("\n二叉树结点总数n=%d",n);printf("\n二叉树叶子结点数n0=%d",n0);printf("\n度为1的结点数n1=%d",n1);printf("\n度为2的结点数n2=%d",n2);printf("\n\n打回车键,继续。");ch=getch;}break;case8:{n=0;n0=0;n1=0;n2=0;/*全局变量置0*/numb3(t);printf("\n二叉树结点总数n=%d",n);printf("\n二叉树叶子结点数n0=%d",n0);printf("\n度为1的结点数n1=%d",n1);printf("\n度为2的结点数n2=%d",n2);printf("\n\n打回车键,继续。");ch=getch;}break;case9:exit(0);}/*switch*/printf("\n----------------");第3页共6页}while(k>=1kdata=e;p->lch=null;p->rch=null;v[i]=p;if(i==1)t=p;/*序号为1的结点是根*/else{j=i/2;if(i%2==0)v[j]->lch=p;/*序号为偶数,做左孩子*/elsev[j]->rch=p;/*序号为奇数,做右孩子*/}printf("\ni,data=。");scanf("%d%d",i,e);}return(t);}/*creat_bt1*//*模仿先序递归遍历方法,建立二叉树*/bitnode*creat_bt2{bitnode*t;inte;printf("\ndata=");scanf("%d",e);if(e==0)t=null;/*对于0值,不分配新结点*/else{t=(bitnode*)malloc(sizeof(bitnode));t->data=e;t->lch=creat_bt2;/*左孩子获得新指针值*/t->rch=creat_bt2;/*右孩子获得新指针值*/}return(t);}/*creat_bt2*//*前序递归遍历二叉树*/voidpreorder(bitnode*p){if(p){printf("%3d",p->data);preorder(p->lch);preorder(p->rch);}}/*preorder*//*中序递归遍历二叉树*/voidinorder(bitnode*p){if(p){inorder(p-第4页共6页>lch);printf("%3d",p->data);inorder(p->rch);}}/*inorder*//*后序递归遍历二叉树*/voidpostorder(bitnode*p){if(p){postorder(p->lch);postorder(p->rch);printf("%3d",p->data);}}/*posorder*//*利用前序递归遍历二叉树的方法,计算树中结点个数*/voidnumb1(bitnode*p){if(p){{printf("%3d",p->data);n++;if(p->lch==nullp->rch==null)n0++;if((p->lch==nullp->rch。=null)||(p->lch。=nullp->rch==null))n1++;if(p->lch。=nullp->rch。=null)n2++;}numb1(p->lch);numb1(p->rch);}}/*numb1*//*利用中序递归遍历二叉树的方法,计算树中结点个数*/voidnumb2(bitnode*p){if(p){numb2(p->lch);{printf("%3d",p->data);n++;if(p->lch==nullp->rch==null)n0++;if((p->lch==nullp->rch。=null)||(p->lch。=nullp->rch==null))n1++;if(p->lch。=nullp->rch。=null)n2++;}numb2(p->rch);}}/*numb2*//*利用后序递归遍历二叉树的方法,计算树中结点个数*/voidnumb3(bitnode*p){if(p){numb3(p->lch);第5页共6页numb3(p->rch);{printf("%3d",p->data);n++;if(p->lch==nullp->rch==null)n0++;if((p->lch==nullp->rch。=null)||(p->lch。=nullp->rch==null))n1++;if(p->lch。=nullp->rch。=null)n2++;}}}/*numb3*/第6页共6页