线索二叉树
1、线索二叉树概述
1.1 线索二叉树的使用背景
对于一颗二叉树而言(如下图所示),每次我们想要查找二叉树里面的一个节点的时候,我们都必须遍历这棵树,以中序遍历为例,遍历的顺序为4251637,当我们遍历到标号为5这个节点的时候,如果有个指针指向5的前一个节点2或者后一个节点1的话,那么我们跳转到2或者1的效率肯定会大大的提升,那我们在哪里去创建这种指针呢?刚好我们的二叉树除了父节点以外,其它的每一个节点都有一个节点指向它,意味着二叉树有(n-1)条链被使用,而我们的二叉树一共有2N条链,所以二叉树会有 2n-(n-1) = n+1 条空链,这n+1条空链存在这空间浪费的情况。所以线索二叉树就是为了合理使用这n+1条空链域而产生的一种数据结构。
1.2线索二叉树创建原理
1、线索二叉树将二叉树中 n+1条空链域用来线索化二叉树,在线索二叉树中,我们将一个节点的前一个节点叫做前驱节点,一个节点的后一个节点叫做后继节点。
2、在线索化二叉树的过程中我们会遇到这样一个问题,有些指针本来就是指向我们的下一个节点的,这样的话我们是分不清楚到底这个指针是指向它的前后驱节点还是子节点的。为了解决这个问题,我们将指针分成两个部分,一个布尔值,一个指向下一个节点的地址,如下图所示:
当LFlag为true时,表示LChild指向这个节点的前驱节点,为False时,LChild指向左节点
同理当RFlag为true时,表示RChild指向该节点的后继节点,为False时,RChild指向右节点
2、线索二叉树代码实现
2.1 线索二叉树创建
线索二叉树的创建
package ClueBinary;
/**
* 二叉树对象
*/
public class ClueNode<T> {
// 用于存放节点的权
private T value;
private boolean lflag;
private boolean rflag;
// 左节点
private ClueNode<T> leftNode;
// 右节点
private ClueNode<T> rightNode;
public ClueNode(T value) {
this.value = value;
}
public ClueNode<T> getLeftNode() {
return leftNode;
}
public boolean isLflag() {
return lflag;
}
public void setLflag(boolean lflag) {
this.lflag = lflag;
}
public boolean isRflag() {
return rflag;
}
public void setRflag(boolean rflag) {
this.rflag = rflag;
}
public void setLeftNode(ClueNode<T> leftNode) {
this.leftNode = leftNode;
}
public ClueNode<T> getRightNode() {
return rightNode;
}
public void setRightNode(ClueNode<T> rightNode) {
this.rightNode = rightNode;
}
/**
* 前序遍历
*/
public void frontShow() {
if (this == null) {
return;
}
//打印根节点
System.out.println(this.value);
if (this.leftNode != null) {
this.leftNode.frontShow();
}
if (this.rightNode != null) {
this.rightNode.frontShow();
}
}
/**
* 中序遍历
*/
public void middelShow() {
if (this == null) {
return;
}
if (this.leftNode != null) {
this.leftNode.middelShow();
}
//打印根节点
System.out.println(this.value);
if (this.rightNode != null) {
this.rightNode.middelShow();
}
}
/**
* 后续遍历
*/
public void afterShow() {
if (this == null) {
return;
}
if (this.leftNode != null) {
this.leftNode.afterShow();
}
if (this.rightNode != null) {
this.rightNode.afterShow();
}
//打印根节点
System.out.println(this.value);
}
/**
* 前序查找
*
* @param value
*/
public ClueNode frontSearch(T value) {
ClueNode treeNode = null;
if (this.value == value) {
return this;
}
if (this.leftNode != null) {
treeNode = this.leftNode.frontSearch(value);
}
if (treeNode != null) {
return treeNode;
}
if (this.rightNode != null) {
treeNode = this.rightNode.frontSearch(value);
}
return treeNode;
}
/**
* 前序查找
*
* @param value
*/
public ClueNode middleSearch(T value) {
ClueNode treeNode = null;
if (this.leftNode != null) {
treeNode = this.leftNode.frontSearch(value);
}
if (treeNode != null) {
return treeNode;
}
if (this.value == value) {
return this;
}
if (this.rightNode != null) {
treeNode = this.rightNode.frontSearch(value);
}
return treeNode;
}
/**
* 前序查找
*
* @param value
*/
public ClueNode afterSearch(T value) {
ClueNode treeNode = null;
if (this.leftNode != null) {
treeNode = this.leftNode.frontSearch(value);
}
if (treeNode != null) {
return treeNode;
}
if (this.rightNode != null) {
treeNode = this.rightNode.frontSearch(value);
}
if (treeNode != null) {
return treeNode;
}
if (this.value == value) {
return this;
}
return treeNode;
}
}
线索二叉树的线索化,和遍历方法,代码以中序遍历为例,二叉树线索化后,调用中序遍历与调用线索化threadNodes()后调用遍历线索二叉树方法threadIterate()的结果是一样的。
package ClueBinary;
/**
* 线索二叉树
*/
public class ClueBinaryTree<T> {
private ClueNode<T> root;
private ClueNode<T> pre;
public ClueBinaryTree(ClueNode<T> root) {
this.root = root;
}
/**
* 遍历线索二叉树
*/
public void threadIterate(){
//临时存储当前遍历节点
ClueNode<T> node = root;
while (node!=null) {
if (root != null) {
//循环找到最开始的节点
while (!node.isLflag()) {
node = node.getLeftNode();
}
// 打印当前节点
System.out.println(node.getValue());
//如果当前节点的右指针指向的的是后继节点,循环找到最后一个
while (node.isRflag()) {
node = node.getRightNode();
System.out.println(node.getValue());
}
//替换遍历的节点
node = node.getRightNode();
}
}
}
/**
* 中序线索化二叉树
*/
public void threadNodes(){
threadNodes(root);
}
/**
* 遍历线索二叉树
* @param node
*/
public void threadNodes(ClueNode<T> node){
if (node == null){
return;
}
// 处理左子树
threadNodes(node.getLeftNode());
//处理前驱节点
if (node.getLeftNode() == null){
node.setLeftNode(pre);
node.setLflag(true);
}
//处理前驱节点的右指针
if (pre!=null && pre.getRightNode() == null){
pre.setRightNode(node);
pre.setRflag(true);
}
pre = node;
//处理右子树
threadNodes(node.getRightNode());
}
/**
* 中序遍历
*/
public void middleShow(){
root.middelShow();
}
/**
* 前序遍历
*/
public void frontShow(){
if (root == null){
return;
}
root.frontShow();
}
}