Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Skip Lists

What is a Skip List


• A skip list for a set S of distinct (key, element) items is a series of
lists
S0, S1 , … , Sh such that
– Each list Si contains the special keys +¥ and -¥
– List S0 contains the keys of S in non-decreasing order
– Each list is a subsequence of the previous one, i.e.,
S0 Ê S1 Ê … Ê Sh
– List Sh contains only the two special keys
• Skip lists are one way to implement the dictionary ADT

S3 -¥ +¥
S2 -¥ 31 +¥
S1 -¥ 23 31 34 64 +¥
S0 -¥ 12 23 26 31 34 44 56 64 78 +¥
Implementation
• We can implement a skip list with
quad-nodes
• A quad-node stores: quad-node
– item
– link to the node before
– link to the node after x
– link to the node below
• Also, we define special keys
PLUS_INF and MINUS_INF, and
we modify the key comparator to
handle them
Search
• We search for a key x in a a skip list as follows:
– We start at the first position of the top list
– At the current position p, we compare x with y ¬ key(after(p))
x = y: we return element(after(p))
x > y: we “scan forward”
x < y: we “drop down”
– If we try to drop down past the bottom list, we return
NO_SUCH_KEY
• Example: search for 78

S3 -¥ +¥
S2 -¥ 31 +¥
S1 -¥ 23 31 34 64 +¥
S0 -¥ 12 23 26 31 34 44 56 64 78 +¥
Exercise: Search
• We search for a key x in a a skip list as follows:
– We start at the first position of the top list
– At the current position p, we compare x with y ¬ key(after(p))
x = y: we return element(after(p))
x > y: we “scan forward”
x < y: we “drop down”
– If we try to drop down past the bottom list, we return
NO_SUCH_KEY
• Ex 1: search for 64: list the (Si, node) pairs visited and the return value
• Ex 2: search for 27: list the (Si, node) pairs visited and the return value

S3 -¥ +¥
S2 -¥ 31 +¥
S1 -¥ 23 31 34 64 +¥
S0 -¥ 12 23 26 31 34 44 56 64 78 +¥
Insertion
• To insert an item (x, o) into a skip list, we use a randomized algorithm:
– We repeatedly toss a coin until we get tails, and we denote with i the
number of times the coin came up heads
– If i ³ h, we add to the skip list new lists Sh+1, … , Si +1, each containing
only the two special keys
– We search for x in the skip list and find the positions p0, p1 , …, pi of
the items with largest key less than x in each list S0, S1, … , Si
– For j ¬ 0, …, i, we insert item (x, o) into list Sj after position pj
• Example: insert key 15, with i = 2
S3 -¥ +¥
p2
S2 -¥ +¥ S2 -¥ 15 +¥
p1
S1 -¥ 23 +¥ S1 -¥ 15 23 +¥
p0
S0 -¥ 10 23 36 +¥ S0 -¥ 10 15 23 36 +¥
Deletion
• To remove an item with key x from a skip list, we proceed as
follows:
– We search for x in the skip list and find the positions p0, p1 ,
…, pi of the items with key x, where position pj is in list Sj
– We remove positions p0, p1 , …, pi from the lists S0, S1, … , Si
– We remove all but one list containing only the two special keys
• Example: remove key 34

S3 -¥ +¥
p2
S2 -¥ 34 +¥ S2 -¥ +¥
p1
S1 -¥ 23 34 +¥ S1 -¥ 23 +¥
p0
S0 -¥ 12 23 34 45 +¥ S0 -¥ 12 23 45 +¥
Randomized Algorithms
• Through probabilistic analysis we
• A randomized algorithm controls can derive the expected running
its execution through random
time of a randomized algorithm
selection (e.g., coin tosses)
• We make the following assumptions
• It contains statements like:
in the analysis:
b ¬ randomBit()
– the coins are unbiased
if b = 0
– the coin tosses are independent
do A …
• The worst-case running time of a
else { b = 1}
randomized algorithm is often large
do B … but has very low probability (e.g., it
• Its running time depends on the occurs when all the coin tosses give
outcomes of the coin tosses “heads”)
• We use a randomized algorithm to
insert items into a skip list to insert
in expected O(log n)-time
• When randomization is used in
data structures they are referred to
as probabilistic data structures
Trie
Trie
• A trie (derived from retrieval) is a multiway tree
data structure used for storing strings over an
alphabet.
• It is used to store a large amount of strings.
• The pattern matching can be done efficiently
using tries.
Structure of Trie node:
• Every node of Trie consists of multiple
branches.
• Each branch represents a possible
character of keys.
• Mark the last node of every key as the end
of the word node.
• A Trie node field isEndOfWord is used to
distinguish the node as the end of the
word node.
Trie is also known as digital tree or prefix tree:

Trie containing
1. aqua
a d
2. dad
3. data
q a
4. day
5. days
6. a u d t y

a a s
Insert Operation in Trie:

• Inserting a key into Trie is a simple approach.


• Every character of the input key is inserted as
an individual Trie node. Note that
the children is an array of pointers (or
references) to next-level trie nodes.
• The key character acts as an index to the
array children.
• If the input key is new or an extension of the
existing key, construct non-existing nodes of
Insertin
g
i) and
ii) ant
Advantages of tries
1. In tries the keys are searched using common
prefixes. Hence it is faster.
2. The lookup of keys depends upon the height in
case of binary search tree.
3. Tries take less space when they contain a large
number of short strings. As nodes are shared
between the keys.
4. Tries help with longest prefix matching, when
we want to find the key.
Search Operation in Trie:
• Searching for a key is similar to the insert
operation. However, It only compares the
characters and moves down. The search can
terminate due to the end of a string or lack of
key in the trie.
• In the former case, if the isEndofWord field of
the last node is true, then the key exists in the
trie.
• In the second case, the search terminates
without examining all the characters of the key,
Search
in Trie

You might also like