In previous article Skip List | Set 2 (Insertion) we discussed the structure of skip nodes and how to insert an element in the skip list. In this article we will discuss how to search and delete an element from skip list.
Searching an element in Skip list
Searching an element is very similar to approach for searching a spot for inserting an element in Skip list. The basic idea is if –
- Key of next node is less than search key 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 lowest level (0), if the element next to the rightmost element (update) has key equal to the search key, then we have found key otherwise failure.
Following is the pseudo code for searching element –
Search(list, searchKey) x := list -> header -- loop invariant: x -> key level downto 0 do while x -> forward[i] -> key forward[i] x := x -> forward if x -> key = searchKey then return x -> value else return failure
Consider this example where we want to search for key 17-
Deleting an element from the Skip list
Deletion of an element k is preceded by locating element in the Skip list using above mentioned search algorithm. Once the element is located, rearrangement of pointers is done to remove element form list just like we do in singly linked list. We start from lowest level and do rearrangement until element next to update[i] is not k.
After deletion of element there could be levels with no elements, so we will remove these levels as well by decrementing the level of Skip list. Following is the pseudo code for deletion –
Delete(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 if x -> key = searchKey then for i := 0 to list -> level do if update[i] -> forward[i] ≠ x then break update[i] -> forward[i] := x -> forward[i] free(x) while list -> level > 0 and list -> header -> forward[list -> level] = NIL do list -> level := list -> level – 1
Consider this example where we want to delete element 6 –
Here at level 3, there is no element (arrow in red) after deleting element 6. So we will decrement level of skip list by 1.
Following is the code for searching and deleting element from 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 17 19 21 26 Level 2: 17 19 21 Found key: 19 Successfully deleted key 19 *****Skip List***** Level 0: 3 6 7 9 12 17 21 25 26 Level 1: 3 17 21 26 Level 2: 17 21
Time complexity of both searching and deletion is same –
Time complexity (Average):
Time complexity (Worst):
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.
- Skip List | Set 1 (Introduction)
- Skip List | Set 2 (Insertion)
- Deletion from a Circular Linked List
- Doubly Circular Linked List | Set 2 (Deletion)
- Deletion at different positions in a Circular Linked List
- AVL Tree | Set 2 (Deletion)
- Ternary Search Tree (Deletion)
- Pattern Searching using a Trie of all Suffixes
- Pattern Searching using Suffix Tree
- Suffix Tree Application 2 - Searching All Patterns
- Length of longest palindrome list in a linked list using O(1) extra space
- Partitioning a linked list around a given value and If we don't care about making the elements of the list "stable"
- Rotate the sub-list of a linked list from position M to N to the right by K places
- Create new linked list from two given linked list with greater element at each node
- Difference between Singly linked list and Doubly linked list