!!(這裏我debug很久才理解過來)** 這裏8的前驅爲null,所以8的leftType=1,但是6是沒有後繼的或者說後繼爲null但是rightType爲0(因爲後繼是在下一個節點來進行連接的,6沒有下一個節點,所以不能實現後繼的線索化,所以rightType=0)**
public void threadedNodes(HeroNode node){
//如果node == null,就不能線索化
if (node == null){
return;
}
//(1)先線索化左子樹
threadedNodes(node.getLeft());
//(2)線索化當前節點
//先處理當前節點的前驅節點
//以8節點來理解
//8節點的left = null,8節點的leftType = 1
if (node.getLeft() == null){
//讓當前節點的左指針指向前驅節點
node.setLeft(pre);
//修改當前節點的做指針的類型
node.setLeftType(1);
}
//處理後繼節點,處理8的後繼節點,讓pre的right指向node
if (pre != null && pre.getRight() == null){
//讓前驅節點的右指針指向當前節點
pre.setRight(node);
//修改前驅節點的右指針類型
pre.setLeftType(1);
}
//!!!!每處理一個節點,讓當前節點是下一個節點的前驅結點
pre = node;
//(3)線索化右子樹
threadedNodes(node.getRight());
}
//遍歷線索化二叉樹的方法
public void threadedList(){
//定義一個遍歷存儲當前遍歷的節點,從root開始
HeroNode node = root;
while (node != null){
//循環找到lefeYype == 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();
}
}
完整代碼 (後序遍歷線索二叉樹沒寫出來,網上找了一些資料看完感覺還是有一點難度,暫時先不深入了)
package tree.threadedbinarytree;
public class ThreadedBrinaryTreeDemo {
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, "dmi");
//二叉樹,我們再遞歸創建,目前簡單手動創建
root.setLeft(node2);
root.setRight(node3);
node2.setLeft(node4);
node2.setRight(node5);
node3.setLeft(node6);
//測試線索化
ThreadedBrinaryTree tBrinaryTree = new ThreadedBrinaryTree();
tBrinaryTree.setRoot(root);
tBrinaryTree.threadedNodes();
//測試以10號節點做測試
// HeroNode left = node3.getLeft();
// System.out.println("10號節點的前驅節點是:"+ left);
// HeroNode right = node3.getRight();
// System.out.println("10號節點的後繼節點是:"+ right);
// System.out.println("使用線索化的方法中序遍歷線索化 二叉樹");
// tBrinaryTree.threadedInfixOrder();
// System.out.println("使用線索化的方法前序遍歷線索化 二叉樹");
// tBrinaryTree.threadedPreOrder();
}
}
//定義ThreadedBinaryTree
class ThreadedBrinaryTree{
private HeroNode root;
//爲了實現線索化,需要創建一個指向當前節點的前驅節點的引用
//在遞歸線索化時,pre總是保留前一個節點
private HeroNode pre = null;
public void setRoot(HeroNode root){
this.root = root;
}
//重載threadedNodes
public void threadedNodes(){
this.threadedNodes(root);
}
//遍歷線索化二叉樹的前序遍歷方法
public void threadedPreOrder(){
HeroNode node = root;
while (node != null){
//打印當前節點
System.out.println(node);
//向左循環有前驅
while(node.getLeftType() == 0){
node = node.getLeft();
System.out.println(node);
}
while (node.getRightType() == 1){
node = node.getRight();
}
node = node.getRight();
}
}
//遍歷線索化二叉樹的中序遍歷方法
public void threadedInfixOrder(){
//定義一個遍歷存儲當前遍歷的節點,從root開始
HeroNode node = root;
while (node != null){
//循環找到lefeYype == 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 threadedNodes(HeroNode node){
//如果node == null,就不能線索化
if (node == null){
return;
}
//(1)先線索化左子樹
threadedNodes(node.getLeft());
//(2)線索化當前節點
//先處理當前節點的前驅節點
//以8節點來理解
//8節點的left = null,8節點的leftType = 1
if (node.getLeft() == null){
//讓當前節點的左指針指向前驅節點
node.setLeft(pre);
//修改當前節點的做指針的類型
node.setLeftType(1);
}
//處理後繼節點,處理8的後繼節點,讓pre的right指向node
if (pre != null && pre.getRight() == null){
//讓前驅節點的右指針指向當前節點
pre.setRight(node);
//修改前驅節點的右指針類型
pre.setRightType(1);
}
//!!!!每處理一個節點,讓當前節點是下一個節點的前驅結點
pre = node;
//(3)線索化右子樹
threadedNodes(node.getRight());
}
//前序遍歷
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 preOrederSearch(int no){
if (root != null){
return root.preOrderSearch(no);
}else {
return null;
}
}
//中序查找
public HeroNode infixOrderSeach(int no){
if (root != null){
return root.infixOrderSearch(no);
}else {
return null;
}
}
//後序查找
public HeroNode postOrderSeach(int no){
if (root != null){
return root.postOrderSearch(no);
}else {
return null;
}
}
//刪除節點
public void delNode(int no){
if (root != null){//判斷root是不是要刪除的節點
if (root.getNo() == no){
root = null;
}else {
root.delNode(no);
}
}
}
}
//創建節點
class HeroNode{
private int no;
private String name;
private HeroNode left;//默認null
private HeroNode right;//默認null;
//說明
//1.如果leftType == 0 表示指向的是左子樹,如果是1表示指向前驅節點
//1.如果rightType == 0 表示指向的是右子樹,如果是1表示指向後繼節點
private int leftType;
private int rightType;
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 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 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 static int i = 1, j = 1, k =1;
//編寫前序查找方法
public HeroNode preOrderSearch(int no){
System.out.println("前序遍歷"+(i++)+"次");
if (this.no == no){
return this;
}
HeroNode heroNode = null;
if (this.left != null){
heroNode = this.left.preOrderSearch(no);
}
//不等於空說明在左邊找到了
if (heroNode != null){
return heroNode;
}
if (this.right != null){
heroNode = this.right.preOrderSearch(no);
}
return heroNode;
}
//中序遍歷查找
public HeroNode infixOrderSearch(int no){
HeroNode heroNode = null;
//先判斷當前節點的左子節點是否爲空,不爲空繼續進行中序查找
if (this.left != null){
heroNode = this.left.infixOrderSearch(no);
}
if (heroNode != null){
return heroNode;
}
System.out.println("中序遍歷"+(j++)+"次");
if (this.no == no){
return this;
}
if (this.right != null){
heroNode = this.right.infixOrderSearch(no);
}
return heroNode;
}
//後序遍歷查找
public HeroNode postOrderSearch(int no){
HeroNode heroNode = null;
//判斷當前節點的左子節點是否爲空,不爲空,則遞歸後序遍歷查找
if (this.left != null){
heroNode = this.left.postOrderSearch(no);
}
if (heroNode != null){
return heroNode;
}
//判斷當前節點的右子節點是否爲空,不爲空,則遞歸後序遍歷查找
if (this.right != null){
heroNode = this.right.postOrderSearch(no);
}
if (heroNode != null){
return heroNode;
}
System.out.println("後序遍歷"+(k++)+"次");
//左右子樹都沒有找到,比較當前節點是不是
if (this.no == no){
return this;
}
return heroNode;
}
//遞歸刪除節點
//規定:如果是葉子節點就刪除節點,如果非葉子節點就刪除子樹
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);
}
}
}