- 先看一个问题
将数列 {1, 3, 6, 8, 10, 14 } 构建成一颗二叉树. n+1=7
问题分析:
- 当我们对上面的二叉树进行中序遍历时,数列为 {8, 3, 10, 1, 6, 14 }
- 但是 6, 8, 10, 14 这几个节点的 左右指针,并没有完全的利用上.
- 如果我们希望充分的利用 各个节点的左右指针, 让各个节点可以指向自己的前后节点,怎么办?
- 解决方案-线索二叉树
线索化二叉树的基本介绍
- n个结点的二叉链表中含有n+1 【公式 2n-(n-1)=n+1】 个空指针域。利用二叉链表中的空指针域,存放指向该结点在某种遍历次序下的前驱和后继结点的指针(这种附加的指针称为"线索")
- 这种加上了线索的二叉链表称为线索链表,相应的二叉树称为线索二叉树(Threaded BinaryTree)。根据线索性质的不同,线索二叉树可分为前序线索二叉树、中序线索二叉树和后序线索二叉树三种
- 一个结点的前一个结点,称为前驱结点
- 一个结点的后一个结点,称为后继结点
线索化二叉树应用案例
应用案例说明:
将下面的二叉树,进行中序线索二叉树。中序遍历的数列为 {8, 3, 10, 1, 14, 6}
思路分析:中序遍历结果{8, 3, 10, 1, 14, 6}
说明: 当线索化二叉树后,Node节点的 属性 left 和 right ,有如下情况:
- left 指向的是左子树,也可能是指向的前驱节点. 比如 ① 节点 left 指向的左子树, 而 ⑩ 节点的 left 指向的就是前驱节点.
- right指向的是右子树,也可能是指向后继节点,比如 ① 节点right 指向的是右子树,而⑩ 节点的right 指向的是后继节点.
代码实现:
public class ThreadedBinaryTree {
private Node root;
// 前驱节点
private Node pre;
public Node getRoot() {
return root;
}
public void setRoot(Node root) {
this.root = root;
}
/**
* 线索化二叉树
*/
public void threadedTree(){
threadedTree(root);
}
/**
* 线索化遍历
*/
public void threadedList() {
if (root == null) {
return;
}
Node temp = root;
// 通过循环找到左节点为null的节点
while (temp != null) {
while (temp.getLeftType() != 1) {
temp = temp.left;
}
System.out.println(temp.getData() + " ");
// 如果该节点的右节点的类型为1,表示 是后继节点,则继续输出
while (temp.getRightType() == 1) {
temp = temp.right;
System.out.println(temp.getData());
}
// 代码走到这,说明该节点的右节点不是后继节点,则把该节点的右节点
temp = temp.right;
}
}
/**
* 线索化二叉树,中序线索化,方法要明白参数node是谁,它的上一级是谁,pre是谁
* @param node
*/
public void threadedTree(Node node) {
if (node == null) {
return;
}
// 因为是中序线索化,所以先要递归该节点左子节,找到该树最左子节点
threadedTree(node.left);
// 在线索化该节点,因为是中序,所以先判断该节点的左子节点
if (node.left == null) {
// 如果该节点的左子节点为空,那么我们就让该节点左子节点指向其前驱节点
node.left = pre;
// 改变该节点的左子节点的类型为1,表示时指向前驱
node.setLeftType(1);
}
// 因为该节点的后继节点,在此次方法调用中我们无法得知,
// 我们只能知道该节点的前驱节点的后继节点是该节点
if (pre != null && pre.right == null) {
pre.right = node;
// 设置该节点的前驱节点的右子节点的类型为1,
pre.setRightType(1);
}
// 一定要把前驱节点设置成该节点,
// 因为根据中序遍历,最先调用该方法的左子节点,然后父节点,最后是右子节点,按照中序遍历来的
pre = node;
// 在递归处理该节点的右子节点
threadedTree(node.right);
}
public void midList() {
if (root == null) {
return;
}
root.midList();
}
}
/**
* 节点
*/
class Node {
private int data;
public Node left;
public Node right;
// 该节点左右指针的类型,如果为0表示指向的是左子节点,1表示的是前驱节点
private int leftType;
// 该节点左右指针的类型,如果为0表示指向的是右子节点,1表示的是前驱节点
private int rightType;
public Node(int data) {
this.data = data;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public int getLeftType() {
return leftType;
}
public void setLeftType(int leftType) {
this.leftType = leftType;
}
public int getRightType() {
return rightType;
}
public void setRightType(int rightType) {
this.rightType = rightType;
}
// 中序遍历
public void midList() {
if (this.left != null) {
this.left.midList();
}
// 输出该节点
System.out.print(this.getData() + " ");
if (this.right != null) {
this.right.midList();
}
}
}
测试类
public class ThreadedBinaryTreeTest {
public static void main(String[] args) {
ThreadedBinaryTree threadedBinaryTree = new ThreadedBinaryTree();
Node node1 = new Node(1);
Node node2 = new Node(3);
Node node3 = new Node(6);
Node node4 = new Node(8);
Node node5 = new Node(10);
Node node6 = new Node(14);
node2.left = node4;
node2.right = node5;
node3.left = node6;
node1.left = node2;
node1.right = node3;
// 线索化二叉树,
threadedBinaryTree.setRoot(node1);
// 中序遍历
System.out.println("中序遍历结果");
threadedBinaryTree.midList();
System.out.println("线索化后");
threadedBinaryTree.threadedTree();
System.out.println("线索化后遍历的结果:");
threadedBinaryTree.threadedList();
}
}
结果:
中序遍历结果
8 3 10 1 14 6 线索化后
线索化后遍历的结果:
8
3
10
1
14
6
结果一致,
线索化代码不好理解,关键要理解递归的调用