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

相关文章

Vue中动态权限到按钮的完整实现方案详解

《Vue中动态权限到按钮的完整实现方案详解》这篇文章主要为大家详细介绍了Vue如何在现有方案的基础上加入对路由的增、删、改、查权限控制,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、数据库设计扩展1.1 修改路由表(routes)1.2 修改角色与路由权限表(role_routes)二、后端接口设计

C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)

《C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)》本文主要介绍了C#集成DeepSeek模型实现AI私有化的方法,包括搭建基础环境,如安装Ollama和下载DeepS... 目录前言搭建基础环境1、安装 Ollama2、下载 DeepSeek R1 模型客户端 ChatBo

Spring Cloud Hystrix原理与注意事项小结

《SpringCloudHystrix原理与注意事项小结》本文介绍了Hystrix的基本概念、工作原理以及其在实际开发中的应用方式,通过对Hystrix的深入学习,开发者可以在分布式系统中实现精细... 目录一、Spring Cloud Hystrix概述和设计目标(一)Spring Cloud Hystr

Qt实现发送HTTP请求的示例详解

《Qt实现发送HTTP请求的示例详解》这篇文章主要为大家详细介绍了如何通过Qt实现发送HTTP请求,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、添加network模块2、包含改头文件3、创建网络访问管理器4、创建接口5、创建网络请求对象6、创建一个回复对

C++实现回文串判断的两种高效方法

《C++实现回文串判断的两种高效方法》文章介绍了两种判断回文串的方法:解法一通过创建新字符串来处理,解法二在原字符串上直接筛选判断,两种方法都使用了双指针法,文中通过代码示例讲解的非常详细,需要的朋友... 目录一、问题描述示例二、解法一:将字母数字连接到新的 string思路代码实现代码解释复杂度分析三、

grom设置全局日志实现执行并打印sql语句

《grom设置全局日志实现执行并打印sql语句》本文主要介绍了grom设置全局日志实现执行并打印sql语句,包括设置日志级别、实现自定义Logger接口以及如何使用GORM的默认logger,通过这些... 目录gorm中的自定义日志gorm中日志的其他操作日志级别Debug自定义 Loggergorm中的

Spring Boot整合消息队列RabbitMQ的实现示例

《SpringBoot整合消息队列RabbitMQ的实现示例》本文主要介绍了SpringBoot整合消息队列RabbitMQ的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目录RabbitMQ 简介与安装1. RabbitMQ 简介2. RabbitMQ 安装Spring

Gin框架中的GET和POST表单处理的实现

《Gin框架中的GET和POST表单处理的实现》Gin框架提供了简单而强大的机制来处理GET和POST表单提交的数据,通过c.Query、c.PostForm、c.Bind和c.Request.For... 目录一、GET表单处理二、POST表单处理1. 使用c.PostForm获取表单字段:2. 绑定到结

springMVC返回Http响应的实现

《springMVC返回Http响应的实现》本文主要介绍了在SpringBoot中使用@Controller、@ResponseBody和@RestController注解进行HTTP响应返回的方法,... 目录一、返回页面二、@Controller和@ResponseBody与RestController

JAVA集成本地部署的DeepSeek的图文教程

《JAVA集成本地部署的DeepSeek的图文教程》本文主要介绍了JAVA集成本地部署的DeepSeek的图文教程,包含配置环境变量及下载DeepSeek-R1模型并启动,具有一定的参考价值,感兴趣的... 目录一、下载部署DeepSeek1.下载ollama2.下载DeepSeek-R1模型并启动 二、J