递归实现二叉树的前 中 后序遍历和查找(JAVA实现)

这里先说一下一些二叉树的性质(和前中后序遍历无关,只是记录一下)

1.二叉树第i层上的结点数最多为2^i(层次:规定树中的根节点的层次为0,其他节点的层次是其双亲节点的层次数+1)

2.深度为h(h>=1)的二叉树中最多有(2^h)-1个节点(深度:树的深度是指树中所有结点的层次数的最大值+1)

3.对于任何一颗二叉树,若叶结点的个数为n0,度为2的结点个数为n2,则有n0 = n2+1;

 

下面是一棵二叉树,要求前中后序遍历。

1.前序遍历: 先输出父节点,再遍历左子树和右子树

2.中序遍历: 先遍历左子树,再输出父节点,再遍历右子树

3.后序遍历: 先遍历左子树,再遍历右子树,最后输出父节点

由图可知,前序遍历为: 1 2 3 5 4

                  中序遍历为: 2 1 5 3 4

                  后序遍历为: 2 5 4 3 1

实现由 二叉树的前中后序的遍历,查找,和删除节点,这里的删除是,当删除的是非叶子节点时,则把整棵树都删除掉。

代码

二叉树对象,及操作

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


    public HeroNode(int no,String name){
        this.no = no;
        this.name = name;
    }


    /**
     * 删除节点.若为叶子结点,则直接删除
     * 如果非叶子结点,则删除整棵树
     * @param no 要删除的no
     */
    public void delHeroNode(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.delHeroNode(no);
        }

        if(this.right != null){
            this.right.delHeroNode(no);
        }

    }

    /**
     * 前序遍历查找
     * @param no 要查找的元素
     * @return
     */
    public HeroNode preOrderSearch(int no){
        if(this.no == no){
            return this;
        }
        HeroNode res = null;
        if(this.left != null){
            res = this.left.preOrderSearch(no);
        }
        if(res != null){
            return res;
        }

        if(this.right != null){
            res = this.right.preOrderSearch(no);
        }
        return res;
    }

    /**
     * 中序遍历查找
     * @param no 查找的元素
     * @return
     */
    public HeroNode midOrderSearch(int no){
        HeroNode res = null;
        if(this.left != null){
            res = this.left.midOrderSearch(no);
        }

        if(res != null){
            return res;
        }

        if(this.no == no){
            return this;
        }
        if(this.right != null){
            res = this.right.midOrderSearch(no);
        }
        return res;
    }

    /**
     * 后序遍历查找
     * @param no 要查找的元素
     * @return
     */
    public HeroNode postOrderSearch(int no){

        HeroNode res = null;
        if(this.left != null){
            res = this.left.postOrderSearch(no);
        }
        if(res != null){
            return res;
        }
        if(this.right != null){
            res = this.right.postOrderSearch(no);
        }
        if(res != null){
            return res;
        }
        if(this.no == no){
            return this;
        }
        return res;
    }

    /**
     * 前序遍历
     */
    public void preOrder(){
        //输出当前节点
        System.out.println(this);
        if(this.left != null){
            this.left.preOrder();
        }
        if(this.right != null){
            this.right.preOrder();
        }
    }
    /**
     * 中序遍历
     */
    public void midOrder(){
        if(this.left != null){
            this.left.midOrder();
        }
        System.out.println(this);
        if(this.right != null){
            this.right.midOrder();
        }
    }
     /**
     * 后序遍历
     */
    public void postOrder(){
        if(this.left != null){
            this.left.postOrder();
        }
        if(this.right != null){
            this.right.postOrder();
        }
        System.out.println(this);
    }


    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", 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;
    }
}

二叉树操作

class BTree{
    private HeroNode root;
    public void set(HeroNode root){
        this.root = root;
    }
    /**
     * 删除节点
     * @param no
     */
    public void delHeroNode(int no){
        if(root != null){
            if(root.getNo() == no){
                root = null;
                return;
            }
            root.delHeroNode(no);
        }else{
            System.out.println("树为空...");
            return;
        }
    }

    /**
     * 前序遍历查找
     * @param no
     * @return
     */
    public HeroNode preOrderSearch(int no){
        HeroNode res = null;
        if(root != null){
            res = root.preOrderSearch(no);
        }
        return res;
    }

    /**
     * 中序遍历查找
     * @param no
     * @return
     */
    public HeroNode midOrderSearch(int no){
        HeroNode res = null;
        if(root != null){
            res = root.midOrderSearch(no);
        }
        return res;
    }

    /**
     * 后序遍历查找
     * @param no
     * @return
     */
    public HeroNode postOrderSearch(int no){
        HeroNode res = null;
        if(root != null){
            res = root.postOrderSearch(no);
        }
        return res;
    }

    /**
     * 前序遍历二叉树
     */
    public void preOrder(){
        if(root != null){
            root.preOrder();
        }else{
            System.out.println("二叉树为空");
        }
    }

    /**
     * 中序遍历二叉树
     */
    public void midOrder(){
        if(root != null){
            root.midOrder();
        }else {
            System.out.println("二叉树为空");
        }
    }

    /**
     * 后序遍历二叉树
     */
    public void postOrder(){
        if(root != null){
            root.postOrder();
        }else{
            System.out.println("二叉树为空");
        }
    }
}

 

测试

/**
 * @author AN
 * @create 2020-09-11 20:35
 */
public class BitTree {

    public static void main(String[] args) {

        BTree bTree = new BTree();

        HeroNode root = new HeroNode(1,"AA");
        HeroNode node2 = new HeroNode(2,"BB");
        HeroNode node3 = new HeroNode(3,"CC");
        HeroNode node4 = new HeroNode(4,"DD");
        HeroNode node5 = new HeroNode(5,"EE");

        bTree.set(root);
        root.setLeft(node2);
        root.setRight(node3);
        node3.setRight(node4);
        node3.setLeft(node5);

        System.out.println("前序遍历为:");
        bTree.preOrder();
        System.out.println("中序遍历为:");
        bTree.midOrder();
        System.out.println("后序遍历为:");
        bTree.postOrder();

        System.out.println("=======================================");

        System.out.println("前序遍历查找.............");
        HeroNode heroNode = bTree.preOrderSearch(4);
        if(heroNode != null){
            System.out.println("找到了:"+heroNode);
        }else{
            System.out.println("没找到该节点");
        }

        System.out.println("中序遍历查找.............");
        HeroNode heroNode1 = bTree.preOrderSearch(4);
        if(heroNode1 != null){
            System.out.println("找到了:"+heroNode1);
        }else{
            System.out.println("没找到该节点");
        }

        System.out.println("后序遍历查找.............");
        HeroNode heroNode2 = bTree.preOrderSearch(4);
        if(heroNode2 != null){
            System.out.println("找到了:"+heroNode2);
        }else{
            System.out.println("没找到该节点");
        }

        System.out.println("===================删除功能===============================");
        System.out.println("删除前:");
        bTree.preOrder();
        bTree.delHeroNode(2);
        System.out.println("删除后:");
        bTree.preOrder();


    }

}

测试结果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值