120 likes | 256 Vues
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.
E N D
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 list • Not really a practical implementation • Probabilistic skip list • Chooses 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 • Would only be practical for presorted data Source: “Skip Lists” by Thomas A. Anastasio
Probabilistic Node Determination • Taller nodes are chosen based on probability factor (mostly random) Source: “Skip Lists” by Thomas A. Anastasio
Adding a node Insert(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 Optimized code: 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 Source: “A Skip List in C#” by Leslie Sanford
Deleting a node Delete(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/341/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