Skip List | Set 3 (Searching and Deletion)
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: 6 9 19 26 Level 2: 19 Found key: 19 Successfully deleted key 19 *****Skip List***** Level 0: 3 6 7 9 12 17 21 25 26 Level 1: 6 9 26
Time complexity of both searching and deletion is same – Time complexity Worst case:
- Access – O(n)
- Search – O(n)
- Insert – O(n)
- Space – O(nlogn)
- Delete – O(n)
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.