如果您无法下载资料,请参考说明:
1、部分资料下载需要金币,请确保您的账户上有足够的金币
2、已购买过的文档,再次下载不重复扣费
3、资料包下载后请先用软件解压,在使用对应软件打开
班级:380911班学号:57000211姓名:徐敏实验报告一,实验目的:·掌握二叉树的链式存储结构;·掌握构造二叉树的方法;·加深对二叉树的中序遍历的理解;二,实验方法:·用递归调用算法中序遍历二叉树。三,实验步骤:·通过链式存储建立一颗二叉树。·设计一个算法实现中序遍历二叉树。四,具体实验步骤:#include<stdio.h>#include<stdlib.h>#defineLEFT0#defineRIGHT1#defineTRUE1#defineFALSE0typedefstruct_BTNODE{charc;struct_BTNODE*lchild;struct_BTNODE*rchild;}BTNODE,*PBTNODE;voidPrintBTree(PBTNODEp,intdepth);voidConstructBTree(PBTNODEp);voidInorderTraverse(PBTNODEp);voidmain(){PBTNODEp;p=(PBTNODE)calloc(1,sizeof(BTNODE));printf("Inputthedata:");ConstructBTree(p);PrintBTree(p,0);printf("NowInorderTraverse:");InorderTraverse(p);printf("\nPressanykeytocontinue...");getchar();}voidPrintBTree(PBTNODEp,intdepth){inti;if(p==NULL){return;}else{for(i=0;i<depth;i++){printf("--");}printf(">");printf("%c\n",p->c);PrintBTree(p->lchild,depth+1);PrintBTree(p->rchild,depth+1);}}voidConstructBTree(PBTNODEp){intside;charc;side=LEFT;while(TRUE){scanf("%c",&c);if(c=='\n'){//printf("EOF\n");return;}//printf("%d\n",c);switch(c){case'|':break;case')':return;case',':side=RIGHT;break;case'(':if(side==LEFT){if(p->lchild==NULL){p->lchild=(PBTNODE)calloc(1,sizeof(BTNODE));}ConstructBTree(p->lchild);}else{if(p->rchild==NULL){p->rchild=(PBTNODE)calloc(1,sizeof(BTNODE));}ConstructBTree(p->rchild);}break;default:if(side==LEFT){p->lchild=(PBTNODE)calloc(1,sizeof(BTNODE));p->lchild->c=c;}else{p->rchild=(PBTNODE)calloc(1,sizeof(BTNODE));p->rchild->c=c;}}}}voidInorderTraverse(PBTNODEp){if(p==NULL){return;}else{InorderTraverse(p->lchild);printf("[%c]",p->c);InorderTraverse(p->rchild);}return;}五,实验过程:·输出:Inputthedate;·输入:1(2(3,4),5(6,7));·输出:NowInorderTraverse:【3】【2】【4】【1】【6】【5】【7】;六,上机实验体会:·体会到熟练掌握各种程序算法的重要性;·通过上机练习,充分理解了链式建立二叉树的算法;·形象的了解二叉树的结构,能够熟练的进行先序,中序