数据结构课程设计线索二叉树算法的实现_第1页
数据结构课程设计线索二叉树算法的实现_第2页
数据结构课程设计线索二叉树算法的实现_第3页
数据结构课程设计线索二叉树算法的实现_第4页
数据结构课程设计线索二叉树算法的实现_第5页
已阅读5页,还剩23页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、数据结构课程设计数据结构课程设计 设设计计说说明明书书 线索二叉树算法的实现 学生姓名 学号 班级 成绩 指导教师 计算机科学与技术系计算机科学与技术系 20112011 年年 9 9 月月 9 9 日日 数据结构数据结构课程设计评阅书 题 目线索二叉树算法的实现线索二叉树算法的实现 学生姓名学号0918014067 指导教师评语及成绩 成绩: 教师签名: 年 月 日 答辩教师评语及成绩 成绩: 教师签名: 年 月 日 教研室意见 总成绩: 室主任签名: 年 月 日 注: 指导老师成绩 60%,答辩成绩 40%,总成绩合成后按五级制计入。 课程设计任务书 20112012 学年第学年第 1 学

2、期学期 专业: 计算机科学与技术 学号: 0918014067 姓名: 穆闻 课程设计名称: 数据结构课程设计 设 计 题 目: 线索二叉树的实现 完 成 期 限:自 2011 年 8 月 29 日至 2011 年 9 月 9 日共 2 周 设计内容: n 个结点的二叉链表中含有 n+1 个空指针域。利用二叉链表中的空指针域,存放指向结点在某种 遍历次序下的前趋和后继结点的指针(这种附加的指针称为线索)。这种加上了线索的二叉树称为 线索二叉树(threaded binarytree)。对一棵非线索二叉树以某种次序遍历使其变为一棵线索二叉树的过 程称为二叉树的线索化。由于线索化的实质是将二叉链表

3、中的空指针改为指向结点前驱或后继的线索, 而一个结点的前驱或后继结点的信息只有在遍历时才能得到,因此线索化的过程即为在遍历过程中修 改空指针的过程。根据线索性质的不同,线索二叉树可分为前序线索二叉树、中序线索二叉树和后序 线索二叉树三种。 运用 vc+编写一个程序实现前序线索二叉树、中序线索二叉树和后序线索二叉树,其中遍历要求 用先左后右的递归或非递归算法来实现。 要求: 1) 阐述设计思想,画出流程图; 2) 任意建立一棵二叉树,采用前序、中序、后序三种方法线索化二叉树; 3) 说明测试方法,写出完整的运行结果; 4) 从时间、空间对算法分析; 5) 较好的界面设计; 6) 编写课程设计报告

4、。 以上要求中第一个阶段的任务完成后,先将设计说明书的草稿交指导老师面审,审查合格后方 可进入后续阶段的工作。设计工作结束后,经指导老师验收合格后将设计说明书打印装订,并进行答 辩。 指导教师(签字): 教研室主任(签字): 批准日期: 年 月 日 摘摘 要要 这是一个关于线索二叉树的程序,该程序具有创建二叉树、遍历二叉树、线索化二叉树。其中, 遍历和线索化都包含了先、中、后三种序列,而且对三种序列线索化后的二叉树也进行了遍历。该程 序采用 vc 6.0 作为软件开发环境,采用 c 语言的各种语句和结构实现二叉树的一系列操作,并采用友 好的界面向用户提供所操作的过程和数据状态,操作简单,界面清

5、晰,易于被用户所接受。 关键字:关键字:线索化;遍历;二叉树;先序;中序;后序 目目 录录 1 1 课题描述课题描述.1 2 2 任务分析任务分析.2 3 3 逻辑设计及描述逻辑设计及描述.3 3.1 二叉树的存储 .3 3.2 二叉树的遍历.4 3.3 二叉树的线索化.4 3.4 线索化二叉树的遍历.7 3.5 主函数 .10 4 4 程序编码程序编码.12 总结总结.21 参考文献参考文献.22 1 1 1 课题描述课题描述 本程序重在设计二叉树的各种线索化实现,以 c 语言作为编程语言,实现对二叉树的先、中、后 三种序列的线索化。旨在使用户在使用过程中能直接调用各种所需函数,以及了解二叉

6、树的各种线索 化过程。其中各函数分别为: bithrtree createbitree();/创建二叉树; bithrtree copybitree(bithrtree void preordertraverse(bithrtree t)/先序遍历二叉树; void inordertraverse(bithrtree t) /中序遍历二叉树; void postordertraverse(bithrtree t)/后序遍历二叉树; bool preorderthreading(bithrtree void prethreading(bithrtree p)/先序搜索结点的建立; bool in

7、orderthreading(bithrtree void inthreading(bithrtree p)/中序搜索结点的建立; void backthreading(bithrtree p)/后序搜索结点的建立; bithrtree backorderthreading(bithrtree bithrtree parent(bithrtree void inordertraverse_thr(bithrtree thrt)/遍历中序线索化二叉树; void backordertraver(bithrtree thrt)/遍历后序线索化二叉树; void inorder_thr_t(bith

8、rtree thrt,bithrtree 开发工具开发工具:c 语言 运行环境运行环境:visual c+6.0。 2 2 2 任务分析任务分析 这是一个能对二叉树线索化的程序,其中包括对二叉树的先序、中序、后序线索化,最后遍历线 索化并输出遍历结果。其中线索化要实现对同一个树的线索化,即应具备还原线索化后二叉树的程序, 并重新对二叉树线索化。主要的功能模块流程图如图 2.1 所示。 创建一棵二叉树 递归先序遍历 递归中序遍历 递归后序遍历 先序线索化 中序线索化 后序线索化 遍历先序线索化 遍历中序线索化 遍历后序线索化 图 2.1 模块流程图 3 3 3 逻辑设计及描述逻辑设计及描述 3.

9、13.1 二叉树的存储二叉树的存储 1.1.创建二叉树( createbitree(t))设计思想:在用户需要创建二叉树时,屏幕提醒输入树的各个 结点,包括空结点的输入,完成输入后,程序自动依次读入各个结点,以空结点作为判断结点位置的 主要依据,对每个结点申请一定的存放空间,然后依次存放各结点。设计流程如图 3.1 所示。 begin y ch=#n y !(t=(bithrtree)malloc(size of(bithrnode) t=null return 0; return t charch; t-data=ch; n 结束 begin bithrtreetree; y rt=null

10、 returntree; !(tree=(bithrtree)malloc(si zeof(bithrnode) y tree-data=rt-data; return 0; tree=null n 结束 n 图 3.1 createbitree(t ) 创建二叉树 图 3.2 copybitree(rt) 复制一棵二叉树 2.2.复制二叉树(copybitree(rt))设计思想:该函数的功能主要是为了避免前后两次的线索化混乱, 其实质是重建二叉树以方便下一次的线索化。复制的方法无外乎将各个结点拷贝至另一棵树,因为是 完全一样的二叉树,所以连左右标志域也要一起复制,结点位置不能发生任何变化。

11、设计流程如图 3.2 所示,算法如算法 3.1 所示。 bithrtree copybitree(bithrtree if(rt=null) tree=null; else if(!(tree=(bithrtree)malloc(sizeof(bithrnode) return 0; tree-data=rt-data;/复制结点 4 tree-ltag=rt-ltag;tree-rtag=rt-rtag;/复制左右标志域 tree-lchild=copybitree(rt-lchild); tree-rchild=copybitree(rt-rchild);/复制左右孩子 return tr

12、ee; 算法 3.1 3.23.2 二叉树的遍历二叉树的遍历 1.1. 先、中、后遍历二叉树,因为三种遍历方法的区别只是将输出结点的位置调换一下就可以实现, 所以在此只列举先序遍历方法的设计思想,该函数是用递归的方法依次遍历根结点、左子、右子,中 序遍历则是左子、根结点、右子,后序遍历只需将根结点的访问放在最后面执行即可。设计流程如图 3.3,主要算法如算法 3.2 所示。 void preordertraverse(bithrtree t)/前序遍历 if (t!=null)printf(%c ,t-data); /*访问根结点*/ preordertraverse(t-lchild);pr

13、eordertraverse(t-rchild); 算法 3.2 begin y t!=nulln printf(%c ,t-data); preordertraverse(t-lchild) preordertraverse(t-rchild) 结束 begin void prethreading(bithrtreep); y !tn thrt-lchild=t; thrt-lchild=thrt; prethreading(thrt) pre-rchild=thrt; pre-rtag=1; thrt-rchild=pre; return 1; 结束 图 3.3 preordertrave

14、rse(t) 先序遍历二叉树 图 3.4 preorderthreading(thrt,t) 先序线索化二叉树 3.33.3 二叉树的线索化二叉树的线索化 1.1. 先序线索化二叉树(preorderthreading(thrt,t))设计思想:由于线索化的实质是将二叉链表中 的空指针改为指向遍历时得到的前驱或后继的线索,因此线索化的过程即为在遍历过程中修改空指针 5 的过程,附设一个指针 pre 始终指向刚刚访问过的结点,若指针 p 指向当前结点,则 pre 指向他的前 驱。由此可得先序遍历建立先序线索化的算法如算法 3.3 和 3.4 所示。 begin y pn y !p-lchildn

15、 y !pre-rchildn p-lchild=pre; pre=p; pre-rchild=p; y p-ltag=0n y p-rtag=0n prethreading(p-lchile); prethreading(p-rchile); 结束 begin y pn inthreading(p-lchild); y !p-lchildn y !pre-rchildn pre=p; p-lchild=pre; pre-rchild=p; 结束 图 3.5 prethreading(p) 先序搜索结点的建立 图 3.6 inthreading(p) 中序搜索结点的建立 2 2中序搜索结点的建

16、立以及线索化如图 36 和 37 所示;后序搜索结点的建立和线索化如图 38 和 39 所示。流程图不再多作说明。 6 begin voidinthreading(bithrtreep); !tn thrt-lchild=t; pre=thrt; inthreading(t); pre-rchild=thrt; pre-rtag=1; thrt-rchild=pre; 结束 y begin y pn postthreading(p-lchild); y !p-lchildn y !pre-rchildn pre=p; p-ltag=1; pre-rtag=1; 结束 图 3.7 inorder

17、threading(thrt,t) 中序线索化二叉树 图 3.8 backthreading(p) 后序搜索结点的建立 bool preorderthreading(bithrtree /先序遍历进行先序线索化 thrt = (bithrtree)malloc(sizeof(bithrnode); thrt-ltag=0; thrt-rtag=1; /创建头结点 thrt-rchild=thrt;/右指针回指 if (!t) thrt-lchild=thrt; /空二叉树 else thrt-lchild = t;pre = thrt; /pre: 刚刚访问过的结点 prethreading(

18、t);pre-rchild=thrt; pre-rtag=1;/最后一个结点线索化 thrt-rchild=pre; return 1; 算法 3.3 void prethreading(bithrtree p) if (p) 7 if(!p-lchild) p-lchild=pre; p-ltag=1; /前驱线索 if(!pre-rchild) pre-rchild=p;pre-rtag=1;/后继线索 pre = p;/保持 pre 指向 p 的前驱 if(p-ltag=0)prethreading(p-lchild); /左子树线索化 if(p-rtag =0)prethreading

19、(p-rchild); /右子树线索化 /前序建立节点的搜索化 算法 3.4 begin bithrtreethrt; y !(thrt=(bithrtree)malloc(size of(bithrnode) n thrt-ltag=0; y !rtn return(thrt); thrt-lchild=rt; thrt-lchild=thrt; pre=thrt; postthreading(rt); thrt-rchild=pre; begin bithrtreetemp; temp-lchild=pn y temp=temp-lchild; temp-lchild!=p y 0=tem

20、p-rtagn temp=temp- lchild; y temp=temp- rchild; 结束 图 3.9 backorderthreading (thrt,t) 后序线索化二叉树 图 3.10 bithrtree parent(thrt,p) 查找结点 3.43.4 线索化二叉树的遍历线索化二叉树的遍历 在程序设计中,实现线索化二叉树的遍历,实质上就是在查找每个结点的前驱和后继,而结点是否有 前驱和后继、他们分别是什么,就要分情况去讨论。 1.1.中序遍历线索化二叉树(inordertraverse_thr(thrt)时,结点的前驱应是遍历左子树时访问的第一 个结点,既左子树最左下方的

21、结点,结点的后继应是遍历右子树时访问的第 8 begin bithrtreep; p!=thrtn printf(%c,p-data); p-ltag=0n (p-rtag=1) p=p-rchild; p=p-rchild; yy y p=p-lchild; 结束 begin bithrtreep; p!=thrtn p-ltag=0n printf(%c,p-data); (p-rtag=1) p=p-rchild; yy p=p-lchild; y 结束 图 3.11 preordertraverse_thr(thrt) 遍历先序线索化二叉树 图 3.12 inordertraverse

22、_thr(thrt) 遍历中序线索化二叉树 一个结点,既右子树最左下方的结点。二叉树最左下方的结点没有前驱,最右下方的结点没有后继。设 计该函数的流程如图 3.12 所示。 2.2.在后序线索树(postordertraverse_thr(thrt)中找结点后继比较复杂,分三种情况(1)根结点没 有后继;(2)若结点是其双亲的右孩子或是其双亲的左孩子且其双亲没有右子树,则后继为双亲; (3)若结点是其双亲的左孩子且其双亲有右子树,则后继为其 双亲的右子树上按后序遍历访问的第一个结点。设计该函数的流程如图 313 所示。 3.3.还原线索化二叉树(inorder_thr_t(thrt,t):涉及

23、该函数的主要目的是在每次线索后 9 begin bithrtreep; 1n p!=thrt n 0=p-ltagn y 0=p-rtagn y printf(%c,p-data); break; y thrt=par printf(%cn,p-data); p=par-rchild|1=par- rtag par-rtag=0 p=par; par=par-rchild; par-ltag=0 par=par-lchild; p=p-lchild p=p-rchild p=thrt p=par y yy n n n yy 结束 n 图 3.13 backordertraverse_thr(t

24、hrt) 遍历后序线索化二叉树 10 都能清除掉前一次线索化过程中所留下的指针,因为不通顺序的线索化,其修改的空指针也不同,因此,进 行下一次线索化前,必须还原空指针。此函数的流程如图 3.14 所示。 begin bithrtreep,post; p!=thrtn p=thrt-rchild; p-ltag=0n p-ltag=0; (p-rtag=1) p-rtag=0; y p=p-lchild; yy 结束 begin intc; c3 y n returnc; printf(n请选择需要执 行的操作序列号:); 结束 图 3.14 inorder_thr_t (thrt,t) 还原线

25、索化后的二叉树 图 3.15 menu_select() 控制选择菜单 3.53.5 主函数主函数 1.1.主函数的设计流程如图 3.17 所示,在进行所需操作之前,屏幕会提示用户建立一棵二叉树,然后 用空的 for 循环来控制不同操作之间的循环,用户选择序号 1,程序会自动执行二叉树的三种遍历,并输 出遍历结果;用户选择 2,程序会自动执行二叉树的线索化操作,并打印出三种线索化后的结果。操作十 11 分方便。 begin bithrtreet,thrt,t; 打印操作菜单 n choice=menu_select(); choice=1 n y n choice=2 choice=3 bre

26、ak; y break; printf(n已结束!谢谢使 用!n); y 后序postordertraverse(t); 前序preordertraverse(t); 中序inordertraverse(t); 前序 preorderthreading(thrt,t); 还原 inorder_thr_t(thrt,t); 中序inorderthreading(thrt,t); 后序backordertraver(thrt); 建立后序 thrt=backorderthreading(t); 遍历线索化 二叉树 遍历二叉树 结束 图 3.17 main()主函数 12 4 4 程序编码程序编码

27、/-有关定义- #include #include #include #include typedef struct bithrnode /线索二叉树中结点的定义 char data; int ltag,rtag; struct bithrnode *lchild,*rchild; bithrnode,*bithrtree; /-构建二叉树- bithrtree createbitree() char ch; bithrtree t; scanf(%c, /从键盘输入 ch; if(ch=#) t=null; /如果 ch=#,则 t 为空指针 else if(!(t=(bithrtree)m

28、alloc(sizeof(bithrnode) return 0; t-data=ch; t-ltag=0; t-rtag=0; /线索标志赋初值 0 t-lchild=createbitree();/先序建立 t-lchild; t-rchild=createbitree();/先序建立 t-rchild; return t; /返回树结点头指针 bithrtree copybitree(bithrtree if(rt=null) tree=null; else if(!(tree=(bithrtree)malloc(sizeof(bithrnode) return 0; tree-data

29、=rt-data; tree-ltag=rt-ltag; tree-rtag=rt-rtag; tree-lchild=copybitree(rt-lchild); tree-rchild=copybitree(rt-rchild); return tree; /-递归遍历二叉树- void preordertraverse(bithrtree t)/先序遍历 13 if (t!=null) printf(%c ,t-data); /*访问根结点*/ preordertraverse(t-lchild); preordertraverse(t-rchild); void inordertrav

30、erse(bithrtree t) /中序遍历 if (t!=null) inordertraverse(t-lchild); printf(%c ,t-data); /*访问根结点*/ inordertraverse(t-rchild); void postordertraverse(bithrtree t) /后序遍历 if (t!=null) postordertraverse(t-lchild); postordertraverse(t-rchild); printf(%c ,t-data); /*访问根结点*/ /-线索化二叉树- bithrtree pre; /全局变量,刚刚访问过

31、的结点 bool preorderthreading(bithrtree thrt = (bithrtree)malloc(sizeof(bithrnode); /创建头结点 thrt-ltag=0; thrt-rtag=1; thrt-rchild=thrt; if (!t) thrt-lchild=thrt; /空二叉树 else thrt-lchild = t; pre = thrt; /pre: 刚刚访问过的结点; prethreading(t); pre-rchild=thrt; pre-rtag=1; thrt-rchild=pre; return 1; void prethrea

32、ding(bithrtree p) if (p) if (!p-lchild) p-lchild=pre;p-ltag=1; /前驱线索 if (!pre-rchild) pre-rchild=p;pre-rtag=1; /后继线索 pre = p; if(p-ltag=0) prethreading(p-lchild); /左子树线索化 if(p-rtag =0) prethreading(p-rchild); /右子树线索化 /先序建立节点的搜索化 bool inorderthreading(bithrtree thrt = (bithrtree)malloc(sizeof(bithrno

33、de); /创建头结点 thrt-ltag=0; thrt-rtag=1; thrt-rchild=thrt; 14 if (!t) thrt-lchild=thrt; /空二叉树 else thrt-lchild = t; pre = thrt; /pre: 刚刚访问过的结点; inthreading(t); pre-rchild=thrt; pre-rtag=1; thrt-rchild=pre; return 1; void inthreading(bithrtree p) if (p) inthreading(p-lchild); /左子树线索化 if (!p-lchild)/前驱线索

34、 p-lchild=pre; p-ltag=1; if (!pre-rchild)/后继线索 pre-rchild=p;pre-rtag=1; pre = p; inthreading(p-rchild); /右子树线索化 void backthreading(bithrtree p) if(p) backthreading(p-lchild); backthreading(p-rchild); if(!p-lchild) p-ltag=1;p-lchild=pre; /前驱线索 if(!pre-rchild) pre-rtag=1;pre-rchild=p;/后继线索 pre=p; /后序搜

35、索化节点的建立 bithrtree backorderthreading(bithrtree if(!(thrt = (bithrtree) malloc (sizeof(bithrnode) exit(1); thrt-ltag= 0;thrt-rtag=1;/建头结点 thrt-rchild=thrt;/右指针回指 if(!rt) thrt-lchild=thrt; /若二叉树空,则左指针回指 else thrt-lchild=rt; pre=thrt; backthreading(rt); /后序遍历进行后序线索化 thrt-rchild=pre; /最后一个节点处理 return th

36、rt; bithrtree parent(bithrtree temp=thrt; if(temp-lchild=p) return temp;/父节点是头结点 else temp=temp-lchild; while( temp-lchild!=p /结点有右结点,往右 else temp=temp-lchild; /如果结点没有右孩子,去左孩子,没有左孩子,去前驱 15 return temp; /-遍历线索化二叉树- void preordertraverse_thr(bithrtree thrt)/thrt:头结点/遍历先序线索化二叉树 bithrtree p; p=thrt-lchi

37、ld; while(p!=thrt) printf(%c,p-data);/此树不空 while(p-ltag = 0) p=p-lchild; printf(%c,p-data); while(p-rtag=1) printf(%c,p-data); if(p-ltag=0) p=p-lchild; else p=p-rchild; / void inordertraverse_thr(bithrtree thrt)/thrt:头结点 /遍历中序线索化二叉树 bithrtree p; p=thrt-lchild; while(p!=thrt) while(p-ltag = 0) p=p-lc

38、hild; printf(%c,p-data); while(p-rtag=1) printf(%cn%c,p-data,p-data); p = p-rchild; / void backordertraver(bithrtree thrt) bithrtree p; bithrtree par; p=thrt-lchild; while(1) while(0=p-ltag) p=p-lchild; if(0=p-rtag) p=p-rchild; /p 指向第一个被访问的结点 else break; while(p!=thrt) printf(%c,p-data); par=parent(

39、thrt,p);/parent 是 p 的双亲: if(thrt=par) p=thrt;/若 parent 是 thrt,即 p 是根结点,则无后继 else if(p=par-rchild|1=par-rtag) p=par; /若 p 是双亲的右孩子,或者是独生左孩子,则后继为双亲 else while(par-rtag=0) /若 p 是有兄弟的左孩子,则后继为双亲的右子树上后序遍历访问的第一个节点。 par=par-rchild; while(par-ltag=0) par=par-lchild; p=par; printf(%cn,p-data); 16 /-将线索化后二叉树还原-

40、 void inorder_thr_t(bithrtree thrt,bithrtree p=thrt-lchild; while(p!=thrt) while(p-ltag = 0) p=p-lchild; p-ltag=0; p-lchild=null; while(p-rtag=1) post=p-rchild; p-rchild=null; p=post; p = p-rchild; p=thrt-rchild; p-rtag=0; p-rchild=null; t=thrt-lchild; free(thrt); /-选择菜单- int menu_select() int c; do

41、 printf(n 请选择需要执行的操作序列号:); scanf(%d, while(c3); return c; /-主函数- void main() int choice; bithrtree t,thrt,t; /定义树 printf(请输入树的结点(#表示结点为空):n); t=createbitree(); /先序算法建立二叉树 t=copybitree(t); printf(n); /复制一颗二叉树 printf( 操作菜单 n); printf( n); printf( 1.遍历操作二叉树 n); printf( n); printf( 2.线索化二叉树 n); printf(

42、n); printf( 3.退出所有操作 n); printf( n); for(;) choice=menu_select(); switch(choice) case 1: printf(n *遍历二叉树*n); printf(n 遍历二叉树结果:n); 17 preordertraverse(t); printf(n); printf(n 遍历二叉树结果:n); inordertraverse(t); printf(n); printf(n 遍历二叉树结果:n); postordertraverse(t); printf(nn); break; case 2: printf(n *线索二叉树*n); inorderthreading(thrt,t);/中序线索化 printf(n 线索化后的内容:n); inordertraverse_thr(thrt); /遍历中序线索化二叉树 printf(n); inorder_thr_t(thrt,t);/还原

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论