当前位置:网站首页>clue binary tree
clue binary tree
2022-08-08 05:25:00 【codefan※】
基本概念
// 线索二叉树的存储结构
typedef struct ThreadNode{
ElemType data; // 数据元素
struct ThreadNode *lchild, *rchild; // 左右孩子指针
int ltag, rtag; // 左右线索标志
}ThreadNode, *ThreadTree;
In the traditional binary tree,若(某结点)无左子树,令 lchild 指向其前驱节点;若无右子树,令 rchild 指向其后继节点
The meaning of the symbol domain
- ltag:为0Said in a node's left child,为1时表示结点的前驱
- rtag:为0That node's right child,为1时表示结点的后继
好处:引入线索二叉树是为了加快查找结点前驱和后继的速度,Using the spare n + 1 个指针
普通的二叉树(链表)Called binary list;Clues binary tree is called the list
中序线索二叉树
构造
- 实质:Binary tree traversal ordinary
// Sequence clues in the recursive algorithm
void InThread(ThreadTree &p, ThreadTree &pre) {
if(p != NULL) {
InThread(p -> lchild, pre); // 找左子树
if(p -> lchild == NULL) {
p -> lchild = pre; // 左子树为空,建立前驱线索
p -> tag = 1; // 标志位
}
if(pre != NULL && pre -> rchild == NULL) {
pre -> rchild = p; // 右子树为空,建立前驱结点的后继线索
pre -> rtag = 1; // 标志位
}
pre = p; // Mark the current nodes as just visit
InThread(p -> rchild, pre); // 找右子树
}
}
// Sequence clues in binary tree to build
void CreateInThread(ThreadTree T) {
ThreadTree pre = NULL;
if(T != NULL) {
InThread(T, pre); // 中序线索化
// The last node of the right subtree(可能为空)For the last node of the subsequent clues
pre -> rchild = NULL;
pre -> tag = 1;
}
}
改进:增加一个headPointers as clues to the head of the linked list node,A two-way clues linked list
void CreateInThread(ThreadTree T) {
ThreadTree head;
ThreadTree pre = NULL;
if(T != NULL) {
head -> lchild = T; // headHead left pointer pointing to the clues of the chain table
InThread(T, pre); // 中序线索化
head -> rchild = pre; // headPointer to the right clues tail chain table
pre -> rchild = head; // The last node pointer refers to the right turn head node
pre -> tag = 1; // 标志化
}
}
其他版本的代码(对比参考)(You can use the second figure you go again)(动图解析)

遍历
// 中序线索二叉树的遍历(不含头结点)
// 第一个结点
ThreadNode *Firstnode(ThreadNode *p) {
while(p -> ltag == 0) p = p -> lchild;
return p;
}
// 结点p的后继
ThreadNode *Nextnode(ThreadNode *p) {
if(p -> rtag == 0) return Firstnode(p -> rchild);
else return p -> rchild;
}
// 最后一个结点
ThreadNode *Lastnode(ThreadNode *p) {
while(p -> rtag == 0) p = p -> rchild;
return p;
}
// 结点p的前驱
ThreadNode *Prenode(ThreadNode *p){
if(p -> ltag == 0) return Fristnode(p -> lchild);
else return p -> lchild;
}
边栏推荐
- 自动化工具
- 硬盘基础知识
- 【Redis】Redis学习——事务
- 0 dictionary tree/string medium LeetCode676. Implement a magic dictionary
- Typescript 命名空间
- postgresql中连接两张表更新第三张表(updata)
- Single host docker builds redis-cluster
- BMS 产品功能和详细设计规格
- Week 8 Generative Adversarial Networks(生成对抗网络 GAN)
- C language force to deduct the length of the last word of the 58th question.Traverse from back to front
猜你喜欢

Sqlmap + dnslog injection of repetition

自动化工具

postman---postman参数化
![Talk about the principle and implementation of Redis distributed lock [Ant Financial Services Three Sides]](/img/ed/dbaeab544066b5dbf19e71bee0f8c9.png)
Talk about the principle and implementation of Redis distributed lock [Ant Financial Services Three Sides]

Leetcode78. Subset

C语言-函数

Unity mouse cursor usage learning

Flutter 教程之高效且精美的滚动组件Slivers (教程含源码)

C language - score and loop statement

C language - function
随机推荐
76. 最小覆盖子串
单主机docker 搭建 redis-cluster
基础了解虚拟 DOM
毕设——基于人脸表情的桌面交互精灵设计(分享一下成果,附上人脸表情的数据集和自己训练出来yolov5模型以及基于PYQT5运行yolov5的交互界面)
Error: [Intervention] Unable to preventDefault inside passive event listener due to target ...
sessionStorage在不同页签中的数据是否共享问题及解决思路
KMP和EXKMP(Z函数)
bpftrace:简便输出调试信息
IP核之RAM实验
关于如何做选择
ES6对象字面量的新功能
14.Unity2D 横版 粒子系统特效 飙血粒子+高处落地粒子+对象池管理所有粒子
tracepoint: 定义函数及调用示例
postgresql中连接两张表更新第三张表(updata)
MYSQL export data dictionary
MySQL4 (multi-table query)
The difference between classification, object detection, semantic segmentation, and instance segmentation
说说Redis分布式锁的原理和实现蚂【蚁金服三面】
Personal Summary of OLTP and OLAP Issues
LVS:NAT模式详解