Rhyme/查找树ADT-二叉查找树的简单模拟Java版

23 篇文章 0 订阅

查找树ADT-二叉查找树的简单模拟Java版

package my.binary.search.tree;

/**
 * 查找树 ADT 二叉查找树的实现
 * 假设元素类型为Integer
 *
 * @author RhymeChiang
 * @date 2018/01/17
 **/
public class BinarySearchTree<T extends Comparable<? super T>> {

    /**
     * 二叉树根节点
     */
    private BinaryNode<T> root;

    /**
     * 静态内部节点类
     *
     * @param <T>
     */
    private static class BinaryNode<T> {

        private T element;
        private BinaryNode<T> left;
        private BinaryNode<T> right;

        public BinaryNode() {
        }

        public BinaryNode(T element) {
            this.element = element;
        }

        public BinaryNode(T element, BinaryNode<T> left, BinaryNode<T> right) {
            this.element = element;
            this.left = left;
            this.right = right;
        }
    }

    /**
     * 清空二叉树
     */
    public void makeEmpty() {
        root = null;
    }

    /**
     * 判断二叉树是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return root == null;
    }

    /**
     * 查找元素element是否存在与二叉搜索树中
     *
     * @param element
     * @return
     */
    public boolean contains(T element) {
        return contains(element, root);
    }

    /**
     * 判断元素element是否存在于二叉查找树root中
     *
     * @param element
     * @param root
     * @return
     */
    private boolean contains(T element, BinaryNode<T> root) {
        if (root == null) {
            return false;
        }
        int compareResult = element.compareTo(root.element);

        if (compareResult > 0) {
            return contains(element, root.right);
        }

        if (compareResult < 0) {
            return contains(element, root.left);
        }

        return true;

    }

    /**
     * 查找二叉查找树中最小的元素
     *
     * @return
     */
    public T findMin() {
        if (findMin(root) != null) {
            return findMin(root).element;
        }
        return null;
    }

    /**
     * 查找二叉搜索树中最小的节点
     *
     * @param root
     * @return
     */
    private BinaryNode<T> findMin(BinaryNode<T> root) {
        if (root == null) {
            return null;
        }
        if (root.left != null) {
            return findMin(root.left);
        }
        return root.left;
    }

    /**
     * 查找二叉树中的最大元素
     *
     * @return
     */
    public T findMax() {
        BinaryNode<T> maxNode = findMax(root);
        if (maxNode != null) {
            return maxNode.element;
        }
        return null;
    }

    /**
     * 查找二叉查找树中的最大节点
     *
     * @param root
     * @return
     */
    private BinaryNode<T> findMax(BinaryNode<T> root) {
        if (root == null) {
            return null;
        }
        if (root.right != null) {
            return findMax(root.right);
        }
        return root.right;
    }

    /**
     * 插入元素element
     *
     * @param element
     */
    public void insert(T element) {
        root = insert(element, root);
    }

    /**
     * 将元素插入到root二叉查找树中
     *
     * @param element
     * @param root
     * @return
     */
    private BinaryNode<T> insert(T element, BinaryNode<T> root) {
        if (root == null) {
            return new BinaryNode<>(element);
        }
        int compareResult = element.compareTo(root.element);
        if (compareResult > 0) {
            root.right = insert(element, root.right);
        } else if (compareResult < 0) {
            root.left = insert(element, root.left);
        }
        return root;
    }

    /**
     * 删除元素一个元素element
     *
     * @param element
     */
    public void remove(T element) {

    }

    /**
     * 在二叉查找树中删除元素element
     *
     * @param element
     * @param root
     * @return
     */
    private BinaryNode<T> remove(T element, BinaryNode<T> root) {
        if (root == null) {
            return null;
        }

        int compareResult = element.compareTo(root.element);

        if (compareResult > 0) {
            root.right = remove(element, root.right);
        } else if (compareResult < 0) {
            root.left = remove(element, root.left);
        }
        // 被删除节点有两个子节点
        else if (root.left != null && root.right != null) {
            root.element = findMin(root.right).element;
            root.right = remove(root.element, root.right);
        }
        // 叶子节点或单叶节点
        else {
            root = (root.left != null) ? root.left : root.right;
        }
        return root;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值