当前位置:网站首页>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;
}
}边栏推荐
- 浅尝不辄止系列之试试腾讯云的TUIRoom(晚上有约,未完待续...)
- Splicing and splitting of integer ints
- Small guide for rapid formation of manipulator (12): inverse kinematics analysis
- Chapter 9 Yunji datacanvas company won the highest honor of the "fifth digital finance innovation competition"!
- 基于深度学习的目标检测的更新迭代总结(持续更新ing)
- 开发那些事儿:Go加C.free释放内存,编译报错是什么原因?
- [MySQL - Basic] transactions
- c语言如何判定是32位系统还是64位系统
- How to implement safety practice in software development stage
- OneSpin | 解决IC设计中的硬件木马和安全信任问题
猜你喜欢

测量楼的高度

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

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

Small guide for rapid formation of manipulator (11): standard nomenclature of coordinate system

Machine learning notes - explore object detection datasets using streamlit

写了个 Markdown 命令行小工具,希望能提高园友们发文的效率!

Small guide for rapid formation of manipulator (12): inverse kinematics analysis
Klocwork 代码静态分析工具

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

CodeSonar网络研讨会
随机推荐
Airiot helps the urban pipe gallery project, and smart IOT guards the lifeline of the city
Oracle 存儲過程之遍曆
c语言如何判定是32位系统还是64位系统
写一下跳表
When easygbs cascades, how to solve the streaming failure and screen jam caused by the restart of the superior platform?
大厂经典指针笔试题
EasyGBS级联时,上级平台重启导致推流失败、画面卡住该如何解决?
Lingyun going to sea | yidiantianxia & Huawei cloud: promoting the globalization of Chinese e-commerce enterprise brands
如何挑选基金产品?2022年7月份适合买什么基金?
Phoenix JDBC
Mrs offline data analysis: process OBS data through Flink job
Solve the problem that the executable file of /bin/sh container is not found
Don't fall behind! Simple and easy-to-use low code development to quickly build an intelligent management information system
实战:sqlserver 2008 扩展事件-XML转换为标准的table格式[通俗易懂]
MIT science and technology review article: AgI hype around Gato and other models may make people ignore the really important issues
School 1 of vulnhub
Update iteration summary of target detection based on deep learning (continuous update ing)
4G设备接入EasyGBS平台出现流量消耗异常,是什么原因?
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
I wrote a markdown command line gadget, hoping to improve the efficiency of sending documents by garden friends!