数据结构之二叉排序树

二叉排序树(Binary Sort Tree),又称二叉查找树(Binary Search Tree),亦称二叉搜索树。

int[] arr = {5,7,23,6,14,6,85,3,83,63};

在这里插入图片描述



/**
 * @Author: Drm
 * @Description:
 * @Date: Created in 12:43 2020/3/22
 * @Modified By: 二叉排序树
 */
public class BinarySortTree {
    public static void main(String[] args) {
        int[] arr = {5, 7, 23, 6, 14, 6, 85, 3, 83, 63};
        BinaryTree binaryTree = new BinaryTree();
        for (int i : arr) {
            binaryTree.add(new Node(i));
        }
        binaryTree.infixOrder();
        binaryTree.del(23);
        binaryTree.del(6);
        binaryTree.del(85);
        binaryTree.del(83);
        binaryTree.infixOrder();
    }
}

/**
 * 二拆排序树
 */
class BinaryTree {
    private Node root;

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

    void infixOrder() {
        if (root != null) {
            root.infixOrder();
        }
    }

    public void del(int value) {
        if (root == null) {
            return;
        }
        //targetNode 目标节点
        Node targetNode = root.searchNode(value);
        if(targetNode == null) return;
        if(root.getLeft() == null && root.getRight() == null){
            root = null;
            return;
        }

        Node parent = root.searchParent(targetNode);
        //第一种情况:目标节点左右子节点为空,即目标节点为叶子节点
        if (targetNode.getLeft() == null && targetNode.getRight() == null) {
            if(parent.getLeft() != null && parent.getLeft() == targetNode) {
                parent.setLeft(null);
            } else {
                parent.setRight(null);
            }
            return;
        }
        //第二种情况:目标节点只有一个子节点
        if(targetNode.getLeft() == null || targetNode.getRight() == null){
            if(targetNode.getLeft() != null){
                if(parent == null){
                    root = targetNode.getLeft();
                }else {
                    if(parent.getLeft() == targetNode){
                        parent.setLeft(targetNode.getLeft());
                    }else {
                        parent.setRight(targetNode.getLeft());
                    }
                }
            }else {
                if(parent == null){
                    root = targetNode.getRight();
                }else {
                    if(parent.getLeft() == targetNode){
                        parent.setLeft(targetNode.getRight());
                    }else {
                        parent.setRight(targetNode.getRight());
                    }
                }
            }
            return;
        }else {
            //第三种情况:目标节点有两个子节点
            //寻找到目标节点右子树的最小值,替换目标节点
            //理由:目标节点右子树所以节点都比目标节点大,所以寻找最小值替换目标节点。
            Node minNode = delRightTreeMin(targetNode);
            minNode.setLeft(targetNode.getLeft());
            minNode.setRight(targetNode.getRight());
            if(parent !=null){
                if(parent.getLeft() == targetNode){
                    parent.setLeft(minNode);
                }else if(parent.getRight() == targetNode){
                    parent.setRight(minNode);
                }
            }else {
                root = minNode;
            }
        }
    }

    /**
     * 删除右子树最小的节点并返回该节点
     * @param node
     * @return
     */
    public Node delRightTreeMin(Node node){
        Node target = node.getRight();
        if(target.getLeft() == null){
            searchParent(target).setRight(target.getRight());
            return target;
        }
        while (target.getLeft() != null) {
            target = target.getLeft();
        }
        searchParent(target).setLeft(target.getRight());
        return target;
    }

    Node search(int value) {
        if (root == null) {
            return null;
        }
        return root.searchNode(value);
    }

    Node searchParent(Node node) {
        return root.searchParent(node);
    }


}

/**
 * 定义树节点
 */
class Node {
    private int value;
    private Node left;
    private Node right;

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

    void add(Node node) {
        if (node != null) {
            if (node.value > this.value) {
                //如果右节点为空,则把该节点加入右节点中
                if (this.right == null) {
                    this.right = node;
                } else {
                    //否则递归右节点
                    this.right.add(node);
                }
            } else {
                if (this.left == null) {
                    this.left = node;
                } else {
                    this.left.add(node);
                }
            }
        }
    }

    //中序遍历(递归)
    void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this.value);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

    Node searchNode(int value) {
        if (this.getValue() == value) {
            return this;
        }
        if (this.getValue() > value) {
            if (this.left == null) {
                return null;
            }
            return this.left.searchNode(value);
        } else {
            if (this.right == null) {
                return null;
            }
            return this.right.searchNode(value);
        }

    }

    Node searchParent(Node node) {
        if(this == node){
            return null;
        }
        if(this.getRight() == node || this.getLeft() == node){
            return this;
        }
        if (this.getValue() > node.getValue()) {
            if (this.getLeft() == node) {
                return this;
            } else {
                return this.getLeft().searchParent(node);
            }
        } else if(this.getValue() < node.getValue()){
            if (this.getRight() == node) {
                return this;
            } else {
                return this.getRight().searchParent(node);
            }
        } else {
            if(this.getLeft().value == node.getValue()){
                return this.getLeft().searchParent(node);
            }else {
                return this.getRight().searchParent(node);
            }
        }
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值