实验三二叉树的遍历.docx
《实验三二叉树的遍历.docx》由会员分享,可在线阅读,更多相关《实验三二叉树的遍历.docx(21页珍藏版)》请在冰豆网上搜索。
![实验三二叉树的遍历.docx](https://file1.bdocx.com/fileroot1/2023-1/26/88aac114-b1a6-4dff-bec0-e27913f03145/88aac114-b1a6-4dff-bec0-e27913f031451.gif)
实验三二叉树的遍历
实验三 二叉树的遍历
一、实验目的
1.进一步掌握指针变量的含义。
2.掌握二叉树的结构特征,以及各种存储结构的特点及使用范围。
3.掌握用指针类型描述、访问和处理二叉树的运算。
二、实验要求
1.认真阅读和掌握本实验的参考程序。
2.按照对二叉树的操作需要,在创建好二叉树后再通过遍历算法验证创建结果。
3.保存程序的运行结果,并结合程序进行分析。
三、实验内容
以下参考程序是按完全二叉树思想将输入的字符串生成二叉树,并通过遍历来验证二叉树创建正确与否,但不能创建非完全二叉树,请认真研究该程序,然后模仿教材例6.4初始化方式创建二叉树:
所有的空指针均用#表示,如教材图6-13对应的二叉树,建立时的初始序列为:
AB#D##CE##F##。
参考程序:
#definemax30
#defineNULL0
#include
#include
typedefstructBNode
{
chardata;/*数据域*/
structBNode*lchild,*rchild;;//指向左右子女
}BinTree;
voidpreorder(BinTree*t);//声明先根遍历函数
voidinorder(BinTree*t);//声明中根遍历函数
voidpostorder(BinTree*t);//声明后根遍历函数
intleafs(BinTree*b);//声明求叶子数函数
inttreedeep(BinTree*p);//声明求树的深度函数
BinTree*swap(BinTree*p);//声明交换二叉树的所有结点的左右子树的函数
//将字符串中的第i个字符开始的m个字符作为数据生成对应的二叉树
BinTree*cre_tree(char*str,inti,intm)
{
BinTree*p;
if(i>=m)//无效结点
returnNULL;
p=(BinTree*)malloc(sizeof(BinTree));//生成新结点
p->data=str[i];
p->lchild=cre_tree(str,2*i+1,m);//创建新结点的左子树
p->rchild=cre_tree(str,2*i+2,m);//创建新结点的右子树
returnp;
}
voidmain()
{
inti,n;
charstr[max];
BinTree*root;//根结点
printf("请输入二叉树的结点数:
");
scanf("%d",&n);
getchar();//输入数字
printf("请输入长度为%d的字符串:
",n);
for(i=0;iscanf("%c",&str[i]);
printf("\n");
root=cre_tree(str,0,n);
printf("二叉树已成功创建!
结点序列为:
");
for(i=0;iprintf("%c",str[i]);
printf("\n");
//先根遍历
printf("\n先根遍历结果:
");
preorder(root);
printf("\n");
//中根遍历
printf("\n中根遍历结果:
");
inorder(root);
printf("\n");
//后根遍历
printf("\n后根遍历结果:
");
postorder(root);
printf("\n");
printf("\n叶子数为:
%d\n",leafs(root));
printf("\n树的深度为:
%d\n",treedeep(root));
printf("\n交换左右子树后先序遍历序列为:
");
preorder(swap(root));
printf("\n\n");
}
voidpreorder(BinTree*t)
{
if(t!
=NULL)
{
printf("%c",t->data);
if(t->lchild)
{
printf("->");
preorder(t->lchild);
}
if(t->rchild)
{
printf("->");
preorder(t->rchild);
}
}
}
voidinorder(BinTree*t)
{
if(t!
=NULL)
{
inorder(t->lchild);
printf("%c",t->data);
inorder(t->rchild);
}
}
voidpostorder(BinTree*t)
{
if(t!
=NULL)
{
postorder(t->lchild);
postorder(t->rchild);
printf("%c",t->data);
}
}
intleafs(BinTree*b)//求叶子数
{
intnum1,num2;
if(b==NULL)return(0);
elseif(b->lchild==NULL&&b->rchild==NULL)return
(1);
else
{
num1=leafs(b->lchild);
num2=leafs(b->rchild);
return(num1+num2);
}
}
inttreedeep(BinTree*p)//求树的深度
{
intldeep,rdeep,deep;
if(p==NULL)deep=0;
else
{
ldeep=treedeep(p->lchild);
rdeep=treedeep(p->rchild);
deep=(ldeep>rdeep?
ldeep:
rdeep)+1;
}
returndeep;
}
BinTree*swap(BinTree*p)//交换二叉树的所有结点的左右子树
{
BinTree*stack[max];
intk=0;
stack[k]=NULL;
if(p!
=NULL)
{
stack[++k]=p->lchild;
p->lchild=p->rchild;
p->rchild=stack[k];
p->lchild=swap(p->lchild);
p->rchild=swap(p->rchild);
}
returnp;
}
//按完全二叉树思想将输入的字符串生成二叉树
#definemax30
#defineNULL0
#include
#include
typedefstructBnode
{chardata;
structBnode*lchild,*rchild;
}Btree;
voidpreorder(Btree*t)
{if(t)
{printf("%c",t->data);
if(t->lchild)
{printf("->");
preorder(t->rchild);
}
if(t->rchild)
{printf("->");
preorder(t->rchild);
}
}
}
voidinorder(Btree*t)
{if(t)
{inorder(t->lchild);
printf("%c",t->data);
inorder(t->rchild);
}
}
voidpostorder(Btree*t)
{if(t)
{postorder(t->lchild);
postorder(t->rchild);
printf("%c",t->data);
}
}
intleafs(Btree*b)
{intnum1,num2;
if(b==NULL)return(0);
elseif(b->lchild==NULL&&b->rchild==NULL)return
(1);
else
{
num1=leafs(b->lchild);
num2=leafs(b->rchild);
return(num1+num2);
}
}
inttreedeep(Btree*p)
{intldeep,rdeep,deep;
if(p==NULL)deep=0;
else
{ldeep=treedeep(p->lchild);
rdeep=treedeep(p->rchild);
deep=(ldeep>rdeep?
ldeep:
rdeep)+1;
}
returndeep;
}
Btree*swap(Btree*p)
{Btree*stack[max];
intk=0;
stack[k]=NULL;
if(p!
=NULL)
{stack[++k]=p->lchild;
p->lchild=p->rchild;
p->rchild=stack[k];
p->lchild=swap(p->lchild);
p->rchild=swap(p->rchild);
}
returnp;
}
Btree*cretree(char*str,inti,intm)
{Btreep;
if(i>=m)
returnNULL;
p=(Btree*)malloc(sizeof(Btree));
p->data=str[i];
p->lchild=cretree(str,2*i+1,m);
p->rchild=cretree(str,2*i+2,m);
returnp;
}
voidmain()
{inti,n;
charstr[max];
Btree*root;
printf("请输入二叉树的结点数:
");
scanf("%d",&n);
getchar();//输入数字
printf("请输入长度为%d的字符串:
",n);
for(i=0;iscanf("%c",&str[i]);
printf("\n");
root=cre_tree(str,0,n);
printf("二叉树已成功创建!
结点序列为:
");
for(i=0;iprintf("%c",str[i]);
printf("\n");
//先根遍历
printf("\n先根遍历结果:
");
preorder(root);
printf("\n");
//中根遍历
printf("\n中根遍历结果:
");
inorder(root);
printf("\n");
//后根遍历
printf("\n后根遍历结果:
");
postorder(root);
printf("\n");
printf("\n叶子数为:
%d\n",leafs(root));
printf("\n树的深度为:
%d\n",treedeep(root));
printf("\n交换左右子树后先序遍历序列为:
");
preorder(swap(root));
printf("\n\n");
}
#definemax30
#include
#include
typedefstructBnode
{chardata;
structBnode*lchild,*rchild;
}Btree;
voidpreorder(Btree*t)
{if(t)
{printf("%c",t->data);
if(t->lchild)
{printf("->");
preorder(t->lchild);
}
if(t->rchild)
{printf("->");
preorder(t->rchild);
}
}
}
voidinorder(Btree*t)
{if(t)
{inorder(t->lchild);
printf("%c",t->data);
inorder(t->rchild);
}
}
voidpostorder(Btree*t)
{if(t)
{postorder(t->lchild);
postorder(t->rchild);
printf("%c",t->data);
}
}
intleafs(Btree*b)
{intnum1,num2;
if(b==NULL)return(0);
elseif(b->lchild==NULL&&b->rchild==NULL)return
(1);
else
{
num1=leafs(b->lchild);
num2=leafs(b->rchild);
return(num1+num2);
}
}
inttreedeep(Btree*p)
{intldeep,rdeep,deep;
if(p==NULL)deep=0;
else
{ldeep=treedeep(p->lchild);
rdeep=treedeep(p->rchild);
deep=(ldeep>rdeep?
ldeep:
rdeep)+1;
}
returndeep;
}
Btree*swap(Btree*p)
{Btree*stack[max];
intk=0;
stack[k]=NULL;
if(p!
=NULL)
{stack[++k]=p->lchild;
p->lchild=p->rchild;
p->rchild=stack[k];
p->lchild=swap(p->lchild);
p->rchild=swap(p->rchild);
}
returnp;
}
Btree*cretree()
{Btree*p;
charch;
ch=getchar();
if(ch=='#')returnNULL;
else
{
p=(Btree*)malloc(sizeof(Btree));
p->data=ch;
p->lchild=cretree();
p->rchild=cretree();
}
returnp;
}
voidmain()
{
Btree*root;
;
root=cretree();
printf("\n");
//先根遍历
printf("\n先根遍历结果:
");
preorder(root);
printf("\n");
//中根遍历
printf("\n中根遍历结果:
");
inorder(root);
printf("\n");
//后根遍历
printf("\n后根遍历结果:
");
postorder(root);
printf("\n");
printf("\n叶子数为:
%d\n",leafs(root));
printf("\n树的深度为:
%d\n",treedeep(root));
printf("\n交换左右子树后先序遍历序列为:
");
preorder(swap(root));
printf("\n\n");
}改过#definemax30
#include
#include
typedefstructBnode
{chardata;
structBnode*lchild,*rchild;
}Btree;
voidpreorder(Btree*t)
{if(t)
{printf("%c",t->data);
if(t->lchild)
{printf("->");
preorder(t->lchild);
}
if(t->rchild)
{printf("->");
preorder(t->rchild);
}
}
}
voidinorder(Btree*t)
{if(t)
{inorder(t->lchild);
printf("%c",t->data);
inorder(t->rchild);
}
}
voidpostorder(Btree*t)
{if(t)
{postorder(t->lchild);
postorder(t->rchild);
printf("%c",t->data);
}
}
intleafs(Btree*b)
{intnum1,num2;
if(b==NULL)return(0);
elseif(b->lchild==NULL&&b->rchild==NULL)return
(1);
else
{
num1=leafs(b->lchild);
num2=leafs(b->rchild);
return(num1+num2);
}
}
inttreedeep(Btree*p)
{intldeep,rdeep,deep;
if(p==NULL)deep=0;
else
{ldeep=treedeep(p->lchild);
rdeep=treedeep(p->rchild);
deep=(ldeep>rdeep?
ldeep:
rdeep)+1;
}
returndeep;
}
Btree*swap(Btree*p)
{Btree*stack[max];
intk=0;
stack[k]=NULL;
if(p!
=NULL)
{stack[++k]=p->lchild;
p->lchild=p->rchild;
p->rchild=stack[k];
p->lchild=swap(p->lchild);
p->rchild=swap(p->rchild);
}
returnp;
}
Btree*cretree()
{Btree*p;
charch;
if((ch=getchar())=='#')returnNULL;
else
{
p=(Btree*)malloc(sizeof(Btree));
p->data=ch;
p->lchild=cretree();
p->rchild=cretree();
}
returnp;
}
voidmain()
{
Btree*root;
root=cretree();
printf("\n");
//先根遍历
printf("\n先根遍历结果:
");
preorder(root);
printf("\n");
//中根遍历
printf("\n中根遍历结果:
");
inorder(root);
printf("\n");
//后根遍历
printf("\n后根遍历结果:
");
postorder(root);
printf("\n");
printf("\n叶子数为:
%d\n",leafs(root));
printf("\n树的深度为:
%d\n",treedeep(root));
printf("\n交换左右子树后先序遍历序列为:
");
preorder(swap(root));
printf("\n\n");
}
#include"stdio.h"
#include"string.h"
#include
#include
#defineMax20//结点的最大个数
usingnamespacestd;
typedefstructnode{
chardata;
structnode*lchild,*rchild;
}BinTNode;//自定义二叉树的结点类型
typedefBinTNode*BinTree;//定义二叉树的指针
intNodeNum,leaf;//NodeNum为结点数,leaf为叶子数
BinTreeCreatBinTree(void)
{//==========基于先序遍历算法创建二叉树==============
//=====要求输入先序序列,其中加入虚结点"#"以示空指针的位置==========
BinTreeT;
charch;
if((ch=getchar())=='#')
return(NULL);//读入#,返回空指针
else{
T=(BinTNode*)malloc(sizeof(BinTNode));//生成结点
T->data=ch;
T->lchild=CreatBinTree();//构造左子树
T->rchild=CreatBinTree();//构造右子树
return(T);
}
}
voidPreorder(BinTreebt)
{//========NLR先序遍历=============
if(bt!
=NULL){
printf("%c",bt->data);//访问结点
Preorder(bt->lchild);//先序遍历左子树
Preorder(bt->rchild);//先序遍历右子树
}
}
voidInorder(BinTreebt)
{//========LNR中序遍历===============
if(bt!
=NULL){
Inorder(bt->lchild);
printf("%c",bt->data);
Inorder(bt->rchild);
}
}
voidPostorder(BinTreebt)
{
if(bt!
=NULL)
{
Postorder(bt->lchild);
Postorder(bt->rchild);
printf("%c",bt->data);
}
}
intTreeDepth(BinTreebt)
{
inthl,hr,max;
if(bt)
{
hl=TreeDepth(bt->lchild);