Skip table Skip List "Java implementation"

Source: Internet
Author: User

The principle of skip list

The LinkedList in Java is a common list structure that supports the random insertion and random deletion of O (1), but its lookup complexity is poor, O (n).

If we have an ordered list below, if we want to find a node with a value of 59, we need to find 7 times. How to improve query efficiency? The usual approach is to use dichotomy, but the LinkedList is also O (n) in random access time, so the naïve dichotomy does not apply. What about that?

We can add additional jumping nodes to the node as follows:

So we can query the jumping node, only need to find 3 times. As for Query 47, we first based on the Jumping node to query, so on node 22, its jumping pointer to 55, greater than 47, so we can know that 47 may be present in the node 22 and node 55, and then based on the normal pointer order to find. We need to look up 5 times.

As the nodes increase, our list structure becomes this way:

The density of the jumping node is half of the normal node, ideally, this structure will be one-fold higher than the original structure query performance. Is there any way to improve it? Yes, we can add a new layer of jumping node on this basis, and the density of this node is half of the first jumping node.

More Intuitive points:

Further limiting the jumping nodes in each layer is generated by the jumping node in its next layer, so our jumping table will eventually look like this.

We do not distinguish between each layer is the original node or jumping node, the bottom layer of the node is called the first layer node, the first node above the second layer of nodes, and then the third layer ... And so on

Such a structure, called a jumping table. Assuming that the number of nodes in each layer is half of the next layer, then the time complexity is O (logn).

Implementation scenarios

As mentioned above, the skip list is an ordered list with a hierarchical structure, and how should the nodes of each layer be generated?

We can use random methods when adding elements to determine how many layers of nodes this element has. This element is set to have only one node probability of 1/2, and only two node probability is 1/4, and only three nodes probability is 1/8, and so on. Then a random event is triggered, when an event with a probability of 1/2 occurs, the element has a layer of nodes, and a probability of 1/2 event occurs when the element has a two-layer node ... In addition, we limit a jump table to have a maximum number of layer limits.

Assuming a jump table has a maximum layer limit of 4, you can set an integer interval of [1, 2^ (4-1)], i.e. [1, 8]. Then take a random number of 1~8, when it falls in the [5, 8] interval There is a layer of nodes, fell in the [3, 4] interval when there are two layers of nodes, fell in the [2, 2] interval when there are three layers, fell on [1, 1] when there are four layers. Since we set the maximum number of layers for the jump table, the probability equation 1 = The last two items of the 1/8 + + + + ... + 1/2^n are the same.

Java implementation

Githup code, see Skiplist.java

Skip table Skip List "Java implementation"

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.