求树和二叉树的深度题目及源程序代码.docx

上传人:b****4 文档编号:4637281 上传时间:2022-12-07 格式:DOCX 页数:11 大小:16.37KB
下载 相关 举报
求树和二叉树的深度题目及源程序代码.docx_第1页
第1页 / 共11页
求树和二叉树的深度题目及源程序代码.docx_第2页
第2页 / 共11页
求树和二叉树的深度题目及源程序代码.docx_第3页
第3页 / 共11页
求树和二叉树的深度题目及源程序代码.docx_第4页
第4页 / 共11页
求树和二叉树的深度题目及源程序代码.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

求树和二叉树的深度题目及源程序代码.docx

《求树和二叉树的深度题目及源程序代码.docx》由会员分享,可在线阅读,更多相关《求树和二叉树的深度题目及源程序代码.docx(11页珍藏版)》请在冰豆网上搜索。

求树和二叉树的深度题目及源程序代码.docx

求树和二叉树的深度题目及源程序代码

树和二叉树

以下问题要求统一在一个大程序里解决。

10、按先序遍历的扩展序列建立二叉树的存储结构

11、二叉树先序、中序、后序遍历的递归算法

12、二叉树中序遍历的非递归算法

13、二叉树层次遍历的非递归算法

14、求二叉树的深度(后序遍历)

15、建立树的存储结构

16、求树的深度

17、

源程序代码:

//

#include""

#include""

#include""

#defineSTACK_INIT_SIZE100

#defineSTACKINCREMENT10

#defineOK1

#defineERROR0

#defineTRUE1

#defineFALSE0

#defineOVERFLOW-1

typedefcharTElemType;//元素数据类型

typedefintStatus;

/*二叉链表储存结构*/

typedefstructBiTNode{

TElemTypedata;

structBiTNode*lchild,*rchild;

}BiTNode,*BiTree;

boolCreateBiTree(BiTree&T){

//先序序列建立二叉树

charch;

scanf("%c",&ch);

if(ch=='*')T=NULL;

else{

if(!

(T=(BiTNode*)malloc(sizeof(BiTNode))))returnERROR;

T->data=ch;

CreateBiTree(T->lchild);

CreateBiTree(T->rchild);

}

returnOK;

}

StatusPrintElement(TElemTypee){

//访问函数

printf("%c",e);

returnOK;

}

StatusPreOrderTraverse(BiTreeT,Status(*Visit)(TElemType)){

//先序遍历二叉树的递归算法

if(T){

if(Visit(T->data))

if(PreOrderTraverse(T->lchild,Visit))

if(PreOrderTraverse(T->rchild,Visit))returnOK;

returnERROR;

}elsereturnOK;

}

StatusInOrderTraverse(BiTreeT,Status(*Visit)(TElemType)){

//中序遍历二叉树的递归算法

if(T){

if(InOrderTraverse(T->lchild,Visit))

if(Visit(T->data))

if(InOrderTraverse(T->rchild,Visit))returnOK;

returnERROR;

}elsereturnOK;

}

StatusPostOrderTraverse(BiTreeT,Status(*Visit)(TElemType)){

//后序遍历二叉树的递归算法

if(T){

if(PostOrderTraverse(T->lchild,Visit))

if(PostOrderTraverse(T->rchild,Visit))

if(Visit(T->data))returnOK;

returnERROR;

}elsereturnOK;

}

/*栈存储结构及操作

*/

typedefstruct{

BiTree*base;

BiTree*top;

intstacksize;

}Stack;

StatusInitStack(Stack&S){

//构造空栈

=(BiTree*)malloc(STACK_INIT_SIZE*sizeof(BiTree));

if(!

exit(OVERFLOW);

=;

=STACK_INIT_SIZE;

returnOK;

}

StatusGetTop(StackS,BiTree&e){

//读栈顶元素

if==returnERROR;

e=*-1);

returnOK;

}

StatusPush(Stack&S,BiTreee){

//入栈

if->={

=(BiTree*)realloc,+STACKINCREMENT)*

sizeof(BiTree));

if(!

exit(OVERFLOW);

=+;

+=STACKINCREMENT;

}

*++=e;

returnOK;

}

StatusPop(Stack&S,BiTree&e){

//出栈

if==returnERROR;

e=*;

returnOK;

}

StatusStackEmpty(StackS){

//判栈空

if==returnTRUE;

elsereturnFALSE;

}

StatusInOrderTraverse2(BiTreeT,Status(*Visit)(TElemType)){

//中序遍历二叉树的非递归算法

StackS;

BiTreep;

InitStack(S);Push(S,T);

while(!

StackEmpty(S)){

while(GetTop(S,p)&&p)Push(S,p->lchild);

Pop(S,p);

if(!

StackEmpty(S)){

Pop(S,p);

if(!

Visit(p->data))returnERROR;

Push(S,p->rchild);

}

}

returnOK;

}

#defineMAXLEN100

voidLevelOrderTraverse(BiTreeT,Status(*Visit)(TElemType)){

//层次遍历二叉树

structnode

{

BiTreevec[MAXLEN];

intf,r;

}q;

=0;

=0;

if(T!

=NULL)Visit(T->data);

[]=T;

=+1;

while<{

T=[];=+1;

if(T->lchild!

=NULL){

Visit(T->lchild->data);

[]=T->lchild;

=+1;

}

if(T->rchild!

=NULL){

Visit(T->rchild->data);

[]=T->rchild;

=+1;

}

}

}

intBiTreeDepth(BiTreeT){

//求二叉树的深度

intdepthval,depthLeft,depthRight;

if(!

T)depthval=0;

else{

depthLeft=BiTreeDepth(T->lchild);

depthRight=BiTreeDepth(T->rchild);

depthval=1+(depthLeft>depthRightdepthLeft:

depthRight);

}

returndepthval;

}

/*树的二叉链表储存结构*/

typedefstructCSNode{

TElemTypedata;

structCSNode*firstchild,*nextsibling;

}CSNode,*CSTree;

/*队列存储结构及操作

*/

typedefstructQNode{

CSTreedata;

structQNode*next;

}QNode,*QueuePtr;

typedefstruct{

QueuePtrfront;

QueuePtrrear;

}LinkQueue;

StatusInitQueue(LinkQueue&Q){

//构造空队列

==(QueuePtr)malloc(sizeof(QNode));

if(!

exit(OVERFLOW);

>next=NULL;

returnOK;

}

StatusDestoryQueue(LinkQueue&Q){

//销毁队列

while{

=>next;

free;

=;

}

returnOK;

}

StatusEnQueue(LinkQueue&Q,CSTreee){

//入队

QueuePtrp;

p=(QueuePtr)malloc(sizeof(QNode));

if(!

p)exit(OVERFLOW);

p->data=e;p->next=NULL;

>next=p;

=p;

returnOK;}

StatusDeQueue(LinkQueue&Q,CSTree&e){

//出队

QueuePtrp;

if==returnERROR;

p=>next;

e=p->data;

>next=p->next;

if==p)=;

free(p);

returnOK;

}

StatusGetHead(LinkQueue&Q,CSTree&e){

//读队头

QueuePtrp;

if==returnERROR;

p=>next;

e=p->data;

returnOK;

}

CSTreeGetTreeNode(TElemTypee){

//建立树的孩子

//-兄弟链表结点

CSTreep;

p=(CSTree)malloc(sizeof(CSNode));

if(!

p)exit(OVERFLOW);

p->data=e;

p->firstchild=NULL;

p->nextsibling=NULL;

returnp;

}

StatusCreatTree(CSTree&T){

//建立树的孩子

//-兄弟链表

charfirst='',second='';

intresult=0;

LinkQueueQ;

CSTreep,s,r;

InitQueue(Q);

T=NULL;

for(scanf("%c%c",&first,&second);second!

='#';result=scanf("%c%c",&first,

&second)){

p=GetTreeNode(second);

EnQueue(Q,p);

if(first=='#')T=p;

else{

GetHead(Q,s);

while(s->data!

=first){

DeQueue(Q,s);GetHead(Q,s);

}

if(!

(s->firstchild)){

s->firstchild=p;

r=p;

}else{

r->nextsibling=p;

r=p;

}

}

}

returnOK;

}

intTreeDepth(CSTreeT){

//求树的深度

inth1,h2;

if(!

T)return0;

else{

h1=TreeDepth(T->firstchild);

h2=TreeDepth(T->nextsibling);

return(((h1+1)>h2)(h1+1):

h2);

}

}

intmain(intargc,char*argv[])

{

BiTreetestT;

printf("请输入二叉树先序序列(如AB*C***):

");

CreateBiTree(testT);

printf("\n");

printf("二叉树的深度是:

");

printf("%d",BiTreeDepth(testT));

printf("\n");

printf("先序递归遍历顺序:

");

PreOrderTraverse(testT,PrintElement);

printf("\n");

printf("中序递归遍历顺序:

");

InOrderTraverse(testT,PrintElement);

printf("\n");

printf("后序递归遍历顺序:

");

PostOrderTraverse(testT,PrintElement);

printf("\n");

printf("层次非递归遍历顺序:

");

LevelOrderTraverse(testT,PrintElement);

printf("\n");

printf("中序非递归遍历顺序:

");

InOrderTraverse2(testT,PrintElement);

printf("\n\n");

while(getchar()!

='\n');//清除缓冲区字符

CSTreetestT2;

printf("自上而下自左至右输入树的各条边(如#AABACADCECFEG##):

");

CreatTree(testT2);

printf("\n");

printf("树的深度是:

");

printf("%d",TreeDepth(testT2));

printf("\n");

return0;

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 总结汇报 > 其它

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

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