Skip lists are linked lists that allow you to skip to the correct node. The performance bottleneck inherent in a sequential scan is avoided, while insertion and deletion remain relatively efficient.
Theory
The indexing scheme employed in skip lists is similar in nature to the method used to lookup names in an address book. To lookup a name, you index to the tab representing the first character of the desired entry. In Figure 3-8, for example, the top-most list represents a simple
# abe art ben bob cal cat dan don
# abe art ben bob cal cat dan don
# abe art ben bob cal cat dan don
0
0
1
0
1
2
Figure 3-8: Skip List Construction
The indexing scheme may be extended as shown in the bottom figure, where we now have an index to the index. To locate an item, level-2 pointers are traversed until the correct segment of the list is identified. Subsequently, level-1 and level-0 pointers are traversed.
easily resolved using a probabilistic technique. A random number generator is used to toss a computer coin. When inserting a new node, the coin is tossed to determine if it should be
level-1. If you win, the coin is tossed again to determine if the node should be level-2. Another
with O(n) search time. However, if sufficient levels are implemented, the skip list may be
viewed as a tree with the root at the highest level, and search time is O(lg n).
Implementation
Source for the skip list algorithm may be found in file skl.c. Typedef T and comparison
To initialize, initList is called. The list header is allocated and initialized. To indicate an empty list, all levels are set to point to the header. Function insertNode allocates a new node,searches for the correct insertion point, and inserts it in the list. While searching, the update
array maintains pointers to the upper-level nodes encountered. This information is subsequently used to establish correct links for the newly inserted node. The newLevel is determined using a random number generator, and the node allocated. The forward links are then established using
No comments:
Post a Comment