You are on page 1of 20

SKIP LISTS

Introduction
 Unordered Array
 Searching
 Deletion Complexity O(n)
 Insertion

 Why Skip List


 Easy to implement
 Use less memory
 Supports for “faster Searching “
Skip List
 Simple Efficient dynamic randomized DS
 Definition: Set a series of lists S0,S1,S2,…,Sh
such that
 S0 is lowest level and Sh is topmost level
 Each list Si contains special elements
 S0 contains items of S in increasing order
 Each list is subsequence of previous one
 Sh contains only 2 special items
Example

h – height & n – number of keys


Skip List Types

Skip List

Perfect Skip List Randomized Skip List


Types
 Perfect skip list / Deterministic slip list
 Every alternate node in lower level is placed in
upper level of perfect skip list
Randomized skip list
 Rather than strictly enforcing every other node
of level i be promoted to level i+1 we simply use
probability to give an "expectation" that every
other node is promoted
 Whenever a node is inserted we will promote it
to the next level with probability p (=1/2 for
now)…we'll keep promoting it while we get
heads
 Given n insertions, how many would you expect
to be promoted to: – Level 1 = n/2, Level 2 =
n/4, Level 3 = n/8
Implementation
 Use of Quad node
 Two additional nodes: +∞ and -∞
 Item value
 Link to previous
 Link to next
 Link to above
 Link to below
Operations
 Searching
 Insertion
 Deletion
Searching
 Search for ‘x’
 Start search at the first position of topmost list
 For current position ‘p’ compare ‘x’ with ‘y’ where
‘y’ p.next.key
 If(x==y) return p.next.element
 If (x>y) then move to next iten in current list
 If(x<y) then drop down to list at lower sight
Insertion
 Start with list S0
 Toss a coin
 If it shows HEADS, add x to list(before the item
which is larger than x). Move to next list ( S1 in case
of S0), toss the coin and repeat this procedure
 If TAILS comes, add x to current list and stop
Deletion
 Search for X in the skip list and find position of
x in all lists
 Remove positions from corresponding lists
 Remove all but one list which has only 2
special items
Procedures
Structure for Qnode
typedef struct qnode np;
struct qnode
{
int data;
np *up;
np *down;
np *left;
np *right;
};
np *list=NULL;
Node Creation
//create new node
np* createNode()
{
np *newnode=(np *)malloc(sizeof(np));
newnode->data=-1;
newnode->left=NULL;
newnode->down=NULL;
newnode->up=NULL;
newnode->right=NULL;
return newnode;
}
Flip a Coin
//flip coin

int toss_coin()
{
if(rand()%2==0)
return 1;
else
return 0;
}
Skip list Search procedure
int search_sl(int item)
{
curr=list;
while(curr!=NULL)
{
temp=curr;
if( (curr->right!=NULL) && ( curr->right->data
< item) )
{
curr=curr->right;
printf("Right-");
}
else if((curr->right != NULL) && (curr->down
== NULL) && (curr->right->data == item) )
{ return 1; }
else
{
curr=curr->down;
printf("Down-");
}
}
return 0;
}

You might also like