c语言实验树.docx
《c语言实验树.docx》由会员分享,可在线阅读,更多相关《c语言实验树.docx(19页珍藏版)》请在冰豆网上搜索。
![c语言实验树.docx](https://file1.bdocx.com/fileroot1/2023-1/4/91dffcf6-cdc3-458d-8d1f-4d065d0de010/91dffcf6-cdc3-458d-8d1f-4d065d0de0101.gif)
c语言实验树
1、验证基于顺序存储的二叉树的基本操作。
#include
#defineNULL0
#defineMaxSize100
typedefstructsqtree
{
chardata[MaxSize];
intnum;
}SqTree;
voidInitTree(SqTree*&t)
{
t=newSqTree;
t->num=0;
}
voidCreaTree(SqTree*t,chartr[])
{
for(inti=0;tr[i]!
='\0';i++)
{
t->data[i]=tr[i];
t->num++;
}
}
voidDispTree(SqTree*t)
{
for(inti=0;inum;i++)
{
cout<data[i];
}
cout<}
voidFindTNode(SqTree*t,charx)
{
for(inti=0;inum;i++)
{
if(x==t->data[i])
{
if(i>=t->num/2)
{
cout<<"值为"<";
cout<data[(i+1)/2]<}
elseif(i==0)
{
cout<<"值为"<cout<<"值为"<";
cout<data[2*i+1]<cout<<"值为"<";
cout<data[2*i+2]<}
else
{
cout<<"值为"<";
cout<data[(i+1)/2]<cout<<"值为"<";
cout<data[2*i+1]<cout<<"值为"<";
cout<data[2*i+2]<}
return;
}
}
cout<<"值为"<"<}
intmain()
{
SqTree*T=NULL;
chara[]="abcdefghijklmn";
charx;
InitTree(T);
CreaTree(T,a);
DispTree(T);
cout<<"请输入要查找的节点:
";
cin>>x;
FindTNode(T,x);
return0;
}
2、验证基于链式存储的二叉树的基本操作。
3、已知某电文中仅有8个不同的字符,各字符出现的次数依次是3,4,8,10,16,18,20,21,请设计程序,实现对各个字符的哈夫曼编码。
#include
#defineMaxSize50
typedefstruct
{
chardata;
intweight;
intparent;
intlchild;
intrchild;
}HTNode;
typedefstruct
{
charcd[MaxSize];
intstart;
}HCode;
voidCreateHT(HTNodeht[],intn)
{
inti,k,lnode,rnode;
floatmin1,min2;
for(i=0;i<2*n-1;i++)
ht[i].parent=ht[i].lchild=ht[i].rchild=-1;
for(i=n;i<2*n-1;i++)
{
min1=min2=32767;
lnode=rnode=-1;
for(k=0;k
{
if(ht[k].parent==-1)
{
if(ht[k].weight{
min2=min1;
rnode=lnode;
min1=ht[k].weight;
lnode=k;
}
elseif(ht[k].weight{
min2=ht[k].weight;
rnode=k;
}
}
}
ht[lnode].parent=i;
ht[rnode].parent=i;
ht[i].weight=ht[lnode].weight+ht[rnode].weight;
ht[i].lchild=lnode;
ht[i].rchild=rnode;
}
}
voidCreateHCode(HTNodeht[],HCodehcd[],intn)
{
inti,f,c;
HCodehc;
for(i=0;i{
hc.start=n;
c=i;
f=ht[i].parent;
while(f!
=-1)
{
if(ht[f].lchild==c)
{
hc.cd[hc.start--]='0';
}
else
{
hc.cd[hc.start--]='1';
}
c=f;
f=ht[f].parent;
}
hc.start++;
hcd[i]=hc;
}
}
voidFillNodeInf(HTNodeht[],charch[],intweight[])
{
inti;
for(i=0;ch[i]!
='\0';i++)
{
ht[i].data=ch[i];
ht[i].weight=weight[i];
}
}
voidOutputHuf_Code(HTNodeht[],HCodehcd[],intn)
{
inti,j;
for(i=0;i{
cout<";
for(j=hcd[i].start;j<=n;j++)
cout<cout<}
}
intmain()
{
HTNodeht[MaxSize];
HCodehcd[MaxSize];
char*ch="abcdefgh";
intweight[]={3,4,8,10,16,18,20,21};
FillNodeInf(ht,ch,weight);
CreateHT(ht,8);
CreateHCode(ht,hcd,8);
cout<<"各字符的哈夫曼编码为:
"<OutputHuf_Code(ht,hcd,8);
return0;
}
4、设计程序完成如下功能:
输出一棵二叉树中所有的叶子结点;输出所有从叶子结点到根结点的路径;输出其中第一条最长的路径。
#include
#defineNULL0
#defineMaxSize100
typedefstructnode
{
chardata;
structnode*lch;
structnode*rch;
}BTNode;
voidCreatBTTree(BTNode*&t,char*s)
{
BTNode*st[MaxSize],*p=NULL;
inttop=-1,k,j=0;
charch;
t=NULL;
ch=s[j];
while(ch!
='\0')
{
switch(ch)
{
case'(':
top++;st[top]=p;k=1;break;
case')':
top--;break;
case',':
k=2;break;
default:
p=newBTNode;
p->data=ch;
p->lch=NULL;
p->rch=NULL;
if(t==NULL)t=p;
else
{
switch(k)
{
case1:
st[top]->lch=p;break;
case2:
st[top]->rch=p;break;
}
}
}
j++;
ch=s[j];
}
}
voidDispBTTree(BTNode*t)
{
if(t!
=NULL)
{
cout<data;
if((t->lch!
=NULL)||(t->rch!
=NULL))
{
cout<<'(';
DispBTTree(t->lch);
if(t->rch!
=NULL)
cout<<',';
DispBTTree(t->rch);
cout<<')';
}
}
}
voidFindLeaf(BTNode*t)
{
if(t!
=NULL)
{
if((t->lch==NULL)&&(t->rch==NULL))
cout<data<<'';
FindLeaf(t->lch);
FindLeaf(t->rch);
}
}
voidDispRout(BTNode*t)
{
BTNode*st[MaxSize],*p;
intflag,i,top=-1;
if(t!
=NULL)
{
do
{
while(t!
=NULL)
{
top++;
st[top]=t;
t=t->lch;
}
p=NULL;
flag=1;
while(top!
=-1&&flag)
{
t=st[top];
if(t->rch==p)
{
if(t->lch==NULL&&t->rch==NULL)
{
for(i=top;i>0;i--)
{
cout<data;
}
cout<data<}
top--;
p=t;
}
else
{
t=t->rch;
flag=0;
}
}
}
while(top!
=-1);
}
}
intmax(intx,inty)
{
if(x>=y)
return(x);
elsereturn(y);
}
intCountDeep(BTNode*t)
{
intdeep;
if(t==NULL)deep=0;
elsedeep=1+max(CountDeep(t->lch),CountDeep(t->rch));
return(deep);
}
voidPrintMLR(BTNode*t)
{
if(t!
=NULL)
{
cout<data;
if(CountDeep(t->lch)>=CountDeep(t->rch))
{
PrintMLR(t->lch);
}
else
{
PrintMLR(t->rch);
}
}
}
voidmain()
{
BTNode*t=NULL,*p=NULL;
CreatBTTree(t,"a(b(e(k,l),f),d(h(m(,n)),j)");
cout<<"创建的树是:
";
DispBTTree(t);
cout<cout<<"树中的叶子结点有:
";
FindLeaf(t);
cout<cout<<"叶子结点到根结点的路径有:
"<DispRout(t);
cout<<"其中一条最长路径为:
";
PrintMLR(t);
cout<}
5.建立二叉家族族谱树
#include
#include
usingnamespacestd;
#defineMaxSize100
#defineNULL0
typedefstructnode
{
stringdata;
structnode*parent;
structnode*lchild;
structnode*rchild;
}BTNode;
voidCreateBTTree(BTNode*&b,string*str,inti)
{
BTNode*st[MaxSize],*p=NULL;
inttop,k=0,j,l=0;
stringch;
top=-1;
j=0;
b=NULL;
ch=str[j];
while(l
{
if(ch=="(")
{
top++;
st[top]=p;
k=1;
}
elseif(ch==")")
{
top--;
}
elseif(ch==",")
{
k=2;
}
else
{
p=newBTNode;
p->data=ch;
p->lchild=p->rchild=NULL;
if(b==NULL)
{
b=p;
b->parent=NULL;
}
else
{
if(k==1)
{
st[top]->lchild=p;
st[top]->lchild->parent=st[top];
}
if(k==2)
{
st[top]->rchild=p;
st[top]->rchild->parent=st[top];
}
}
}
j++;
l++;
ch=str[j];
}
}
voidTravelTree(BTNode*t)
{
if(t!
=NULL)
{
BTNode*st[10];
intfront,rear;
front=rear=0;
rear++;
st[rear]=t;
while(front!
=rear)
{
front=(front+1)%10;
cout<data<<'';
if(st[front]->lchild!
=NULL)
{
rear=(rear+1)%10;
st[rear]=st[front]->lchild;
}
if(st[front]->rchild!
=NULL)
{
rear=(rear+1)%10;
st[rear]=st[front]->rchild;
}
}
}
else
{
cout<<"家族谱为空"<}
}
voidDispBTTree(BTNode*t)
{
if(t!
=NULL)
{
cout<data;
if((t->lchild!
=NULL)||(t->rchild!
=NULL))
{
cout<<'(';
DispBTTree(t->lchild);
if(t->rchild!
=NULL)
cout<<',';
DispBTTree(t->rchild);
cout<<')';
}
}
}
BTNode*FindNode(BTNode*t,stringx)
{
BTNode*p=NULL;
if(t==NULL)
return0;
elseif(t->data==x)
returnt;
else
{
p=FindNode(t->lchild,x);
if(p!
=NULL)
returnp;
else
returnFindNode(t->rchild,x);
}
}
voidDisancester(BTNode*t,BTNode*r)
{
BTNode*p;
p=r->parent;
cout<data<<"的祖先是:
";
while(p!
=NULL)
{
cout<data<<'';
p=p->parent;
}
cout<}
voidmain()
{
stringx;
stringstr[]={"王威","(","王喜","(","王硕","(","王波","(","王蕊",")",",",
"王涌",")",",","王石","(","王海",")",")",",","王嘉","(","王磊","(","王涛",",","王帆",")",",","王燕",")",")"};
BTNode*t=NULL,*p=NULL;
CreateBTTree(t,str,32);
cout<<"创建的树是:
"<DispBTTree(t);
cout<cout<<"按层遍历家族谱:
"<TravelTree(t);
cout<p=FindNode(t,"王蕊");
Disancester(t,p);
}