自己实现一个二叉查找树BinarySearchTree

需求

自己实现一个简单的二叉查找树BinarySearchTree

二叉排序树或者是一棵空树,或者是具有下列性质的二叉树:

  • 若左子树不空,则左子树上所有结点的值均小于或等于它的根结点的值;
  • 若右子树不空,则右子树上所有结点的值均大于或等于它的根结点的值;
  • 左、右子树也分别为二叉排序树;

图示

包含功能:

  • insert
  • remove
  • contains
  • findMax
  • findMin
  • toString

代码:


import java.util.NoSuchElementException;
import java.util.StringJoiner;

public class BinarySearchTree<T extends Comparable<? super T>> {
    private BinaryNode<T> root;

    public BinarySearchTree() {
        root = null;
    }

    public BinarySearchTree(T[] arr) {
        for (T t : arr) {
            insert(t);
        }
    }

    public void markEmpty() {
        root = null;
    }

    public boolean isEmpty() {
        return root == null;
    }

    public boolean contains(T t) {
        return contains(t, root);
    }

    public T findMin() {
        if (isEmpty())
            throw new NoSuchElementException();
        return findMin(root).element;
    }

    public T findMax() {
        if (isEmpty())
            throw new NoSuchElementException();
        return findMax(root).element;
    }

    public void insert(T t) {
        root = insert(t, root);
    }

    public void remove(T t) {
        root = remove(t, root);
    }

    @Override
    public String toString() {
        StringJoiner joiner = new StringJoiner(" ", "[", "]");
        listAll(this.root, joiner);
        return joiner.toString();
    }

    private void listAll(BinaryNode<T> node, StringJoiner joiner) {
        if (node.left != null)
            listAll(node.left, joiner);
        joiner.add(node.element.toString());
        if (node.right != null)
            listAll(node.right, joiner);
    }

    private boolean contains(T t, BinaryNode<T> node) {
        if (node == null)
            return false;
        int compareResult = t.compareTo(node.element);
        if (compareResult < 0)
            return contains(t, node.left);
        else if (compareResult > 0)
            return contains(t, node.right);
        else
            return true;
    }

    private BinaryNode<T> findMin(BinaryNode<T> node) {
        if (node == null)
            return null;
        if (node.left == null)
            return node;
        return findMin(node.left);
    }

    private BinaryNode<T> findMax(BinaryNode<T> node) {
        if (node == null)
            return null;
        while (node.right != null)
            node = node.right;
        return node;
    }

    private BinaryNode<T> insert(T t, BinaryNode<T> node) {
        if (node == null)
            return new BinaryNode<T>(t);
        int compareResult = t.compareTo(node.element);
        if (compareResult < 0)
            node.left = insert(t, node.left);
        else if (compareResult > 0)
            node.right = insert(t, node.right);
        return node;
    }

    private BinaryNode<T> remove(T t, BinaryNode<T> node) {
        if (node == null)
            return null;
        int compareResult = t.compareTo(node.element);
        if (compareResult < 0) {
            node.left = remove(t, node.left);
        } else if (compareResult > 0) {
            node.right = remove(t, node.right);
        } else if (node.left != null && node.right != null) {
            // two children
            node.element = findMin(node.right).element;
            node.right = remove(node.element, node.right);
        } else {
            node = (node.left != null) ? node.left : node.right;
        }

        return node;
    }

    private static class BinaryNode<T> {
        T element;
        BinaryNode<T> left;
        BinaryNode<T> right;

        public BinaryNode(T element) {
            this(element, null, null);
        }

        public BinaryNode(T element, BinaryNode<T> left, BinaryNode<T> right) {
            this.element = element;
            this.left = left;
            this.right = right;
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值