28_线索二叉树

28_线索二叉树

一、代码实现

1.1 前期准备

public class ThreadedBinaryTreeDemo {
    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);
    }
}

//创建HeroNode
class HeroNode{
    private int no;
    private String name;
    private HeroNode left;  //默认null
    private HeroNode right; //默认null

    //说明:
    //1.如果leftType == 0表示指向左子树,== 1表示指向前驱结点
    //2.如果rightType == 0表示指向右子树,== 1表示指向后继结点
    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;
    }

    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 + '\'' +
                '}';
    }
}

1.2 线索化二叉树

package no9_tree.threadedbinarytree;

public class ThreadedBinaryTreeDemo {
    public static void main(String[] args) {
        //测试中序线索化:以10号节点测试
        ThreadedBinaryTree threadedBinaryTree = new ThreadedBinaryTree();
        threadedBinaryTree.setRoot(root);
        threadedBinaryTree.threadedNodes();

        HeroNode left = node5.getLeft();
        System.out.println("10号结点的前驱结点是 = " + left);
        HeroNode right = node5.getRight();
        System.out.println("10号结点的后继结点是 = " + right);
    }
}

//定义ThreadedBinaryTree 实现线索化功能的二叉树
class ThreadedBinaryTree{
    private HeroNode root;

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

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    //对threadedNodes方法进行重载
    public void threadedNodes(){
        this.threadedNodes(root);
    }

    /**
     * 编写对二叉树进行中序线索化的方法
     * @param node  当前需要线索化的结点
     */
    public void threadedNodes(HeroNode node){
        //如果node == null,不能线索化
        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());
    }
}

------------------------------
"D:\IntelliJ IDEA 2019.3\jbr\bin\java.exe" "-javaagent:D:\IntelliJ IDEA 2019.3\lib\idea_rt.jar=5814:D:\IntelliJ IDEA 2019.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\Administrator\Desktop\StudyAlgorithm\out\production\StudyAlgorithm;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\junit\arquillian-junit-container\1.7.0.Alpha9\arquillian-junit-container-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\junit\arquillian-junit-core\1.7.0.Alpha9\arquillian-junit-core-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\test\arquillian-test-api\1.7.0.Alpha9\arquillian-test-api-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\core\arquillian-core-api\1.7.0.Alpha9\arquillian-core-api-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\test\arquillian-test-spi\1.7.0.Alpha9\arquillian-test-spi-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\core\arquillian-core-spi\1.7.0.Alpha9\arquillian-core-spi-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\container\arquillian-container-test-api\1.7.0.Alpha9\arquillian-container-test-api-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\shrinkwrap\shrinkwrap-api\1.2.6\shrinkwrap-api-1.2.6.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\shrinkwrap\descriptors\shrinkwrap-descriptors-api-base\2.0.0\shrinkwrap-descriptors-api-base-2.0.0.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\container\arquillian-container-test-spi\1.7.0.Alpha9\arquillian-container-test-spi-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\container\arquillian-container-spi\1.7.0.Alpha9\arquillian-container-spi-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\core\arquillian-core-impl-base\1.7.0.Alpha9\arquillian-core-impl-base-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\test\arquillian-test-impl-base\1.7.0.Alpha9\arquillian-test-impl-base-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\container\arquillian-container-impl-base\1.7.0.Alpha9\arquillian-container-impl-base-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\config\arquillian-config-api\1.7.0.Alpha9\arquillian-config-api-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\config\arquillian-config-impl-base\1.7.0.Alpha9\arquillian-config-impl-base-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\config\arquillian-config-spi\1.7.0.Alpha9\arquillian-config-spi-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\shrinkwrap\descriptors\shrinkwrap-descriptors-spi\2.0.0\shrinkwrap-descriptors-spi-2.0.0.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\container\arquillian-container-test-impl-base\1.7.0.Alpha9\arquillian-container-test-impl-base-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\shrinkwrap\shrinkwrap-impl-base\1.2.6\shrinkwrap-impl-base-1.2.6.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\shrinkwrap\shrinkwrap-spi\1.2.6\shrinkwrap-spi-1.2.6.jar no9_tree.threadedbinarytree.ThreadedBinaryTreeDemo
10号结点的前驱结点是 = HeroNode{no=3, name='jack'}
10号结点的后继结点是 = HeroNode{no=1, name='tom'}    

1.3 遍历线索化二叉树

package no9_tree.threadedbinarytree;

public class ThreadedBinaryTreeDemo {
    public static void main(String[] args) {
        //遍历线索二叉树
        threadedBinaryTree.threadedList();
    }
}

//定义ThreadedBinaryTree 实现线索化功能的二叉树
class ThreadedBinaryTree{
    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 threadedList(){
        //定义变量存储当前遍历的结点,从root开始
        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();
        }
    }
}

-------------------------
"D:\IntelliJ IDEA 2019.3\jbr\bin\java.exe" "-javaagent:D:\IntelliJ IDEA 2019.3\lib\idea_rt.jar=5814:D:\IntelliJ IDEA 2019.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\Administrator\Desktop\StudyAlgorithm\out\production\StudyAlgorithm;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\junit\arquillian-junit-container\1.7.0.Alpha9\arquillian-junit-container-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\junit\arquillian-junit-core\1.7.0.Alpha9\arquillian-junit-core-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\test\arquillian-test-api\1.7.0.Alpha9\arquillian-test-api-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\core\arquillian-core-api\1.7.0.Alpha9\arquillian-core-api-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\test\arquillian-test-spi\1.7.0.Alpha9\arquillian-test-spi-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\core\arquillian-core-spi\1.7.0.Alpha9\arquillian-core-spi-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\container\arquillian-container-test-api\1.7.0.Alpha9\arquillian-container-test-api-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\shrinkwrap\shrinkwrap-api\1.2.6\shrinkwrap-api-1.2.6.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\shrinkwrap\descriptors\shrinkwrap-descriptors-api-base\2.0.0\shrinkwrap-descriptors-api-base-2.0.0.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\container\arquillian-container-test-spi\1.7.0.Alpha9\arquillian-container-test-spi-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\container\arquillian-container-spi\1.7.0.Alpha9\arquillian-container-spi-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\core\arquillian-core-impl-base\1.7.0.Alpha9\arquillian-core-impl-base-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\test\arquillian-test-impl-base\1.7.0.Alpha9\arquillian-test-impl-base-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\container\arquillian-container-impl-base\1.7.0.Alpha9\arquillian-container-impl-base-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\config\arquillian-config-api\1.7.0.Alpha9\arquillian-config-api-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\config\arquillian-config-impl-base\1.7.0.Alpha9\arquillian-config-impl-base-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\config\arquillian-config-spi\1.7.0.Alpha9\arquillian-config-spi-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\shrinkwrap\descriptors\shrinkwrap-descriptors-spi\2.0.0\shrinkwrap-descriptors-spi-2.0.0.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\arquillian\container\arquillian-container-test-impl-base\1.7.0.Alpha9\arquillian-container-test-impl-base-1.7.0.Alpha9.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\shrinkwrap\shrinkwrap-impl-base\1.2.6\shrinkwrap-impl-base-1.2.6.jar;D:\self-optimal\maven\apache-maven-3.5.3\repository\org\jboss\shrinkwrap\shrinkwrap-spi\1.2.6\shrinkwrap-spi-1.2.6.jar no9_tree.threadedbinarytree.ThreadedBinaryTreeDemo
HeroNode{no=8, name='mary'}
HeroNode{no=3, name='jack'}
HeroNode{no=10, name='king'}
HeroNode{no=1, name='tom'}
HeroNode{no=14, name='dim'}
HeroNode{no=6, name='smith'}    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值