A skip list is a probabilistic data structure that allows for efficient searching, insertion, and deletion of nodes. Skip lists have a similar structure to linked lists, but with additional levels of nodes that "skip" over sections of the list.
Advantages of Skip Lists
One of the main advantages of skip lists is their simplicity. While balanced trees require complex algorithms to maintain their balance, skip lists use a straightforward probabilistic approach. This makes them easier to implement and understand. Additionally, skip lists have the same average-case time complexity as balanced trees, but with a lower constant factor. This means that skip lists can be faster in practice, especially for smaller data sets.
Where L1 is the number of nodes in the first layer and L2 is the number of nodes in the second layer. To minimize it, we let , since L2 is the n, so we want to L1 to be Sqrt(L2).
Searching in Skip Lists
Searching in a skip list is similar to searching in a linked list, but with the addition of multiple levels of nodes. Starting at the top level, we compare the value we are searching for to the value of the next node. If the value is bigger than the next node’s value, we move down to the next level and repeat the process. If the value is smaller than or equal to the next node’s value, we move to the next node on the current level and repeat the process. We continue this process until we find the node we are searching for, or determine that it does not exist in the list.
Insert in Skip Lists
To insert a node into a skip list, we first search for the correct position for the node. Once we have found the correct position, we create a new node and insert it into the list at that position. We then "promote" the node to higher levels with some probability, using a random number generator. The search operation in a skip list is similar to that of a binary search tree, but with the added benefit of multiple levels. This allows for a logarithmic search time complexity, which is the same as a balanced binary search tree.