Hop table skiplist

Source: Internet
Author: User

1. Let's have a chat about the author of the table.

2. Let's get down to the truth and jump table Introduction

3. Table jumping Data Storage Model

4. Jump tableCodeImplementation Analysis

5. Papers, code downloads and references

<1>. chat with the author about others and other things.  

The jump table was invented by William put. He published skip lists: A Probabilistic alternative to balanced trees in communications of the ACM June 1990, 33 (6) 668-676, in this paper, the data structure and insert/delete operations of the table are explained in detail.

William API is also findbug (not used before. This is a Java static code analysis tool that directly analyzes Java bytecode to identify many potential errors in Java bytecode .) One of the authors. It is now a professor at the University of Maryland, College Park (the University of Maryland, in Maryland, which ranks around 50 or 60. His research with his students deeply affects the implementation of memory pools in Java.

It's a computer genius!

<2>. Back-to-end, jump table Introduction  

This is the author of the jump table. The explanation described above is as follows:

Skip lists are a data structure that can be used in place of balanced trees.
Skip lists use probabilistic balancing rather than strictly enforced balancing
And as a result the algorithms for insertion and deletion in Skip lists are
Much simpler and significantly faster than equivalent algorithms
Balanced trees.

Table jumping is an alternative data structure of the Balance Tree, but it is different from the red and black trees. The implementation of table jumping's balance for the tree is based on a randomizationAlgorithmIn this way, it is relatively simple to insert or delete a table.

Next we will study the core idea of table jumping:

First, start from the linked list. If it is a simple linked list, we know that if we look for an element I in the linked list, we need to traverse the entire linked list once.

 

If the linked list is sorted and a pointer to the second node is stored in the node, you only need to traverse n/2 nodes when searching for a node.

 

This is basically the core idea of table jumping. In fact, it is also an algorithm that exchanges space for time. By adding a forward pointer to each node, this improves the search efficiency.

<3>. Data Storage Model for table jumping  

We define:

If a base point has K forward pointers, the node is a K-layer node.

The maxlevel of a hop table is the maximum number of layers of all nodes in the hop table.

The following shows a complete jump table:

 

How can we store the data structure in binary format? Through the above jump table, it is easy to design such a data structure:

Define each node type:

// Here is just a pointer typedef struct nodestructure * node;

Typedef struct nodestructure

{Keytype key; // Key value valuetype value; // Value // forward pointer array, pointing to an array of different sizes based on the number of layers of the node forward [1]; };

 

Each struct corresponds to each node in the figure. If a node is a layer-by-layer node (such as a node 7 or 12), the corresponding forward will point to an array containing only one element, and so on.

Define the table data type:

// Define the data type typedef struct liststructure {int level; /* Maximum level of the List (1 more than the number of levels in the list) */struct nodestructure * Header;/* pointer to header */

} * List;

The data type of the table to be jumped contains the necessary information to maintain the table to be jumped. level indicates the number of layers of the table to be jumped. the header is as follows:

 

Define auxiliary variables:

Nil variable in definition: node nil;

# Define maxnumberoflevels 16

# Define maxlevel (MaxNumberOfLevels-1)

Define auxiliary methods:

 

// Newnodeoflevel generates a nodestructure structure, and generates l node * array pointer # define newnodeoflevel (L) (node) malloc (sizeof (struct nodestructure) + (l) * sizeof (node *))

The basic data structure definition has been completed. Next, we will analyze an operation on the table.

<4>. Code Implementation Analysis for table jumping  

4.1 Initialization

The initialization process is very simple. It is only part of the Red Line area that is generated, that is, the basic structure of the table to be jumped:

 

List newlist () {list l; int I; // Apply for memory of the list type L = (list) malloc (sizeof (struct liststructure); // set the layer level of the table to be jumped, the initial layer is layer 0 (array starts from 0) L-> level = 0; // generate header part l-> header = newnodeoflevel (maxnumberoflevels ); // clear the forward array of the header for (I = 0; I <maxnumberoflevels; I ++) L-> header-> forward [I] = nil; Return (L );

};

4.2 Insert operation

Because the data structure of the table is ordered as a whole, you must first find the proper position when inserting the table, and then modify the pointer (similar to the operation in the linked list ), then update the level variable of the table.

 

Boolean insert (L, key, value) Register list l; Register keytype key; Register valuetype value; {register int K; // use the update array node update [maxnumberoflevels]; register node p, q; P = L-> header; k = L-> level; /******************* Step 1 ****************** * **/do { // Find the Insert Location While (q = p-> forward [K], Q-> key <key) P = Q; // Set the update Array Update [k] = P; } While (-- k> = 0 ); // Traverse each layer // The appropriate position has been found here, and the update array has // Fill in the element if (Q-> key = Key) {q-> value = value; Return (false ); }; // Randomly generate a number of layers K = randomlevel ();   If (k> L-> level ){ // If the number of new layers is larger than the number of layers in the table to be jumped, // increase the number of layers in the table to be jumped. K = ++ L-> level; // Point the newly added layer to L-> header in the update Array Update [k] = L-> header; }; /******************* 2-step ****************** ***/ // Generate number of layers of nodes q = newnodeoflevel (k); q-> key = key; q-> value = value; // Update the two pointer domains do { P = update [k]; Q-> forward [k] = p-> forward [k]; P-> forward [k] = Q; } While (-- k> = 0 ); // If Program Run here, the program has inserted the node return (true );

}

4.3 Delete A node

It is the same as insert. First, find the node to be deleted. If this node is found, you only need to update the pointer field. If the table level needs to be updated, update it.

 

Boolean Delete (L, key) register list l; register keytype key; {register int K, M; // generates an auxiliary array update node update [maxnumberoflevels]; register node P, q; P = L-> header; k = m = L-> level; // similar to the search section, the final update includes: // do { While (q = p-> forward [K], Q-> key <key) P = Q; Update [k] = P; } While (-- k> = 0 ); // If this node is found, if (Q-> key = Key ){ // Pointer operation For (k = 0; k <= M & (P = update [k])-> forward [k] = Q; k ++) // Here it is possible to modify the value of the L-> header-> forward array P-> forward [k] = Q-> forward [k]; // Release the actual memory Free (Q ); // If the node at the maximum layer is deleted, you need to re-maintain the table // Number of layers level While (L-> header-> forward [m] = nil & M> 0) M --; L-> level = m; Return (true ); } Else // The node is not found and no deletion is performed. Return (false );

}

4.4 search

The search operation is included in the insert and delete operations, which is relatively simple. For more information, seeSource code.

<5>. Papers, code downloads and references  

Skiplist

/Files/xuqiang/skiplists.rar

//--------------------------------------------------------------------------------

Add the jump Table C # implementation code pm

The data structure model shown above is obtained directly from the table jumping model. There is also a Data Structure Model:

The hop table node type. Each hop table type only stores nodes on the left and the following:

 

The operation code for this model is as follows:

1. The initialization has completed the following operations:

 

2. insert operation: this operation is similar to the insert operation described above. First, locate the insert position, generate an update array, then randomly generate a level, and then modify the pointer.

3. delete operation: similar to the delete operation described above, the node to be deleted is found. If the node cannot be found, an exception is thrown. If the node to be deleted is found, modify the pointer, release the memory of the deleted node.

Download Code:

/Files/xuqiang/skiplist_csharp.rar

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.