前言

最近在读霍罗维兹的《数据结构基础》(Fundamentals of Data Structures in C),本篇博客为阅读笔记和知识总结。


0x00 线索(threads)

具有 个结点的二叉链表共有 个链域,其中 为空链域。

A.J.Perlis 与 C.Thornton 提出一种方法,用用原来的空链域存放指针,指向树中的其他结点。

这种指针就被称为 线索(threads),记 ptr 指向二叉链表中的一个结点,一下是建线索的规则:

① 如果 ptr->leftChild 为空,则存放指向中序遍历排列中该节点的前驱结点。该节点成为 ptr 的 中序前驱(inorder predecessor)

② 如果 ptr->rightChild 为空,则存放指向中序遍历序列中该结点的后继节点。这个节点称为 ptr 的中序后继(inorder successor)。

当我们在内存中表示树时,我们必须能够区分线程和普通指针。

这是通过在节点结构中添加两个额外的字段来实现的,即 left_threadright_thread

typedef struct threaded_tree* threaded_pointer;
typedef struct threaded_tree {short int left_thread;threaded_pointer left_child;char data;threaded_pointer right_child;short int right_thread;
};

我们假设所有线程二叉树都有一个头结点。

线程二叉树的无序遍历 确定一个节点的无序继承者。

[Program 5.10]  Finding the inorder successor of a node.

threaded_pointer insucc(threaded_pointer tree)
{/* find the inorder successor of treein a threaded binary tree */threaded_pointer temp;temp = tree->right_child;if (!tree->right_thread)while (!temp->left_thread)temp = temp->left_child;return temp;
}

为了进行中序遍历,我们反复调用 insucc

[Program 5.11]  Inorder traversal of a threaded binary tree.

void tinorder(threaded_pointer tree)
{/* traverse the threaded binary tree inorder */threaded_pointer temp = tree;for (; ; ) {temp = insucc(temp);if (temp == tree) break;printf("%3c", temp->data);}
}

向线程二叉树插入一个节点,假设我们有一个节点,即 parent,它的右子树是空的。我们希望插入child 作为父节点的右子节点。

要做到这一点,我们必须:

① 将 parent-> right_thread 改为 FALSE

② 将 child->left_threadchild->right_thread 改为 TRUE

③ 将 child->left_child 改为指向 parent

④ 将 child->right_child 改为 parent->right_child

⑤ 将 parent->right_child 改为指向 child 

对于 parent 具有非空右子树的情况:

处理这两种情况的 C 代码如下:

[Program 5.12] : Right insertion in a threaded binary tree

void insert_right(threaded_pointer parent, threaded_pointer child) {/* insert child as the right child of parent in a threaded binary tree */threaded_pointer temp;child->right_child = parent->right_child;child->right_thread = parent->right_thread;child->left_child = parent;child->left_thread = TRUE;parent->right_child = child;parent->right_thread = FALSE;if (!child->right_thread) {temp = insucc(child);temp->left_child = child;}
}

参考资料

Fundamentals of Data Structures in C

【霍罗维兹数据结构】线索二叉树 | THREADED BINARY TREES相关推荐

  1. 【霍罗维兹数据结构】二叉树前中后序遍历 | 层序遍历 | 复制二叉树 | 判断两个二叉树全等 | 可满足性问题

    写在前面 学习二叉树结构,最简单的方式就是遍历.所谓二叉树遍历,就是按照某种特定的规则,一次对二叉树中的节点进行相应的操作,并且每个节点只操作一次. 访问节点所做的操作要看具体的应用问题.遍历是二叉树 ...

  2. 【霍罗维兹数据结构】多维数组 | 字符串 | 模式匹配

    前言: 最近在读霍罗维兹的<数据结构基础>(Fundamentals of Data Structures in C),本篇博客为阅读笔记和知识总结. 目录: Ⅰ.  多维数组的表示 0x ...

  3. 编程基础 - 线索二叉树 (Threaded Binary Tree)

    编程基础 - 线索二叉树 (Threaded Binary Tree) 返回分类:全部文章 >> 基础知识 返回上级:编程基础 - 二叉树 (Binary Tree) 本文将介绍线索二叉树 ...

  4. C语言实现线索二叉树Threaded Binary Tree (附完整源码)

    C语言实现线索二叉树Threaded Binary Tree 树节点定义 实现以下7个接口 完整实现和main测试源码 树节点定义 typedef struct Node {int data; /** ...

  5. (数据结构基础)Among the following threaded binary trees (the threads are represented by dotted curves),……

    当年学这些的时候真的是苦于没有人讲,现在复习考研,我会见到这种题就写下来,学弟学妹们欢迎点个关注,最近也在创业想实习的可以找我联系:没看懂的话是我表述有问题,欢迎指出和私戳. Among the fo ...

  6. C语言 题目 1698: 数据结构-线索二叉树

    <大话数据结构>上的,大家可以参考一下  题目描述 在遍历二叉树的过程中,是按照一定的规则将二叉树中的结点排列成一个线性序列,从而得到二叉树中结点的先序序列或中序序列或后序序列.但是,当以 ...

  7. (原)数据结构——线索二叉树

    原文地址:http://www.cnblogs.com/Security-Darren/p/4716082.html 转载务必注明出处! 线索二叉树的思想来源于二叉树的存储结构中,存在一些空的指针域, ...

  8. 数据结构——线索二叉树(TBT)

    文章目录 一.什么是线索二叉树(TBT)? 二.线索二叉树的建立 三.线索二叉树的实现 一.什么是线索二叉树(TBT)? 线索二叉树是一种优化的二叉树结构,对于以结构体指针实现二叉树的方案进行了顺序遍 ...

  9. 二叉树(Binary Trees)

    二叉树 通常在一棵树中,树中的每个节点可能有任意数量的子节点.二叉树是普通树的一个特例,在二叉树中,每个节点至多有俩个子节点,其中一个成为左子节点,另外一个称为右子节点. 二叉树的属性 在非空二叉树中 ...

最新文章

  1. 访问远程数据库,把远程数据库当做本地库来用
  2. 【数据平台】Python解析Ngnix日志
  3. 【转】分布式websocket服务器
  4. 回车 触发 提交事件
  5. 线程池是如何重复利用空闲的线程来执行任务的?
  6. 按图索骥:SQL中数据倾斜问题的处理思路与方法
  7. tornado异步客户端(Future)
  8. java把字体做成滚动的_Scroll text - JS实现文字自动循环滚动效果
  9. TENER: Adapting Transformer Encoder for Name Entity Recognition
  10. C语言随笔小算法:char字节流与结构体变量相互转换
  11. Matlab中TCP通讯-实现外部程序提供优化目标函数解
  12. java.lang.UnsupportedOperationException: This parser does not support specification “null“ version “
  13. SLAM技术与市场杂谈
  14. 分享新作:休闲小游戏『Flying Stone』
  15. 如何提高关键词的质量度?
  16. Ceres Solver实现简单的光束法平差
  17. python脚本报错-qt.qpa.plugin: Could not load the Qt platform plugin “xcb“
  18. 用c语言编程一个英尺转换器,C语言中关于英尺、英寸、厘米的换算
  19. 代码随想录第十八天 LeetCode513、112、113、106、105
  20. 【计算机毕业设计】188校园商铺管理系统设计与实现

热门文章

  1. Windows XP注册表信息
  2. 洛谷P5238 整数校验器【三月月赛】
  3. 模式识别:三层BP神经网络的设计与实现
  4. 2018推荐的android手机,2018年7月份Android手机性价比排行榜
  5. php如何计算三角函数,使用Python三角函数公式计算三角形的夹角代码示例
  6. 基于MATLAB的图片中文字的提取及识别
  7. Java类有个星号标记_Java中import包带*(星号)问题
  8. python 中文姓名库_中文人名语料库。中文姓名,姓氏,名字,称呼,日本人名,翻译人名,英文人名。...
  9. outlook邮箱邮件大小限制_附件大小超过了允许的限制错误 - Outlook | Microsoft Docs...
  10. KSO-.NETCore中配置swagger分级