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

本文深入探讨了二叉树的线索化过程,包括前序、中序和后序线索化的方法及遍历策略,提供了详细的算法实现和示例代码,帮助读者理解如何通过线索化提高二叉树遍历效率。

文章目录

节点信息

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;
    
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值