Java实现跳表

2024-01-19 22:38
文章标签 java 实现 跳表

本文主要是介绍Java实现跳表,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

简介

链表是一种基本的数据结构,而跳表是一种特殊的链表。跳表的每一个节点都有四个指针,上、下、左、右。当进行数据查询时,可以从最顶层开始查找,并可以向下移动,从而提高查询效率。

基本结构

在这里插入图片描述

查找数的基本步骤

  1. 从最左上角的head开始,假如当前数等于目标数,直接返回;假如当前数右指针的数小于目标数,向右移动;假如当前数右指针的数大于目标数,向下移动。
  2. 当down指针为null时,没有找到。

Java节点结构

private class Node<T> {//score不能为负数long score;T value;Node up, down, left, right;public Node(T value) {this.score = -1;this.value = value;}public Node(long score, T value) {this.score = score;this.value = value;}@Overridepublic String toString() {return "Node{" +"score=" + score +", value=" + value +", up=" + up +", down=" + down +", left=" + left +", right=" + right +'}';}
}

Java实现跳表代码

private class SkipList<T> {// 最上层链表的头指针private Node<T> head;// 最上层聊表的尾指针private Node<T> tail;// 跳表的层数private int level;// 插入链表元素的个数private int size;// 用来生成随机数private Random random;public SkipList() {this.head = new Node(Long.MIN_VALUE, null);this.tail = new Node(Long.MAX_VALUE, null);this.level = 1;this.size = 0;this.random = new Random();this.head.right = this.tail;this.tail.left = this.head;}//判断跳表中是否有指定score的节点public boolean contain(long score) {Node help = head;while (help != null) {if (help.score == score) {return true;} else if (help.right.score > score) {help = help.down;} else if (help.right.score <= score) {help = head.right;}}return false;}//返回指定score的元素public Node find(long score) {Node help = head;while (help != null) {if (help.score == score) {return help;} else if (help.right.score > score) {help = help.down;} else if (help.right.score <= score) {help = head.right;}}return null;}//在最底层,找到指定score节点的前面一个节点,public Node findPreNode(long score) {//拿到最底层的head指针,从头开始判断最底部的链表,效率不行//Node help = headToButtom();Node help = head;//开始查询指定score节点的前面一个节点。如果链表中有指定score的节点,则返回相同节点的前面一个节点while (true) {if (score <= help.right.score) {if (help.down == null) return help;else help = help.down;} else {help = help.right;}}}//返回最底层的head指针private Node headToButtom() {Node help = head;while (help.down != null) {help = help.down;}return help;}//判断跳表是否为空public boolean isEmpty() {return size != 0;}//打印最底层的所有节点public void printAll() {Node help = headToButtom();while (help != null) {System.out.println(help);help = help.right;}}//插入节点public void insert(long score, T value) {//找到目标位置的前一个节点Node pre = findPreNode(score);//判断后面节点是否是和要插入节点一样的scoreif (pre.right.score == score) {pre = pre.right;while (pre != null) {pre.value = value;pre = pre.up;}return;}//插入节点Node target = new Node(score, value);target.left = pre;target.right = pre.right;pre.right.left = target;pre.right = target;//当前所属的层级int currLevel = 1;//随机往上沿升while (random.nextDouble() > 0.5) {currLevel++;if (currLevel <= level) {//不用再最上层生成一个新的链表Node upNode = new Node(score, value);Node right = target.right;while (right.up == null) {right = right.right;}right = right.up;Node left = target.left;while (left.up == null) {left = left.left;}left = left.up;upNode.left = left;left.right = upNode;upNode.right = right;right.left = upNode;target = upNode;} else {//需要在最上方生成一个新的链表this.level++;Node upNode = new Node(score, value);Node upHead = new Node(Long.MIN_VALUE, null);Node upTail = new Node(Long.MAX_VALUE, null);upHead.right = upNode;upNode.left = upHead;upTail.left = upNode;upNode.right = upTail;target = upNode;upHead.down = this.head;this.head.up = upHead;this.head = upHead;upTail.down = this.tail;this.tail.up = upTail;this.tail = upTail;}}}//通过分值移除某一个节点public boolean remove(long score) {//先找到最底层的节点Node target = find(score);if (target == null) {return false;} else {while (target != null) {target.left.right = target.right;target.right.left = target.left;target = target.up;}return true;}}//移除某一个节点public void remove(Node node) {while (node != null) {node.left.right = node.right;node.right.left = node.left;node = node.up;}}//范围删除,包括startScore的节点public void rangeRemove(long startScore) {//找到目标位置的前一个节点Node pre = findPreNode(startScore);while (pre.right.score < Long.MAX_VALUE) {remove(pre.right);}}//范围删除,包括startScore的节点,包括endScore的节点public void rangeRemove(long startScore, long endScore) {//找到目标位置的前一个节点Node pre = findPreNode(startScore);while (pre.right.score <= endScore) {remove(pre.right);}}//包括startScore之后的节点数目public int getNodeCount(long startScore) {int result = 0;//找到目标位置的前一个节点Node pre = findPreNode(startScore);while (pre.right.score < Long.MAX_VALUE) {result++;pre = pre.right;}return result;}//获取包括startScore的节点,包括endScore的节点的节点数目public int getNodeCount(long startScore, long endScore) {int result = 0;//找到目标位置的前一个节点Node pre = findPreNode(startScore);while (pre.right.score <= endScore) {result++;pre = pre.right;}return result;}private class Node<T> {//score不能为负数long score;T value;Node up, down, left, right;public Node(T value) {this.score = -1;this.value = value;}public Node(long score, T value) {this.score = score;this.value = value;}@Overridepublic String toString() {return "Node{" +"score=" + score +", value=" + value +", up=" + up +", down=" + down +", left=" + left +", right=" + right +'}';}}}

这篇关于Java实现跳表的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/623922

相关文章

Java NoClassDefFoundError运行时错误分析解决

《JavaNoClassDefFoundError运行时错误分析解决》在Java开发中,NoClassDefFoundError是一种常见的运行时错误,它通常表明Java虚拟机在尝试加载一个类时未能... 目录前言一、问题分析二、报错原因三、解决思路检查类路径配置检查依赖库检查类文件调试类加载器问题四、常见

Java注解之超越Javadoc的元数据利器详解

《Java注解之超越Javadoc的元数据利器详解》本文将深入探讨Java注解的定义、类型、内置注解、自定义注解、保留策略、实际应用场景及最佳实践,无论是初学者还是资深开发者,都能通过本文了解如何利用... 目录什么是注解?注解的类型内置注编程解自定义注解注解的保留策略实际用例最佳实践总结在 Java 编程

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

Java 实用工具类Spring 的 AnnotationUtils详解

《Java实用工具类Spring的AnnotationUtils详解》Spring框架提供了一个强大的注解工具类org.springframework.core.annotation.Annot... 目录前言一、AnnotationUtils 的常用方法二、常见应用场景三、与 JDK 原生注解 API 的

Java controller接口出入参时间序列化转换操作方法(两种)

《Javacontroller接口出入参时间序列化转换操作方法(两种)》:本文主要介绍Javacontroller接口出入参时间序列化转换操作方法,本文给大家列举两种简单方法,感兴趣的朋友一起看... 目录方式一、使用注解方式二、统一配置场景:在controller编写的接口,在前后端交互过程中一般都会涉及

Java中的StringBuilder之如何高效构建字符串

《Java中的StringBuilder之如何高效构建字符串》本文将深入浅出地介绍StringBuilder的使用方法、性能优势以及相关字符串处理技术,结合代码示例帮助读者更好地理解和应用,希望对大家... 目录关键点什么是 StringBuilder?为什么需要 StringBuilder?如何使用 St

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

Java并发编程之如何优雅关闭钩子Shutdown Hook

《Java并发编程之如何优雅关闭钩子ShutdownHook》这篇文章主要为大家详细介绍了Java如何实现优雅关闭钩子ShutdownHook,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起... 目录关闭钩子简介关闭钩子应用场景数据库连接实战演示使用关闭钩子的注意事项开源框架中的关闭钩子机制1.

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句