概述
算法:与中序遍历算法类似,只要将遍历算法中访问结点的操作具体为把当前访问的结点与其非空中序前驱结点线索化。
设置一前驱结点pre(初始值为NULL),始终指向刚刚访问过的结点;设置p指针指向当前正在访问的结点。pre为p的前驱,而p为pre的后继。
01 | typedef enum {Link,Thread}PointerTag; |
02 | typedef struct node{ |
03 | ElemType data; |
04 | PointerTag ltag,rtag; |
05 | struct node lchild,rchild; |
06 | } *BinTree; |
07 | BinTree pre=NULL; |
08 | void InOrderThreading(BinTree t) |
09 | { |
10 | if (t) |
11 | { |
12 | InOrderThreading(t->lchild); //左子树线索化 |
13 | //以下建立正在访问结点与其前驱结点之间的线索 |
14 | t->ltag=(t->lchild)?Link:Thread; |
15 | t->rtag=(t->rchild)?Link:Thread; |
16 | if (pre) |
17 | { |
18 | if (pre->rtag==Thread) |
19 | pre->rchild=t; |
20 | if (t->ltag==Thread) |
21 | t->lchild=pre; |
22 | } |
23 | pre=t; |
24 | InOrderThreading(t->rchild); //右子树线索化 |
25 | } |
26 | } |
转载于:https://my.oschina.net/taisha/blog/36325
最后
以上就是害怕面包为你收集整理的二叉树中序线索化(递归)的全部内容,希望文章能够帮你解决二叉树中序线索化(递归)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复