数据结构之二叉树的删除

二叉树的删除,要完成的操作(要求):
删除的是叶子结点,那就删除叶子结点
如果删除的是非叶子节点,将该节点置空

思路:
注意:如果是空树 root==null, 或者只有一个root节点,等价于把二叉树置空

1.二叉树是单向的,判断当前节点的子节点是不是要被删除,而不是判断要删除的节点。(类似于单链表的删除,要找到被删除的前一个)
2.如果当前节点(根节点)的左子节点不为空,并且左子节点就是要删除的,就将this.left=null,即可,并且返回结果(递归操作结束)
3.如果当前节点的右子节点不为空,并且右子节点就是要删除的,就将this.right=null,即可,并且返回结果(递归操作结束)
4.如果2.3没有完成,那么递归进行左子树删除
5.如果4没有完成,递归右子树的删除

代码实现:

package tree;

public class BinaryTree {

    public static void main(String[] args) {
        //创建二叉树(手动非递归)
        BinaryTreeClass bt = new BinaryTreeClass();
        HreoNode root = new HreoNode(1, "宋江");
        HreoNode h2 = new HreoNode(2, "卢俊义");
        HreoNode h3 = new HreoNode(3, "吴用");
        HreoNode h4 = new HreoNode(4, "公孙胜");
        HreoNode h5 = new HreoNode(5, "关胜");

        root.setLeft(h2);
        root.setRight(h3);
        h3.setRight(h4);
        h4.setLeft(h5);
        bt.setRoot(root);

        //测试
      /*  //前序
        System.out.println("前序");
        bt.preOrder();
        //中序
        System.out.println("中序");
        bt.midOrder();
        //后序
        System.out.println("后 序");
        bt.postOrder();*/

        //查找
 /*       System.out.println("前序");
        System.out.println(bt.preSerach(1));
        System.out.println("中序");
        System.out.println(bt.preSerach(3));
        System.out.println("后序");
        System.out.println(bt.preSerach(5));
*/

        //删除
        System.out.println("删除前的遍历");
        bt.preOrder();
//        bt.delNode(5);
        bt.delNode(3);
        System.out.println("删除后的遍历");
        bt.preOrder();
    }


}


//二叉树类
class BinaryTreeClass {
    private HreoNode root;

    //前序遍历------------------------------------------
    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("当前二叉树为空");
    }

    //前序查找 --------------------------------------------
    public HreoNode preSerach(int no) {
        if (root != null) return root.midSreach(no);
        else return null;
    }

    //中序查找
    public HreoNode midSreach(int no) {
        if (root != null) return root.midSreach(no);
        else return null;
    }

    //后序查找
    public HreoNode postSreach(int no) {
        if (root != null) return root.postSreach(no);
        else return null;
    }


    //删除节点
    public void delNode(int no){
       if(root!=null){
           if (root.getNo()!=no){
               root.delNode(no);
           }else {
               root=null;
           }

       }else System.out.println("空树");

    }

    public HreoNode getRoot() {
        return root;
    }

    public void setRoot(HreoNode root) {
        this.root = root;
    }
}


//节点类
class HreoNode {
    private int no;
    private String name;
    //默认null
    private HreoNode left;
    //默认null
    private HreoNode right;


    //遍历
    //先序
    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);
    }


    //查找
    //先序
    public HreoNode preSerach(int no) {
        if (this.no == no) return this;
        HreoNode hreoNode = null;
        if (this.left != null) hreoNode = this.left.preSerach(no);
        //左找到返回
        if (hreoNode != null) return hreoNode;
        if (this.right != null) hreoNode = this.right.preSerach(no);
        return hreoNode;
    }
    //中序

    public HreoNode midSreach(int no) {
        HreoNode hreoNode = null;
        if (this.left != null) hreoNode = this.left.midSreach(no);
        if (hreoNode != null) return hreoNode;
        if (this.no == no) return this;
        if (this.right != null) hreoNode = this.right.midSreach(no);
        return hreoNode;

    }

    //后序
    public HreoNode postSreach(int no) {
        HreoNode hreoNode = null;
        if (this.left != null) hreoNode = this.left.postSreach(no);
        if (hreoNode != null) return hreoNode;
        if (this.right != null) hreoNode = this.right.postSreach(no);
        if (hreoNode != null) return hreoNode;
        if (this.no == no) return this;
        return hreoNode;
    }

    //删除
    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 HreoNode(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 HreoNode getLeft() {
        return left;
    }

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

    public HreoNode getRight() {
        return right;
    }

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

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

打印结果:删除5号
在这里插入图片描述

打印结果:删除3, 3不是叶子结点,所以4号也被删除
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值