- #include "bintree.h"
- char *a="ABC##D#E##F##";
- void preorder1(bintree t)
- {
- seqstack s;
- s.top=0;
- while((t) || (s.top!=0))
- {
- if(t)
- {
- printf("%c ",t->data);
- push(&s,t);
- t=t->lchild;
- }
- else
- {
- t=pop(&s);
- t=t->rchild;
- }
- }
- }
- int main()
- { bintree t;
- t=creatbintree();
- printf("二叉树的前序序列为:\n");
- preorder1(t);
- return 0;
- }
- #include "bintree.h"
- char *a="ABC##D#E##F##";
- void levelbintree(bintree t)
- {
- bintree queue[100];
- int f=0,r=1;
- bintree p;
- queue[0]=t;
- while(f<r)
- {
- p=queue[f]; f++; printf("%c",p->data);
- if(p->lchild)
- queue[r++]=p->lchild;
- if(p->rchild)
- queue[r++]=p->rchild;
- }
- }
- int main()
- { bintree t;
- t=creatbintree();
- printf("二叉树的层次序列为:\n");
- levelbintree(t);
- return 0;
- }
- #include "bintree.h"
- char *a="ABC##D##EF#G###";
- bintree prelast(bintree t)
- {
- bintree p;
- if(t)
- {
- p=t;
- while(p&&p->lchild||p->rchild)
- {
- if(p->rchild)
- {
- p=p->rchild;
- }
- else
- {
- p=p->lchild;
- }
- }
- }
- return p;
- }
- bintree postfirst(bintree t)
- {
- bintree p;
- if(t)
- {
- while(p&&p->lchild||p->rchild)
- {
- if(p->lchild)
- {
- p=p->lchild;
- }
- else
- {
- p=p->rchild;
- }
- }
- }
- return p;
- }
- int main()
- { bintree t,p,q;
- t=creatbintree();
- p=prelast(t);
-
- if (t!=NULL)
- { printf("前序遍历最后一个结点为:%c\n",p->data);
-
- }
- else printf("二叉树为空!");
- return 0;
- }
- #include "bintree.h"
- char *a="ABC##D##EF#G###";
- int Depth(bintree t,char x)
- {
- int num1,num2,n;
- if(t==NULL)
- {
- return 0;
- }
- else
- {
- if(t->data==x)
- {
- return 1;
- }
- num1=Depth(t->lchild,x);
- num2=Depth(t->rchild,x);
- n=num1+num2;
- if(num1!=0||num2!=0)
- {
- n++;
- }
- }
- return n;
- }
- int main()
- { bintree root;
- char x;
- int k=0;
- root=creatbintree();
- printf("请输入树中的1个结点值:\n");
- scanf("%c",&x);
- k=Depth(root,x);
- printf("%c结点的层次为%d\n",x,k);
- }
- #include "bintree.h"
- char *a="ABC##D##EF#G###";
- void change(bintree t)
- {
- bintree p;
- if(t)
- {
- p=t->lchild;
- t->lchild=t->rchild;
- t->rchild=p;
- change(t->lchild);
- change(t->rchild);
- }
- }
- int main()
- { bintree root;
- root=creatbintree();
- change(root);
- preorder(root);
- }
- #include "bintree.h"
- #include <string.h>
- char *a="";
- bintree buildBintree(char *pre, char *mid,int length)
- {
- bintree t;
- int i=0;
- if(length)
- {
- t=(bintree)malloc(sizeof(binnode));
- t->data=pre[i];
- while(i<length&&mid[i]!=pre[0])
- i++;
- t->lchild=buildBintree(pre+1,mid,i);
- t->rchild=buildBintree(pre+i+1,mid+i+1,length-i-1);
- }
- else
- return NULL;
- return t;
- }
- int main()
- { bintree root;
- char pre[100],mid[100];
- puts("请输入前序序列:");
- gets(pre);
- puts("请输入中序序列:");
- gets(mid);
- root=buildBintree(pre,mid,strlen(pre));
- puts("后序序列是:");
- postorder(root);
- }
头文件这里就不贴出来了
本文地址:
http://liuyanzhao.com/3411.html
转载请注明
-
微信
- 交流学习,有偿服务
-
-
博客/Java交流群
- 资源分享,问题解决,技术交流。群号:590480292
-
赞
3
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏