ImageVerifierCode 换一换
格式:DOCX , 页数:21 ,大小:100.40KB ,
资源ID:7868386      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/7868386.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(实验三二叉树的遍历.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

实验三二叉树的遍历.docx

1、实验三 二叉树的遍历实验三 二叉树的遍历一、 实验目的1 进一步掌握指针变量的含义。2 掌握二叉树的结构特征,以及各种存储结构的特点及使用范围。3 掌握用指针类型描述、访问和处理二叉树的运算。二、 实验要求1 认真阅读和掌握本实验的参考程序。2 按照对二叉树的操作需要,在创建好二叉树后再通过遍历算法验证创建结果。3 保存程序的运行结果,并结合程序进行分析。三、 实验内容 以下参考程序是按完全二叉树思想将输入的字符串生成二叉树,并通过遍历来验证二叉树创建正确与否,但不能创建非完全二叉树,请认真研究该程序,然后模仿教材例6.4初始化方式创建二叉树:所有的空指针均用#表示,如教材图6-13对应的二叉

2、树,建立时的初始序列为:AB#D#CE#F#。参考程序:#define max 30#define NULL 0#include #include typedef struct BNode char data; /*数据域 */ struct BNode *lchild,*rchild; /指向左右子女 BinTree;void preorder(BinTree *t); /声明先根遍历函数void inorder(BinTree *t); /声明中根遍历函数void postorder(BinTree *t);/声明后根遍历函数int leafs(BinTree *b); /声明求叶子数函数

3、int treedeep(BinTree *p); /声明求树的深度函数BinTree *swap(BinTree *p); /声明交换二叉树的所有结点的左右子树的函数/将字符串中的第i个字符开始的m个字符作为数据生成对应的二叉树BinTree *cre_tree(char *str,int i,int m) BinTree *p; if(i=m) /无效结点 return NULL; p=(BinTree *)malloc(sizeof(BinTree);/生成新结点 p-data=stri; p-lchild=cre_tree(str,2*i+1,m);/创建新结点的左子树 p-rchil

4、d=cre_tree(str,2*i+2,m);/创建新结点的右子树 return p;void main() int i,n; char strmax; BinTree *root;/根结点 printf(请输入二叉树的结点数:); scanf(%d,&n); getchar();/输入数字 printf(请输入长度为 %d 的字符串 :,n); for(i=0;in;i+) scanf(%c,&stri); printf(n); root=cre_tree(str,0,n); printf(二叉树已成功创建! 结点序列为:); for(i=0;idata); if(t-lchild) pr

5、intf(-); preorder(t-lchild); if(t-rchild) printf(-); preorder(t-rchild); void inorder(BinTree *t) if(t!=NULL) inorder(t-lchild); printf( %c ,t-data); inorder(t-rchild); void postorder(BinTree *t) if(t!=NULL) postorder(t-lchild); postorder(t-rchild); printf( %c ,t-data); int leafs(BinTree *b)/求叶子数 in

6、t num1,num2; if(b=NULL) return (0); else if(b-lchild=NULL & b-rchild=NULL) return (1); else num1=leafs(b-lchild); num2=leafs(b-rchild); return(num1+num2); int treedeep(BinTree *p)/求树的深度 int ldeep,rdeep,deep; if(p=NULL) deep=0; else ldeep=treedeep(p-lchild); rdeep=treedeep(p-rchild); deep=(ldeeprdeep

7、?ldeep:rdeep)+1; return deep;BinTree *swap(BinTree *p)/交换二叉树的所有结点的左右子树 BinTree *stackmax; int k=0; stackk=NULL; if(p!=NULL) stack+k=p-lchild; p-lchild=p-rchild; p-rchild=stackk; p-lchild=swap(p-lchild); p-rchild=swap(p-rchild); return p;/按完全二叉树思想将输入的字符串生成二叉树#define max 30#define NULL 0#include#inclu

8、detypedef struct Bnode char data; struct Bnode *lchild,*rchild;Btree;void preorder(Btree *t) if(t) printf(%c,t-data); if(t-lchild) printf(-); preorder(t-rchild); if(t-rchild) printf(-); preorder(t-rchild); void inorder(Btree *t) if(t) inorder(t-lchild); printf(%c,t-data); inorder(t-rchild);void post

9、order(Btree *t) if(t) postorder(t-lchild); postorder(t-rchild); printf(%c,t-data);int leafs(Btree *b) int num1,num2; if(b=NULL) return(0); else if(b-lchild=NULL&b-rchild=NULL) return(1); else num1=leafs(b-lchild); num2=leafs(b-rchild); return(num1+num2); int treedeep(Btree *p) int ldeep,rdeep,deep;

10、if(p=NULL) deep=0; else ldeep=treedeep(p-lchild); rdeep=treedeep(p-rchild); deep=(ldeeprdeep?ldeep:rdeep)+1; return deep;Btree *swap(Btree *p) Btree *stackmax; int k=0; stackk=NULL; if(p!=NULL) stack+k=p-lchild; p-lchild=p-rchild; p-rchild=stackk; p-lchild=swap(p-lchild); p-rchild=swap(p-rchild); re

11、turn p;Btree *cretree(char *str,int i,int m) Btree p; if(i=m) return NULL; p=(Btree *)malloc(sizeof(Btree); p-data=stri; p-lchild=cretree(str,2*i+1,m); p-rchild=cretree(str,2*i+2,m); return p;void main() int i,n; char strmax; Btree *root; printf(请输入二叉树的结点数:); scanf(%d,&n); getchar();/输入数字 printf(请输入

12、长度为 %d 的字符串 :,n); for(i=0;in;i+) scanf(%c,&stri); printf(n); root=cre_tree(str,0,n); printf(二叉树已成功创建! 结点序列为:); for(i=0;in;i+) printf( %c ,stri); printf(n); /先根遍历 printf(n先根遍历结果:); preorder(root); printf(n); /中根遍历 printf(n中根遍历结果:); inorder(root); printf(n); /后根遍历 printf(n后根遍历结果:); postorder(root); pr

13、intf(n); printf(n叶子数为:%dn,leafs(root); printf(n树的深度为:%dn,treedeep(root); printf(n交换左右子树后先序遍历序列为:); preorder(swap(root); printf(nn);#define max 30#include#includetypedef struct Bnode char data; struct Bnode *lchild,*rchild;Btree;void preorder(Btree *t) if(t) printf(%c,t-data); if(t-lchild) printf(-);

14、 preorder(t-lchild); if(t-rchild) printf(-); preorder(t-rchild); void inorder(Btree *t) if(t) inorder(t-lchild); printf(%c,t-data); inorder(t-rchild);void postorder(Btree *t) if(t) postorder(t-lchild); postorder(t-rchild); printf(%c,t-data);int leafs(Btree *b) int num1,num2; if(b=NULL) return(0); el

15、se if(b-lchild=NULL&b-rchild=NULL) return(1); else num1=leafs(b-lchild); num2=leafs(b-rchild); return(num1+num2); int treedeep(Btree *p) int ldeep,rdeep,deep; if(p=NULL) deep=0; else ldeep=treedeep(p-lchild); rdeep=treedeep(p-rchild); deep=(ldeeprdeep?ldeep:rdeep)+1; return deep;Btree *swap(Btree *p

16、) Btree *stackmax; int k=0; stackk=NULL; if(p!=NULL) stack+k=p-lchild; p-lchild=p-rchild; p-rchild=stackk; p-lchild=swap(p-lchild); p-rchild=swap(p-rchild); return p;Btree *cretree() Btree *p; char ch; ch=getchar(); if(ch=#) return NULL; else p=(Btree *)malloc(sizeof(Btree); p-data=ch; p-lchild=cret

17、ree(); p-rchild=cretree(); return p;void main() 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叶子数为:%dn,leafs(root); printf(n树的深度为:%dn,treedeep(

18、root); printf(n交换左右子树后先序遍历序列为:); preorder(swap(root); printf(nn);改过 #define max 30#include#includetypedef struct Bnode char data; struct Bnode *lchild,*rchild;Btree;void preorder(Btree *t) if(t) printf(%c,t-data); if(t-lchild) printf(-); preorder(t-lchild); if(t-rchild) printf(-); preorder(t-rchild)

19、; void inorder(Btree *t) if(t) inorder(t-lchild); printf(%c,t-data); inorder(t-rchild);void postorder(Btree *t) if(t) postorder(t-lchild); postorder(t-rchild); printf(%c,t-data);int leafs(Btree *b) int num1,num2; if(b=NULL) return(0); else if(b-lchild=NULL&b-rchild=NULL) return(1); else num1=leafs(b

20、-lchild); num2=leafs(b-rchild); return(num1+num2); int treedeep(Btree *p) int ldeep,rdeep,deep; if(p=NULL) deep=0; else ldeep=treedeep(p-lchild); rdeep=treedeep(p-rchild); deep=(ldeeprdeep?ldeep:rdeep)+1; return deep;Btree *swap(Btree *p) Btree *stackmax; int k=0; stackk=NULL; if(p!=NULL) stack+k=p-

21、lchild; p-lchild=p-rchild; p-rchild=stackk; p-lchild=swap(p-lchild); p-rchild=swap(p-rchild); return p;Btree *cretree() Btree *p; char ch; if(ch=getchar()=#) return NULL; else p=(Btree *)malloc(sizeof(Btree); p-data=ch; p-lchild=cretree(); p-rchild=cretree(); return p;void main() Btree *root; root=c

22、retree(); printf(n); /先根遍历 printf(n先根遍历结果:); preorder(root); printf(n); /中根遍历 printf(n中根遍历结果:); inorder(root); printf(n); /后根遍历 printf(n后根遍历结果:); postorder(root); printf(n); printf(n叶子数为:%dn,leafs(root); printf(n树的深度为:%dn,treedeep(root); printf(n交换左右子树后先序遍历序列为:); preorder(swap(root); printf(nn);#inc

23、ludestdio.h#includestring.h#include#include#define Max 20 /结点的最大个数using namespace std;typedef struct node char data; struct node *lchild,*rchild;BinTNode; /自定义二叉树的结点类型typedef BinTNode *BinTree; /定义二叉树的指针int NodeNum,leaf; /NodeNum为结点数,leaf为叶子数BinTree CreatBinTree(void)/=基于先序遍历算法创建二叉树=/=要求输入先序序列,其中加入虚

24、结点#以示空指针的位置= BinTree T; char ch; if(ch=getchar()=#) return(NULL); /读入#,返回空指针 else T=(BinTNode *)malloc(sizeof(BinTNode); /生成结点 T-data=ch; T-lchild=CreatBinTree(); /构造左子树 T-rchild=CreatBinTree(); /构造右子树 return(T); void Preorder(BinTree bt)/=NLR 先序遍历= if(bt!=NULL) printf(%c,bt-data); /访问结点 Preorder(bt

25、-lchild); /先序遍历左子树 Preorder(bt-rchild); /先序遍历右子树 void Inorder(BinTree bt)/=LNR 中序遍历= if(bt!=NULL) Inorder(bt-lchild); printf(%c,bt-data); Inorder(bt-rchild);void Postorder(BinTree bt) if(bt!=NULL) Postorder(bt-lchild); Postorder(bt-rchild); printf(%c,bt-data); int TreeDepth(BinTree bt) int hl,hr,max; if(bt) hl=TreeDepth(bt-lchild);

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1