二叉树后序线索化
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
3
百度文库 - 让每个人平等地提升自我
2、设计框架图
4
百度文库 - 让每个人平等地提升自我
开始
系统主菜单
建立一棵二叉 树
创建根结点 bt
创
创
建
建
各
各
结
结
点
点
的
的
左
右
孩
孩
子
子
对二叉树后序 线索化
输出二叉树的后序 序列
结束
输出一棵建立好的 二叉树结构
5
百度文库 - 让每个人平等地提升自我
3、设计流程图
百度文库 - 让每个人平等地提升自我
目录
一、功能概述····················································································2 二、总体设计····················································································2
printf(" **************************************\n");
}
else
{ printf("〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓\n");
printf("***** 二叉树后序序列如下 *****\n");
五、心得·························································································15 六、参考文献···················································································16
1
百度文库 - 让每个人平等地提升自我
二叉树后序线索化
一、功能概述
本系统所实现的功能是采用二叉链表存储二叉树,并按后序遍历对二 叉树进行线索化处理。本系统的二叉树构造简单,无需知道其先序序列、 中序序列或者后序序列,任何一个二叉树的构造可由用户跟根系统提示输 入二叉的各个结点,构造完二叉树后可对二叉树进行后序线索化,并输出 该二叉树的后序序列。
2、建立一棵二叉树各结点 ·····························································8 (1)功能描述 ··········································································8 (2)算法描述 ··········································································8 (3)程序代码 ··········································································8
1、设计思路 ···············································································2 2、设计框架图 ··················································································4 3、设计流程图 ··················································································6 三、详细设计····················································································7
四、效果及存在问题··········································································11 1、主菜单界面 ···········································································11 2、创建根结点 ···········································································12 3、创建各个结点左右孩子 ····························································12 4、输出二叉树各结点 ··································································14 5、对二叉树后序线索化 ·······························································15 6、存在的问题 ···········································································15
(3)程序代码:
void main()
{
int a,j;
BTREE bt;
bt=null;
start:
printf(" ---------------------------------------------------------\n");
printf("\n ┃★ ★ ★ ★ ★ ★ ★
★ ★ ★ ★ ★ ★┃");
if(!T->lchild) {
T->lbit=1; T->lchild=post;
} if (!T->rchild)
T->rbit=1; if (post && post->rbit==1)
post->rchild=T; post=T;
printf("%d",T->data);
K=1
K=2
K=3
if(q->lchild!=null)
2
百度文库 - 让每个人平等地提升自我
空则提示左孩子已建立,请重新输入,并返回上层菜单;若为空,则申请 一个新的结点空间,并将 X 的左孩子指针指向新生成的结点。
建立好一棵二叉树后,对其进行线索化处理,以后序遍历为例,在遍 历的过程中,依次判断某结点的左孩子或右孩子是否为空,若不为空,则 将其标志域 lbit 或 rbit 置为 1。
输出二叉树的结构
printf("%d\t %d\ n",i++,bt->data); if(bt->lchild!=null) outbtree(bt->lchild); if(bt->rchild!=null) outbtree(bt->rchild);
int k
if(bt==null)
N
对二叉树后序化 POSTREAD(T->lchild); POSTREAD(T->rchild);
进入本系统主菜单,一是建立一个二叉树,二是对二叉树进行后序线 索化,在构建二叉树时,系统提示先创建二叉树根跟结点,然后进入系统 构建二叉树的子菜单,该菜单会依次提示用户创建各个结点的左孩子和右 孩子,二叉树建立完成后自动返回主采单,用户可选择对该二叉树进后序 线索化。
二、总体Hale Waihona Puke Baidu计
1、设计思路
为了得到一个棵线索二叉树,可以利用某结点空的左指针域指出该结 点在某种遍历序中的直接前驱结点的位置,利用结点空的右指针域指出该 结点在某种遍历序列中的直接后继结点的位置,对于那些非空的指针域扔 然存放指向该结点左、右孩子的指针。
printf("请选择: ");
scanf("%d",&j);
switch(j)
{
case 1:
{
printf(" ***********************\n");
printf("┃输入二叉树的根结点: ┃\n");
printf(" ***********************\n");
printf("请输入结点:");
二叉树后序线 索化流程图 08321224 饶立平
建立一棵二叉树
BTREE bt; bt=null;
开始 Made by:饶立平
Y
进行后序线索化 BTREE bt; bt=null;
建立根结点(bt) top=-1;
STACK[++top]=bt
Goto start
while(top>-1)
N
Y
BTREE T,q; q=STACK[top]
3、对二叉树进行后序线索化 ·························································10 (1)功能描述 ·········································································10 (2)算法描述 ·········································································10 (3)程序代码 ·········································································10
scanf("%d",&a);
bt=BThead(a,null,null); /*建立二叉树根结点*/
bttree(bt);
/*建立二叉树的各个结点*/
goto start;
/*返回主菜单*/
}
case 2:
{
7
百度文库 - 让每个人平等地提升自我
if(bt==null)
{
printf(" **************************************\n"); printf(" 二叉树不存在,请重新建立二叉树。 \n");
K=4
top=-1
输出后序 序列
结束
6
三、详细设计
百度文库 - 让每个人平等地提升自我
1、主函数
(1)功能描述:
构造系统的主菜单界面,有两个选项,一是建立一个二叉树,二是对 二叉树进行后序线索化,并用一些编程工具能支持的字符美化主界面。
(2)实现过程:
通过 printf 语句来实现美化整个主界面输出的效果,和主菜单的各个 选项,用 switch 语句来实现主菜单的选择事件的发生。
printf("\n ┃
【1】. 建立一棵二叉树
┃");
printf("\n ┃
【2】. 二叉树后序线索化
┃");
printf("\n ┃
Made By : 08321224 饶立平 ┃");
printf("\n ┃★ ★ ★ ★ ★ ★ ★
★ ★ ★ ★ ★ ★┃\n");
printf(" ---------------------------------------------------------\n");
1、主函数 ··················································································7 (1)功能描述 ··········································································7 (2)实现过程 ··········································································7 (3) 程序代码 ·········································································7
在得到一棵线索二叉树之前,我们必需先建立一个二叉树,为了使系 统使用的更方便更简易,我定义一个构造二叉树的函数,和几个生成左孩 子和右孩子的函数,在构造二叉树的函数中,根据用户需要,判断用户输 入的要求,不断调用左孩子或右孩子函数,从而完成二叉树的建立。比如, 用户要在 X 结点中生成一个左孩子,则先判断 X 的左孩子是否为空,不为
if(q->rchild!=null)
q=STACK[top--]
N
N
q=(BTREE)malloc(siz eof(BTNode));
Y
Y
bt->lchild=q;
T=q;
STACK[++top]=T
q=(BTREE)malloc(siz eof(BTNode)); bt->rchild=q; T=q; STACK[++top]=T
百度文库 - 让每个人平等地提升自我
2、设计框架图
4
百度文库 - 让每个人平等地提升自我
开始
系统主菜单
建立一棵二叉 树
创建根结点 bt
创
创
建
建
各
各
结
结
点
点
的
的
左
右
孩
孩
子
子
对二叉树后序 线索化
输出二叉树的后序 序列
结束
输出一棵建立好的 二叉树结构
5
百度文库 - 让每个人平等地提升自我
3、设计流程图
百度文库 - 让每个人平等地提升自我
目录
一、功能概述····················································································2 二、总体设计····················································································2
printf(" **************************************\n");
}
else
{ printf("〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓\n");
printf("***** 二叉树后序序列如下 *****\n");
五、心得·························································································15 六、参考文献···················································································16
1
百度文库 - 让每个人平等地提升自我
二叉树后序线索化
一、功能概述
本系统所实现的功能是采用二叉链表存储二叉树,并按后序遍历对二 叉树进行线索化处理。本系统的二叉树构造简单,无需知道其先序序列、 中序序列或者后序序列,任何一个二叉树的构造可由用户跟根系统提示输 入二叉的各个结点,构造完二叉树后可对二叉树进行后序线索化,并输出 该二叉树的后序序列。
2、建立一棵二叉树各结点 ·····························································8 (1)功能描述 ··········································································8 (2)算法描述 ··········································································8 (3)程序代码 ··········································································8
1、设计思路 ···············································································2 2、设计框架图 ··················································································4 3、设计流程图 ··················································································6 三、详细设计····················································································7
四、效果及存在问题··········································································11 1、主菜单界面 ···········································································11 2、创建根结点 ···········································································12 3、创建各个结点左右孩子 ····························································12 4、输出二叉树各结点 ··································································14 5、对二叉树后序线索化 ·······························································15 6、存在的问题 ···········································································15
(3)程序代码:
void main()
{
int a,j;
BTREE bt;
bt=null;
start:
printf(" ---------------------------------------------------------\n");
printf("\n ┃★ ★ ★ ★ ★ ★ ★
★ ★ ★ ★ ★ ★┃");
if(!T->lchild) {
T->lbit=1; T->lchild=post;
} if (!T->rchild)
T->rbit=1; if (post && post->rbit==1)
post->rchild=T; post=T;
printf("%d",T->data);
K=1
K=2
K=3
if(q->lchild!=null)
2
百度文库 - 让每个人平等地提升自我
空则提示左孩子已建立,请重新输入,并返回上层菜单;若为空,则申请 一个新的结点空间,并将 X 的左孩子指针指向新生成的结点。
建立好一棵二叉树后,对其进行线索化处理,以后序遍历为例,在遍 历的过程中,依次判断某结点的左孩子或右孩子是否为空,若不为空,则 将其标志域 lbit 或 rbit 置为 1。
输出二叉树的结构
printf("%d\t %d\ n",i++,bt->data); if(bt->lchild!=null) outbtree(bt->lchild); if(bt->rchild!=null) outbtree(bt->rchild);
int k
if(bt==null)
N
对二叉树后序化 POSTREAD(T->lchild); POSTREAD(T->rchild);
进入本系统主菜单,一是建立一个二叉树,二是对二叉树进行后序线 索化,在构建二叉树时,系统提示先创建二叉树根跟结点,然后进入系统 构建二叉树的子菜单,该菜单会依次提示用户创建各个结点的左孩子和右 孩子,二叉树建立完成后自动返回主采单,用户可选择对该二叉树进后序 线索化。
二、总体Hale Waihona Puke Baidu计
1、设计思路
为了得到一个棵线索二叉树,可以利用某结点空的左指针域指出该结 点在某种遍历序中的直接前驱结点的位置,利用结点空的右指针域指出该 结点在某种遍历序列中的直接后继结点的位置,对于那些非空的指针域扔 然存放指向该结点左、右孩子的指针。
printf("请选择: ");
scanf("%d",&j);
switch(j)
{
case 1:
{
printf(" ***********************\n");
printf("┃输入二叉树的根结点: ┃\n");
printf(" ***********************\n");
printf("请输入结点:");
二叉树后序线 索化流程图 08321224 饶立平
建立一棵二叉树
BTREE bt; bt=null;
开始 Made by:饶立平
Y
进行后序线索化 BTREE bt; bt=null;
建立根结点(bt) top=-1;
STACK[++top]=bt
Goto start
while(top>-1)
N
Y
BTREE T,q; q=STACK[top]
3、对二叉树进行后序线索化 ·························································10 (1)功能描述 ·········································································10 (2)算法描述 ·········································································10 (3)程序代码 ·········································································10
scanf("%d",&a);
bt=BThead(a,null,null); /*建立二叉树根结点*/
bttree(bt);
/*建立二叉树的各个结点*/
goto start;
/*返回主菜单*/
}
case 2:
{
7
百度文库 - 让每个人平等地提升自我
if(bt==null)
{
printf(" **************************************\n"); printf(" 二叉树不存在,请重新建立二叉树。 \n");
K=4
top=-1
输出后序 序列
结束
6
三、详细设计
百度文库 - 让每个人平等地提升自我
1、主函数
(1)功能描述:
构造系统的主菜单界面,有两个选项,一是建立一个二叉树,二是对 二叉树进行后序线索化,并用一些编程工具能支持的字符美化主界面。
(2)实现过程:
通过 printf 语句来实现美化整个主界面输出的效果,和主菜单的各个 选项,用 switch 语句来实现主菜单的选择事件的发生。
printf("\n ┃
【1】. 建立一棵二叉树
┃");
printf("\n ┃
【2】. 二叉树后序线索化
┃");
printf("\n ┃
Made By : 08321224 饶立平 ┃");
printf("\n ┃★ ★ ★ ★ ★ ★ ★
★ ★ ★ ★ ★ ★┃\n");
printf(" ---------------------------------------------------------\n");
1、主函数 ··················································································7 (1)功能描述 ··········································································7 (2)实现过程 ··········································································7 (3) 程序代码 ·········································································7
在得到一棵线索二叉树之前,我们必需先建立一个二叉树,为了使系 统使用的更方便更简易,我定义一个构造二叉树的函数,和几个生成左孩 子和右孩子的函数,在构造二叉树的函数中,根据用户需要,判断用户输 入的要求,不断调用左孩子或右孩子函数,从而完成二叉树的建立。比如, 用户要在 X 结点中生成一个左孩子,则先判断 X 的左孩子是否为空,不为
if(q->rchild!=null)
q=STACK[top--]
N
N
q=(BTREE)malloc(siz eof(BTNode));
Y
Y
bt->lchild=q;
T=q;
STACK[++top]=T
q=(BTREE)malloc(siz eof(BTNode)); bt->rchild=q; T=q; STACK[++top]=T