博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据结构——线索二叉树
阅读量:4099 次
发布时间:2019-05-25

本文共 1177 字,大约阅读时间需要 3 分钟。

原文地址:

遍历一个二叉树要么用递归,要么用附加的栈。线索二叉树的思想是让中序遍历更快,或者不用栈和递归。一个二叉树通过构建所有右孩子的指针建立索引,这个指针一般为NULL指向中序后继节点(如果存在的话)。

线索二叉树有两种类型:

单线索:使一个NULL右指针指向中序先驱(如果后继存在)

双线索:左右NULL指针分别指向中序的先驱和中序的后继。在翻转中序遍历与后序遍历的时候,前驱线索就很有用啦。

线索在快速访问祖先节点的时候也很有用。

下图是一个单线索二叉树的例子,虚线表示的是线索。

这里写图片描述

C表示的带线索的节点

下面是C表示的带线索的节点

struct Node {    int data;    Node *left, *right;    bool rightThread;  }

因为右指针有两个用途,所以布尔变量rightThread用于表示右指针指向的是右孩子还是中序后继。类似地,我们可以添加leftThread作为双线索二叉树。

用线索中序遍历

下面是中序遍历线索二叉树的C代码。

// Utility function to find leftmost node in atree rooted with nstruct Node* leftMost(struct Node *n){    if (n == NULL)       return NULL;    while (n->left != NULL)        n = n->left;    return n;}// C code to do inorder traversal in a threadded binary treevoid inOrder(struct Node *root){    struct Node *cur = leftmost(root);    while (cur != NULL)    {        printf("%d ", cur->data);        // If this node is a thread node, then go to        // inorder successor        if (cur->rightThread)            cur = cur->right;        else // Else go to the leftmost child in right subtree            cur = leftmost(cur->right);    }}

下图描述了用线索做中序遍历。

这里写图片描述

我们将很快讨论线索二叉树的插入删除操作。

来源:

www.cs.berkeley.edu/~kamil/teaching/su02/080802.ppt

你可能感兴趣的文章
PHP 7 的五大新特性
查看>>
php使用 memcache 来存储 session
查看>>
php实现socket(转)
查看>>
PHP底层的运行机制与原理
查看>>
深入了解php底层机制
查看>>
PHP中的stdClass 【转】
查看>>
XHProf-php轻量级的性能分析工具
查看>>
PHP7新特性 What will be in PHP 7/PHPNG
查看>>
比较strtr, str_replace和preg_replace三个函数的效率
查看>>
ubuntu 下编译PHP5.5.7问题:configure: error: freetype.h not found.
查看>>
PHP编译configure时常见错误 debian centos
查看>>
configure: error: Please reinstall the BZip2 distribution
查看>>
OpenCV gpu模块样例注释:video_reader.cpp
查看>>
【增强学习在无人驾驶中的应用】
查看>>
《python+opencv实践》四、图像特征提取与描述——29理解图像特征
查看>>
《python+opencv实践》四、图像特征提取与描述——30Harris 角点检测
查看>>
《python+opencv实践》四、图像特征提取与描述——31 Shi-Tomasi 角点检测& 适合于跟踪的图像特征
查看>>
OpenCV meanshift目标跟踪总结
查看>>
人工神经网络——神经元模型介绍
查看>>
人工神经网络——感知器介绍
查看>>