Title: Red-Black Tree
1Red-Black Tree
9
15
4
21
6
2
12
7
2Red-Black Tree
- A Binary Search Tree.
- Every node in this tree is colored in either Red
or Black. - A historically popular alternative to the AVL
tree. - Operation on red-black trees take O(log n) time
in the worst case.
3Red-Black Tree
- Root Property The root is black
- External Property Every external node is black
- Internal Property The children of a red node are
black - Depth Property All the external nodes have the
same black depth
4
6
5
3
OR
2
7
3
5
4Height of a Red-Black Tree
- Theorem
- A red-black tree storing n items has height
O(log n)
Proof Depth Property All external nodes have
same black depth d. If all nodes were black
then d ? log(n1) Internal node Property The
children of a red node are black. i.e. h ?
2d Thus log(n1) ? h ? 2 log(n1) so height
is O(log n)
By the above theorem, searching in a red-black
tree takes O(log n) time
5Insertion
- we execute the insertion algorithm for binary
search trees - Let the newly inserted node is root then color it
black else color it red - We preserve the root, external, and depth
properties - If the parent of the node is black, we also
preserve the internal property and we are done - Else (parent is red ) we have a double red (i.e.,
a violation of the internal property), which
requires a reorganization of the tree - Example Sequence 6, 3, 8, 4
6
6
v
8
8
3
3
z
4
6Remedying a Double Red
- Consider a double red with child z and parent v,
and let w be the sibling of v
- Case 1 w is black
- Restructure Same as done for AVL trees.
z
6
4
v
w
v
7
2
4
7
z
w
6
2
7Restructuring
- A restructuring remedies a child-parent double
red when the parent red node has a black sibling - The internal property is restored and the other
properties are preserved
z
4
6
v
v
w
7
2
7
4
z
w
2
6
8Restructuring (cont.)
- There are four restructuring configurations
depending on whether the double red nodes are
left or right children
2
6
6
2
6
2
4
4
4
4
2
6
4
2
6
9Remedying a Double Red
- Consider a double red with child z and parent v,
and let w be the sibling of v
- Case 2 w is red
- The double red corresponds to an overflow
- Recolor and continue up
4
4
v
w
v
w
7
2
7
2
z
z
6
6
10Recoloring
- A recoloring remedies a child-parent double red
when the parent red node has a red sibling - The parent v and its sibling w become black and
the grandparent u becomes red, unless it is the
root - The double red violation may propagate to the
grandparent u
4
4
v
v
w
w
7
7
2
2
z
z
6
6
11Analysis of Insertion
- Recall that a red-black tree has O(log n) height
- Step 1 takes O(log n) time because we visit O(log
n) nodes - Step 2 takes O(1) time
- Step 3 takes O(log n) time because we perform
- O(log n) recolorings, each taking O(1) time, and
- at most one restructuring taking O(1) time
- Thus, an insertion in a red-black tree takes
O(log n) time
Algorithm insertItem(k, e) 1. We search for key
k to locate the external insertion node z 2. We
add the new item (k, e) at node z and color z red
3. while doubleRed(z) if isBlack(sibling(parent
(z))) z ? restructure(z) return else
sibling(parent(z) is red z ? recolor(z)
12Deletion
- To perform operation remove(k), we first execute
the deletion algorithm for binary search trees - Let v be the internal node removed, w the
external node removed, and r the sibling of w - If either v or r was red, we color r black and we
are done - else (v and r were both black) we color r double
black, which is a violation of the internal
property requiring a reorganization of the tree - Example the deletion of 4 is simple
6
6
8
3
3
8
r
v
4
r
w
13Deletion
- To perform operation remove(k), we first execute
the deletion algorithm for binary search trees - Let v be the internal node removed, w the
external node removed, and r the sibling of w - If either v or r was red, we color r black and we
are done - else (v and r were both black) we color r double
black, which is a violation of the internal
property requiring a reorganization of the tree - Example where the deletion of 8 causes a double
black
6
6
v
r
8
3
3
r
w
4
4
14Remedying a Double Black
- The algorithm for remedying a double black node r
with sibling w considers three cases - Case 1 w is black and has a red child
- We perform a restructuring, equivalent to a
transfer , and we are done - Case 2 w is black and its children are both
black - We perform a recoloring, equivalent to a fusion,
which may propagate up the double black violation - Case 3 w is red
- We perform an adjustment, after which either Case
1 or Case 2 applies - Deletion in a red-black tree takes O(log n) time
15Remedying a Double Black
- Case 1 w is black and has a red child
- We perform a restructuring
- We color r black
- We color a and c black.
- We color b with the same color as the parent of r.
c
6
4
w
r
a
6
3
3
r
b
4
16Remedying a Double Black
- Case 2 w is black and its children are both
black - We perform a re-coloring
- We color r black
- We color w red
- If the parent is red we color it black else we
color it double black.
9
9
15
15
w
r
12
12
17Remedying a Double Black
- Case 2 w is black and its children are both
black - We perform a recoloring
- We color r black
- We color w red
- If the parent is red we color it black else we
color it double black.
9
9
15
15
w
r
12
12
Propagates Double Black
18Remedying a Double Black
- Case 3 w is red
- We perform a restructure
- Color w black
- Color parent of r red.
- Now sibling of r is black. So use Case 1 or Case
2.
w
6
3
w
r
6
3
r
4
4
4
4
19Summary of Red-Black Trees
- Insertion or deletion may cause local
perturbation - two consecutive red nodes or a double-black node.
- The perturbation is either
- resolved locally (restructuring)
- propagated to a higher level in the tree by
re-coloring - O(1) time for a restructuring or re-coloring
- At most one restructuring per insertion and at
most two restructuring per deletion. - O(log n) re-coloring
- Total time O(log n)
20Skip Lists
21Skip List
- Question
- Can we create a structure that adds the best
properties of Array and Linked list Data
Structure? - Query O(log n) in sorted Arrays
- Insert/Removal O(1) in Linked List
22What is a Skip List
- A skip list for a set S of distinct (key,
element) items is a series of lists S0, S1 , ,
Sh such that - Each list Si contains the special keys ? and -?
- List S0 contains the keys of S in nondecreasing
order - Each list is a subsequence of the previous one,
i.e., S0 ? S1 ? ? Sh - List Sh contains only the two special keys
- We show how to use a skip list to implement the
dictionary ADT
S3
S2
?
31
-?
S1
64
?
31
34
-?
23
S0
23Search
- We search for a key x in a skip list as follows
- We start at the first position of the top list
- At the current position p, we compare x with y ?
key(after(p)) - x y we return element(after(p))
- x gt y we scan forward
- x lt y we drop down
- If we try to drop down past the bottom list, we
return NO_SUCH_KEY - Example search for 78
S3
S2
?
31
-?
S1
64
?
31
34
-?
23
S0
56
64
78
?
31
34
44
-?
12
23
26
24Insertion
- To insert an item (x, o) into a skip list, we use
a randomized algorithm - We repeatedly toss a coin until we get tails, and
we denote with i the number of times the coin
came up heads - If i ? h, we add to the skip list new lists Sh1,
, Si 1, each containing only the two special
keys - We search for x in the skip list and find the
positions p0, p1 , , pi of the items with
largest key less than x in each list S0, S1, ,
Si - For j ? 0, , i, we insert item (x, o) into list
Sj after position pj - Example insert key 15, with i 2
S3
p2
S2
S2
?
-?
p1
S1
S1
?
-?
23
p0
S0
S0
?
-?
10
36
23
25Deletion
- To remove an item with key x from a skip list, we
proceed as follows - We search for x in the skip list and find the
positions p0, p1 , , pi of the items with key
x, where position pj is in list Sj - We remove positions p0, p1 , , pi from the
lists S0, S1, , Si - We remove all but one list containing only the
two special keys - Example remove key 34
S3
-?
?
p2
S2
S2
-?
?
-?
?
34
p1
S1
S1
-?
?
23
-?
?
23
34
p0
S0
S0
-?
?
45
12
23
-?
?
45
12
23
34
26Implementation
- We can implement a skip list with quad-nodes
- A quad-node stores
- item
- link to the node before
- link to the node after
- link to the node below
- link to the node above
- Also, we define special keys PLUS_INF and
MINUS_INF, and we modify the key comparator to
handle them
quad-node
x
27Space Usage
- Consider a skip list with n items
- By Fact 1, we insert an item in list Si with
probability 1/2i - By Fact 2, the expected size of list Si is n/2i
- The expected number of nodes used by the skip
list is
- The space used by a skip list depends on the
random bits used by each invocation of the
insertion algorithm - We use the following two basic probabilistic
facts - Fact 1 The probability of getting i consecutive
heads when flipping a coin is 1/2i - Fact 2 If each of n items is present in a set
with probability p, the expected size of the set
is np
- Thus, the expected space usage of a skip list
with n items is O(n)
28Height
- The running time of the search an insertion
algorithms is affected by the height h of the
skip list - We show that with high probability, a skip list
with n items has height O(log n) - We use the following additional probabilistic
fact - Fact 3 If each of n events has probability p,
the probability that at least one event occurs is
at most np
- Consider a skip list with n items
- By Fact 1, we insert an item in list Si with
probability 1/2i - By Fact 3, the probability that list Si has at
least one item is at most n/2i - By picking i 3log n, we have the probability
that S3log n has at least one item is at most
n/23log n n/n3 1/n2 - Thus a skip list with n items has height at most
3log n with probability at least 1 - 1/n2
29Search and Update Times
- The search time in a skip list is proportional to
- the number of drop-down steps, plus
- the number of scan-forward steps
- The drop-down steps are bounded by the height of
the skip list and thus are O(log n) with high
probability - To analyze the scan-forward steps, we use yet
another probabilistic fact - Fact 4 The expected number of coin tosses
required in order to get tails is 2
- When we scan forward in a list, the destination
key does not belong to a higher list - A scan-forward step is associated with a former
coin toss that gave tails - By Fact 4, in each list the expected number of
scan-forward steps is 2 - Thus, the expected number of scan-forward steps
is O(log n) - We conclude that a search in a skip list takes
O(log n) expected time - The analysis of insertion and deletion gives
similar results
30Summary
- A skip list is a data structure for dictionaries
that uses a randomized insertion algorithm - In a skip list with n items
- The expected space used is O(n)
- The expected search, insertion and deletion time
is O(log n)
- Using a more complex probabilistic analysis, one
can show that these performance bounds also hold
with high probability - Skip lists are fast and simple to implement in
practice