找回密码
 立即注册
首页 业界区 业界 浅谈二叉树线索化相关问题

浅谈二叉树线索化相关问题

幽淆 2025-9-26 11:30:25
二叉树线索化最简单的就是中序了,无脑按照中序遍历递归算法套就OK了。
现在我们有如下代码实现
  1. //file:threadBiTree.h
  2. #ifndef THREAD_BI_TREE_H
  3. #define THREAD_BI_TREE_H
  4. typedef struct threadBiNode {
  5.     int data;
  6.     struct threadBiNode *lchild, *rchild;
  7.     int ltag, rtag;
  8. }threadBiNode, *threadBiTree;
  9. void _thread_in(threadBiTree &T, threadBiNode *&pre);
  10. void create_thread_in(threadBiTree &T);
  11. #endif
  12. /**********************/
  13. //file:threadBiTree.cpp
  14. #include "threadBiTree.h"
  15. void _thread_in(threadBiTree &T, threadBiNode *&pre)
  16. {
  17.     if (!T)
  18.         return;
  19.     _thread_in(T->lchild, pre);
  20.     if (pre && pre->rchild == nullptr)
  21.     {
  22.         pre->rtag = 1;
  23.         pre->rchild = T;
  24.     }
  25.     if (T->lchild == nullptr)
  26.     {
  27.         T->ltag = 1;
  28.         T->lchild = pre;
  29.     }
  30.     pre = T;
  31.     _thread_in(T->rchild, pre);
  32. }
  33. //assume that the tags of tree have been initialize by zero
  34. void create_thread_in(threadBiTree &T)
  35. {
  36.     if (!T)
  37.         return;
  38.     threadBiNode *pre = nullptr;
  39.     _thread_in(T, pre);
  40.     if (pre)
  41.     {
  42.         pre->rtag = 1;
  43.         pre->rchild = nullptr;
  44.     }
  45. }
复制代码
在笔者看来中序二叉树是一种很方便的数据结构,中序线索二叉树类似于一个双链表,可以沿着中序下第一个节点向后作中序遍历也可以沿着中序下最后一个节点向前作遍历,但是对于任意前序线索二叉树不一定能作反向前序遍历,任意后序线索二叉树不一定能作正向后序遍历。而要实现中序二叉树的双向遍历功能,只需要几个简单的辅助函数即可。而这些辅助函数的实现也很好理解,因为中序的第一个结点,最后一个节点,某个节点的直接前驱或后继都非常好找,而要怎么找这里就请看下面代码注释不赘述
于是我们有如下辅助函数和用于正反向中序遍历的函数
  1. //file: threadBiTree.cpp
  2. /**********用于正向中序遍历**********/
  3. // assume that the data structure is validate
  4. threadBiNode *first_thread_in(threadBiTree &T)
  5. {
  6.     if (!T)
  7.         return nullptr;
  8.     threadBiNode *p = T;
  9.     while (p->ltag == 0)        //中序下第一个节点必然是整棵树最左下节点
  10.         p = p->lchild;
  11.     return p;
  12. }
  13. // assume that the data structure is validate
  14. threadBiNode *next_thread_in(threadBiNode *&p)
  15. {
  16.     if (!p)
  17.         return nullptr;
  18.     if (p->rtag == 1)        //某个节点的后继要么是由其后继指针指定(即无右子树)
  19.         return p->rchild;
  20.     else
  21.         return first_thread_in(p->rchild);        //要么是其右子树在中序下的第一个节点,简单理解为因为中序是LNR,现在存在右子树那就是R了,另外中序遍历是递归的
  22. }
  23. // assume that the data structure is validate
  24. void traverse_thread_in(threadBiTree &T, void (*visit)(threadBiNode *&p))
  25. {
  26.     for (threadBiNode *p = first_thread_in(T); p != nullptr; p = next_thread_in(p))
  27.         visit(p);
  28. }
  29. /**********用于逆向中序遍历**********/
  30. // assume that the data structure is validate
  31. threadBiNode *last_thread_in(threadBiTree &T)
  32. {
  33.     if (!T)
  34.         return nullptr;
  35.     threadBiNode *p = T;
  36.     while (p->rtag == 0)        //中序下最后一个节点必然是整棵树最右下节点
  37.         p = p->rchild;
  38.     return p;
  39. }
  40. // assume that the data structure is validate
  41. threadBiNode *previous_thread_in(threadBiNode *&p)
  42. {
  43.     if (!p)
  44.         return nullptr;
  45.     if (p->ltag == 1)        //前驱就往左子树上找,和找后继原理是一样的,无左子树则其前驱为该节点左线索所指明的节点
  46.         return p->lchild;
  47.     else
  48.         return last_thread_in(p->lchild);        //有左子树,前驱就是左子树中序遍历下的最后一个节点
  49. }
  50. // assume that the data structure is validate
  51. void traverse_reverse_thread_in(threadBiTree &T, void (*visit)(threadBiNode *&p))
  52. {
  53.     for (threadBiNode *p = last_thread_in(T); p != nullptr; p = previous_thread_in(p))
  54.         visit(p);
  55. }
复制代码
中序和后序二叉树实现方法上是一致的,照搬递归遍历算法即可,只是需要注意一下在最后一个节点线索化时不能照搬中序的,因为中序遍历下的最后一个结点必然无右孩子,于是这个结点的右指针必然是线索,但是后序显然不是
于是我们有如下二叉树后序线索化实现
  1. /******postorder thread******/
  2. // cannot be called by user
  3. void _thread_post(threadBiTree &T, threadBiNode *&pre)
  4. {
  5.     if (!T)
  6.         return;
  7.     _thread_post(T->lchild, pre);
  8.     _thread_post(T->rchild, pre);
  9.     if (T->lchild == nullptr)
  10.     {
  11.         T->ltag = 1;
  12.         T->lchild = pre;
  13.     }
  14.     if (pre && pre->rchild == nullptr)
  15.     {
  16.         pre->rtag = 1;
  17.         pre->rchild = T;
  18.     }
  19.     pre = T;
  20. }
  21. // assume that the tags of tree have been initialize by zero
  22. void create_thread_post(threadBiTree &T)
  23. {
  24.     if (!T)
  25.         return;
  26.     threadBiNode *pre = nullptr;
  27.     _thread_post(T, pre);
  28.     if (pre && pre->rchild == nullptr)        //如果最后一个结点右指针确实为空那么可以设其为线索
  29.     {
  30.         pre->rtag = 1;
  31.     }
  32. }
复制代码
至于用于反向遍历后序线索二叉树得到代码这里不给出,我也没去细想,反正考研不考,也就问问线索怎么个连法
最后一个就是前序二叉树线索化,前序二叉树线索化需要注意的就是避免某个结点被重复线索化,因为前序遍历的特点,必定是根先被线索化。可以这样想某个前序线索化到某个节点后,直接调用前序线索化函数,如果左指针是线索,那么就开始处理之前已经线索化的结点。试想,如果采用中序、后序类似的代码而不对函数递归调用作出限制,那么至少会产生无限递归的问题,例如我们可以考虑对图中对节点A的线索化
1.webp

所以,限制以下递归调用的条件会产生正确结果,从而我们有如下二叉树先序线索化实现
  1. //file: threadBiTree.cpp
  2. /******preorder thread******/
  3. // cannot be called by user
  4. void _thread_pre(threadBiTree &T, threadBiNode *&pre)
  5. {
  6.     if (!T)
  7.         return;
  8.     if (pre && pre->rchild == nullptr)
  9.     {
  10.         pre->rtag = 1;
  11.         pre->rchild = T;
  12.     }
  13.     if (T->lchild == nullptr)
  14.     {
  15.         T->ltag = 1;
  16.         T->lchild = pre;
  17.     }
  18.     pre = T;
  19.     if (T->ltag == 0) // 若已经线索化再次进入则会因为指针不空再次被线索化所以当未被线索化才递归执行线索化
  20.         _thread_pre(T->lchild, pre);
  21.     if (T->rtag == 0)
  22.         _thread_pre(T->rchild, pre);
  23. }
  24. // assume that the tags of tree have been initialize by zero
  25. void create_thread_pre(threadBiTree &T)
  26. {
  27.     if (!T)
  28.         return;
  29.     threadBiNode *pre = nullptr;
  30.     _thread_pre(T, pre);
  31.     if (pre && pre->rchild == nullptr)
  32.     {
  33.         pre->rtag = 1;
  34.         pre->rchild = nullptr;
  35.     }
  36. }
复制代码
另外,先序遍历下的第一个节点明显根节点,某个节点的后继也很好找,如果该节点有左子树,那么左孩子就是其后继,若无左子树有右子树,那么就是右孩子,如果为叶节点,那么就是后继线索所指向的节点,这样我们能非常轻松地写出先序线索二叉树的正向先序遍历代码,如下所示
  1. //file: threadBiTree.cpp
  2. /**********用于正向中序遍历**********/
  3. // assume that the data structure is validate
  4. threadBiNode *first_thread_pre(threadBiTree &T) {
  5.     return T;
  6. }
  7. // assume that the data structure is validate
  8. threadBiNode *next_thread_pre(threadBiNode *&p) {
  9.     if(!p)
  10.         return nullptr;
  11.     threadBiNode *q = p;
  12.     if(q->ltag == 0)
  13.         return q->lchild;
  14.     else
  15.         return q->rchild;
  16. }
  17. // assume that the data structure is validate
  18. void traverse_thread_pre(threadBiTree &T, void (*visit)(threadBiNode *&p)) {
  19.     for(threadBiNode *p = first_thread_pre(T); p != nullptr; p = next_thread_pre(p))
  20.         visit(p);
  21. }
复制代码
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册