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