当前位置:网站首页>线索二叉树
线索二叉树
2022-08-08 05:21:00 【codefan※】
基本概念
// 线索二叉树的存储结构
typedef struct ThreadNode{
ElemType data; // 数据元素
struct ThreadNode *lchild, *rchild; // 左右孩子指针
int ltag, rtag; // 左右线索标志
}ThreadNode, *ThreadTree;
在传统的二叉树上,若(某结点)无左子树,令 lchild 指向其前驱节点;若无右子树,令 rchild 指向其后继节点
标志域的含义
- ltag:为0时表示结点的左孩子,为1时表示结点的前驱
- rtag:为0时表示结点的右孩子,为1时表示结点的后继
好处:引入线索二叉树是为了加快查找结点前驱和后继的速度,利用了闲置的 n + 1 个指针
普通的二叉树(链表)叫二叉链表;线索二叉树称为线索链表
中序线索二叉树
构造
- 实质:遍历普通的二叉树
// 中序线索化递归算法
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; // 标记当前的结点作为刚刚访问的结点
InThread(p -> rchild, pre); // 找右子树
}
}
// 中序线索二叉树构建
void CreateInThread(ThreadTree T) {
ThreadTree pre = NULL;
if(T != NULL) {
InThread(T, pre); // 中序线索化
// 最后一个结点的右子树(可能为空)为最后一个结点的后继线索
pre -> rchild = NULL;
pre -> tag = 1;
}
}
改进:增加一个head指针作为线索链表的头结点,变为双向线索链表
void CreateInThread(ThreadTree T) {
ThreadTree head;
ThreadTree pre = NULL;
if(T != NULL) {
head -> lchild = T; // head的左指针指向线索链表表头
InThread(T, pre); // 中序线索化
head -> rchild = pre; // head的右指针指向线索链表表尾
pre -> rchild = head; // 最后一个结点的右指针指回头结点
pre -> tag = 1; // 标志化
}
}
其他版本的代码(对比参考)(可以使用第二个图自己走一遍)(动图解析)

遍历
// 中序线索二叉树的遍历(不含头结点)
// 第一个结点
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;
}
边栏推荐
- VSCode已经设置过为中文但变成英文的解决办法
- 121 distributed interview questions and answers
- Unity鼠标光标使用学习
- The shell updates the terminal output information in place
- L3-007 ladder map (test point 2 is stuck, you can see it)
- 121道分布式面试题和答案
- Object.prototype.toString()如何判断数据类型及注意点
- Leetcode78. 子集
- Error: [Intervention] Unable to preventDefault inside passive event listener due to target ...
- 单主机docker 搭建 redis-cluster
猜你喜欢

vulnhub-DC-3 drone penetration record

spark入门学习-3-SparkSQL数据抽象

Awk syntax-03-awk expressions (if statements, while loops, for loops), execute shell commands in awk

nonebot插件:说话的艺术

The big and small end problem caused by union union

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

预处理笔记

Database sub-database sub-table, when?How to divide?

2022-08-07 mysql/stonedb slow SQL-subquery-semi-join

Leetcode78. 子集
随机推荐
Flatten multidimensional array to one dimension
【Win10】Several sleep problems and countermeasures
Use of Filter
中间件的一些坑记录
[Redis] Redis Learning - Transaction
Connect two tables to update the third table (updata) in postgresql
走进音视频的世界——RGB与YUV格式
Machine Learning Notes: Learning Rate Warmup
KMP和EXKMP(Z函数)
C language force to deduct the length of the last word of the 58th question.Traverse from back to front
76. The minimum cover substring
Talk about the principle and implementation of Redis distributed lock [Ant Financial Services Three Sides]
shell原地更新终端输出信息
KDD‘22推荐系统论文梳理(24篇研究&36篇应用论文)
【猜拳游戏 基于Objective-C语言】
Hard Disk Basics
Redis设置开机自启动
OLTP和OLAP问题的个人总结
The difference between CHAR_LENGTH() and LENGTH() in MySQL
Week 9 10 Neural Networks