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