Java实现:二叉搜索树(Binary Search Tree),突围金九银十面试季

/**

  • 节点

  • @param

*/

private static class BinaryNode {

BinaryNode(AnyType theElement) {

this(theElement, null, null);

}

BinaryNode(AnyType theElement, BinaryNode left, BinaryNode right) {

element = theElement;

left = left;

right = right;

}

AnyType element; // the data in the node

BinaryNode left; // Left child

BinaryNode right; // Right child

}

1.3、构造器和成员变量

private BinaryNode root;

private Comparator<? super AnyType> cmp;

/**

  • 无参构造器

*/

public BinarySearchTree() {

this(null);

}

/**

  • 带参构造器,比较器

  • @param c 比较器

*/

public BinarySearchTree(Comparator<? super AnyType> c) {

root = null;

cmp = c;

}

1.3、公共方法(public method)

主要包括插入,删除,找到最大值、最小值,清空树,查看元素是否包含;

/**

  • 清空树

*/

public void makeEmpty() {

root = null;

}

public boolean isEmpty() {

return root == null;

}

public boolean contains(AnyType x){

return contains(x,root);

}

public AnyType findMin(){

if (isEmpty()) throw new BufferUnderflowException();

return findMin(root).element;

}

public AnyType findMax(){

if (isEmpty()) throw new BufferUnderflowException();

return findMax(root).element;

}

public void insert(AnyType x){

root = insert(x, root);

}

public void remove(AnyType x){

root = remove(x,root);

}

1.4、比较函数

如果有比较器,就使用比较器,否则要求对象实现了Comparable接口;

private int myCompare(AnyType lhs, AnyType rhs) {

if (cmp != null) {

return cmp.compare(lhs, rhs);

} else {

return lhs.compareTo(rhs);

}

}

1.5、contains 函数

本质就是一个树的遍历;

private boolean contains(AnyType x, BinaryNode t) {

if (t == null) {

return false;

}

int compareResult = myCompare(x, t.element);

if (compareResult < 0) {

return contains(x, t.left);

} else if (compareResult > 0) {

return contains(x, t.rig

【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】

浏览器打开:qq.cn.hn/FTf 免费领取

ht);

} else {

return true;

}

}

1.6、findMin

因为二叉搜索树的性质,最小值一定是树的最左节点,要注意树为空的情况。

/**

  • Internal method to find the smallest item in a subtree

  • @param t the node that roots the subtree

  • @return node containing the smallest item

*/

private BinaryNode findMin(BinaryNode t) {

if (t == null) {

return null;

}

if (t.left == null) {

return t;

}

return findMin(t.left);

}

1.7、findMax

最右节点;

/**

  • Internal method to find the largest item in a subtree

  • @param t the node that roots the subtree

  • @return the node containing the largest item

*/

private BinaryNode findMax(BinaryNode t){

if (t == null){

return null;

}

if (t.right == null){

return t;

}

return findMax(t.right);

}

1.8、insert

这个主要是根据二叉搜索树的性质,注意当树为空的情况,就可以加入新的节点了,还有当该值已经存在时,默认不进行操作;

/**

  • Internal method to insert into a subtree

  • @param x the item to insert

  • @param t the node that roots the subtree

  • @return the new root of the subtree

*/

private BinaryNode insert(AnyType x, BinaryNode t){

if (t == null){

return new BinaryNode<>(x,null,null);

}

int compareResult = myCompare(x,t.element);

if (compareResult < 0){

t.left = insert(x,t.left);

}

else if (compareResult > 0){

t.right = insert(x,t.right);

}

else{

//Duplicate; do nothing

}

return t;

}

1.9、remove

imgimgimg

注意当空树时,返回null;

最后一个三元表达式,是在之前已经排除掉节点有两个儿子的情况下使用的。

/**

  • Internal method to remove from a subtree

  • @param x the item to remove

  • @param t the node that roots the subtree

  • @return the new root of the subtree

*/

private BinaryNode remove(AnyType x, BinaryNode t){

if (t == null){

return t; // Item not found ,do nothing

}

int compareResult = myCompare(x,t.element);

if (compareResult < 0){

t.left = remove(x,t.left);

}

else if (compareResult > 0){

t.right = remove(x,t.right);

}

else if (t.left !=null && t.right!=null){

//Two children

t.element = findMin(t.right).element;

t.right = remove(t.element,t.right);

}

else

t = (t.left !=null) ? t.left:t.right;

return t;

}

二、完整代码实现(Java)


/**

  • @author LongRookie

  • @description: 二叉搜索树

  • @date 2021/6/26 19:41

*/

import com.sun.source.tree.BinaryTree;

import java.nio.BufferUnderflowException;

import java.util.Comparator;

/**

  • 二叉搜索树

*/

public class BinarySearchTree<AnyType extends Comparable<? super AnyType>> {

/**

  • 节点

  • @param

*/

private static class BinaryNode {

BinaryNode(AnyType theElement) {

this(theElement, null, null);

}

BinaryNode(AnyType theElement, BinaryNode left, BinaryNode right) {

element = theElement;

left = left;

right = right;

}

AnyType element; // the data in the node

BinaryNode left; // Left child

BinaryNode right; // Right child

}

private BinaryNode root;

private Comparator<? super AnyType> cmp;

/**

  • 无参构造器

*/

public BinarySearchTree() {

this(null);

}

/**

  • 带参构造器,比较器

  • @param c 比较器

*/

public BinarySearchTree(Comparator<? super AnyType> c) {

root = null;

cmp = c;

}

/**

  • 清空树

*/

public void makeEmpty() {

root = null;

}

public boolean isEmpty() {

return root == null;

}

public boolean contains(AnyType x){

return contains(x,root);

}

public AnyType findMin(){

if (isEmpty()) throw new BufferUnderflowException();

return findMin(root).element;

}

public AnyType findMax(){

if (isEmpty()) throw new BufferUnderflowException();

return findMax(root).element;

}

public void insert(AnyType x){

root = insert(x, root);

}

public void remove(AnyType x){

root = remove(x,root);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
做一门精致,全面详细的 java数据结构与算法!!!让天下没有难学的数据结构,让天下没有难学的算法,不吹不黑,我们的讲师及其敬业,可以看到课程视频,课件,代码的录制撰写,都是在深夜,如此用心,其心可鉴,他不掉头发,谁掉头发???总之你知道的,不知道的,我们都讲,并且持续更新,走过路过,不要错过,不敢说是史上最全的课程,怕违反广告法,总而言之,言而总之,这门课你值得拥有,好吃不贵,对于你知识的渴求,我们管够管饱话不多说,牛不多吹,我们要讲的本门课程内容:稀疏数组、单向队列、环形队列、单向链表、双向链表、环形链表、约瑟夫问题、栈、前缀、中缀、后缀表达式、中缀表达式转换为后缀表达式、递归与回溯、迷宫问题、八皇后问题、算法的时间复杂度、冒泡排序、选择排序、插入排序、快速排序、归并排序、希尔排序、基数排序(桶排序)、堆排序、排序速度分析、二分查找、插值查找、斐波那契查找、散列、哈希表、二叉树、二叉树与数组转换、二叉排序树(BST)、AVL树、线索二叉树、赫夫曼树、赫夫曼编码、多路查找树(B树B+树和B*树)、图、图的DFS算法和BFS、程序员常用10大算法、二分查找算法(非递归)、分治算法、动态规划算法、KMP算法、贪心算法、普里姆算法、克鲁斯卡尔算法、迪杰斯特拉算法、弗洛伊德算法马踏棋盘算法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值