Skip Lists

12
Skip Lists by Arlen Fletcher and Tim Heuett

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 of Skip Lists

Page 1: Skip Lists

Skip Lists

by Arlen Fletcher

and Tim Heuett

Page 2: Skip Lists

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…)

Page 3: Skip Lists

Skip Lists – “Express Lanes”!

Express Lanes

Highway

Roads

Page 4: Skip Lists

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

Page 5: Skip Lists

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

Page 6: Skip Lists

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

Page 7: Skip Lists

Probabilistic Node Determination

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

Source: “Skip Lists” by Thomas A. Anastasio

Page 8: Skip Lists

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

Page 9: Skip Lists

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

Page 10: Skip Lists

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

Page 11: Skip Lists

THE END!

“It’s like the Event Horizon!”

Page 12: Skip Lists

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