中序线索化二叉树并对其进行遍历

package Tree.threaded;

import java.util.ArrayList;

public class ThreadedBinaryTree {
    public static void main(String[] args) {
        Hero root = new Hero(1, "tom");
        Hero node2 = new Hero(3, "jack");
        Hero node3 = new Hero(6, "smith");
        Hero node4 = new Hero(8, "mary");
        Hero node5 = new Hero(10, "king");
        Hero node6 = new Hero(14, "dim");

        root.left = node2;
        root.right = node3;
        node2.left = node4;
        node2.right = node5;
        node3.left = node6;

//        测试中序线索化
        binary binary = new binary(root);
        binary.threadedNodes(root);

//        测试
//        Hero leftnode = node5.left;
//        System.out.println(leftnode);
        binary.threadedList();
    }
}

class Hero {
    int id;
    String name;
    Hero left;
    Hero right;
    static ArrayList<Hero> heroes = new ArrayList<>();

    //    说明
//    1. 如果leftType == 0 表示指向的是左子树 如果1则表示指向前驱节点
//    2. 如果rightType == 0 表示指向的是右子树 如果1则表示指向后继节点
    int leftType;
    int rightType;

    public Hero(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Hero{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    //    前序遍历
    public void preorderTraversal() {
        System.out.println(this);
        if (this.left != null) {
            this.left.preorderTraversal();
        }
        if (this.right != null) {
            this.right.preorderTraversal();
        }
    }

    //中序遍历
    public void inorderTraversal(){
        if(left!=null){
            this.left.inorderTraversal();
        }
        System.out.println(this);
        if(right!=null){
            this.right.inorderTraversal();
        }
    }

    //后序遍历
    public ArrayList<Hero> postorderTraversal(){

        if(this.left!=null){
            this.left.postorderTraversal();
        }
        if(this.right!=null){
            this.right.postorderTraversal();
        }
//        System.out.println(this);
        heroes.add(this);
//        System.out.println(heroes);
        return heroes;
    }

    //    中序查找
    public Hero inorderSearch(int num) {
        Hero res = null;
//        先左子树
        if (this.left != null) {
            res = this.left.inorderSearch(num);
        }
        if (res != null) {
            return res;
        }
//      再父节点
        if (this.id == num) {
            return this;
        }
//      再 右子树
        if (this.right != null) {
            res = this.right.inorderSearch(num);
        }
        if (res != null) {
            return res;
        }
        return null;
    }

    //    删除
    public void del(int num) {
        if (this.left != null && this.left.id == num && this.left.left == null) {
            this.left = null;
            return;
        }
        if (this.right != null && this.right.id == num && this.right.right == null) {
            this.right = null;
            return;
        }
        if (this.left != null && this.left.id == num && this.left.left != null) {
            this.left.left = null;
            return;
        }
        if (this.right != null && this.right.id == num && this.right.right != null) {
            this.right.right = null;
            return;
        }
        if (this.left != null && this.left.id != num) {
            this.left.del(num);
        }
        if (this.right != null && this.right.id != num) {
            this.right.del(num);
        }
    }
}

class binary {
    Hero root;

    //    为了实现线索化 需要创建要给指向当前节点的前驱节点的指针
//    在递归进行线索化时, pre总是保留前一个节点
    Hero pre = null;

    public binary(Hero root) {
        this.root = root;
    }

    //    遍历线索化二叉树的方法
    public void threadedList() {
        Hero node = root;
        while (node != null) {
            while (node.leftType == 0) {
                node = node.left;
            }
            System.out.println(node);
            while (node.rightType == 1) {
                node = node.right;
                System.out.println(node);
            }
            node = node.right;
        }

    }


//    中序线索化二叉树
    public void threadedNodes(Hero node) {
//        如果node==null 不能线索化
        if (node == null) {
            return;
        }
//        (一)先线索化左子树
        threadedNodes(node.left);
//        (二)线索化当期节点

//        处理当前节点的前驱节点
        if (node.left == null) {
//            让当前节点的左指针指向前驱节点
            node.left = pre;
//            修改当前节点的左指针的类型, 指向前驱节点
            node.leftType = 1;
        }
//        处理后继节点
        if (pre != null && pre.right == null) {

            pre.right = node;
            pre.rightType = 1;
        }
//        !!!尤其重要
//        每处理一个节点后, 让当前节点时下一个节点的前驱节点
        pre = node;

//        (三) 在线索化右子树
        threadedNodes(node.right);
    }

    public void preorderTraversal() {
        root.preorderTraversal();
    }

    public void inorderTraversal() {
        root.inorderTraversal();
    }

    public ArrayList<Hero> postorderTraversal() {
        return root.postorderTraversal();
    }

    public void inorderSearch(int num) {

        System.out.println(root.inorderSearch(num));
    }

    public void del(int num) {
        if (root.id == num) {
            root = null;
            System.out.println("根节点已删除");
            return;
        }
        root.del(num);
    }
}

以上是对二叉树进行中序线索化

下面是遍历线索化二叉树

//    遍历线索化二叉树的方法
    public void threadedList() {
        Hero node = root;
        while (node != null) {
            while (node.leftType == 0) {
                node = node.left;
            }
            System.out.println(node);
            while (node.rightType == 1) {
                node = node.right;
                System.out.println(node);
            }
            node = node.right;
        }

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值