线索化二叉树前序、中序、后序遍历

文章目录

节点信息

public class HeroNode {
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;
    private HeroNode child;

    //说明
    //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 String getName() {
        return name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
     //前序遍历
    public void preOrder(){
        println(this);
        //递归左子树
        if (this.left != null){
             this.left.preOrder();
        }
        //递归向右子树遍历
        if (this.right != null)
        {
            this.right.preOrder();
        }
    }

    //中序遍历
    public void middleOrder(){

        //递归左子树
        if (this.left != null){
            this.left.middleOrder();
        }
        println(this);
        //递归向右子树遍历
        if (this.right != null)
        {
            this.right.middleOrder();
        }
    }

    //后序遍历
    public void postOrder(){

        //递归左子树
        if (this.left != null){
            this.left.postOrder();
        }
        //递归向右子树遍历
        if (this.right != null)
        {
            this.right.postOrder();
        }
        println(this);
    }

    /**
     * 前序遍历查找
     *  no 查找序号
     * @param no
     * @return
     */
    public HeroNode preOrderSearch(int no){
        println("前序遍历");
        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;
    }


    /**
     *中序遍历查找
     *  no 查找序号
     * @param no
     * @return
     */
    public HeroNode middleOrderSearch(int no){
        HeroNode resNode = null;
        if (this.left != null){
            resNode = this.left.middleOrderSearch(no);
        }
        if (resNode != null){
            return resNode;
        }
        println("中序遍历");
        if (this.no == no){
            return this;
        }

        if (this.right != null){
            resNode = this.right.middleOrderSearch(no);
        }
        return resNode;
    }


    /**
     *后序遍历查找
     *  no 查找序号
     * @param no
     * @return
     */
    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;
        }
        println("后序遍历");
        //左右子树搜没有找到,比较当前节点
        if (this.no == no){
            return this;
        }
        return resNode;
    }

    //递归删除结点
    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 setLefttype(int lefttype) {
        this.lefttype = lefttype;
    }

    public void setRighttype(int righttype) {
        this.righttype = righttype;
    }

    public int getLefttype() {
        return lefttype;
    }

    public int getRighttype() {
        return righttype;
    }

    public HeroNode getChild() {
        return child;
    }

    public void setChild(HeroNode node) {
        this.child = node;
    }
}
二叉树的构建

在这里插入图片描述

前序线索化二叉树

在这里插入图片描述

前序遍历

0 1 2 3 4 5
前序线索化思路
1、 先找到线索化的起始点—当前节点的左节点为空 node.getLeft() == null
2、然后把当前节点右指针指向前驱结点
3、在处理后继结点,并把前驱结点的右指针设置为当前节点
4、最后把当前节点设置为前驱结点
5、线索化左子树
6、线索化右子树
前序线索化遍历思路
1、 先找到线索化的起始点,边找边输出当前节点
2、然后寻找当前结点的后继结点,并输出
 /**
     *二叉树进行前序线索化的方法
     * @param node 就是当前需要线索化的节点
     */
    public void prethreadedNodes(HeroNode node){
        if (node == null){
            return;
        }
        //一、线索化当前节点
        //先处理当前结点的前驱结点
        if (node.getLeft() == null)
        {
            //让当前结点的左指针指向前驱结点
            node.setLeft(pre);
            //修改当前结点的左指针类型,指向前驱结点
            node.setLefttype(1);
        }
        //处理后继结点
        if (pre!=null && pre.getRight() == null){
            //让前驱结点的右指针指向当前结点
            pre.setRight(node);
            //修改前驱结点的右指针类型
            pre.setRighttype(1);
        }

        // 每处理一个结点后,让当前结点是下一个结点的前驱结点
        pre  = node;
        //二、先线索化左子树
        if (node.getLefttype()==0)
            prethreadedNodes(node.getLeft());
        //三、再线索化右子树
        if (node.getRighttype()==0)
        prethreadedNodes(node.getRight());
    }

前序线索化二叉树遍历

  //前序遍历线索化二叉树
    public void prethreadedList(){
        //定义一个变量,存储当前遍历的结点,从root开始
        HeroNode node = root;
        while (node != null){

            //循环的找到leftType == 1的结点
            //后面随着遍历而变化,因为leftType==1,说明该结点是按照线索化处理后的有效结点
            while (node.getLefttype() == 0){
                //打印当前结点
                println(node);
                node = node.getLeft();
            }
            //如果当前结点的右指针指向的是后继结点,就一直输出
            while (node.getRighttype() == 1){
                //打印当前结点
                println(node);
                //获取到当前结点的后继结点
                node = node.getRight();
            }
            println(node);
            //替换这个遍历的结点
            node  = node.getRight();
        }
    }

中序线索化二叉树

在这里插入图片描述

中序遍历

1 3 2 0 4 5
中序线索化思路
1、先线索化左子树
2、找到线索化的起始点—当前节点的左节点为空 node.getLeft() == null
3、然后把当前节点右指针指向前驱结点
4、在处理后继结点,并把前驱结点的右指针设置为当前节点
5、最后把当前节点设置为前驱结点
6、线索化右子树
中序线索化遍历思路
1、 先找到线索化的起始点
2、输出当前节点信息
3、然后寻找当前结点的后继结点,并输出

    /**
     *二叉树进行中序线索化的方法
     * @param node 就是当前需要线索化的节点
     */
    public void middlethreadedNodes(HeroNode node){
        if (node == null){
            return;
        }
        //一、先线索化左子树
        middlethreadedNodes(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;
        //三、再线索化右子树
        middlethreadedNodes(node.getRight());
    }

中序线索化二叉树遍历

 //中序遍历线索化二叉树
    public void middlethreadedList(){
        //定义一个变量,存储当前遍历的结点,从root开始
        HeroNode node = root;
        while (node != null){
            //循环的找到leftType == 1的结点,第一个找到就是结点8
            //后面随着遍历而变化,因为leftType==1,说明该结点是按照线索化处理后的有效结点
            while (node.getLefttype() == 0){
                 node = node.getLeft();
            }
            //打印当前结点
            println(node);
            //如果当前结点的右指针指向的是后继结点,就一直输出
            while (node.getRighttype() == 1){
                //获取到当前结点的后继结点
                node = node.getRight();
                println(node);
            }
            //替换这个遍历的结点
            node  = node.getRight();
        }
    }

后序线索化二叉树

后序遍历

1 3 2 0 4 5
后序线索化思路
1、先线索化左子树
2、在线索化右子树
3、找到线索化的起始点—当前节点的左节点为空 node.getLeft() == null
4、然后把当前节点右指针指向前驱结点
5、在处理后继结点,并把前驱结点的右指针设置为当前节点,如果前驱结点的右节点不为空,那么就把前驱结点的孩子设置为当前节点(避免遍历时进入到死循环)
6、最后把当前节点设置为前驱结点
后序线索化遍历思路
1、 先找到线索化的起始点
2、然后寻找当前结点的后继结点,并输出
3、如果当前节点是根节点就输出信息并返回
4、如果当前结点的右节点与前驱结点相同,输出当前结点信息并迭代孩子结点
5、获取右结点
  /**
     *二叉树进行后序线索化的方法
     * @param node 就是当前需要线索化的节点
     */
    public void postthreadedNodes(HeroNode node){
        if (node == null){
            return;
        }
        //一、先线索化左子树
        postthreadedNodes(node.getLeft());

        //二、再线索化右子树
        postthreadedNodes(node.getRight());

        //三、线索化当前节点
        //先处理当前结点的前驱结点
        if (node.getLeft() == null)
        {
            //让当前结点的左指针指向前驱结点
            node.setLeft(pre);
            //修改当前结点的左指针类型,指向前驱结点
            node.setLefttype(1);
        }
        //处理后继结点
        if (pre!=null && pre.getRight() == null){
            //让前驱结点的右指针指向当前结点
            pre.setRight(node);
            //修改前驱结点的右指针类型
            pre.setRighttype(1);
        }else if (pre!=null && pre.getRight()!=null){
            pre.setChild(node);
        }
        // 每处理一个结点后,让当前结点是下一个结点的前驱结点
        pre  = node;
    }

后序线索化二叉树遍历

//后序遍历线索化二叉树
    public void postthreadedList(){
        //定义一个变量,存储当前遍历的结点,从root开始
        HeroNode node = root;
        HeroNode Prev = null;
        while (node != null){
            //循环的找到leftType == 1的结点
            //后面随着遍历而变化,因为leftType==1,说明该结点是按照线索化处理后的有效结点
            while (node.getLefttype() == 0){
                node = node.getLeft();
            }
            //如果当前结点的右指针指向的是后继结点,就一直输出
            while (node.getRighttype() == 1){
                println(node);
                Prev = node;
                //获取到当前结点的后继结点
                node = node.getRight();
            }
            if (node ==root){
                println(node);
                return;
            }
            while (node !=null && node.getRight()==Prev){
                println(node);
                Prev = node;
                //替换这个遍历的结点
                node  = node.getChild();
            }
            if(node!=null && node.getRighttype()==0) {
                node = node.getRight();
            }
        }
    }

测试

/**
 * 线索化二叉树
 */
public class ThreadedBinaryTreeDemo {
    public static void main(String[] args) {
        //中序遍历
        HeroNode root = new HeroNode(0, "tom");
        HeroNode node1 = new HeroNode(1, "jack");
        HeroNode node2 = new HeroNode(2, "smith");
        HeroNode node3 = new HeroNode(3, "mary");
        HeroNode node4 = new HeroNode(4, "king");
        HeroNode node5 = new HeroNode(5, "dim");
//        HeroNode node6 = new HeroNode(15, "tim");
        //二叉树创建
        root.setLeft(node1);
        root.setRight(node4);
//        node2.setLeft(node6);
        node1.setRight(node2);
        node2.setLeft(node3);
        node4.setRight(node5);
        //测试线索化
        ThreadBinaryTree threadBinaryTree = new ThreadBinaryTree();
        threadBinaryTree.setRoot(root);
        //后序遍历
        threadBinaryTree.postOrder();
        //线索化
//        threadBinaryTree.prethreadedNodes();
//        threadBinaryTree.middlethreadedNodes();
        threadBinaryTree.postthreadedNodes();


        //测试
        HeroNode nodeLeft = node3.getLeft();
        println("结点对的前驱结点: "+nodeLeft);
        HeroNode nodeRight = node3.getRight();
        println("结点对的后继结点: "+nodeRight);

//        println("使用线索化的方式先序遍历线索化二叉树");
//        threadBinaryTree.prethreadedList();
//
//        println("使用线索化的方式后序遍历线索化二叉树");
//        threadBinaryTree.middlethreadedList();

        println("使用线索化的方式后序遍历线索化二叉树");
        threadBinaryTree.postthreadedList();

    }
}

  • 7
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值