We have already discussed the idea of Skip list and how they work in Skip List | Set 1 (Introduction). In this article, we will be discussing how to insert an element in Skip list.
Deciding nodes level
Each element in the list is represented by a node, the level of the node is chosen randomly while insertion in the list. Level does not depend on the number of elements in the node. The level for node is decided by the following algorithm –
randomLevel() lvl := 1 //random() that returns a random value in [0...1) while random() < p and lvl < MaxLevel do lvl := lvl + 1 return lvl
MaxLevel is the upper bound on number of levels in the skip list. It can be determined as – . Above algorithm assure that random level will never be greater than MaxLevel. Here p is the fraction of the nodes with level i pointers also having level i+1 pointers and N is the number of nodes in the list.
Each node carries a key and a forward array carrying pointers to nodes of a different level. A level i node carries i forward pointers indexed through 0 to i.
Insertion in Skip List
We will start from highest level in the list and compare key of next node of the current node with the key to be inserted. Basic idea is If –
- Key of next node is less than key to be inserted then we keep on moving forward on the same level
- Key of next node is greater than the key to be inserted then we store the pointer to current node i at update[i] and move one level down and continue our search.
At the level 0, we will definitely find a position to insert given key. Following is the pseudo code for the insertion algorithm –
Insert(list, searchKey) local update[0...MaxLevel+1] x := list -> header for i := list -> level downto 0 do while x -> forward[i] -> key forward[i] update[i] := x x := x -> forward lvl := randomLevel() if lvl > list -> level then for i := list -> level + 1 to lvl do update[i] := list -> header list -> level := lvl x := makeNode(lvl, searchKey, value) for i := 0 to level do x -> forward[i] := update[i] -> forward[i] update[i] -> forward[i] := x
Here update[i] holds the pointer to node at level i from which we moved down to level i-1 and pointer of node left to insertion position at level 0. Consider this example where we want to insert key 17 –
Following is the code for insertion of key in Skip list –
Successfully Inserted key 3 Successfully Inserted key 6 Successfully Inserted key 7 Successfully Inserted key 9 Successfully Inserted key 12 Successfully Inserted key 19 Successfully Inserted key 17 Successfully Inserted key 26 Successfully Inserted key 21 Successfully Inserted key 25 *****Skip List***** Level 0: 3 6 7 9 12 17 19 21 25 26 Level 1: 3 6 12 17 25 Level 2: 6 12 17 25 Level 3: 12 17 25
Note: The level of nodes is decided randomly, so output may differ.
Time complexity (Average):
Time complexity (Worst):
In next article we will discuss searching and deletion in Skip List.
This article is contributed by Atul Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Skip List | Set 1 (Introduction)
- Skip List | Set 3 (Searching and Deletion)
- Doubly Circular Linked List | Set 1 (Introduction and Insertion)
- Doubly Linked List | Set 1 (Introduction and Insertion)
- Circular Singly Linked List | Insertion
- Insertion Sort for Singly Linked List
- Recursive insertion and traversal linked list
- Insertion in Unrolled Linked List
- Insertion at Specific Position in a Circular Doubly Linked List
- Insertion in a sorted circular linked list when a random pointer is given
- AVL Tree | Set 1 (Insertion)
- ScapeGoat Tree | Set 1 (Introduction and Insertion)
- Proto Van Emde Boas Tree | Set 3 | Insertion and isMember Query
- Van Emde Boas Tree | Set 2 | Insertion, Find, Minimum and Maximum Queries
- m-Way Search Tree | Set-2 | Insertion and Deletion
- Fibonacci Heap - Insertion and Union
- Optimal sequence for AVL tree insertion (without any rotations)
- Insertion in a Trie recursively
- Octree | Insertion and Searching
- Design a structure which supports insertion and first non-repeating element in O(1) time
Improved By : ManasChhabra2