二叉顺序树(BST)

package com.monster.BST;

/**
 * @author Monster
 * @version v1.0
 * @time 04-16-2021 16:26:38
 * @description:
 */
public class BinarySortTreeDemo {
    public static void main(String[] args) {
        BinarySortTree bst = new BinarySortTree();

        int[] arr = {13, 6, 3, 8, 1, 9, 7};
        for (int data : arr) {
            bst.add(new Node(data));
        }

        System.out.println("中序遍历");
        bst.inOrder();
        System.out.println("删除节点后");
        bst.delNode(13);
        bst.inOrder();
    }
}

class BinarySortTree {

    private Node root;

    public void add(Node node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }

    public void delNode(int data) {
        // 查找目标节点
        Node targetNode = search(data);
        // 如果没有找到
        if (targetNode == null) {
            return;
        }
        // 如果当前树只有一个节点
        if (root.left == null && root.right == null) {
            root = null;
        }

        // 程序走到这里说明:当前树不止一个节点
        // 查找目标节点的父节点
        Node parent = searchParent(data);

        // 情况一:该目标节点是叶子节点
        // 由于当前树不止一个节点,由目标节点是叶子节点,所以父节点不为空,不会出现空指针异常
        if (targetNode.left == null && targetNode.right == null) {
            /* 可以用于替换    if(root.left == null && root.right == null) { root = null; }
            if(parent == null) {
                root = null;
                return;
            }*/
            // 判断目标节点是父节点的左节点还是右节点
            if (parent.left != null && parent.left.data == data) {
                parent.left = null;
            } else {
                parent.right = null;
            }
            // 情况三:该目标节点有左子节点和右子节点
        } else if (targetNode.left != null && targetNode.right != null) {
            // 删除并返回目标节点右子树的最小叶子结点,并赋值给目标节点的data,就完成了删除该目标节点
            // 原理是用比该目标节点大的临近的节点代替该目标节点
            targetNode.data = delTreeMin(targetNode.right);

            // 情况二:该目标节点只有一个子节点
        } else {
            // 如果目标节点没有父节点,即目标节点为根节点
            if (parent == null) {
                if (targetNode.left != null) {
                    root = targetNode.left;
                } else {
                    root = targetNode.right;
                }
                return;
            }
            if (targetNode.left != null) {
                if (parent.left.data == data) {
                    parent.left = targetNode.left;
                } else {
                    parent.right = targetNode.left;
                }
            } else {
                if (parent.left.data == data) {
                    parent.left = targetNode.right;
                } else {
                    parent.right = targetNode.right;
                }
            }
        }
    }

    // 返回并删除当前根节点的最小子节点
    private int delTreeMin(Node node) {
        Node target = node;
        // 得到最小子节点,也是叶子节点
        while (target.left != null) {
            target = target.left;
        }
        // 删除该叶子节点
        delNode(target.data);
        return target.data;
    }

    // 查找当前节点的父节点
    public Node searchParent(int data) {
        if (root == null) {
            return null;
        }
        return root.searchParent(data);
    }

    // 查找当前节点
    public Node search(int data) {
        if (root == null) {
            return null;
        }
        return root.search(data);
    }

    public void inOrder() {
        root.inOrder();
    }
}

class Node {
    int data;
    Node left;
    Node right;

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

    // 查找当前节点
    public Node search(int data) {
        if (this.data == data) {
            return this;
        } else if (this.data > data && this.left != null) {
            return this.left.search(data);
        } else if (this.data < data && this.right != null) {
            return this.right.search(data);
        }
        return null;
    }

    // 查找当前节点的父节点
    public Node searchParent(int data) {
        if ((this.left != null && this.left.data == data) || (this.right != null && this.right.data == data)) {
            return this;
        } else if (this.data > data && this.left != null) {
            return this.left.searchParent(data);
        } else if (this.data <= data && this.right != null) {
            return this.right.searchParent(data);
        }
        return null;
    }

    // 添加节点
    public void add(Node node) {
        if (this.data > node.data) {
            if (this.left == null) {
                this.left = node;
            } else {
                this.left.add(node);
            }
        } else {
            if (this.right == null) {
                this.right = node;
            } else {
                this.right.add(node);
            }
        }
    }

    @Override
    public String toString() {
        return "Node{" +
                "data=" + data +
                '}';
    }

    // 中序遍历
    public void inOrder() {
        if (this.left != null) {
            this.left.inOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.inOrder();
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值