[Java数据结构][10]Java二叉树

[Java数据结构][10]二叉树前、中、后序遍历(递归)的Java代码实现

二叉树定义

二叉树(binary tree)是指树中节点的度不大于2的有序树,它是一种最简单且最重要的树。
在这里插入图片描述
满二叉树在一棵二叉树中。如果所有分支结点都存在左子树和右子树,并且所有叶子都在同一层上,这样的二叉树称为满二叉树。

完全二叉树如果该二叉树的所有叶子节点都在最后一层或者倒数第二层,而且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续,我们称为完全二叉树
在这里插入图片描述

1.遍历

1.1前序遍历

先输出根节点,然后输出左节点,右节点,代码思路很简单

    /**
     * 前序遍历方法
     */
    public void preOrder() {
        System.out.println(this);//先输出父节点
        //向左子树递归
        if (this.left != null) {
            this.left.preOrder();
        }
        //递归向右子树递归
        if (this.right != null) {
            this.right.preOrder();
        }
    }
1.2中序遍历

先输出左节点、根节点然后输出右节点

    /**
     * 中序遍历
     */
    public void infixOrder() {
        //向左子树遍历
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);//输出父节点
        //向右子树遍历
        if (this.right != null) {
            this.right.infixOrder();
        }
    }
1.3后序遍历

先输出左节点,右节点然后输出根节点

    /**
     * 后续遍历
     */
    public void postOrder() {
        if (this.left != null) {
            this.left.postOrder();
        }
        if (this.right != null) {
            this.right.postOrder();
        }
        System.out.println(this);//输出父节点
    }

2.查找

2.1前序遍历查找

按照前序遍历的顺序和思路进行查找

/**
     * 前序遍历查找
     *
     * @param no 查找no
     * @return 返回Node,否则返回null
     */
    public HeroNode preOrderSearch(int no) {
        //比较当前节点
        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;//返回找到的或者为空
    }
2.2中序遍历查找

按照中序遍历的顺序和思路进行查找

/**
     * 中序遍历
     *
     * @param no 查找no
     * @return 返回Node,否则返回null
     */
    public HeroNode infixOrderSearch(int no) {
        HeroNode resNode = null;
        //左子节点
        if (this.left != null) {
            resNode = this.left.infixOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        //根节点
        if (this.no == no) {
            return this;
        }
        //向右查找
        if (this.right != null) {
            resNode = this.right.infixOrderSearch(no);
        }
        return resNode;
    }
2.3后序遍历查找

按照后序遍历的顺序和思路进行查找

    /**
     * 后序遍历
     *
     * @param no 查找no
     * @return 返回Node,否则返回null
     */
    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.infixOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        //根节点
        if (this.no == no) {
            return this;
        }
        return resNode;
    }

3.删除节点

这里采取简易删除的方式,如果删除的是叶子节点则直接删除,如果不是,则删除该子树。

    /**
     * 删除节点,叶子节点则删除
     * 非叶子节点则删除该子树
     *
     * @param no 节点信息
     */
    public void delNode(int no) {
        if (this.left.no == no && this.left != null) {
            this.left = null;
            return;
        }
        if (this.right.no == no && this.right != null) {
            this.right = null;
            return;
        }
        if (this.left != null) {
            this.left.delNode(no);
        }
        if (this.right != null) {
            this.right.delNode(no);
        }
    }

4.线索化二叉树

4.1 线索化二叉树

线索化的含义和作用就不在这里展开了,线索化之前需要给各个节点一个LeftType以及RightType用于区分线索化的左右节点还是结构意义的左右节点,给二叉树定义一个pre用于存放前一个节点。

/**
     *编写对二叉树进行中序线索化的方法
     * @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());
    }
4.2线索化遍历
    //遍历线索化二叉树的方法
    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();

        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值