二叉树操作二叉树操作
实验七 二叉树操作
实验目的:
1、熟悉将算法转换成程序代码的过程。 2、了解二叉树的逻辑结构特性,熟练掌握二叉树的存储方法。
3、熟练掌握数二叉树的基本操作。
实验内容:
1、用先序序列生成二叉树;
2、给出先序遍历,中序遍历和后序遍历各项操作结果。 3、给出二叉树的高度。
4、给出二叉树的叶子数。
5、二叉树的每层结点数。
6、查找二叉树的是否存在结点x。
实验步骤:
#include
#include
#include
#include
typedef char Dat...
二叉树操作
实验七 二叉树操作
实验目的:
1、熟悉将算法转换成程序代码的过程。 2、了解二叉树的逻辑结构特性,熟练掌握二叉树的存储方法。
3、熟练掌握数二叉树的基本操作。
实验内容:
1、用先序序列生成二叉树;
2、给出先序遍历,中序遍历和后序遍历各项操作结果。 3、给出二叉树的高度。
4、给出二叉树的叶子数。
5、二叉树的每层结点数。
6、查找二叉树的是否存在结点x。
实验步骤:
#include
#include
#include
#include
typedef char DataType;
/* 不妨设结点内容的数据类型为字符型 */ //int count=0; typedef struct bnode { DataType data;
struct bnode *lchild, *rchild; } Bnode,*Bintree;
void CreateBinTree( Bintree *T) {/* 以加入空结点的先序序列输入,构造二叉链表 */
char ch;
printf("\n输入二叉树的结点数据域内容:");
ch=getch();
printf("%c",ch);
if(ch=='#')
{
*T=NULL;
}
else
{
*T=(Bnode *)malloc(sizeof(Bnode));
(*T)->data=ch;
CreateBinTree(&((*T)->lchild));
CreateBinTree(&((*T)->rchild));
}
return;
}
void preorder(Bnode *t)
{ if(t)
{ printf("%c\t",t->data);
preorder(t->lchild);
preorder(t->rchild);
}
}
void InOrder ( Bnode *t ) {
if ( t )
{
InOrder ( t->lchild );
printf("%c\t",t->data);
InOrder ( t->rchild );
}
}
void PostOrder ( Bnode * t ) {
if ( t )
{
PostOrder ( t->lchild );
PostOrder ( t->rchild );
printf("%c\t",t->data);
}
}
int Count( Bnode *t ) {
int lcount,rcount;
if (t==NULL) return 0;
else
{
lcount=Count(t->lchild); rcount=Count(t->rchild);
return lcount+rcount+1; }
}
int Height ( Bnode * t ) { int h1,h2;
if (t==NULL) return 0;
else
{
h1=Height ( t->lchild ); /*求左子树的高度*/
h2=Height ( t->rchild ); /*求右子树的高度*/
if (h1>h2) return h1+1;
return h2+1;
}
}
void Levcount (Bnode *t, int L, int num[ ] )
/* 求链式存储的二叉树t中每层结点个数* /
/*L是当前t所指结点对应的层次,每层结点个数放在num数组中,主函数 调用前,num数组全赋0,t指向树根,所以L初始赋1 */
{
if ( t)
{
num[L]++;
Levcount (t->lchild, L+1, num);
Levcount (t->rchild, L+1, num);
}
}
int Leaves_Num1(Bintree t)
{
if(t)
{
if(t->lchild==NULL&&t->rchild==NULL)
{
return 1;
}
return Leaves_Num1(t->lchild)+Leaves_Num1(t->rchild);
}
return 0;
}
/****************************************************/
/* 查找某个信息是否在这棵树中 */
/****************************************************/
Bintree locate_x(Bintree t,char x)
{
(由学生填写)
}
void main()
{
(由学生填写)
}
实验用测试数据和相关结果分析:(由学生填写)
实验:(由学生填写)
本文档为【二叉树操作】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。