二叉树遍历与查找简单删除

二叉树遍历与查找、简单删除

一、遍历

使用前序,中序和后序对下面的二叉树进行遍历.

  1. 前序遍历: 先输出父节点,再遍历左子树和右子树
  2. 中序遍历: 先遍历左子树,再输出父节点,再遍历右子树
  3. 后序遍历: 先遍历左子树,再遍历右子树,最后输出父节点
  4. 小结: 看输出父节点的顺序,就确定是前序,中序还是后序
    在这里插入图片描述

二、查找

在这里插入图片描述

三、简单删除

  1. 如果删除的节点是叶子节点,则删除该节点
  2. 如果删除的节点是非叶子节点,则删除该子树.
    在这里插入图片描述
//二叉树
class BinaryTree {
    //根结点
    private HeroNode root;

    //设置根节点
    public void setRoot(HeroNode root) {
        this.root = root;
    }
    //前序遍历
    public void preOrder() {
        //如果不为空,进行前序遍历
        if (this.root != null) {
            this.root.preOrder();
        } else {
            System.out.println("二叉树为空,无法遍历");
        }
    }
   //中序遍历
    public void infixOrder() {
        //如果不为空,进行中序遍历
        if (this.root != null) {
            this.root.infixOrder();
        } else {
            System.out.println("二叉树为空,无法遍历");
        }
    }

    //后序排序
    public void postOrder() {
        //如果不为空,进行后序遍历
        if (this.root != null) {
            this.root.postOrder();
        }else
        {
            System.out.println("二叉树为空,无法遍历");
        }
    }
    //前序遍历
    public HeroNode preOrder(int no) {
        //如果不为空,进行前序遍历
        if (this.root != null) {
           return this.root.preOrder(no);
        } else {
            return null;
        }
    }
    //中序遍历
    public HeroNode infixOrder(int no) {
        //如果不为空,进行中序遍历
        if (this.root != null) {
            return this.root.infixOrderSearch(no);
        } else {
            return null;
        }
    }

    //后序排序
    public HeroNode postOrder(int no) {
        //如果不为空,进行后序遍历
        if (this.root != null) {
            return  this.root.postOrderSearch(no);
        }else
        {
           return null;
        }
    }
    //删除结点
    public void delNode(int no)
    {
        //如果根结点不为空
        if (root!=null)
        {
            //如果根节点等于
            if (root.getNo()==no)
            {
                root=null; //删除
            }else{
                root.delNode(no); //递归删除
            }
        }
        else
        {
            System.out.println("空树,不能删除");
        }
    }
}
//节点类
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;
    }

    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;
    }

    @Override
    public String toString() {
        return "Heronode{" +
                "no=" + no +
                ", name='" + name +
                '}';
    }

    //前序遍历
    public void preOrder()
    {
        //输出当前结点
        System.out.println(this);
        //如果左结点不为空
        if (this.left!=null)
        {
            //向左递归前序遍历
            this.left.preOrder();
        }
        //如果右结点不为空
        if (this.right!=null)
        {
            //向右递归前序遍历
            this.right.preOrder();
        }
    }

    //中序遍历
    public void infixOrder()
    {
        //如果左节点不为空,递归进行中序遍历
        if (this.left!=null)
        {
            this.left.infixOrder();
        }
        //输出当前结点
        System.out.println(this);
        //如果右节点不为空,递归进行中序遍历
        if (this.right!=null)
        {
            this.right.infixOrder();
        }
    }

    //后序遍历
    public  void postOrder()
    {
        如果左节点不为空,递归进行后序遍历
        if (this.left!=null)
        {
            this.left.postOrder();
        }
        //如果右节点不为空,递归进行后序遍历
        if (this.right!=null)
        {
            this.right.postOrder();
        }
        //输出当前结点
        System.out.println(this);
    }
    //前序查找
    public HeroNode preOrder(int no)
    {
        //比较当前结点
        if(this.no==no)
        {
            return this;
        }
        HeroNode resNode=null;
        //如果左节点不为空,递归左查找
        if (this.left!=null)
        {
            resNode=this.left.preOrder(no);
        }
        //说明左子树找到
        if (resNode!=null)
        {
            return resNode;
        }
        //如果右结点不为空
      if (this.right!=null)
        {
            resNode=this.right.preOrder(no);
        }

        return resNode;
    }

    //中序遍历查找
    public HeroNode infixOrderSearch(int no)
    {
        HeroNode resNode=null;
        //如果左节点奴为空,递归左查找
        if (this.left!=null)
        {
            resNode=this.left.infixOrderSearch(no);
        }
        //说明左子树查找到
        if (resNode!=null)
        {
            return resNode;
        }
        System.out.println("进入中序遍历");
        //比较当前结点
        if (this.no==no)
        {
            return this;
        }
        //如果右结点不为空,递归查找右子树
        if (this.right!=null)
        {
            resNode=this.right.infixOrderSearch(no);
        }

        return resNode;
    }

    //后序查找
    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;
        }

        System.out.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);
        }
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值