二叉树的遍历.docx
- 文档编号:8648939
- 上传时间:2023-02-01
- 格式:DOCX
- 页数:14
- 大小:16.82KB
二叉树的遍历.docx
《二叉树的遍历.docx》由会员分享,可在线阅读,更多相关《二叉树的遍历.docx(14页珍藏版)》请在冰豆网上搜索。
二叉树的遍历
设计4二叉树的遍历
设计一个程序演示在二叉树上进行三种遍历的过程。
一、需求分析
(1)从键盘上输入二叉树的每一个结点,演示二叉树T的建立过程。
(2)演示各种遍历的遍历过程。
二、概要设计
1.二叉树以及栈的存储表示
#definestack_init_size100
#definestackincreament10
#definemaxlength10
#defineoverflow-2
typedefchartelemtype;
typedefstructbitnode{
telemtypedata;
structbitnode*lchild,*rchild;
}bitnode,*bitree;//定义树的类型
typedefbitreeselemtype;
typedefstruct{
selemtype*base;
selemtype*top;
intstacksize;
}Sqstack;//定义栈的类型
2.栈的基本函数声明
voidinitstack(Sqstack&s);//初始化栈
voidpush(Sqstack&s,selemtypee);//进栈
boolgettop(Sqstack&s,selemtype&e);//提取栈顶元素
voidpop(Sqstack&s,selemtype&e);//出栈
boolstackempty(Sqstacks);//判断栈是否非空
3.树创建和遍历函数声明
voidcreatebitree(bitree&bt);//树的创建
voidpreorderbitree(bitreebt);//先序非递归遍历
voidinorderbitree(bitreebt);//中序非递归遍历
voidpostorderbitree(bitreebt);//后序非递归遍历
voidlevelorderbitree(bitreebt);//层序遍历
三、详细设计
1.栈的初始化
函数名:
initstack(Sqstack&s)
参数:
(传入)Sqstack&s顺序栈
作用:
对栈进行初始化
算法:
voidinitstack(Sqstack&s){//初始化栈
s.base=(selemtype*)malloc(stack_init_size*sizeof(selemtype));
if(!
s.base)
{
cout<<"e";
exit(overflow);
}
s.top=s.base;
s.stacksize=stack_init_size;
}
2.进栈
函数名:
push(Sqstack&s,selemtypee)
参数:
Sqstack&s,selemtypee
作用:
将e写入栈S中
算法:
voidpush(Sqstack&s,selemtypee){//进栈
if(s.top-s.base>=s.stacksize){
s.base=(selemtype*)realloc(s.base,
(s.stacksize+stackincreament)*sizeof(selemtype));
if(!
s.base)exit(overflow);
s.stacksize+=stackincreament;
}
*s.top++=e;
}
3.提取栈顶元素
函数名:
gettop(Sqstack&s,selemtype&e)
参数:
Sqstack&s,selemtype&e
作用:
提取栈顶元素
算法:
boolgettop(Sqstack&s,selemtype&e){//提取栈顶元素
if(s.top==s.base)
returnfalse;
e=*(s.top-1);
returntrue;
}
4.出栈
函数名:
pop(Sqstack&s,selemtype&e)
参数:
Sqstack&s,selemtype&e
作用:
删除栈顶元素,用e返回其值
算法:
voidpop(Sqstack&s,selemtype&e){//出栈
if(s.top==s.base){
cout<<"e";
exit(overflow);
}
e=*--s.top;
}
5.判断栈是否为空
函数名:
stackempty(Sqstacks)
参数:
(传入)Sqstacks
作用:
判断栈S是否为空
算法:
boolstackempty(Sqstacks){//判断栈是否非空
if(s.top==s.base)
returntrue;
else
returnfalse;
}
6.树的创建
函数名:
createbitree(bitree&bt)
参数:
bitree&bt
作用:
创建一棵二叉树
算法:
voidcreatebitree(bitree&bt){
charch;
cin.get(ch);
if(ch=='')bt=NULL;
else{
if(!
(bt=(bitnode*)malloc(sizeof(bitnode))))exit(overflow);
bt->data=ch;
createbitree(bt->lchild);
createbitree(bt->rchild);
}
}
7.先序遍历二叉树
函数名:
preorderbitree(bitreebt)
参数:
bitreebt
作用:
先序遍历一棵二叉树
算法:
voidpreorderbitree(bitreebt){//先序非递归遍历
bitreep;p=bt;
Sqstacks;initstack(s);
push(s,bt);
while(!
stackempty(s))
{
while(gettop(s,p)&&p)
{
cout<
push(s,p->lchild);
}
pop(s,p);
if(!
stackempty(s))
{
pop(s,p);
push(s,p->rchild);
}
}
}
8.中序遍历二叉树
函数名:
inorderbitree(bitreebt)
参数:
bitreebt
作用:
中序非递归遍历二叉树
算法:
voidinorderbitree(bitreebt){//中序非递归遍历
Sqstacks;initstack(s);
bitreep;p=bt;
while(p||!
stackempty(s)){
if(p){
push(s,p);
p=p->lchild;
}
else
{
pop(s,p);
cout<
p=p->rchild;
}
}
}
9.后序遍历二叉树
函数名:
postorderbitree(bitreebt)
参数:
bitreebt
作用:
后序非递归遍历二叉树
算法:
voidpostorderbitree(bitreebt){//后序非递归遍历
Sqstacks;initstack(s);
bitreep,q;p=bt;
push(s,p);
while(p||!
stackempty(s)){
while(gettop(s,p)&&p)
push(s,p->lchild);
pop(s,p);
if(!
stackempty(s)){
gettop(s,p);
if(p->rchild==NULL||q==p->rchild)
{
cout<
q=p;
pop(s,p);
push(s,NULL);
}
elsepush(s,p->rchild);
}
}
}
10.层序遍历二叉树
函数名:
levelorderbitree(bitreebt)
参数:
bitreebt
作用:
层序遍历二叉树
算法:
voidlevelorderbitree(bitreebt){//层序遍历
bitreequeue[maxlength];
intfront=0,rear=0;
bitreep;
if(bt){
queue[rear]=bt;
rear=(rear+1)%maxlength;
}
while(front!
=rear){
p=queue[front];
front=(front+1)%maxlength;
cout<
if(p->lchild){
queue[rear]=p->lchild;
rear=(rear+1)%maxlength;
}
if(p->rchild){
queue[rear]=p->rchild;
rear=(rear+1)%maxlength;
}
}
}
四、运行结果及分析
附:
主要源代码
#include
#include
#definestack_init_size100
#definestackincreament10
#definemaxlength10
#defineoverflow-2
typedefchartelemtype;
typedefstructbitnode{
telemtypedata;
structbitnode*lchild,*rchild;
}bitnode,*bitree;//定义树的类型
typedefbitreeselemtype;
typedefstruct{
selemtype*base;
selemtype*top;
intstacksize;
}Sqstack;//定义栈的类型
//栈的基本函数声明
voidinitstack(Sqstack&s);
voidpush(Sqstack&s,selemtypee);
boolgettop(Sqstack&s,selemtype&e);
voidpop(Sqstack&s,selemtype&e);
boolstackempty(Sqstacks);
//树创建和遍历函数声明
voidcreatebitree(bitree&bt);
voidpreorderbitree(bitreebt);
voidinorderbitree(bitreebt);
voidpostorderbitree(bitreebt);
voidlevelorderbitree(bitreebt);
voidmain(){
bitreebt;
cout<<"请按先序的输入树的各个结点(空格表示空树):
"< createbitree(bt); cout<<"递归算法略! "< cout<<"该树的先序非递归遍历为: "; preorderbitree(bt); cout< cout<<"该树的中序非递归遍历为: "; inorderbitree(bt); cout< cout<<"该树的后序非递归遍历为: "; postorderbitree(bt); cout< cout<<"该树的层序遍历为: "; levelorderbitree(bt); cout< } voidinitstack(Sqstack&s){//初始化栈 s.base=(selemtype*)malloc(stack_init_size*sizeof(selemtype)); if(! s.base) { cout<<"e"; exit(overflow); } s.top=s.base; s.stacksize=stack_init_size; } voidpush(Sqstack&s,selemtypee){//进栈 if(s.top-s.base>=s.stacksize){ s.base=(selemtype*)realloc(s.base, (s.stacksize+stackincreament)*sizeof(selemtype)); if(! s.base)exit(overflow); s.stacksize+=stackincreament; } *s.top++=e; } boolgettop(Sqstack&s,selemtype&e){//提取栈顶元素 if(s.top==s.base) returnfalse; e=*(s.top-1); returntrue; } voidpop(Sqstack&s,selemtype&e){//出栈 if(s.top==s.base){ cout<<"e"; exit(overflow); } e=*--s.top; } boolstackempty(Sqstacks){//判断栈是否非空 if(s.top==s.base) returntrue; else returnfalse; } //树的创建************************* voidcreatebitree(bitree&bt){ charch; cin.get(ch); if(ch=='')bt=NULL; else{ if(! (bt=(bitnode*)malloc(sizeof(bitnode))))exit(overflow); bt->data=ch; createbitree(bt->lchild); createbitree(bt->rchild); } } //***************************** //树的非递归遍历函数************************************* voidpreorderbitree(bitreebt){//先序非递归遍历 bitreep;p=bt; Sqstacks;initstack(s); push(s,bt); while(! stackempty(s)) { while(gettop(s,p)&&p) { cout< push(s,p->lchild); } pop(s,p); if(! stackempty(s)) { pop(s,p); push(s,p->rchild); } } } voidinorderbitree(bitreebt){//中序非递归遍历 Sqstacks;initstack(s); bitreep;p=bt; while(p||! stackempty(s)){ if(p){ push(s,p); p=p->lchild; } else { pop(s,p); cout< p=p->rchild; } } } voidpostorderbitree(bitreebt){//后序非递归遍历 Sqstacks;initstack(s); bitreep,q;p=bt; push(s,p); while(p||! stackempty(s)){ while(gettop(s,p)&&p) push(s,p->lchild); pop(s,p); if(! stackempty(s)){ gettop(s,p); if(p->rchild==NULL||q==p->rchild) { cout< q=p; pop(s,p); push(s,NULL); } elsepush(s,p->rchild); } } } voidlevelorderbitree(bitreebt){ //层序遍历*************** bitreequeue[maxlength]; intfront=0,rear=0; bitreep; if(bt){ queue[rear]=bt; rear=(rear+1)%maxlength; } while(front! =rear){ p=queue[front]; front=(front+1)%maxlength; cout< if(p->lchild){ queue[rear]=p->lchild; rear=(rear+1)%maxlength; } if(p->rchild){ queue[rear]=p->rchild; rear=(rear+1)%maxlength; } } } //**************************
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 二叉 遍历