1、二叉树的遍历设计4 二叉树的遍历设计一个程序演示在二叉树上进行三种遍历的过程。一、需求分析(1)从键盘上输入二叉树的每一个结点,演示二叉树T的建立过程。(2)演示各种遍历的遍历过程。二、概要设计1.二叉树以及栈的存储表示#define stack_init_size 100#define stackincreament 10#define maxlength 10#define overflow -2typedef char telemtype;typedef struct bitnode telemtype data; struct bitnode *lchild,*rchild;bitno
2、de,*bitree;/定义树的类型typedef bitree selemtype;typedef struct selemtype *base; selemtype *top; int stacksize;Sqstack;/定义栈的类型2.栈的基本函数声明void initstack(Sqstack &s); /初始化栈void push(Sqstack &s,selemtype e); /进栈bool gettop(Sqstack &s,selemtype &e); /提取栈顶元素void pop(Sqstack &s,selemtype &e); /出栈bool stackempty(
3、Sqstack s); /判断栈是否非空3树创建和遍历函数声明void createbitree(bitree &bt); /树的创建void preorderbitree(bitree bt); /先序非递归遍历void inorderbitree(bitree bt); /中序非递归遍历void postorderbitree(bitree bt); /后序非递归遍历void levelorderbitree(bitree bt); /层序遍历三、详细设计1. 栈的初始化函数名:initstack(Sqstack &s)参数:(传入)Sqstack &s顺序栈作用:对栈进行初始化算法:vo
4、id initstack(Sqstack &s) /初始化栈 s.base=(selemtype*)malloc (stack_init_size*sizeof(selemtype); if(!s.base) cout=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(Sqstac
5、k &s,selemtype &e)参数:Sqstack &s,selemtype &e作用:提取栈顶元素算法:bool gettop(Sqstack &s,selemtype &e) /提取栈顶元素 if(s.top=s.base) return false; e=*(s.top-1); return true;4.出栈函数名:pop(Sqstack &s,selemtype &e)参数:Sqstack &s,selemtype &e作用:删除栈顶元素,用e返回其值算法:void pop(Sqstack &s,selemtype &e) /出栈 if(s.top=s.base) coutda
6、ta=ch; createbitree(bt-lchild); createbitree(bt-rchild); 7.先序遍历二叉树函数名:preorderbitree(bitree bt)参数:bitree bt作用:先序遍历一棵二叉树算法:void preorderbitree(bitree bt) /先序非递归遍历 bitree p;p=bt; Sqstack s; initstack(s); push(s,bt); while(!stackempty(s) while(gettop(s,p)&p) coutdatalchild); pop(s,p); if(!stackempty(s)
7、 pop(s,p); push(s,p-rchild); 8.中序遍历二叉树函数名:inorderbitree(bitree bt)参数:bitree bt作用:中序非递归遍历二叉树算法:void inorderbitree(bitree bt) /中序非递归遍历 Sqstack s;initstack(s); bitree p; p=bt; while(p|!stackempty(s) if(p) push(s,p); p=p-lchild; else pop(s,p); coutdatarchild; 9.后序遍历二叉树函数名:postorderbitree(bitree bt)参数:bi
8、tree bt作用:后序非递归遍历二叉树算法:void postorderbitree(bitree bt) /后序非递归遍历 Sqstack s;initstack(s); bitree p,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) coutdatarchild); 10.层序遍历二叉树函数名:levelorderbitree(bitr
9、ee bt)参数:bitree bt作用:层序遍历二叉树算法: void levelorderbitree(bitree bt) /层序遍历 bitree queuemaxlength; int front=0,rear=0; bitree p; if(bt) queuerear=bt; rear=(rear+1)%maxlength; while(front!=rear) p=queuefront; front=(front+1)%maxlength; coutdatalchild) queuerear=p-lchild; rear=(rear+1)%maxlength; if(p-rchi
10、ld)queuerear=p-rchild; rear=(rear+1)%maxlength; 四、运行结果及分析附:主要源代码#include#include#define stack_init_size 100#define stackincreament 10#define maxlength 10#define overflow -2typedef char telemtype;typedef struct bitnode telemtype data; struct bitnode *lchild,*rchild;bitnode,*bitree;/定义树的类型typedef bitr
11、ee selemtype;typedef struct selemtype *base; selemtype *top; int stacksize;Sqstack;/定义栈的类型/栈的基本函数声明void initstack(Sqstack &s);void push(Sqstack &s,selemtype e);bool gettop(Sqstack &s,selemtype &e);void pop(Sqstack &s,selemtype &e);bool stackempty(Sqstack s);/树创建和遍历函数声明void createbitree(bitree &bt);v
12、oid preorderbitree(bitree bt);void inorderbitree(bitree bt);void postorderbitree(bitree bt);void levelorderbitree(bitree bt);void main() bitree bt; cout请按先序的输入树的各个结点(空格表示空树):endl; createbitree(bt); cout递归算法略!endl; cout该树的先序非递归遍历为:; preorderbitree(bt); coutendl; cout该树的中序非递归遍历为:; inorderbitree(bt); c
13、outendl; cout该树的后序非递归遍历为:; postorderbitree(bt); coutendl; cout该树的层序遍历为: ; levelorderbitree(bt); coutendl;void initstack(Sqstack &s) /初始化栈 s.base=(selemtype*)malloc (stack_init_size*sizeof(selemtype); if(!s.base) cout=s.stacksize) s.base=(selemtype*)realloc(s.base, (s.stacksize+stackincreament)*sizeo
14、f(selemtype); if(!s.base) exit(overflow); s.stacksize+=stackincreament; *s.top+=e;bool gettop(Sqstack &s,selemtype &e) /提取栈顶元素 if(s.top=s.base) return false; e=*(s.top-1); return true;void pop(Sqstack &s,selemtype &e) /出栈 if(s.top=s.base) coutdata=ch; createbitree(bt-lchild); createbitree(bt-rchild)
15、; /*/树的非递归遍历函数*void preorderbitree(bitree bt) /先序非递归遍历 bitree p;p=bt; Sqstack s; initstack(s); push(s,bt); while(!stackempty(s) while(gettop(s,p)&p) coutdatalchild); pop(s,p); if(!stackempty(s) pop(s,p); push(s,p-rchild); void inorderbitree(bitree bt) /中序非递归遍历 Sqstack s;initstack(s); bitree p; p=bt;
16、 while(p|!stackempty(s) if(p) push(s,p); p=p-lchild; else pop(s,p); coutdatarchild; void postorderbitree(bitree bt) /后序非递归遍历 Sqstack s;initstack(s); bitree p,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
17、-rchild) coutdatarchild); void levelorderbitree(bitree bt) /层序遍历*bitree queuemaxlength; int front=0,rear=0; bitree p; if(bt) queuerear=bt; rear=(rear+1)%maxlength; while(front!=rear) p=queuefront; front=(front+1)%maxlength; coutdatalchild) queuerear=p-lchild; rear=(rear+1)%maxlength; if(p-rchild)queuerear=p-rchild; rear=(rear+1)%maxlength; /*
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1