欢迎来到天天文库
浏览记录
ID:39579082
大小:59.50 KB
页数:3页
时间:2019-07-06
《数据结构树的相关操作》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、/*递归方法相关操作*/#include#definemaxsize100#includetypedefchardatatype;typedefstructnode{datatypedata;structnode*lchild,*rchild;}bitree;/*建立*/bitree*Q[maxsize];bitree*creatree(){charch;intfront,rear;bitree*root,*s;root=NULL;front=1;rear=0;ch=getchar();while(ch!='#'){s=NULL;if(ch!='@
2、'){s=malloc(sizeof(bitree));s->data=ch;s->lchild=NULL;s->rchild=NULL;}rear++;Q[rear]=s;if(rear==1)root=s;else{if(s&&Q[front])if(rear%2==0)Q[front]->lchild=s;elseQ[front]->rchild=s;if(rear%2==1)front++;}ch=getchar();}returnroot;}/*前序遍历*/voidpreorder(bitree*t){if(t){printf("%c",t->data);preorder(t
3、->lchild);preorder(t->rchild);}}/*中序遍历*/voidinorder(bitree*t){if(t){inorder(t->lchild);printf("%c",t->data);inorder(t->rchild);}}/*后序遍历*/voidpostorder(bitree*t){if(t){postorder(t->lchild);postorder(t->rchild);printf("%c",t->data);}}voidmain(){bitree*t;printf("Pleaseinputyourtree:");t=creatree(
4、);printf("");printf("preorder:");preorder(t);printf("");printf("inorder:");inorder(t);printf("");printf("postorder:");postorder(t);getch();}运行结果:
此文档下载收益归作者所有