Professional Documents
Culture Documents
Data Structure Lec40 Handout
Data Structure Lec40 Handout
Lecture No. 40
___________________________________________________________________
Data Structures
Lecture No. 40
Reading Material
Data Structures and Algorithm Analysis in C++
Chapter. 10
10.4.2
Summary
Skip List
Skip List Search
Insertion in Skip List
Deletion from Skip List
In the previous lecture, we had started the discussion on the concept of the skip lists.
We came across a number of definitions and saw how the use of additional pointers
was effective in the list structures. It was evident from the discussion that a
programmer prefers to keep the data in the linked list sorted. If the data is not sorted,
we cannot use binary search algorithm. And the insert, find and remove methods are
proportional to n. But in this case, we want to use the binary search on linked list. For
this purpose, skip list can be used. In the skip list, there is no condition of the upper
limit of the array.
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 -
S0 S1 Sh
List Sh contains only the two special keys
Now lets see an example for the skip list. First of all, we have S0 i.e. a linked list. We
did not show the arrows in the list in the figure. The first and last nodes of S0 contain
- and + respectively. In the computer, we can put these values by max(int) and
max(int). The values can also be used about which we are sure that these will not be
in the data items. However, for the sake of discussion to show these values, the - and
+ are the best notations. We can see that - is less than any value of data item while
+ is greater than any value of data item. If we insert any value much ever, it is large
the + will be greater than it. Moreover, we see that the numbers in S0 are in the nondecreasing order. This S0 is the first list in which all keys are present.
Now we will take some nodes from this list and link them. That will be not every
other node or every fourth node. It may happen this way. However, we will try that
S3
S2
S1
S0
31
23
12
23
26
31
34
31
34
64
56
4
64
78
4
Figure 40.1:
Now we have the list i.e. from S0 to S3. Actually, these list are made during insert
operation. We will see the insert method later. Lets first talk about the search method.
x = y: we return element(after(p))
S3
S2
S1
S0
31
23
12
23
26
31
34
31
34
64
56
4
64
78
4
Suppose we want to search 78. We start from the first node of the top list i.e. S3. The
78 will be the current node and we denote it with p. Now we look at the value in the
node after p. In the figure, it is +. Now as the + is greater than 78, we drop down
to S2. Note in the figure that we drop down vertically. We dont go to the first element
p in the down list. This process of going down will be discussed later. Now we drop
from S3 to S2. This is our current pointer. Now we look for the value in the next node
to it. We see that this value is 31. Now 31 is less than 78, so we will do scan forward.
The next node is + that is obviously greater than 78. So we drop from here and go to
the list S1. In this list, the current position is 34. We compare 78 with this node. As 34
is less than 78, we scan forward in the list. The next node in the list is 64 that is also
less than 78. So we look at the next node and note that the next node is + that is
greater than 78. Due to this we drop down to list S0. Here we look at the next node of
64 and find that this is 78. Thus at last we reach at the node that we are searching for.
If i > h, we add to the skip list new lists Sh+1, , Si +1, each containing only
the two special keys
Here we compare i (that is the count of heads came up) with h (that is the number of
list) if i is greater than or equal to h then we add new lists to the skip list. These new
lists are Sh+1, , Si +1. Suppose if i is 8 and h is 4 , we add additional lists S5, S6,
S7, S8 and S9. These lists initially will contain the only two special keys that means
- and +. The next steps are:
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
Now we will start from the left most node of the top list i.e. Si and will find the
position for the new data item. We find this position as we want to keep the data in the
list sorted.
S2
S1
S0
23
10
23
36
4
Figure 40.3:
Now we proceed with this list and insert a value in it. The value that we are going to
insert is 15. We have tossed the coin and figured out that the value of i is 2. As it is
randomized algorithm so in this case, the value of i has become 2. The value of I is
the count number of heads before the tail comes up by tossing the coin. From the
above figure, we see that the value of h is 2. As h and i are equal, we are not adding
S3, S4 and S5 to the list. Rather, we will apply the search algorithm. We start from the
left most node of the top list. We call this position p2. We label these positions for
their identification. We see that the item being added i.e.15 is less than the +, so we
drop down to list S1. We denote this step with p1. In this list, the next value is 23 that
is also greater than 15. So we again drop down and come in the list S0. Our current
position is still the first node in the list as we did not have any scan forward. Now the
next node is 10 that is less than 15. So we skip forward. We note this skip forward
with p0. Now after p0 the value in next node is 23 that is greater than 15. As we are in
the bottom list, there is no more drop down. So here is the position of the new node.
This position of new node is after the node 10 and before the node 23. We have
labeled the positions po, p1 and p2 to reach there. Now we add the value 15
additionally to the list that we have traversed to find the positions and labeled them to
remember. After this we add a list S3 that contains only two keys that are - and +,
as we have to keep such a list at the top of the skip list. The following figure shows
this insertion process.
S2
15
S1
15
23
S0
15
23
10
36
4
b random()
if b <= 0.5 // head
do A
else
// tail
do B
Its running time depends on the outcome of the coin tosses, i.e, head or tail
In the randomized algorithm, we take a number from the random() function between 0
and 1. This number is up to one decimal place. However, we can keep it up to nay
decimal places. As stated above ,after getting a number we check its value. If this is
less than or equal to 0.5, we consider it as head and execute the process A. In our
algorithm, we increase the value of i by 1. However, if value is greater than 0.5, we
consider it as tail and do the process B. In our algorithm, the process B takes place
S3
S2
S1
S0
12
34
23
34
23
34
45
4
S2
S1
S0
23
12
23
45
4