1、(6)输出二叉树b的叶子结点个数。2编写一程序,实现二叉树的先序遍历、中序遍历和后序遍历的各种递归和非递归算法,以及层次遍历的算法。三、实验预习内容二叉树存储结构,二叉树基本运算(创建二叉树、寻找结点、找孩子结点、求高度、输出二叉树)三、实验结果与分析 7-1#include malloc.h#define MaxSize 100typedef char ElemType;typedef struct node ElemType data; struct node *lchild; struct node *rchild; BTNode;void CreateBTNode(BTNode *&b
2、,char *str) BTNode *StMaxSize,*p=NULL; int top=-1,k,j=0; char ch; b=NULL; ch=strj; while (ch!=0) switch(ch) case (:top+;Sttop=p;k=1; break;)top-;break;,k=2; default:p=(BTNode *)malloc(sizeof(BTNode); p-data=ch;p-lchild=p-rchild=NULL; if (b=NULL) b=p; else switch(k) case 1:Sttop-lchild=p; case 2:rchi
3、ld=p; j+; ch=strj; BTNode *FindNode(BTNode *b,ElemType x) BTNode *p; if (b=NULL) return NULL; else if (b-data=x) return b; else p=FindNode(b-lchild,x); if (p!=NULL) return p; else return FindNode(b-rchild,x);BTNode *LchildNode(BTNode *p) return p-lchild;BTNode *RchildNode(BTNode *p)rchild;int BTNode
4、Depth(BTNode *b) int lchilddep,rchilddep; if (b=NULL) return(0); lchilddep=BTNodeDepth(b-lchild); rchilddep=BTNodeDepth(b-rchild); return (lchilddeprchilddep)? (lchilddep+1):(rchilddep+1);void DispBTNode(BTNode *b) if (b!=NULL) printf(%c,b-data); if (b-lchild!=NULL | b-rchild! printf(); DispBTNode(b
5、- if (b-=NULL) printf(,)int BTWidth(BTNode *b) struct int lno; BTNode *p; QuMaxSize; int front,rear; int lnum,max,i,n; front=rear=0; rear+; Qurear.p=b; Qurear.lno=1; while (rear!=front) front+; b=Qufront.p; lnum=Qufront.lno;=NULL) rear+; Qurear.p=b- Qurear.lno=lnum+1; max=0;lnum=1;i=1; while (i=rear
6、) n=0; while (imax) max=n; return max; return 0;int Nodes(BTNode *b) int num1,num2; if (b=NULL) lchild=NULL & b-rchild=NULL) return 1; num1=Nodes(b- num2=Nodes(b- return (num1+num2+1);int LeafNodes(BTNode *b) num1=LeafNodes(b- num2=LeafNodes(b- return (num1+num2);void main() BTNode *b,*p,*lp,*rp; Cr
7、eateBTNode(b,A(B(D,E(H(J,K(L,M(,N),C(F,G(,I) printf(输出二叉树:DispBTNode(b);printf(nH结点: p=FindNode(b, if (p! lp=LchildNode(p); if (lp!左孩子为%c ,lp- else无左孩子 rp=RchildNode(p); if (rp!右孩子为%c,rp-无右孩子 二叉树b的深度:%dn,BTNodeDepth(b);二叉树b的宽度:,BTWidth(b);二叉树b的结点个数:,Nodes(b);二叉树b的叶子结点个数:,LeafNodes(b);7-2void PreOrde
8、r(BTNode *b)%c PreOrder(b-void PreOrder1(BTNode *b) BTNode *StMaxSize,*p; int top=-1; top+; Sttop=b; while (top-1) p=Sttop; top-;,p- if (p- Sttop=p-void InOrder(BTNode *b) InOrder(b-void InOrder1(BTNode *b) p=b; while (top-1 | p! while (p! top+; Sttop=p; p=p- if (top-1) p=Sttop; top-; printf(void Po
9、stOrder(BTNode *b) PostOrder(b-void PostOrder1(BTNode *b) BTNode *StMaxSize; int flag,top=-1; do while (b! Sttop=b; b=b- p=NULL; flag=1; while (top!=-1 & flag) b=Sttop; if (b-rchild=p) printf( top-; p=b; else b=b- flag=0; while (top!=-1); void LevelOrder(BTNode *b) BTNode *p; BTNode *quMaxSize; front=rear=-1; rear+; qurear=b; while (front!=rear) front=(front+1)%MaxSize; p=qufront; if (p- rear=(rear+1)%MaxSize; qurear=p- BTNode *b;二叉树b:先序遍历序列:递归算法:PreOrder(b);非递归算法:PreOrder1(b);中序遍历序列:InOrder(b);InOrder1(b);后序遍历序列:PostOrder(b);PostOrder1(b);层次遍历序列:LevelOrder(b);注:空间不够,可以增加页码。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1