Java数据结构《二叉排序树》的建立和删除节点

package binarysorttree;

public class BinarySortTreeClassDemo {
    public static void main(String[] args) {
        int[] arr = {7,3,10,12,5,1,9,6};
        BinarySortTree binarySortTree = new BinarySortTree();
        binarySortTree.createBinarySortTree(arr);
        binarySortTree.infixList();
        binarySortTree.deleteNode(7);
        binarySortTree.infixList();
    }

}
class BinarySortTree{
    public Node root;
    //将数组建立成排序二叉树
    public void createBinarySortTree(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            if (root == null) {
                root = new Node(arr[i]);
            }else {
                root.add(new Node(arr[i]));
            }
        }
    }
    //中序遍历
    public void infixList(){
        if (root == null) {
            System.out.println("该树为空~~~~~");
        }else {
            root.infixOrder();
        }
    }
    public void deleteNode(int value){
        if (root == null) {
            System.out.println("该树为空,不能删除");
            return;
        }
        if (root.value == value){
            if (root.left == null && root.right == null) {//叶子节点的情况
                root = null;
            }else if (root.left != null && root.right == null){
                root = root.left;
            }else if (root.right != null && root.left == null ){
                root = root.right;
            }else {//可以把这个封装成一个方法
                Node pre = root;
                Node curr = root.right;
                if (curr.left == null) {
                    root.value = curr.value;
                    root.right = curr.right;
                }else {
                    while (curr.left != null){
                        pre = curr;
                        curr = curr.left;
                    }
                    root.value = curr.value;
                    if (curr.right == null) {
                        pre.left = null;
                    }else {
                        pre.left = curr.right;
                    }
                }
            }
        }else {
            root.delete(value);
        }
    }

}
class Node{
    public int value;
    public Node left;
    public Node right;

    public Node(int value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }
    //添加元素
    public void add(Node node){
        if (node.value < this.value){
            if (this.left != null) {//要添加的值小于当前节点的值就往左边放
                this.left.add(node);
            }else {
                this.left = node;
            }
        }else {
            if (this.right != null) {//大于就往右边放
                this.right.add(node);
            }else {
                this.right = node;
            }
        }
    }
    //中序遍历
    public void infixOrder(){
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }
    //删除节点
    public void delete(int value){
        if (this.left != null && this.left.value == value) {
            Node parent = this;
            Node target = this.left;
            if (target.left == null && target.right == null) {//叶子节点的情况
                parent.left = null;
            }else if (target.left != null && target.right == null){
                parent.left = target.left;
            }else if (target.right != null && target.left == null ){
                parent.left = target.right;
            }else {
                Node pre = target;
                Node curr = target.right;
                if (curr.left == null) {
                    target.value = curr.value;
                    target.right = curr.right;
                }else {
                    while (curr.left != null){
                        pre = curr;
                        curr = curr.left;
                    }
                    target.value = curr.value;
                    if (curr.right == null) {
                        pre.left = null;
                    }else {
                        pre.left = curr.right;
                    }
                }
            }
        }
        if (this.right != null && this.right.value == value){
            Node parent = this;
            Node target = this.right;
            if (target.left == null && target.right == null) {//叶子节点的情况
                parent.right = null;
            }else if (target.left != null && target.right == null){
                parent.right = target.left;
            }else if (target.right != null && target.left == null ){
                parent.right = target.right;
            }else {
                Node pre = target;
                Node curr = target.right;
                if (curr.left == null) {
                    target.value = curr.value;
                    target.right = curr.right;
                }else {
                    while (curr.left != null){
                        pre = curr;
                        curr = curr.left;
                    }
                    target.value = curr.value;
                    if (curr.right == null) {
                        pre.left = null;
                    }else {
                        pre.left = curr.right;
                    }
                }
            }
        }
        if (this.left != null) {
            this.left.delete(value);
        }
        if (this.right != null){
            this.right.delete(value);
        }
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值