二叉排序树(Java)

二叉排序树的删除代码,在网上看到很多,都感觉有点繁杂难以理解,于是,我结合了TreeMap的remove()方法写出二叉排序树的实现,如果有错误的地方,还请大家多多指正~~

性质

二叉排序树又称“二叉查找树”、“二叉搜索树”。二叉排序树:或者是一棵空树,或者是具有下列性质的二叉树:

  1. 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值。
  2. 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值。
  3. 它的左、右子树也分别为二叉排序树。

代码实现

package com.xqq.二叉排序树BST;

import java.util.Stack;

public class MyBST {
    private Node head;
    private int size;

    public MyBST() {
    }

    public MyBST(int value) {
        head = new Node(value);
        size++;
    }

    public MyBST(int[] values) {
        size += values.length;
        for (int i = 0; i < values.length; i++) {
            insert(values[i]);
        }
    }

    /**
     * 插入节点
     */
    public void insert(int value) {
        if (head == null) {
            head = new Node(value);
        } else {
            insert(head, value);
        }
    }

    /**
     * 递归插入元素
     */
    public void insert(Node head, int value) {
        if (value <= head.value) {
            if (head.left == null) {
                head.left = new Node(value);
                size++;
            } else {
                insert(head.left, value);
            }
        } else {
            if (head.right == null) {
                head.right = new Node(value);
                size++;
            } else {
                insert(head.right, value);
            }
        }
    }

    /**
     * 检查是否包含该元素
     */
    public boolean contains(int value) {
        Node curr = head;
        while(curr != null){
            if(curr.value > value){
                curr = curr.left;
            }else if(curr.value < value){
                curr = curr.right;
            }else {
                return true;
            }
        }
        return false;
    }

    /**
     * 移除节点:分3种情况
     * 1. 该节点为叶子节点,直接让其父节点指向的该节点置为空
     * 2. 该节点有一个孩子节点,则让其父节点指向其孩子节点
     * 3. 该节点有两个孩子,则让其右孩子节点的最左节点与该节点交换位置,
     *    转换为情况1或2,删除该节点
     * 
     * 注意:若该节点为根节点时,需要对head进行相应的操作
     */
    public boolean remove(int value){
        // 查找节点
        Node curr = search(value);
        // 节点不存在,直接返回false
        if(curr == null) return false;
        // 查找其父亲节点
        Node father = getFather(value);

        // 如果该节点有两个孩子,则找到其右孩子节点的最左节点
        if(curr.left != null && curr.right != null){
            // 找到右孩子节点的最左节点
            Node temp = getMostLeft(curr);
            // 找到其父亲节点
            father = getFather(temp.value);
            // 将找到的该节点上移至需要删除节点的位置
            curr.value = temp.value;
            // 准备删除找到的节点
            curr = temp;
        }

        // 找到需要删除的节点替补的孩子节点
        Node replacement = (curr.left != null ? curr.left : curr.right);

        if(replacement != null){

            if(father == null){
                head = replacement;
            }else if(father.left == curr){
                father.left = replacement;
            }else {
                father.right = replacement;
            }

            curr.left = curr.right = null;

        }else if(father == null){//为根节点

            head = null;

        }else {//无孩子,为叶子节点

            if(father.left == curr)
                father.left = null;
            else 
                father.right = null;
        }
        return true;
    }

    /**
     * 找到节点的右孩子的最左节点
     */
    private Node getMostLeft(Node curr) {
        Node p = curr.right;
        while(p.left != null){
            p = p.left;
        }
        return p;
    }

    /**
     * 获取父节点
     */
    public Node getFather(int value){
        Node curr = head;
        Node father = null;
        while(curr != null){
            if(curr.value < value){
                father = curr;
                curr = curr.right;
            }else if(curr.value > value){
                father = curr;
                curr = curr.left;
            }else {
                return father;
            }
        }
        return null;
    }
    /**
     * 查找节点
     */
    public Node search(int value){
        Node curr = head;
        while(curr != null){
            if(curr.value < value){
                curr = curr.right;
            }else if(curr.value > value){
                curr = curr.left;
            }else {
                return curr;
            }
        }
        return null;
    }

    public int size() {
        return size;
    }

    /**
     * 非递归:中序遍历
     */
    public void inOrder(){
        System.out.print("非递归中序遍历: ");
        Node curr = head;
        if(curr == null) return ;
        Stack<Node> stack = new Stack<Node>();
        while(!stack.isEmpty() || curr != null){
            if(curr != null){
                stack.push(curr);
                curr = curr.left;
            }else {
                curr = stack.pop();
                System.out.print(curr.value + " ");
                curr = curr.right;
            }
        }
        System.out.println();
    }

    @Override
    public String toString() {
        inOrder();
        return "";
    }

    public static class Node {
        int value;
        Node left, right;

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

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


测试代码:
package com.xqq.二叉排序树BST;


public class Test {
    public static void main(String[] args) {
        MyBST bst = new MyBST(new int[]{3, 2, 1, 4});
        bst.toString();
        System.out.println(bst.contains(2));

        System.out.println(bst.remove(3));
        System.out.println(bst.remove(6));
        bst.toString();
    }
}

运行结果:
非递归中序遍历: 1 2 3 4 
true
true
false
非递归中序遍历: 1 2 4 

二叉排序树性能分析

  • 每个结点的Ci为该结点的层次数。
  • 最好的情况是二叉排序树的形态和折半查找的判定树相同,其平均查找长度和logn成正比(O(log2(n)))。
  • 最坏情况下,当先后插入的关键字有序时,构成的二叉排序树为一棵斜树,树的深度为n,其平均查找长度为(n + 1) / 2。也就是时间复杂度为O(n),等同于顺序查找。
  • 因此,如果希望对一个集合按二叉排序树查找,最好是把它构建成一棵平衡的二叉排序树(平衡二叉树)。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值