java二叉查找树的实现代码
作者:evasean 发布时间:2022-08-12 02:36:43
标签:java,二叉查找树
本文实例为大家分享了java二叉查找树的具体代码,供大家参考,具体内容如下
package 查找;
import edu.princeton.cs.algs4.Queue;
import edu.princeton.cs.algs4.StdOut;
public class BST<Key extends Comparable<Key>, Value> {
private class Node {
private Key key; // 键
private Value value;// 值
private Node left, right; // 指向子树的链接
private int n; // 以该节点为根的子树中的节点总数
public Node(Key key, Value val, int n) {
this.key = key;
this.value = val;
this.n = n;
}
}
private Node root;
public int size() {
return size(root);
}
private int size(Node x) {
if (x == null)
return 0;
else
return x.n;
}
/**
* 如果树是空的,则查找未命中 如果被查找的键小于根节点,则在左子树中继续查找 如果被查找的键大于根节点,则在右子树中继续查找
* 如果被查找的键和根节点的键相等,查找命中
*
* @param key
* @return
*/
public Value get(Key key) {
return get(root, key);
}
private Value get(Node x, Key key) {
if (x == null)
return null;
int cmp = key.compareTo(x.key);
if (cmp < 0)
return get(x.left, key);
else if (cmp > 0)
return get(x.right, key);
else
return x.value;
}
/**
* 二叉查找树的一个很重要的特性就是插入的实现难度和查找差不多。 当查找到一个不存在与树中的节点(null)时,new 新节点,并将上一路径指向该节点
*
* @param key
* @param val
*/
public void put(Key key, Value val) {
root = put(root, key, val);
}
private Node put(Node x, Key key, Value val) {
if (x == null)
return new Node(key, val, 1);
int cmp = key.compareTo(x.key);
if (cmp < 0)
x.left = put(x.left, key, val);
else if (cmp > 0)
x.right = put(x.right, key, val);
else
x.value = val;
x.n = size(x.left) + size(x.right); // 要及时更新节点的子树数量
return x;
}
public Key min() {
return min(root).key;
}
private Node min(Node x) {
if (x.left == null)
return x;
return min(x.left);
}
public Key max() {
return max(root).key;
}
private Node max(Node x) {
if (x.right == null)
return x;
return min(x.right);
}
/**
* 向下取整:找出小于等于该键的最大键
*
* @param key
* @return
*/
public Key floor(Key key) {
Node x = floor(root, key);
if (x == null)
return null;
else
return x.key;
}
/**
* 如果给定的键key小于二叉查找树的根节点的键,那么小于等于key的最大键一定出现在根节点的左子树中
* 如果给定的键key大于二叉查找树的根节点,那么只有当根节点右子树中存在大于等于key的节点时,
* 小于等于key的最大键才会出现在右子树中,否则根节点就是小于等于key的最大键
*
* @param x
* @param key
* @return
*/
private Node floor(Node x, Key key) {
if (x == null)
return null;
int cmp = key.compareTo(x.key);
if (cmp == 0)
return x;
else if (cmp < 0)
return floor(x.left, key);
else {
Node t = floor(x.right, key);
if (t == null)
return x;
else
return t;
}
}
/**
* 向上取整:找出大于等于该键的最小键
*
* @param key
* @return
*/
public Key ceiling(Key key) {
Node x = ceiling(root, key);
if (x == null)
return null;
else
return x.key;
}
/**
* 如果给定的键key大于二叉查找树的根节点的键,那么大于等于key的最小键一定出现在根节点的右子树中
* 如果给定的键key小于二叉查找树的根节点,那么只有当根节点左子树中存在大于等于key的节点时,
* 大于等于key的最小键才会出现在左子树中,否则根节点就是大于等于key的最小键
*
* @param x
* @param key
* @return
*/
private Node ceiling(Node x, Key key) {
if (x == null)
return null;
int cmp = key.compareTo(x.key);
if (cmp == 0)
return x;
else if (cmp > 0) {
return ceiling(x.right, key);
} else {
Node t = floor(x.left, key);
if (t == null)
return x;
else
return t;
}
}
/**
* 选择排名为k的节点
*
* @param k
* @return
*/
public Key select(int k) {
return select(root, k).key;
}
private Node select(Node x, int k) {
if (x == null)
return null;
int t = size(x.left);
if (t > k)
return select(x.left, k);
else if (t < k)
return select(x.right, k - t - 1);// 根节点也要排除掉
else
return x;
}
/**
* 查找给定键值的排名
*
* @param key
* @return
*/
public int rank(Key key) {
return rank(key, root);
}
private int rank(Key key, Node x) {
if (x == null)
return 0;
int cmp = key.compareTo(x.key);
if (cmp < 0)
return rank(key, x.left);
else if (cmp > 0)
return 1 + size(x.left) + rank(key, x.right);
else
return size(x.left);
}
/**
* 删除最小键值对
*/
public void deleteMin(){
root = deleteMin(root);
}
/**
* 不断深入根节点的左子树直到遇见一个空链接,然后将指向该节点的链接指向该结点的右子树
* 此时已经没有任何链接指向要被删除的结点,因此它会被垃圾收集器清理掉
* @param x
* @return
*/
private Node deleteMin(Node x){
if(x.left == null) return x.right;
x.left = deleteMin(x.left);
x.n = size(x.left)+size(x.right) + 1;
return x;
}
public void deleteMax(){
root = deleteMax(root);
}
private Node deleteMax(Node x){
if(x.right == null ) return x.left;
x.right = deleteMax(x.right);
x.n = size(x.left)+size(x.right) + 1;
return x;
}
public void delete(Key key){
root = delete(root,key);
}
private Node delete(Node x, Key key){
if(x == null) return null;
int cmp = key.compareTo(x.key);
if(cmp < 0) x.left = delete(x.left,key);
else if(cmp > 0) x.right = delete(x.right,key);
else{
if(x.right == null) return x.left;
if(x.left == null ) return x.right;
/**
* 如果被删除节点有两个子树,将被删除节点暂记为t
* 从t的右子树中选取最小的节点x,将这个节点x的左子树设为t的左子树
* 这个节点x的右子树设为t的右子树中删除了最小节点的子树,这样就成功替换了t的位置
*/
Node t = x;
x = min(t.right);
x.left = t.left;
x.right = deleteMin(t.right);
}
x.n = size(x.left) + size(x.right) +1;
return x;
}
public void print(){
print(root);
}
private void print(Node x){
if(x == null ) return;
print(x.left);
StdOut.println(x.key);
print(x.right);
}
public Iterable<Key> keys(){
return keys(min(),max());
}
public Iterable<Key> keys(Key lo, Key hi){
Queue<Key> queue = new Queue<Key>();
keys(root, queue, lo, hi);
return queue;
}
private void keys(Node x, Queue<Key> queue, Key lo, Key hi){
if(x == null) return;
int cmplo = lo.compareTo(x.key);
int cmphi = lo.compareTo(x.key);
if(cmplo < 0 ) keys(x.left,queue,lo,hi);
if(cmplo <= 0 && cmphi >= 0) queue.enqueue(x.key);
if(cmphi > 0 ) keys(x.right,queue,lo,hi);
}
}
来源:http://www.cnblogs.com/evasean/p/7327278.html


猜你喜欢
- 有时您可能想限制可以在参数化类型中用作类型参数的类型。 例如,对数字进行操作的方法可能只希望接受Number或其子类的实例。 这就是有界类型
- 这篇文章主要讲述服务追踪组件zipkin,Spring Cloud Sleuth集成了zipkin组件。一、简介Add sleuth to
- 这里的问题:当我点击确定按钮,也就是 AlertDialog 里的 PositiveButton 的时候,我们需要判断用户是输入是否符合我们
- 本文实例分析了C#中var关键字用法。分享给大家供大家参考。具体方法如下:C#关键字是伴随着.NET 3.5以后,伴随着匿名函数、LINQ而
- 继承JpaRepository,找不到findOne()方法问题:以前一直使用findOne(ID id);,这次用SpringBoot构建
- 微服务feign调用添加token1.一般情况是这么配置的具体的怎么调用就不说了 如下配置,就可以在请求头中添加需要的请求头信息。packa
- 本文实例讲述了C#针对xml文件转化Dictionary的方法。分享给大家供大家参考。具体实现方法如下:下面是xml文件:<?xml
- 什么是jdkjdk是什么呢?jdk的是java development kit的缩写,意思是java程序开发的工具包。也可以说jdk是jav
- 教你一步一步用 java 设计生成二维码在物联网的时代,二维码是个很重要的东西了,现在无论什么东西都要搞个二维码标志,唯恐落伍,就差人没有用
- 问题描述我在接受 mq 消息的时候,需要做一个重试次数限制,如果超过 maxNum 就发邮件告警,不再重试。所以我需要对 consumer
- 本文实例为大家分享了Unity封装延时调用定时器的具体代码,供大家参考,具体内容如下封装一个延时调用定时器类using System.Col
- 写在前面:今天用保存QQ账号和密码的实战演练,带大家掌握Android存储中最基本的文件存储方式文件存储是Android中最基本的一种数据存
- 本文实例为大家分享了C#简单聊天室雏形的具体代码,供大家参考,具体内容如下程序使用的控制台的黑窗口模拟程序,第一次涉及网络编程,写出来方便以
- 在Unity中,一般的方法都是顺序执行的,一般的方法也都是在一帧中执行完毕的,当我们所写的方法需要耗费一定时间时,便会出现帧率下降,画面卡顿
- 为什么需要网关呢?我们知道我们要进入一个服务本身,很明显我们没有特别好的办法,直接输入IP地址+端口号,我们知道这样的做法很糟糕的,这样的做
- 1 读取操作系统和CLR的版本OperatingSystem os = System.Environment.OSVersion; Cons
- 一、首先看下Android开发用到的sdk目录:build-tools保存着一些Android平台相关通用工具,比如adb、和aapt、ai
- 这两天因为要做一个随机的地图生成系统,所以一直在研究随机迷宫生成算法,好吧,算是有一点小小的成果。随机迷宫生成我自己的理解简而言之分为以下几
- java 中设计模式(值对象)的实例详解应用场景:在Java开发时,需要来回交换大量的数据,比如要为方法传入参数,也要获取方法的返回值,该如
- 前面文章介绍了Android利用麦克风采集并显示模拟信号的实现方法,这种采集手段适用于无IO控制、单纯读取信号的情况。如果传感器本身需要包含