线索二叉树基本介绍
1)
n
个结点的二叉链表中含有
n+1 【
公式
2n-
(n-1)=
n+1】
个
空指针域。利用二叉链表中的空指针域,存放指
向
该
结
点
在
某种遍历次序
下的前驱和后继结
点的指针(
这种附加的指针称为
"
线索
"
)
●
2)
这
种加上了线索的二叉链表称为
线索链表
,相应的二叉树称为
线索二叉树
(Threaded
BinaryTree
)
。根据线索性质的不同,线索二叉树可分为
前序线索二叉树、中序线索二叉树
和
后序线索二叉树
三
种
●
3)
一个结点的前一
个结点
,称为
前驱
结点
4)
一个结点
的后一
个结点
,称为
后继
结点
代码演示:
package com.liu.stream;
public class StreamBinaryTreeDemo {
public static void main(String[] args) {
// 测试一把中序线索二叉树的功能
HeroNode root = new HeroNode(1, "tom");
HeroNode node2 = new HeroNode(3, "jack");
HeroNode node3 = new HeroNode(6, "smith");
HeroNode node4 = new HeroNode(8, "mary");
HeroNode node5 = new HeroNode(10, "king");
HeroNode node6 = new HeroNode(14, "dim");
// 二叉树,后面我们要递归创建, 现在简单处理使用手动创建
root.setLeft(node2);
root.setRight(node3);
node2.setLeft(node4);
node2.setRight(node5);
node3.setLeft(node6);
// 测试中序线索化
StreamBinarytree threadedBinaryTree = new StreamBinarytree();
threadedBinaryTree.setRoot(root);
threadedBinaryTree.StreamNode();
// 测试: 以10号节点测试
HeroNode leftNode = node4.getLeft();
HeroNode rightNode = node4.getRight();
System.out.println("8号结点的前驱结点是 =" + leftNode); // null
System.out.println("8号结点的后继结点是=" + rightNode); // 3
// 当线索化二叉树后,能在使用原来的遍历方法
// threadedBinaryTree.infixOrder();
System.out.println("使用线索化的方式遍历 线索化二叉树");
threadedBinaryTree.StreamList(); // 8, 3, 10, 1, 14, 6
}
}
//定义StreamBinarytree 实现了线索化功能的二叉树
class StreamBinarytree {
private HeroNode root;
// 为了实现线索化,需要创建要给指向当前结点的前驱结点的指针
// 在递归进行线索化时,pre 总是保留前一个结点
private HeroNode pre = null;
public void setRoot(HeroNode root) {
this.root = root;
}
// 重载一把threadedNodes方法
public void StreamNode() {
this.StreamNode(root);
}
public void StreamList() {
HeroNode node = root;
while (node != null) {
//循环的找到leftType == 1的结点,第一个找到就是8结点
//后面随着遍历而变化,因为当leftType==1时,说明该结点是按照线索化
//处理后的有效结点
while (node.getLeftType() == 0) {
node = node.getLeft();
}
//打印当前这个结点
System.out.println(node);
//如果当前结点的右指针指向的是后继结点,就一直输出
while (node.getRightType() == 1) {
node = node.getRight();
System.out.println(node);
}
node = node.getRight();
}
}
public void StreamNode(HeroNode node) {
if (node == null) {
return;
}
//(一)先线索化左子树
StreamNode(node.getLeft());
//(二)线索化当前结点
//处理当前结点的前驱结点
//以8结点来理解
//8结点的.left = null , 8结点的.leftType = 1
if (node.getLeft() == null) {
//让当前结点的左指针指向前驱结点
node.setLeft(pre);//第一次时的pre为null
// System.out.println("pre="+pre);
//修改当前结点的左指针的类型,指向前驱结点
node.setLeftType(1);
}
if (pre != null && pre.getRight() == null) {
//让前驱结点的右指针指向当前结点
pre.setRight(node);
System.out.println("pre="+pre+"node="+node);
//修改前驱结点的右指针类型
pre.setRightType(1);
}
//!!! 每处理一个结点后,让当前结点是下一个结点的前驱结点
pre = node;
//(三)在线索化右子树
StreamNode(node.getRight());
}
}
class HeroNode {
private int id;
private String name;
private HeroNode left;
private HeroNode right;
private HeroNode node;
private int LeftType;
private int RightType;
public int getLeftType() {
return LeftType;
}
public void setLeftType(int leftType) {
LeftType = leftType;
}
public int getRightType() {
return RightType;
}
public void setRightType(int rightType) {
RightType = rightType;
}
public HeroNode(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public HeroNode getLeft() {
return left;
}
public void setLeft(HeroNode left) {
this.left = left;
}
public HeroNode getRight() {
return right;
}
public void setRight(HeroNode right) {
this.right = right;
}
public HeroNode getNode() {
return node;
}
public void setNode(HeroNode node) {
this.node = node;
}
@Override
public String toString() {
return "HeroNode [id=" + id + ", name=" + name + "]";
}
public void Preorder() {
System.out.println(this);
if (this.left != null) {
this.left.Preorder();
}
if (this.right != null) {
this.right.Preorder();
}
}
public void Midorder() {
if (this.left != null) {
this.left.Midorder();
}
System.out.println(this);
if (this.right != null) {
this.right.Midorder();
}
}
public void Postorder() {
if (this.left != null) {
this.left.Postorder();
}
if (this.right != null) {
this.right.Postorder();
}
System.out.println(this);
}
public HeroNode PreorderSearch(int id) {
System.out.println("开始查找");
if (this.id == id) {
return this;
}
HeroNode resNode = null;
if (this.left != null) {
resNode = this.left.PreorderSearch(id);
}
if (resNode != null) {
return resNode;
}
if (this.right != null) {
resNode = this.right.PreorderSearch(id);
}
return resNode;
}
public HeroNode MidorederSearch(int id) {
HeroNode resNode = null;
if (this.left != null) {
resNode = this.left.MidorederSearch(id);
}
if (resNode != null) {
return resNode;
}
System.out.println("开始查找");
if (this.id == id) {
return this;
}
if (this.right != null) {
resNode = this.right.MidorederSearch(id);
}
return resNode;
}
public HeroNode PostorderSearch(int id) {
HeroNode resNode = null;
if (this.left != null) {
resNode = this.left.PostorderSearch(id);
}
if (resNode != null) {
return resNode;
}
if (this.right != null) {
resNode = this.right.PostorderSearch(id);
}
if (resNode != null) {
return resNode;
}
System.out.println("开始查找");
if (this.id == id) {
return this;
}
return resNode;
}
}