CS 332: Algorithms: Skip Lists
CS 332: Algorithms: Skip Lists
CS 332: Algorithms: Skip Lists
Skip Lists
Administration
Hand back homework 3
Hand back exam 1
Go over exam
h = O(lg n)
We described the properties of red-black trees
We proved that these guarantee h = O(lg n)
Next: describe operations on red-black trees
called rotation:
y
x
A
C
B
rightRotate(y)
A
leftRotate(x)
y
B
rbInsert(x)
treeInsert(x);
x->color = RED;
// Move violation of #3 up tree, maintaining #4 as invariant:
while (x!=root && x->p->color == RED)
if (x->p == x->p->p->left)
y = x->p->p->right;
if (y->color == RED)
x->p->color = BLACK;
y->color = BLACK;
x->p->p->color = RED;
x = x->p->p;
else
// y->color == BLACK
if (x == x->p->right)
x = x->p;
leftRotate(x);
x->p->color = BLACK;
x->p->p->color = RED;
rightRotate(x->p->p);
else
// x->p == x->p->p->right
(same as above, but with
right & left exchanged)
Case 1
Case 2
Case 3
rbInsert(x)
treeInsert(x);
x->color = RED;
// Move violation of #3 up tree, maintaining #4 as invariant:
while (x!=root && x->p->color == RED)
if (x->p == x->p->p->left)
y = x->p->p->right;
if (y->color == RED)
x->p->color = BLACK;
y->color = BLACK;
x->p->p->color = RED;
x = x->p->p;
else
// y->color == BLACK
if (x == x->p->right)
x = x->p;
leftRotate(x);
x->p->color = BLACK;
x->p->p->color = RED;
rightRotate(x->p->p);
else
// x->p == x->p->p->right
(same as above, but with
right & left exchanged)
Case 2
Case 3
if (y->color == RED)
x->p->color = BLACK;
y->color = BLACK;
x->p->p->color = RED;
x = x->p->p;
equal-black-height subtrees
case 1
D y
B x
C new x
D
B
Change colors of some nodes, preserving #4: all downward paths have equal b.h.
The while loop now continues with xs grandparent as the new x
Case 2:
Uncle is black
Node x is a right child
Transform to case 3 via a left-
rotation
B x
case 2
A x
x->p->color = BLACK;
x->p->p->color = RED;
rightRotate(x->p->p);
C
B
A x
case 3
Red-Black Trees
Red-black trees do what they do very well
What do you think is the worst thing about red-
black trees?
A: coding them up
Skip Lists
A relatively recent data structure
A probabilistic alternative to balanced trees
A randomized algorithm with benefits of r-b trees
O(lg n) expected time for Search, Insert
O(1) time for Min, Max, Succ, Pred
Linked Lists
Think about a linked list as a structure for dynamic
Search()?
Insert()?