//线索二叉树存储结构
typedef struct TBTNode
{
int data;
int lTag;
int rTag;
TBTNode* lChild;
TBTNode* rChild;
}TBTNode;
//中序线索化
//pre指向p的前驱结点,对比二叉树遍历的代码
void inThread(TBTNode *p, TBTNode *&pre)
{
if (p != NULL)
{
inThread(p->lChild, pre);
if (p->lChild == NULL)//上一层循环条件已经判断过p不为空
{
p->lChild = pre;
p->lTag = 1;
}
if (pre != NULL && pre->rChild == NULL)
{
pre->rChild = p;
pre->rTag = 1;
}
pre = p;
inThread(p->rChild, pre);
}
}
//先序线索化
void preThread(TBTNode *p, TBTNode *&pre)
{
if (p != NULL)
{
if (p->lChild == NULL)//上一层循环条件已经判断过p不为空
{
p->lChild = pre;
p->lTag = 1;
}
if (pre != NULL && pre->rChild == NULL)
{
pre->rChild = p;
pre->rTag = 1;
}
pre = p;
if (p->lTag == 0)//先线索化会导致p指针在线索分支打转,增加的判断条件
inThread(p->lChild, pre)
树 二叉树线索化
最新推荐文章于 2024-09-11 14:37:20 发布
本文深入探讨了二叉树的线索化过程,这是一种优化二叉树遍历的方法。通过在二叉树节点中添加线索,可以使得在不保存额外指针的情况下实现前驱和后继节点的快速访问。内容包括线索二叉树的概念、建立线索二叉树的步骤以及如何进行线索化的遍历操作。
摘要由CSDN通过智能技术生成