数据结构二叉树操作.docx
《数据结构二叉树操作.docx》由会员分享,可在线阅读,更多相关《数据结构二叉树操作.docx(25页珍藏版)》请在冰豆网上搜索。
数据结构二叉树操作
实验目的:
1.基本要求:
深刻理解二叉树性质和及各种存储结构的特点及适用范围;掌握用指针类型描述、访问和处理二叉树的运算;熟练掌握二叉树的遍历算法;
2.较高要求:
在遍历算法的基础上设计二叉树更复杂操作算法;认识哈夫曼树、哈夫曼编码的作用和意义;掌握树与森林的存储与便利。
实验内容:
1.以二叉链表为存储结构,实现二叉树的创建、遍历(实验类型:
验证型)
1)问题描述:
在主程序中设计一个简单的菜单,分别调用相应的函数功能:
1…建立树
2…前序遍历树
3…中序(非递归)遍历树
4…后序遍历树
0…结束
2)实验要求:
在程序中定义下述函数,并实现要求的函数功能:
CreateTree():
按从键盘输入的前序序列,创建树
PreOrderTree():
前序遍历树(递归)
InOrderTree():
中序(非递归)遍历树
LaOrderTree():
后序遍历树(递归)
3)实验提示:
✧二叉链表存储数据类型定义
#defineElemTypechar//元素类型
typedefstructBiTNode
{ElemTypedata;
structBiTNode*lchild,*rchild;
}BiTNode,*BiTree;
✧元素类型可以根据实际情况选取。
4)注意问题:
✧注意理解递归算法的执行步骤。
✧注意字符类型数据在输入时的处理。
✧重点理解如何利用栈结构实现非递归算法
2.编写算法交换二叉树中所有结点的左、右子树(实验类型:
综合型)
1)问题描述:
编写一算法,交换二叉树中所有结点的左、右子树
2)实验要求:
以二叉链表作为存储结构
3)实现提示:
设二叉树的根指针未t,且以二叉链表表示,可利用一个类型为seqstack的指针来实现,且栈单元包含两个域,一个为data,另一个为top,整个栈容量为maxsize,当树非空时,将当前的树根结点入栈,同时将当前栈顶元素出栈当作根结点,然后依据当前的根结点是否具有孩子结点来判定是否将其左、右指针进行交换;再将交换后的左指针或右指针入栈,这样反复进行,直到栈空为止。
4)注意问题:
✧注意理解算法中栈结构的利用
3.试编写按层次顺序遍历二叉树的算法(实验类型:
综合型)
1)问题描述:
编写按层次顺序遍历二叉树的算法
2)实验要求:
以二叉链表作为存储结构
3)实现提示:
本算法要采用一个队列q,先将二叉树根结点入队列,然后出队列,输出该结点;若它有左子树,便将左子树根结点入队列;若它有右子树,便将右子树根结点入队列,直到队列空为止。
因为队列的特点是先进先出,从而达到按层次顺序遍历二叉树目的。
4)注意问题:
✧理解算法中队列结构的利用
4.实现一个哈夫曼编/译码系统(实验类型:
综合型)
1)问题描述:
利用哈夫曼编码进行信息通信可以大大编写按层次顺序遍历二叉树的算法提高信道利用率,缩短信息传输时间,降低传输成本。
但是,这要求在发送端通过一个编码系统对待传输数据预先编码,在接收端将传来的数据进行译码。
对于双工信道,每端都需要一个完整的编码/译码系统。
试为这样的信息收发站写一个哈夫曼的编/译码系统。
2)实验要求:
一个完整的系统应具有以下功能:
(1)I:
初始化(Initialization)。
从终端读入字符集大小n,以及n个字符和n个权值,建立哈夫曼树,并将它存于文件hfmTree中。
(2)E:
编码(Encoding)。
利用已建好的哈夫曼树对文件ToBeTran中的正文进行编码,然后将结果存入文件CodeFile中。
(3)D:
译码(Decoding)。
利用已建好的哈夫曼树将文件CodeFile中的代码进行译码,结果存入文件TextFile中。
(4)P:
打印代码文件(Print)。
将文件CodeFile以紧凑格式显示在终端上,每行50个代码。
同时将此字符形式的编码文件写入文件CodePrin中。
(5)T:
打印哈夫曼树(Treeprinting)。
将已在内存中的哈夫曼树以直观的方式显示在终端上,同时将此字符形式的哈夫曼树写入文件TreePrint中。
3)实现提示:
(1)文件CodeFile的基类型可以设为字节型。
(2)用户界面可以设计为“菜单”方式:
显示上述功能符号,再加上“Q”,表示退出运行Quit。
请用户键入一个选择功能符。
此功能执行完毕后再显示此菜单,直至某次用户选择了“E”为止。
(3)在程序的一次执行过程中,第一次执行I、D或C命令之后,哈夫曼树已经在内存了,不必再读入。
每次执行中不一定执行I命令,因为文件hfmTree可能早已建好。
5.森林(孩子兄弟链表)的建立与遍历(实验类型:
验证型)
1)问题描述:
以“孩子兄弟二叉链表”为存储结构,建立和遍历一个森林
2)实验要求:
以“孩子兄弟二叉链表”作为存储结构
3)实现提示:
✧可参考二叉树的前序遍历和中序遍历算法
4)注意问题:
✧理解二叉树与树的对应关系
✧理解树和森林遍历的实质
实验内容(应包括实验题目、实验要求、实验任务等)
附录(可包括源程序清单或其它说明)
实验源程序代码如下:
第一题:
#include
#include
#include
#defineMax50
typedefcharEType;
typedefstructBiTNode
{
ETypedata;
structBiTNode*lchild,*rchild;
}BiTNode;
typedefBiTNode*BiTree;
BiTreeroot;
BiTreeCreate()
{
charch;
BiTreeT;
cin>>ch;
if(ch=='#')
T=NULL;
else
{
if(!
(T=(BiTNode*)malloc(sizeof(BiTNode))))
exit
(1);
T->data=ch;
T->lchild=Create();
T->rchild=Create();
}
returnT;
}
voidPreOrderTree(BiTreeroot)/////////前序遍历树(递归)
{
if(root==NULL)
return;
cout<data;
PreOrderTree(root->lchild);
PreOrderTree(root->rchild);
}
voidInOrderTree(BiTreeroot)//////////////中序(非递归)遍历树
{
BiTreep,stack[Max];
inttop=0;
if(root==NULL)
return;
p=root;
while(!
(p==NULL&&top==0))
{
while(p!
=NULL)
{
if(top{
stack[top]=p;
top++;
}
else
{
cout<<"栈溢出!
!
"<return;
}
p=p->lchild;
}
if(top<=0)
return;
else
{
top--;
p=stack[top];
cout<data;
p=p->rchild;
}
}
}
voidLaOrderTree(BiTreeroot)///////////////后序遍历树(递归)
{
if(root==NULL)
return;
PreOrderTree(root->lchild);
PreOrderTree(root->rchild);
cout<data;
}
voidmain()
{
BiTreeT;
intchoice;
for(;;)
{
cout<cout<<"============================="<cout<<"1.建立新树"<cout<<"2.先序遍历树"<cout<<"3.中序遍历树"<cout<<"4.后序遍历树"<cout<<"0.退出程序"<cout<<"请输入选项序号:
"<cin>>choice;
if(choice==0)
break;
switch(choice)
{
case1:
cout<<"开始建立新树:
"<T=Create();
break;
case2:
cout<<"先序遍历树的结果为:
"<PreOrderTree(T);
break;
case3:
cout<<"中序遍历树的结果为:
"<InOrderTree(T);
break;
case4:
cout<<"后序遍历树的结果为:
"<LaOrderTree(T);
break;
}
}
}
第二题:
#include
#include
typedefcharelemtype;
#definemaxnode100
typedefelemtypesqbitree[maxnode];
sqbitreebt;
typedefstructbitnode
{
elemtypedata;
structbitnode*lchild,*rchild,*root;
}bitnode;
typedefbitnode*bitree;
bitreecreate(){
charch;
bitreet;
cout<<"请输入树的节点值"<cin>>ch;
if(ch=='#')
t=NULL;
else
{
if(!
(t=(bitnode*)malloc(sizeof(bitnode))))
exit
(1);
t->data=ch;
t->lchild=create();
t->rchild=create();
}
returnt;
}
voidpreorder(bitreeroot){
if(root)
{
bitreep,stack[maxnode];
inttop=0;
p=root;
while(!
(p==NULL&&top==0))
{
while(p!
=NULL)
{cout<data<<'';
if(top{
stack[top]=p;
top++;
}
p=p->lchild;
}
if(top<=0)return;
else
{
top--;
p=stack[top];
p=p->rchild;
}
}
}
}
voidexchange(bitreet)
{
inttop=0;
bitrees[100];
bitreep,q;
q=t;
if(t)
{
s[++top]=q;
while(top>0)
{
q=s[top--];
if(q->lchild||q->rchild)
{p=q->lchild;
q->lchild=q->rchild;
q->rchild=p;
}
if(q->lchild)s[++top]=q->lchild;
if(q->rchild)s[++top]=q->rchild;
}
}
cout<}
voidmain()
{
bitreet;
t=create();
cout<<"交换前先序遍历为"<preorder(t);
exchange(t);
cout<<"交换后先序遍历为"<第三题:
#include
#include
typedefcharelemtype;
#definemaxnode100
typedefelemtypesqbitree[maxnode];
sqbitreebt;
typedefstructbitnode
{
elemtypedata;
structbitnode*lchild,*rchild,*root;
}bitnode;
typedefbitnode*bitree;
bitreecreate()//创建二叉树
{
charch;
bitreet;
cout<<"请输入树的节点值"<cin>>ch;
if(ch=='#')
t=NULL;
else
{
if(!
(t=(bitnode*)malloc(sizeof(bitnode))))
exit
(1);
t->data=ch;
t->lchild=create();
t->rchild=create();
}
returnt;
}
voidpreorder(bitreeroot)//非递归先序遍历二叉树
{
if(root)
{
bitreep,stack[maxnode];
inttop=0;
p=root;
while(!
(p==NULL&&top==0))
{
while(p!
=NULL)
{cout<data<<'';
if(top{
stack[top]=p;
top++;
}
p=p->lchild;
}
if(top<=0)return;
else
{
top--;
p=stack[top];
p=p->rchild;
}
}
}
}
voidinorder(bitreeroot)//递归中序遍历二叉树
{
if(root==NULL)return;
else
preorder(root->lchild);
cout<data;cout<<"";
preorder(root->rchild);
}
voidpostorder(bitreeroot)//非递归后序遍历二叉树
{
if(root==NULL)return;
preorder(root->lchild);
preorder(root->rchild);
cout<data;
}
voidmain()
{
bitreep;
p=create();
cout<<"先序遍历顺序为:
"<preorder(p);cout<cout<<"中序遍历顺序为:
"<inorder(p);cout<cout<<"后序遍历顺序为:
"<postorder(p);cout<}////递归后序遍历二叉树
typedefstruct{
BiTreelink;
intflag;
}StackType;
voidPostOrderTraverse2(BiTreeT)
{
StackTypetm[MAX];
BiTreep;
inttop=0,sign;
p=T;
while(!
(top==0&&p==NULL))
{if(p!
=NULL)
{
tm[top].link=p;
tm[top].flag=1;
top++;
p=p->lchild;
}else{
top--;
p=tm[top].link;
sign=tm[top].flag;
if(sign==1)
{
tm[top].link=p;
tm[top].flag=2;
top++;
p=p->rchild;
}
else{
cout<data<<'';
p=NULL;
}
}
}
}
voidmain()
{BiTreeT;
T=CreateBitree2();//charstr[80];
//cout<<"输入一组字符以建立二叉树"<//T=CreateBitree1();
//cin.getline(str,80,'\n');
PreOrderTraverse1(T);
cout<PreOrderTraverse2(T);
cout<InOrderTraverse1(T);
cout<InOrderTraverse2(T);
cout<PostOrderTraverse1(T);
cout<PostOrderTraverse2(T);
cout<}
第四题:
#include
#include
#include
#include
#defineN20
typedefstructstack
{
chardata[N];
inttop;
};
stack*creat()
{
stack*S;
S=(stack*)malloc(sizeof(stack));
S->top=-1;
returnS;
}
stack*push(stack*S,chara)
{
S->top++;
S->data[S->top]=a;
returnS;
}
stack*pop(stack*S)
{
chara;
a=S->data[S->top];
S->top--;
returnS;
}
voidmain()
{
chars[N];
stack*S;
S=creat();
cout<<"请输入内容:
"<cin>>s;
for(inti=0;s[i]!
='\0';i++)
{
push(S,s[i]);
}
for(i;s[i]!
='\0';i--)
{
if(s[i]==S->data[S->top])
pop(S);
else
continue;
}
if(S->top==-1)
cout<<"是回文!
!
"<else
cout<<"不是回文!
!
"<}
第五题:
#include
#include
#include
#include
#defineMAX100
typedefcharTElemType;
typedefstructCSNode
{
TElemTypedata;
CSNode*firstchild,*nextsibling;
}CSNode,*CSTree;
typedefCSTreeQElemType;
typedefstructQNode
{
QElemTypedata;
QNode*next;
}*QueuePtr;
structLinkQueue
{
QueuePtrfront,rear;
};
voidInitQueue(LinkQueue&Q)
{
if(!
(Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode))))
exit
(1);
Q.front->next=NULL;
}
intQueueEmpty(LinkQueueQ)
{
if(Q.front->next==NULL)
return1;
else
return0;
}
voidEnQueue(LinkQueue&Q,QElemTypee)
{
QueuePtrp;
if(!
(p=(QueuePtr)malloc(sizeof(QNode))))
exit
(1);
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
}
voidDeQueue(LinkQueue&Q,QElemType&e)
{
QueuePtrp;
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p)
Q.rear=Q.front;
free(p);
}
CSTreeCreateCSTree()
{CSTreeT;
charc[20];
CSTreep,p1;
LinkQueueq;
inti,m;
InitQueue(q);
cout<<"输入根节点(直接输入回车表示树为空)"<cin>>c[0];
if(c[0]!
='\n')
{
T=(CSTree)malloc(sizeof(CSNode));
T->data=c[0];
T->nextsibling=NULL;
EnQueue(q,T);
while(!
QueueEmpty(q))
{
DeQueue(q,p);
cout<<"请按长幼顺序输入"<data<<"的所有子孩子"<gets(c);
m=strlen(c);
if(m>0)
{
p1=p->firstchild=(CSTree)malloc(sizeof(CSNode));
p1->data=c[0];
for(i=1;i{
p1->nextsibling=(CSTree)malloc(sizeof(CSNode));
EnQueue(q,