文章目录

  • 1、二叉搜索树
    • 1.1、 基本概念
    • 1.2、树的节点(BinaryNode)
    • 1.3、构造器和成员变量
    • 1.3、公共方法(public method)
    • 1.4、比较函数
    • 1.5、contains 函数
    • 1.6、findMin
    • 1.7、findMax
    • 1.8、insert
    • 1.9、remove
  • 二、完整代码实现(Java)

1、二叉搜索树

1.1、 基本概念

二叉树的一个性质是一棵平均二叉树的深度要比节点个数N小得多。分析表明其平均深度为O(N)\mathcal{O}(\sqrt{N})O(N​),而对于特殊类型的二叉树,即二叉查找树(binary search tree),其深度的平均值为O(logN)\mathcal{O}(log N)O(logN)。

二叉查找树的性质: 对于树中的每个节点X,它的左子树中所有项的值小于X中的项,而它的右子树中所有项的值大于X中的项。

由于树的递归定义,通常是递归地编写那些操作的例程。因为二叉查找树的平均深度为O(logN)\mathcal{O}(log N)O(logN),所以一般不必担心栈空间被用尽。

1.2、树的节点(BinaryNode)

二叉查找树要求所有的项都能够排序,有两种实现方式;

  1. 对象实现接口 Comparable, 树中的两项使用compareTo方法进行比较;
  2. 使用一个函数对象,在构造器中传入一个比较器;

本篇文章采用了构造器重载,并定义了myCompare方法,使用了泛型,因此两种方式都支持,在后续的代码实现中可以看到。

节点定义:

    /*** 节点** @param <AnyType>*/private static class BinaryNode<AnyType> {BinaryNode(AnyType theElement) {this(theElement, null, null);}BinaryNode(AnyType theElement, BinaryNode<AnyType> left, BinaryNode<AnyType> right) {element = theElement;left = left;right = right;}AnyType element; // the data in the nodeBinaryNode<AnyType> left; // Left childBinaryNode<AnyType> right; // Right child}

1.3、构造器和成员变量

    private BinaryNode<AnyType> root;private Comparator<? super AnyType> cmp;/*** 无参构造器*/public BinarySearchTree() {this(null);}/*** 带参构造器,比较器** @param c 比较器*/public BinarySearchTree(Comparator<? super AnyType> c) {root = null;cmp = c;}

关于比较器的知识可以参考下面这篇文章:
Java中Comparator的使用
关于泛型的知识可以参考下面这篇文章:
如何理解 Java 中的 <T extends Comparable<? super T>>

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<AnyType> 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.right);} 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<AnyType> findMin(BinaryNode<AnyType> 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<AnyType> findMax(BinaryNode<AnyType> 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<AnyType> insert(AnyType x, BinaryNode<AnyType> 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




注意当空树时,返回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<AnyType> remove(AnyType x, BinaryNode<AnyType> 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 childrent.element = findMin(t.right).element;t.right = remove(t.element,t.right);}elset = (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 <AnyType>*/private static class BinaryNode<AnyType> {BinaryNode(AnyType theElement) {this(theElement, null, null);}BinaryNode(AnyType theElement, BinaryNode<AnyType> left, BinaryNode<AnyType> right) {element = theElement;left = left;right = right;}AnyType element; // the data in the nodeBinaryNode<AnyType> left; // Left childBinaryNode<AnyType> right; // Right child}private BinaryNode<AnyType> 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);}private int myCompare(AnyType lhs, AnyType rhs) {if (cmp != null) {return cmp.compare(lhs, rhs);} else {return lhs.compareTo(rhs);}}private boolean contains(AnyType x, BinaryNode<AnyType> 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.right);} else {return true;}}/*** 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<AnyType> findMin(BinaryNode<AnyType> t) {if (t == null) {return null;}if (t.left == null) {return t;}return findMin(t.left);}/*** 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<AnyType> findMax(BinaryNode<AnyType> t){if (t == null){return null;}if (t.right == null){return t;}return findMax(t.right);}/*** 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<AnyType> remove(AnyType x, BinaryNode<AnyType> 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 childrent.element = findMin(t.right).element;t.right = remove(t.element,t.right);}elset = (t.left !=null) ? t.left:t.right;return t;}/*** 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<AnyType> insert(AnyType x, BinaryNode<AnyType> 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;}}

二叉搜索树(Binary Search Tree)(Java实现)相关推荐

  1. C++二叉搜索树(Binary Search Tree)(附完整源码)

    二叉搜索树Binary Search Tree node结构体定义 Queue结构体定义 二叉搜索树Binary Search Tree算法的完整源码(定义,实现,main函数测试) node结构体定 ...

  2. 实现一个二叉搜索树(Binary Search Tree)

    文章目录 定义 代码实现 一.Github代码地址 二.节点 三.树实现接口 四.4种遍历方式 五.搜索 六.删除 七.插入 结尾 定义 二叉搜索树(Binary Search Tree),又名二叉排 ...

  3. 数据结构与算法(八)二分搜索树(Binary Search Tree)

    本文主要包括以下内容: 二分搜索树的基本概念 二分搜索树的基本操作 1. 插入 2. 删除 3. 查询 实现二分搜索树 二分搜索树的不足 二分搜索树的基本概念 二分搜索树(英语:Binary Sear ...

  4. 平衡搜索树(Binary Search Tree BST)、索引二叉搜索树

    文章目录 二叉搜索树 索引二叉搜索树 测试代码 这里只给出代码实现. 更多的性质日后再补. 二叉搜索树 // // Created by SongyangJi on 2020/12/23. //#if ...

  5. 538. 把二叉搜索树转换为累加树(java)

    538. 把二叉搜索树转换为累加树 题目描述 给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的 ...

  6. 不同的二叉搜索树-战胜100%的Java用户

    不同的二叉搜索树 执行耗时:0 ms,击败了100.00% 的Java用户 内存消耗:36.3 MB,击败了66.22% 的Java用户 如下图,在二叉搜索树中,数值最大的节点N一定是在树的最右端,其 ...

  7. 【数据结构与算法】二叉搜索树V2.0的Java实现

    更新说明 在二叉搜索树V1.0的编程实现中,我们实现了BST的查找.插入,左右儿子删除的功能,但写的确实很一般,这里就Update一下. 功能介绍 void insert(x) → Insert x ...

  8. 【剑指offer】面试题36:二叉搜索树与双向链表(java)

    输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的循环双向链表.要求不能创建任何新的节点,只能调整树中节点指针的指向. 代码: package offer; class BineryTreeNode ...

  9. 【LeetCode笔记】剑指 Offer 36. 二叉搜索树与双向链表(Java、二叉树、链表、原地算法)

    文章目录 题目描述 思路 && 代码 1. 非原地算法 2. 原地算法 二刷 题目描述 谈到二叉搜索树,那就得考虑考虑中序遍历啦- 这道题对中序遍历的理解提升很有好处! 思路 & ...

最新文章

  1. matlab中,怎样把矩阵中所有的0改为2
  2. vs社区版到期离线激活_vs2019离线安装包
  3. 使用Xshell密钥认证机制远程登录Linux
  4. linux进程被中断打断,linux – 当中断处理程序被另一个中断中断时,中断上下文如何“恢复”?...
  5. Linux/Unix下tar命令详解
  6. 萌新的Python练习菜鸟100例(十八)求s=a+aa+aaa+aaaa+aa...a的值
  7. 杰出企业家的20个好习惯
  8. 数据挖掘概念与技术(第三版)课后答案——第三章
  9. Python编写九九乘法表代码以及解决方案
  10. matlab怎么将程序加密,对于MATLAB M文件怎么加密
  11. 抖音运营详细教程,算法解读、平台规则、热门涨粉......丨国仁网络
  12. 媒体:28省份已开展不动产登记机构整合工作
  13. CentOS7.6 部署 Snipe-it 资产管理系统
  14. CSDN博客专家申请的条件及规则
  15. 服务器iis建站维护,云服务器iis建站教程
  16. 日常工作记录:安卓运行时出现的Cause: Dex cannot parse version 52 byte code.问题
  17. BlueTooth: 蓝牙Profile的概念和常见种类
  18. andriod写计时器
  19. 吕本富:从平台经济到平台经济学
  20. 来与大家分享一些我们珍藏的、有力量的语句

热门文章

  1. 国外的电子商务开发情况
  2. vue data属性中的值绑定到dom上的一些问题
  3. 嵌入式开发环境搭建:开发板tftp下载环境搭建
  4. 前端mvc与mvvm
  5. SQL 一次插入多条记录
  6. 一本计划中的WPF图书目录
  7. 玩转SecureCRT 安装
  8. Selenium Grid的搭建方法
  9. linux新建脚本文件,linux shell脚本编程2—修改文件时间和创建新文件即touch命令的使用...
  10. python调用usb摄像头黑屏_ORB-SLAM2编译安装和USB摄像头例程运行