本文已参与「新人创作礼」活动,一起开启掘金创作之路。
线索化二叉树
将数列 {1, 3, 6, 8, 10, 14 } 构建成一颗二叉树. n+1=7
- 上面的二叉树进行中序遍历时,数列为 {8, 3, 10, 1, 14,6 }
- 6, 8, 10, 14 这几个节点的 左右指针,并没有完全的利用上
线索二叉树的介绍
-
n个结点的二叉链表中含有n+1 【公式 2n-(n-1)=n+1】 个空指针域。利用二叉链表中的空指针域,存放指向该结点在某种遍历次序下的前驱和后继结点的指针(附加的指针则为线索)
-
线索二叉树 (Threaded BinaryTree):加上了线索的二叉链表称为线索链表。
- 根据线索性质的不同,线索二叉树可分为
- 前序线索二叉树
- 中序线索二叉树
- 后序线索二叉树
-
一个结点的前一个结点,称为前驱结点
- 第一个元素没有前驱结点
-
一个结点的后一个结点,称为后继结点
- 最后一个元素没有后继结点
中序遍历结果:{8, 3, 10, 1, 14, 6}
当线索化二叉树后,Node节点的 属性 left 和 right
- left 指向的是左子树,也可能是指向的前驱节点. 比如 ① 节点 left 指向的左子树, 而 ⑩ 节点的 left 指向的就是前驱节点
- right指向的是右子树,也可能是指向后继节点,比如 ① 节点right 指向的是右子树,而⑩ 节点的 right 指向的是后继节点
遍历线索化二叉树
分析
因为线索化后,各个结点指向有变化,因此原来的遍历方式不能使用, 这时需要使用新的方式遍历线索化二叉树,各个节点可以通过线型方式遍历, 因此无需使用递归方式,这样也提高了遍历的效率。 遍历的次序应当和中序遍历保持一致
package com.tree.threadbinarytree;
/**
* @author Kcs 2022/9/11
*/
public class ThreadBinaryTreeDemo {
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, "kong");
HeroNode node6 = new HeroNode(14, "十二");
//手动创建二叉树
root.setLeft(node2);
root.setRight(node3);
node2.setLeft(node4);
node2.setRight(node5);
node3.setLeft(node6);
ThreadBinaryTree threadBinaryTree = new ThreadBinaryTree();
threadBinaryTree.setRoot(root);
threadBinaryTree.infixThreadedNodes();
System.out.println("====================中序线索化======================");
//中序线索化测试
//前驱结点
System.out.println("中序线索化~~~");
HeroNode infixLeftNode = node5.getLeft();
System.out.println("10号的前驱结点为:" + infixLeftNode);
//后继结点
HeroNode infixRightNode = node5.getRight();
System.out.println("10号的后继结点为:" + infixRightNode);
//线索化二叉树遍历
System.out.println("中序线索化二叉树遍历~~~~");
threadBinaryTree.infixThreadedList();
}
}
/**
* 线索化二叉树
*/
class ThreadBinaryTree {
private HeroNode root;
/**
* 线索化,当前结点的指针, 保留前一个结点
*/
private HeroNode pre = null;
public void setRoot(HeroNode root) {
this.root = root;
}
/**
* 重载中序线索化方法
*/
public void infixThreadedNodes() {
this.infixThreadedNodes(root);
}
/**
* 中序线索化二叉树
* @param node 当前线索化结点
*/
public void infixThreadedNodes(HeroNode node) {
//空结点,则不能线索化
if (node == null) {
return;
}
//1.左子树线索化
infixThreadedNodes(node.getLeft());
//2.线索化当前结点
//当前结点的前驱结点
if (node.getLeft() == null) {
//左指针 指向前驱结点
node.setLeft(pre);
//左指针类型重置
node.setLeftType(1);
}
//当前结点的后继结点
if (pre != null && pre.getRight() == null) {
//前驱结点的右指针指向当前接结点
pre.setRight(node);
//重置右指针类型
pre.setRightType(1);
}
//下一个结点的前驱结点
pre = node;
//3.线索化右子树
infixThreadedNodes(node.getRight());
}
/**
* 中序遍历线索化二叉树
*/
public void infixThreadedList() {
//当前遍历结点,root开始
HeroNode node = root;
while (node != null) {
//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();
}
}
}
/**
* 创建 HeroNode节点 实体类
*/
class HeroNode {
/**
* 序号
*/
private int no;
/**
* 姓名
*/
private String name;
/**
* 左节点,默认null
*/
private HeroNode left;
/**
* 右节点,默认null
*/
private HeroNode right;
/**
* 为0:指向左子树;为1指向前驱结点
*/
private int leftType;
/**
* 为0:指向右子树;为1指向后继结点
*/
private int rightType;
/**
* 构造器
* @param no 编号
* @param name 姓名
*/
public HeroNode(int no, String name) {
this.no = no;
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
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 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;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + ''' +
'}';
}
}