資料結構--線索化二叉樹(Java)
博客說明
文章所涉及的資料來自互聯網整理和個人總結,意在于個人學習和經驗匯總,如有什么地方侵權,請聯系本人洗掉,謝謝!
說明
- n個結點的二叉鏈表中含有n+1 【公式 2n-(n-1)=n+1】 個空指標域,利用二叉鏈表中的空指標域,存放指向該結點在某種遍歷次序下的前驅和后繼結點的指標(這種附加的指標稱為"線索")
- 這種加上了線索的二叉鏈表稱為線索鏈表,相應的二叉樹稱為線索二叉樹(Threaded BinaryTree),根據線索性質的不同,線索二叉樹可分為前序線索二叉樹、中序線索二叉樹和后序線索二叉樹三種
- 一個結點的前一個結點,稱為前驅結點
- 一個結點的后一個結點,稱為后繼結點
代碼
package cn.guizimo.thread.tree;
/**
* @author guizimo
* @date 2020/8/7 11:08 上午
*/
public class ThreadTree {
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);
//線索化
ThreadedBinaryTree threadedBinaryTree = new ThreadedBinaryTree();
threadedBinaryTree.setRoot(root);
threadedBinaryTree.threadedNodes();
//遍歷
threadedBinaryTree.threadedList();
}
}
/**
* 二叉樹
*/
class ThreadedBinaryTree {
//根節點
private HeroNode root;
//輔助節點
private HeroNode pre = null;
public void setRoot(HeroNode root) {
this.root = root;
}
public void threadedNodes() {
this.threadedNodes(root);
}
//中序遍歷
public void threadedList() {
HeroNode node = root;
while (node != null) {
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 threadedNodes(HeroNode node) {
if(node == null) {
return;
}
threadedNodes(node.getLeft());
if(node.getLeft() == null) {
node.setLeft(pre);
node.setLeftType(1);
}
if (pre != null && pre.getRight() == null) {
pre.setRight(node);
pre.setRightType(1);
}
pre = node;
threadedNodes(node.getRight());
}
//洗掉二叉樹的節點
public void delNode(int no) {
if (root != null) {
if (root.getNo() == no) {
root = null;
} else {
root.delNode(no);
}
} else {
System.out.println("二叉樹為空");
}
}
//前序
public void preOrder() {
if (this.root != null) {
this.root.preOrder();
} else {
System.out.println("二叉樹為空");
}
}
//中序
public void infixOrder() {
if (this.root != null) {
this.root.infixOrder();
} else {
System.out.println("二叉樹為空");
}
}
//后序
public void postOrder() {
if (this.root != null) {
this.root.postOrder();
} else {
System.out.println("二叉樹為空");
}
}
//前序查找
public HeroNode preOrderSearch(int no) {
if (root != null) {
return root.preOrderSearch(no);
} else {
return null;
}
}
//中序查找
public HeroNode infixOrderSearch(int no) {
if (root != null) {
return root.infixOrderSearch(no);
} else {
return null;
}
}
//后序查找
public HeroNode postOrderSearch(int no) {
if (root != null) {
return this.root.postOrderSearch(no);
} else {
return null;
}
}
}
/**
* 節點
*/
class HeroNode {
private int no;
private String name;
private HeroNode left;
private HeroNode 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;
}
private int leftType;
private int rightType;
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;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
//洗掉節點
public void delNode(int no) {
//判讀左節點是否為空,找到
if (this.left != null && this.left.no == no) {
this.left = null;
return;
}
//判斷右節點,找到
if (this.right != null && this.right.no == no) {
this.right = null;
return;
}
//判斷左節點,未找到,遞回
if (this.left != null) {
this.left.delNode(no);
}
//判斷右節點,未找到,遞回
if (this.right != null) {
this.right.delNode(no);
}
}
//前序
public void preOrder() {
System.out.println(this);
if (this.left != null) {
this.left.preOrder();
}
if (this.right != null) {
this.right.preOrder();
}
}
//中序
public void infixOrder() {
if (this.left != null) {
this.left.infixOrder();
}
System.out.println(this);
if (this.right != null) {
this.right.infixOrder();
}
}
//后序
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 no) {
if (this.no == no) {
return this;
}
HeroNode resNode = null;
//判斷左子樹
if (this.left != null) {
resNode = this.left.preOrderSearch(no);
}
if (resNode != null) {
return resNode;
}
//判斷右子樹
if (this.right != null) {
resNode = this.right.preOrderSearch(no);
}
return resNode;
}
//中序遍歷查找
public HeroNode infixOrderSearch(int no) {
HeroNode resNode = null;
if (this.left != null) {
resNode = this.left.infixOrderSearch(no);
}
if (resNode != null) {
return resNode;
}
if (this.no == no) {
return this;
}
if (this.right != null) {
resNode = this.right.infixOrderSearch(no);
}
return resNode;
}
//后序遍歷查找
public HeroNode postOrderSearch(int no) {
HeroNode resNode = null;
if (this.left != null) {
resNode = this.left.postOrderSearch(no);
}
if (resNode != null) {
return resNode;
}
if (this.right != null) {
resNode = this.right.postOrderSearch(no);
}
if (resNode != null) {
return resNode;
}
if (this.no == no) {
return this;
}
return resNode;
}
}
感謝
尚硅谷
萬能的網路
以及勤勞的自己
關注公眾號: 歸子莫,獲取更多的資料,還有更長的學習計劃
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/98190.html
標籤:Java
上一篇:原始碼分析過濾器與攔截器的區別
下一篇:高可用高并發的 9 種技術架構!
