Explore the c # Skip table (SkipList ),

Source: Internet
Author: User

Explore the c # Skip table (SkipList ),

Reading directory:

Basic Introduction

SkipList was proposed by William put forward in 1990. It is a data structure that can replace the Balance Tree. SkipList is relatively simple to implement. For example, SkipList can be easily implemented under a limited time condition, but it cannot implement B, red/Black, and AVL trees, considering how to delete a single B-tree, we need to consider a lot of details. Although SkipList is simple, its performance is very high. On average, the time complexity of inserting, deleting, and searching data is O (log (N )), the worst case is O (N), which is lower than the Balance Tree.
Because skipList is efficient and easy to maintain, SkipList is used for maintaining an ordered list in many big data systems. For example, the MemTable structure for storing data in the memory of LevelDB is implemented using SkipList, redis also uses SkipList In the Sorted Set data structure, and Lucene also uses SkipList to quickly search the inverted list.
SkipList relies on randomly generated numbers to maintain a balanced distribution of data on the tree. Therefore, SkipList is also a probabilistic data structure, and the previously introduced BoolFilter belong to a type C # Bloom filter (Bloom filter ).

Algorithm IDEA

For example, the landlord needs to go back to Yangjiang Yulan xiangyuan after finishing the street. If he wants to make a bus back from People's Square, he must pass through many stations:

Think about how miserable the journey is (finding the same problem with big data). It is much faster to take the subway, and then change to guanglan Road. This is the core idea of SkipList. Now the route is changed:


Because it can cross many unwanted sites at a time, it is much faster. If you can take a ride with a friend, it will become:

This figure is very close to the SkipList structure and idea.

Evolution steps

Let's get a general idea of what is going on and how to implement it. First, we forget the advanced concepts and structures such as trees and graphs, and return to the time when we first learned the linked list. Looking at the home road map above, we regard the bottom layer as a linked list, and each node (Station) Pointer Points to the next node (station ). Single ordered linked list:

According to the traditional method of operating the ordered linked list, if you need to find a piece of data, you need to traverse it in sequence. According to the train of thought, if you add a pointer to a part of the nodes that point to the back, if half of the nodes are added, You can traverse [n/2] more than once to find any node. Here we add more pointers to nodes 18, 23, 33, 40, and 47 to point them to the following nodes:

And so on, add 3 or 4 more pointers and point them to the far back node, which can improve the query efficiency. Three nodes:

If it is ideal, it is similar to binary search. The SkipList is determined by a random number (coin-dropping). When a node is inserted, the system randomly determines the number of pointers on the node to execute subsequent nodes. There are several node pointers behind the execution, that is, the layer. For example, 18 has three pointers pointing to the back, which are on the third layer, and 23 has two pointers on the second layer.

Implement detail search

The same layer of nodes is the same as the ordinary ordered linked list. The nodes are searched backward and returned. Otherwise, the nodes are searched backward at the next layer. For example, if you search for 35, It will compare 18 from the top-level search and return equal results. If the value is greater than 40, continue to the next layer. After the value is 1, 23, 33, and 40, continue to the next layer, comparison between 33 and 35 is returned correctly, otherwise it does not exist.

Update

Search for the value and update it:

        SkipListNode<TKey, TValue> position;        bool found = search(key, out position);        if(found)            position.value = value;
Insert

During insertion, if the value exists, it is updated, and there is no insert. For example, if you want to insert 29, You need to first find 27 and insert it to the back. If you get 3 after throwing a coin, add a pointer to the back node in turn.

Random Number

It is also called coin-dropping.

       Random generator = new Random();        int levels = 0;        while (generator.NextDouble() < 0.5&&levels<=maxlevel)            levels++;        return levels;

Delete is the same as insert. If it is found, adjust the pointer sequence and delete the node.

Summary

C # version of an implementation https://github.com/kencausey/SkipList

 

Explore C # series navigation

Related Article

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.