实验7 二叉树
1、编写算法函数void preorder1(bintree t)实现二叉树t的非递归前序遍历。
#include "bintree.h"
char *a="ABC##D#E##F##";
void preorder1(bintree t)
{
seqstack s;
init(&s);
while ( !empty(&s) || t )
{
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;
}
2、编写算法函数void levelbintree(bintree t),实现二叉树的层次遍历。
#include "bintree.h"
char *a="ABC##D#E##F##";
void levelbintree(bintree t)
{
bintree queue[100];
int f,r;
f=0;r=1;
if (t)
{
queue[0]=t;
while (f<r)
{
t=queue[f++];
printf("%c",t->data);
if (t->lchild)
queue[r++]=t->lchild;
if (t->rchild)
queue[r++]=t->rchild;
}
}
}
int main()
{ bintree t;
t=creatbintree();
printf("二叉树的层次序列为:\n");
levelbintree(t);
return 0;
}
3、编写函数bintree prelist(bintree t),bintree postfirst(bintree t),分别返回二叉树t在前序遍历下的最后一个结点地址和后序遍历下的第一个结点地址。
#include "bintree.h"
char *a="ABC##D##EF#G###";
bintree prelast(bintree t)
{
if (!t)
return t;
else
if (t->lchild==NULL &&t->rchild==NULL)
return t;
else
if (t->rchild)
return prelast(t->rchild);
else
return prelast(t->lchild);
}
bintree prelast1(bintree t)
{
bintree p=t;
if (p)
{
while (p->lchild || p->rchild )
if (p->rchild)
p=p->rchild;
else
p=p->lchild;
}
return p;
}
bintree postfirst(bintree t)
{ bintree p=t;
if (p)
{
while (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);
q=postfirst(t);
if (t!=NULL)
{ printf("前序遍历最后一个结点为:%c\n",p->data);
printf("后序遍历第一个结点为:%c\n",q->data);
}
else printf("二叉树为空!");
return 0;
}
4、假设二叉树采用链式方式存储,t为其根结点,编写一个函数int Depth(bintree t, char x),求值为x的结点在二叉树中的层次。
#include "bintree.h"
char *a="ABC##D##EF#G###";
int Depth(bintree t,char x)
{
int m,n;
if( !t ) return -1;
if( t->data == x ) return 1;
m = Depth(t->lchild,x);
if( m != -1 ) return m+1;
else
{ n = Depth(t->rchild,x);
if( n != -1 ) return n+1;
else return -1;
}
}
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);
}
5、 试编写一个函数,将一棵给定二叉树中所有结点的左、右子女互换。
#include "bintree.h"
char *a="ABC##D##EF#G###";
void change(bintree t)
{
bintree temp;
if (t)
{
temp = t->lchild;
t->lchild = t->rchild;
t->rchild = temp;
if( t->lchild ) change( t->lchild );
if( t->rchild ) change( t->rchild );
}
}
int main()
{ bintree root;
root=creatbintree();
change(root);
preorder(root);
}
6、试编写一个递归函数bintree buildBintree(char *pre, char *mid, int length),根据二叉树的前序序列pre、中序序列mid和前序序列长度length,构造二叉树的二叉链表存储结构,函数返回二叉树的树根地址。
#include "bintree.h"
#include <string.h>
char *a="";
bintree buildBintree(char *pre, char *mid,int length)
{
if( length )
{
bintree root = (bintree)malloc(sizeof(binnode));
root->data = pre[0];
int i;
for(i=0;mid[i] != pre[0];i++) ;
mid[i] = '\0';
root->lchild = buildBintree(pre+1,mid,i);
root->rchild = buildBintree(pre+i+1,mid+i+1,length-i-1);
return root;
}
else return NULL;
}
int main()
{ bintree root;
char pre[100],mid[100];
puts("请输入前序序列:");
gets(pre);
puts("请输入中序序列:");
gets(mid);
root=buildBintree(pre,mid,strlen(pre));
puts("后序序列是:");
postorder(root);
}