19 二叉树删除指定节点

二叉树删除指定节点

  1. 规定:

    1.1 如果删除的节点是叶子节点,则删除该节点。

    1.2 如果删除的节点是非叶子节点,则删除该子树。

  2. 步骤:

    2.1 如果树是空树,无需操作直接返回。如果根节点是要删除的节点,则直接将二叉树置空。

    2.2 如果当前节点的左子节点不为空,并且左子节点就是要删除的节点,就将 this.left = null,并且直接返回。

    2.3 如果当前节点的右子节点不为空,并且左子节点就是要删除的节点,就将 this.right = null,并且直接返回。

    2.4 如果第2步和第3步没有删除节点,那么就需要向左子树进行递归删除。

    2.5 如果第4步也没有删除节点,则应当向右子树进行递归删除。

  3. 删除:

    public class BinaryTreeDemo {
        public static void main(String[] args) {
            // 手动构建二叉树
            Node root = new Node(1);
            Node node2 = new Node(2);
            Node node3 = new Node(3);
            Node node4 = new Node(4);
            Node node5 = new Node(5);
            root.setLeft(node2);
            root.setRight(node3);
            node3.setLeft(node4);
            node3.setRight(node5);
            BinaryTree binaryTree = new BinaryTree(root);
    
            // 前序遍历二叉树
            //binaryTree.preOrder();  // 1 2 3 4 5
            // 中序遍历二叉树
            //binaryTree.infixOrder();  // 2 1 4 3 5
            // 后序遍历二叉树
            //binaryTree.postOrder();  // 2 4 5 3 1
    
            // 前序查找节点
            //binaryTree.preOrderSearch(7);
            // 中序查找节点
            //binaryTree.infixOrderSearch(3);
            // 后序查找节点
            //binaryTree.postOrderSearch(5);
    
            // 删除前,前序遍历结果
            binaryTree.preOrder();
            // 删除
            binaryTree.delNode(3);
            System.out.println("-------------------");
            // 删除后,前序遍历结果
            binaryTree.preOrder();
        }
    }
    
    /**
     * 定义二叉树
     */
    class BinaryTree {
        // 二叉树的根节点
        private Node root;
    
        public BinaryTree(Node root) {
            this.root = root;
        }
    
        // 删除节点
        public void delNode(int id) {
            if (root != null && root.getId() == id) {
                root = null;
                return;
            }
            if (root != null && root.getLeft() != null) {
                root.delNode(id);
            }
            if (root != null && root.getRight() != null) {
                root.delNode(id);
            }
    
        }
    
        // 前序遍历
        public void preOrder() {
            if (root == null) {
                System.out.println("此二叉树为空!");
            } else {
                root.preOrder();
            }
        }
        // 中序遍历
        public void infixOrder() {
            if (root == null) {
                System.out.println("此二叉树为空!");
            } else {
                root.infixOrder();
            }
        }
        // 后序遍历
        public void postOrder() {
            if (root == null) {
                System.out.println("此二叉树为空!");
            } else {
                root.postOrder();
            }
        }
    
        // 前序查找节点
        public void preOrderSearch(int id) {
            if (root == null) {
                System.out.println("二叉树为空,查找节点失败");
            } else {
                if(root.preOrderSearch(id) != null) {
                    System.out.println(root.preOrderSearch(id));
                } else {
                    System.out.println("没有该节点");
                }
            }
        }
    
        // 中序查找节点
        public void infixOrderSearch(int id) {
            if (root == null) {
                System.out.println("二叉树为空,查找节点失败");
            } else {
                if(root.infixOrderSearch(id) != null) {
                    System.out.println(root.infixOrderSearch(id));
                } else {
                    System.out.println("没有该节点");
                }
            }
        }
    
        // 后序查找节点
        public void postOrderSearch(int id) {
            if (root == null) {
                System.out.println("二叉树为空,查找节点失败");
            } else {
                if(root.postOrderSearch(id) != null) {
                    System.out.println(root.postOrderSearch(id));
                } else {
                    System.out.println("没有该节点");
                }
            }
        }
    
    }
    
    /**
     * 定义节点类
     */
    class Node {
        private int id;
        private Node left;
        private Node right;
    
        public Node(int id) {
            this.id = id;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public Node getLeft() {
            return left;
        }
    
        public void setLeft(Node left) {
            this.left = left;
        }
    
        public Node getRight() {
            return right;
        }
    
        public void setRight(Node right) {
            this.right = right;
        }
    
        @Override
        public String toString() {
            return "Node{" +
                    "id=" + id +
                    '}';
        }
    
        // 删除节点
        public void delNode(int id) {
            if ( this.left != null) {
                if (this.left.getId() == id) {
                    this.left = null;
                    return;
                } else {
                    this.left.delNode(id);
                }
            }
            if (this.right != null) {
                if (this.right.getId() == id) {
                    this.right = null;
                    return;
                } else {
                    this.right.delNode(id);
                }
            }
        }
    
        // 前序遍历
        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 Node preOrderSearch(int id) {
            if (this.id == id) {
                return this;
            }
            Node res = null;
            if (this.left != null) {
                res = this.left.preOrderSearch(id);
            }
            if (res != null) {
                return res;
            }
            if (this.right != null) {
                res = this.right.preOrderSearch(id);
            }
            return res;
        }
    
        // 中序查找节点
        public Node infixOrderSearch(int id) {
            Node res = null;
            if (this.left != null) {
                res = this.left.infixOrderSearch(id);
            }
            if (res != null) {
                return res;
            }
            if (this.id == id) {
                return this;
            }
            if (this.right != null) {
                res = this.right.infixOrderSearch(id);
            }
            return res;
        }
    
        // 后序查找节点
        public Node postOrderSearch(int id) {
            Node res = null;
            if (this.left != null) {
                res = this.left.postOrderSearch(id);
            }
            if (res != null) {
                return res;
            }
            if (this.right != null) {
                res = this.right.postOrderSearch(id);
            }
            if (res != null) {
                return res;
            }
            if (this.id == id) {
                return this;
            }
            return res;
        }
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值