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

相关文章

Spring Security常见问题及解决方案

《SpringSecurity常见问题及解决方案》SpringSecurity是Spring生态的安全框架,提供认证、授权及攻击防护,支持JWT、OAuth2集成,适用于保护Spring应用,需配置... 目录Spring Security 简介Spring Security 核心概念1. ​Securit

Python实现终端清屏的几种方式详解

《Python实现终端清屏的几种方式详解》在使用Python进行终端交互式编程时,我们经常需要清空当前终端屏幕的内容,本文为大家整理了几种常见的实现方法,有需要的小伙伴可以参考下... 目录方法一:使用 `os` 模块调用系统命令方法二:使用 `subprocess` 模块执行命令方法三:打印多个换行符模拟

SpringBoot+EasyPOI轻松实现Excel和Word导出PDF

《SpringBoot+EasyPOI轻松实现Excel和Word导出PDF》在企业级开发中,将Excel和Word文档导出为PDF是常见需求,本文将结合​​EasyPOI和​​Aspose系列工具实... 目录一、环境准备与依赖配置1.1 方案选型1.2 依赖配置(商业库方案)二、Excel 导出 PDF

Python实现MQTT通信的示例代码

《Python实现MQTT通信的示例代码》本文主要介绍了Python实现MQTT通信的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 安装paho-mqtt库‌2. 搭建MQTT代理服务器(Broker)‌‌3. pytho

SpringBoot改造MCP服务器的详细说明(StreamableHTTP 类型)

《SpringBoot改造MCP服务器的详细说明(StreamableHTTP类型)》本文介绍了SpringBoot如何实现MCPStreamableHTTP服务器,并且使用CherryStudio... 目录SpringBoot改造MCP服务器(StreamableHTTP)1 项目说明2 使用说明2.1

spring中的@MapperScan注解属性解析

《spring中的@MapperScan注解属性解析》@MapperScan是Spring集成MyBatis时自动扫描Mapper接口的注解,简化配置并支持多数据源,通过属性控制扫描路径和过滤条件,利... 目录一、核心功能与作用二、注解属性解析三、底层实现原理四、使用场景与最佳实践五、注意事项与常见问题六

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

Java中Arrays类和Collections类常用方法示例详解

《Java中Arrays类和Collections类常用方法示例详解》本文总结了Java中Arrays和Collections类的常用方法,涵盖数组填充、排序、搜索、复制、列表转换等操作,帮助开发者高... 目录Arrays.fill()相关用法Arrays.toString()Arrays.sort()A

Spring Boot Maven 插件如何构建可执行 JAR 的核心配置

《SpringBootMaven插件如何构建可执行JAR的核心配置》SpringBoot核心Maven插件,用于生成可执行JAR/WAR,内置服务器简化部署,支持热部署、多环境配置及依赖管理... 目录前言一、插件的核心功能与目标1.1 插件的定位1.2 插件的 Goals(目标)1.3 插件定位1.4 核

如何使用Lombok进行spring 注入

《如何使用Lombok进行spring注入》本文介绍如何用Lombok简化Spring注入,推荐优先使用setter注入,通过注解自动生成getter/setter及构造器,减少冗余代码,提升开发效... Lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter