当前位置:网站首页>Write a jump table
Write a jump table
2022-07-07 20:24:00 【kyhoon】
class Skiplist {
// 1. Define the nodes in the jump table
class Node{
// value
int val;
// The right node and the lower node of the node
Node right, down;
public Node(int val, Node right, Node down){
this.val = val;
this.right = right;
this.down = down;
}
public Node(int val){
this(val, null, null);
}
}
//2. Define properties related to jump table
// Define the default value of the header node
final int DEFAULT_VALUE = -1;
// Define the default header node
final Node HEAD = new Node(DEFAULT_VALUE);
// The level corresponding to the skip table
int levels;
// The number of nodes in the linked list in the jump list
int length;
Node head;
// 3. initialization , The default level is 1, The length is 1,, Default header node
public Skiplist() {
this.levels = 1;
this.length = 1;
this.head = HEAD;
}
// 4. Define the previous node that gets the node corresponding to the search target value from a node ( Because there is no attribute between nodes that refers to the forward node , And operate the nodes in the skip table
// You need to start from the previous node of the target node , So we take the front node of the target node )
private Node get(int num, Node from){
// 1. Which node to search from
Node node = from;
// 2. The logic of the loop is to start from left to right , Then search from top to bottom
while(node!=null){
// 2.1 If the right node of the current node is not empty and the value of the right node is less than the target value , Then continue to search to the right
while(node.right!=null && node.right.val<num){
node = node.right;
}
// 2.2 Jump out of the loop when the right node is not empty , Then it is only possible that the value of the right node is greater than or equal to the target value
// If it's equal to , So it's found , Directly return to the previous node of the target node
Node right = node.right;
if(right!=null && right.val == num){
return node;
}
// 2.3 If not equal , Then continue to look down
node = node.down;
}
return null;
}
// 5. Query exists
public boolean search(int target) {
// Directly call the above query method , Start from the header to find , If the return is not empty , On behalf of finding
Node node = get(target, head);
return node != null;
}
// 6. Insert node , The general idea is to first find the location of the inserted node in the original linked list and then insert ,
// Then flip a coin to decide whether to build an index node
public void add(int num) {
// 6.1 Define a temporary array to store the front node of the inserted node at the position to be inserted in each layer
Node[] nodes = new Node[this.levels];
// 6.2 Define the subscript variable of the temporary array
int i = 0;
// 6.3 Define the start node of the search
Node node = head;
// 6.4 and get The principle of the method is the same
while(node!=null){
// 6.4.1 First from left to right
while(node.right!=null &&node.right.val < num){
node = node.right;
}
// 6.4.2 Out of the loop , in any case ( No matter the right node is empty , Or the value of the right node is greater than or equal to the target value ),
// At this time, this node of the current layer is the front node of the inserted node , So we record
nodes[i++] = node;
//6.4.3 Keep looking down
node = node.down;
}
// 6.5 Cycle is completed , At this time, the last node of the temporary array is the front node of the target node in the original linked list
Node pre = nodes[--i];
// 6.6 The node that defines the target value
Node newNode =new Node(num, pre.right, null);
// 6.7 Insert
pre.right = newNode;
// 6.8 Length plus one
this.length++;
// 6.9 Flip a coin to decide whether to build an index node
coinsFlip(nodes, newNode, i);
}
// The general logic of tossing a coin to decide whether to build an index node is : adopt 0,1 Whether the random number and level series are less than the benchmark value determines whether to build an index
// If you want to build , First of all, we have to see whether the current layer that needs to be indexed is within the original level ,
// If it is , Then insert , And continue to flip coins to decide whether to build the upper floor
// If not, then , Create a new header node , Access the new inode to create a new layer
private void coinsFlip(Node[] nodes, Node newNode, int i) {
// 1. Definition 0,1 Random number generation of
Random random = new Random();
int coins = random.nextInt(2);
// 2. Define the next node of the inode
Node downNode = newNode;
// 3. Flip a coin to decide whether to build an index
while(coins == 1 && this.levels<(this.length>>6)) {
// 3.1 When the coin tossed is 1 And the current level is less than the reference value , Create an index node
if (i > 0) {
//3.2 If it is currently required to insert an inode, it is within the current level , Then get
// Insert the front node of the inode
Node pre = nodes[--i];
// Define a new inode
Node newIndex = new Node(newNode.val, pre.right, downNode);
// Set the right node of the front node as the new inode
pre.right = newIndex;
// Set the new inode as the lower node of the new inode to be built in the next upper layer
downNode = newIndex;
// Keep tossing coins
coins = random.nextInt(2);
} else {
// If it has exceeded the current layer , Then create a new inode
Node newIndex = new Node(newNode.val, null, downNode);
// Create a new head node and set the right node as the new index node
Node newHead = new Node(DEFAULT_VALUE, newIndex, head);
// Point the head node of the jump table to the new head node
this.head = newHead;
// Number of levels plus one
this.levels++;
}
}
}
// 7. Delete node , The general idea is to find the front node of the target node of each layer to delete , And delete
public boolean erase(int num) {
// 7.1 Define the result variable , The default is false No node can be deleted
boolean result = false;
// 7.2 Find the front node of the target node from the head node
Node node = get(num, head);
// 7.3 If there is , Then delete , And delete the nodes of the next layer
while(node != null){
// 7.3.1 Get delete node
Node right = node.right;
// 7.3.2 Point the right node of the front node of the deleted node to the right node of the node to be deleted
node.right = right.right;
// 7.3.3 Point the right node of the deleted node to null , Trigger gc
right.right = null;
// 7.3.4 Assign the result value to true
result = true;
// 7.3.5 Continue to check the front node of the target node to be deleted in the next layer
node = get(num, node.down);
}
// 7.4 If the deletion is successful , Then the length is reduced by one
if(result){
this.length--;
}
return result;
}
}边栏推荐
- 最新版本的CodeSonar改进了功能安全性,支持MISRA,C ++解析和可视化
- Small guide for rapid formation of manipulator (11): standard nomenclature of coordinate system
- 深度学习模型压缩与加速技术(七):混合方式
- Force buckle 2319 Judge whether the matrix is an X matrix
- ERROR: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
- Spark 判断DF为空
- Network principle (1) - overview of basic principles
- 一. 基础概念
- 实战:sqlserver 2008 扩展事件-XML转换为标准的table格式[通俗易懂]
- 软件缺陷静态分析 CodeSonar 5.2 新版发布
猜你喜欢

Tensorflow2.x下如何运行1.x的代码

测量楼的高度

如何满足医疗设备对安全性和保密性的双重需求?

How to test CIS chip?

软件缺陷静态分析 CodeSonar 5.2 新版发布

ERROR: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your

【mysql篇-基础篇】事务

机械臂速成小指南(十二):逆运动学分析

Helix QAC 2020.2新版静态测试工具,最大限度扩展了标准合规性的覆盖范围

OneSpin | 解决IC设计中的硬件木马和安全信任问题
随机推荐
论文解读(ValidUtil)《Rethinking the Setting of Semi-supervised Learning on Graphs》
POJ 1742 Coins ( 单调队列解法 )「建议收藏」
怎样用Google APIs和Google的应用系统进行集成(1)—-Google APIs简介
I Basic concepts
I wrote a markdown command line gadget, hoping to improve the efficiency of sending documents by garden friends!
写了个 Markdown 命令行小工具,希望能提高园友们发文的效率!
Force buckle 1961 Check whether the string is an array prefix
How C language determines whether it is a 32-bit system or a 64 bit system
恢复持久卷上的备份数据
Is it safe to open a stock account at present? Can I open an account online directly.
复杂因子计算优化案例:深度不平衡、买卖压力指标、波动率计算
You want to kill a port process, but you can't find it in the service list. You can find this process and kill it through the command line to reduce restarting the computer and find the root cause of
School 1 of vulnhub
FTP steps for downloading files from Huawei CE switches
如何满足医疗设备对安全性和保密性的双重需求?
Force buckle 912 Sort array
2022如何评估与选择低代码开发平台?
Force buckle 1232 Dotted line
kubernetes之创建mysql8
guava多线程,futurecallback线程调用不平均