二叉排序树Java

二叉排序树与数组和链表之间的区别:

数组:

        优点:直接在数组尾添加,速度快。

        缺点:查找速度慢

链表:

        优点:添加和删除元素比数组快,不需要整体移动。

        缺点:查找速度慢。

因此引入二叉排序树来解决这个问题。

二叉排序树代码如下:

package DataStructures.binarysorttree;

public class BinarySortTreeDemo {
    public static void main(String[] args) {
        int[] arr = {7,3,10,12,5,1,9};
        BinarySortTree binarySortTree = new BinarySortTree();
        //循环添加节点到二叉排序树
        for( int i = 0; i < arr.length; i++ ) {
            binarySortTree.add(new Node(arr[i]));
        }

        binarySortTree.delNode(2);
        binarySortTree.delNode(5);
        binarySortTree.delNode(9);
        binarySortTree.delNode(12);
        binarySortTree.delNode(7);
        binarySortTree.delNode(3);
        binarySortTree.delNode(10);
        binarySortTree.delNode(1);

        //中序遍历
        System.out.println("中序遍历二叉排序树:");
        binarySortTree.infixOrder();

    }
}

/**
 * 创建二叉排序树
 */
class BinarySortTree {
    private Node root;

    /**
     * 添加节点
     * @param node
     */
    public void add( Node node ) {
        if ( root == null ) {
            root = node;
        } else {
            root.add(node);
        }
    }

    /**
     * 中序遍历
     */
    public void infixOrder() {
        if ( root != null ) {
            root.infixOrder();
        } else {
            System.out.println("二叉排序树为空,不能遍历!");
        }
    }

    /**
     * 查找要删除的节点
     * @param value
     * @return
     */
    public Node search(int value) {
        if (root == null) {
            return null;
        } else {
            return root.search(value);
        }
    }

    /**
     * 查找要删除节点的父节点
     * @param value
     * @return
     */
    public Node serachParent(int value) {
        if (root == null) {
            return null;
        } else {
            return root.searchParent(value);
        }
    }

    /**
     * 1.返回以 node为根节点的二叉排序树的最小节点的值
     * 2.删除以 node为根节点的二叉排序树的最小节点
     * @param node
     * @return  返回以 node为根节点的二叉排序树的最小节点的值
     */
    public int delRightTreeMin(Node node) {
        Node target = node;
        while (target.left != null){
            target = target.left;
        }
        delNode(target.value);
        return target.value;
    }

    /**
     * 删除节点
     * @param value
     */
    public void delNode(int value) {
        if ( root == null ) {
            return;
        } else {
            Node targetNode = search(value);
            if ( targetNode == null ) {
                return;
            }
            if (root.left == null && root.right == null) {
                root = null;
                return;
            }

            Node parent = serachParent(value);

            if (targetNode.left == null && targetNode.right == null) {//若要删除的节点是叶子节点
                if (parent.left != null && parent.left.value == value) {
                    parent.left = null;
                } else if (parent.right != null && parent.right.value == value) {
                    parent.right = null;
                }
            } else if (targetNode.left != null && targetNode.right != null) {//删除有两颗子树的节点
                int minVal = delRightTreeMin(targetNode.right);
                targetNode.value = minVal;
            } else {//删除只有一颗子树的节点
                //如果要删除的节点有左子节点
                if (targetNode.left != null) {
                    if (parent != null){
                        if (parent.left.value == value){  //如果targetNode是parent的左子节点
                            parent.left = targetNode.left;
                        } else {  //如果targetNode是parent的右子节点
                            parent.right = targetNode.left;
                        }
                    } else {
                        root = targetNode.left;
                    }
                } else {//如果要删除的节点有右子节点
                    if (parent != null){
                        if (parent.left.value == value){  //如果targetNode是parent的左子节点
                            parent.left = targetNode.right;
                        } else {  //如果targetNode是parent的右子节点
                            parent.right = targetNode.right;
                        }
                    } else {
                        root = targetNode.right;
                    }
                }
            }


        }
    }

}

/**
 * 创建 Node节点
 */
class Node {
    int value;
    Node left;
    Node right;
    public Node(int value) {
        this.value = value;
    }

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

    /**
     * 添加节点
     * @param node  要添加的节点
     */
    public void add( Node node ) {
        if ( node == null ) {
            return;
        }

        if ( node.value < this.value ) {
            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);
            }
        }
    }

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

    /**
     * 查找要删除的节点
     * @param value  要删除节点的值
     * @return 如果找到就返回该节点,否则返回 null
     */
    public Node search( int value ) {
        if ( value == this.value ) {
            return this;
        } else if ( value < this.value ) {
            if ( this.left == null ) {
                return null;
            }
            return this.left.search(value);
        } else {
            if ( this.right == null ) {
                return null;
            }
            return this.right.search(value);
        }
    }

    /**
     * 查找要删除节点的父节点
     * @param value 要删除节点的值
     * @return  返回的是要删除节点的父节点,没有就返回 null
     */
    public Node searchParent( int value ) {
        if ( (this.left != null && this.left.value == value ) || (this.right != null && this.right.value == value) ) {
            return this;
        } else {
            if ( value < this.value && this.left != null ) {
                //左递归
                return this.left.searchParent(value);
            } else if ( value >= this.value && this.right != null ){
                //右递归
                return this.right.searchParent(value);
            } else {
                return null;
            }
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值