Skip List | Set 2 (Insertion)
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 the highest level in the list and compare the key of the next node of the current node with the key to be inserted. The 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 the 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. References
This article is contributed by Atul Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.