skip lists

Post on 19-Jan-2016

39 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Skip Lists. by Arlen Fletcher and Tim Heuett. What are skip lists?. Developed around 1989 by William Pugh as an alternative to balanced trees A probabilistic data structure based on parallel linked lists (we’ll get to this…). Skip Lists – “Express Lanes”!. Express Lanes. Highway. Roads. - PowerPoint PPT Presentation

TRANSCRIPT

Skip Lists

by Arlen Fletcher

and Tim Heuett

What are skip lists?

Developed around 1989 by William Pugh as an alternative to balanced trees

A probabilistic data structure based on parallel linked lists (we’ll get to this…)

Skip Lists – “Express Lanes”!

Express Lanes

Highway

Roads

Why skip lists?

Same average running time as Binary Search Tree– O(log n)

Requires no extra space Linked list structure means no

rotations or complex reorganization

Implementation

Ideal skip listNot really a practical implementation

Probabilistic skip listChooses node heights as it is adding

data in the middle based mostly on random numbers

Basic idea (ideal skip list)

Nodes are evenly spaced for max efficiency

Source: “Skip Lists” by Thomas A. Anastasio

Would only be practical for presorted data

Probabilistic Node Determination

Taller nodes are chosen based on probability factor (mostly random)

Source: “Skip Lists” by Thomas A. Anastasio

Adding a nodeInsert(list, searchKey, newValue) local update[1..MaxLevel] x := list->header --loop invariant: x->key < searchKey for i := list->level downto 1 do while x->forward[i]->key < searchKey do x := x->forward[i] --x->key < searchKey <= x->forward[1]->key update[i] := x x := x->forward[1] if x->key = search then x->value := newValue else lvl := randomLevel() if lvl > list->level then for i := list->level + 1 to lvl do update[i] := list->header list->level := lvl x := makeNode(lvl, searchKey, newValue) for i := 1 to lvl do x->forward[i] := update[i]->forward[i] update[i]->forward[i] := x

Source: “A Skip List in C#” by Leslie Sanford

Defining height of new nodes

randomLevel() lvl := 1 --random() that returns a random value in [0...1) while random() < p and lvl < MaxLevel do lvl := lvl + 1 return lvl

randomLevel(list) lvl := 1 --random() that returns a random value in [0...1) while random() < p and lvl < MaxLevel and lvl <= list->level do lvl := lvl + 1 return lvl

Optimized code:

Source: “A Skip List in C#” by Leslie Sanford

Deleting a nodeDelete(list, searchKey) local update[1..MaxLevel] x := list->header --loop invariant: x->key < searchKey for i := list->level downto 1 do while x->forward[i]->key < searchKey do x := x->forward[i] --x->key < searchKey <= x->forward[1]->key update[i] := x x := x->forward[1] if x->key = searchKey then for i := 1 to list->level do if update[i]->forward[i] != x then break update[i]->forward[i] := x->forward[i] free(x) while list->level > 1 and list->header->forward[list->level] = NIL do list->level := list->level - 1

Source: “A Skip List in C#” by Leslie Sanford

THE END!

“It’s like the Event Horizon!”

Sources

“Skip Lists” by Thomas A. Anastasio http://www.csee.umbc.edu/courses/undergraduate/34

1/fall01/Lectures/SkipLists/skip_lists/skip_lists.html

“A Skip List in C#” by Leslie Sanford http://www.codeproject.com/KB/recipes/skiplist1.aspx

Wikipedia! <3 http://en.wikipedia.org/wiki/Skip_lists

top related