One of the basic data structures of the storage system: skiplist)

Source: Internet
Author: User

In the next series of articles, we will introduce a series of data structures applied to storage and IO subsystems. These data structures are highly correlated, and we hope that we can clearly introduce them in different categories without degrading their mission. This article is the first section to introduce a simple and useful data structure: skiplist)

 

Before discussing the table jumping, let's first describe the core idea of the table jumping.

The Skip List is an ordered linear linked list. It is usually necessary to traverse the Linear Linked List. Therefore, binary search cannot be used as a fast method (imagine the complexity of locating intermediate elements in the linked list ). To increase the search rate, we can scatter these linear linked lists and organize them into a tree structure. Such a tree is called a search tree. The search tree, especially the balanced search tree, has the lowest cost. Therefore, the balanced binary search tree is the best data structure for searching in memory. As an implementation of the balanced binary search tree, the red/black tree is often used in this scenario.

However, the balance tree must be balanced. After a series of Tree insertion and deletion, the tree is no longer balanced. In this case, you need to adjust the balanced algorithm. The complexity of the Red-black tree is reflected here. I believe that all the children's shoes that have written the red-black tree will still remember it.

At this time, the hop table was born on a sunny day. It supports fast retrieval without complex balancing operations. Due to its simplicity, it often has better performance than the red/black tree.

 

Is a jump table, in the figure (a) is a normal ordered list, and (B) to (e) is a jump table of different levels.

Definition:

For a node, if it hasKPointer, it is calledLevel KNode.

The maximum level of a table to be jumped is the level of the largest node in the list. For an empty list, level is 1.

Inference:

If every second (2I) A node has a pointer pointing to the backward number (2I) Node, instead of pointing to the following node, then all the nodes basically meet the following distribution: 50% of the nodes are at level 1, of the nodes are at Level 2, 12.5% of nodes are at level 3.

If the level of a node is randomly selected and the random distribution complies with the previous inference, then the level K node's I pointer only needs to point to the next level greater than or equal to I to construct the table as shown in, do not need to strictly point to the II-1 node.

Algorithm:

1> Initialization

Only the first list is created. The leve value of this list is 1.

2> Search

A) Search for the largest element that is smaller than the key to be retrieved from the maximum level list.

B) if it cannot be found, reduce a level to continue searching.

C) if it cannot be found at level 1, it indicates that the element to be searched must be located at the next node of the stopped position.

Search(list, key){    x = list->header    for (i = list.level; i >= 1; i--)    {        while (x->forward[i]->key < key)           x = x->forward[i]    }    x = x->forward[1]   if (x->key == key) return x->vlaue   else return failure}

3> insert

A) check whether the key to be inserted exists. If so, update the value.

B) if it does not exist, insert it. It should be noted that in the search process, we should use an array update [1... maxlevel] To save the nodes in each level that are searched for and stopped. These nodes are used to point to the newly added nodes.

C) generate a new node and randomly specify the level of the node. If the random level is larger than the current maximum level, expand the update array to the new maxlevel element and set the new update element to the list header.

D) from 1 to maxlevel, set the forward data of the new node to update [I... and update [I... set forward [I] of maxlevel] to X.

 1 insert(list, key, value){ 2   local update[1...MaxLevel] 3   x = list->header 4   for i = list->level downto 1 do 5     while x->forward[i]->key < key 6       x = x->forward[i] 7    update[i] = x 8   if x->key == key then x->value = value; 9   else10     lvl = randomLevel()11     if lvl > list->level then12       for i = list->level + 1 to lvl 13         update[i] = list->header14       list->level = lvl15     x = MakeNode(lvl, key, value)16     for i = 1 to level 17      x->forward[i] = update[i]->forward[i];18      update[i]->forward[i] = x;   19 } 

> Select the level of the new node.

The level of the new node is completely generated by a random function:

1 randomLevel()2     lvl = 13     while random() < p and lvl < MaxLevel do4       lvl = lvl + 15     return lvl

 

Complexity Analysis:

The search complexity analysis method is very simple. The conclusion is a very definite O (logn), and the rest of the comparison is a constant, as shown in:

As you can see, the table has a great advantage in constant terms.

 

-- Reference --

[1] William put, 1990, communications of the ACM 33.6, skip lists: A Probabilistic alternative to balanced trees.

One of the basic data structures of the storage system: skiplist)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.