Download - Advanced Trees Part III Briana B. Morrison Adapted from Alan Eugenio & William J. Collins
Advanced TreesPart III
Briana B. Morrison
Adapted from Alan Eugenio &
William J. Collins
Advanced Trees (Part III) 2
Topics
2-3-4 Search Trees Red Black Trees
Advanced Trees (Part III) 3
2-3-4 Trees
2-3-4 Trees are just like 2-3 trees, except on inserts and deletes, the split (and/or merge) takes place before the insert (it’s done on the way down the tree rather than on the way back up).
2-3-4 trees are implemented, as a red-black tree.
Advanced Trees (Part III) 4
3 Possible Kinds of Nodes
A
2 -n o d e
v alu e < A v alu e > A
A B
3 -n o d e
A < B
v alu e < A A < v alu e < B v alu e > B
A B C
4 -n o d e
A < B < C
v alu e < A A < v alu e < B v alu e > CB < v alu e < C
Advanced Trees (Part III) 5
2-3-4 Tree Example
2 1 5 3 5 5 5
1 2
2 54 8 1 0
115 7 9
Advanced Trees (Part III) 6
Example of Insertion in a 2-3-4 Tree
A
A B C
S T VU
A
B
S T VU
C
Advanced Trees (Part III) 7
Another Insertion Example of 2-3-4 Tree
2 1 2 1 52 2 1 5
In s ert 2 In s ert 1 2In s ert 1 5
1 52
1 2
Sp lit 4 -n o d e (2 , 1 2 , 1 5 )
1 5
1 2
2 4
In s ert 4
1 5
1 2
2 4 8
Insert: 2, 15, 12, 4, 8, 10, 25, 35, 55, ….11
2
4 1 2
1 58 2
4 1 2
1 58 1 0
Sp lit 4 -n o d e (2 , 4 , 8 ) In s ert 1 0
2
4 1 2
8 1 0 1 5 2 5 2
4 1 2
2
4 1 2 2 5
8 1 0 1 5 3 5 2
4 1 2 2 5
8 1 0 1 5 3 5 5 5
1 5 2 5 3 58 1 0
I n s er t 2 5 I n s er t 3 5
I n s er t 5 5S p lit 4 - n o d e ( 1 5 , 2 5 , 3 5 )
Advanced Trees (Part III) 8
Example (Cont…)
2 8 1 0 1 5 3 5 5 5
2 54
1 2
2 8 1 0 1 1 1 5 3 5 5 5
2 54
1 2
Sp lit 4 -n o d e (1 5 , 2 5 , 3 5 ) In s ert 1 1
2 1 5 3 5 5 5
2 5
1 2
4 1 0
1 18 2 1 5 3 5 5 5
2 5
1 2
4 1 0
1 18 9
Sp lit 4 -n o d e (8 , 1 0 , 1 1 ) In s ert 9
(4, 12, 25)
Insert 11, 9, …5
Advanced Trees (Part III) 9
Example (Cont…)
2 1 5 3 5 5 5
2 5
1 2
4 8 1 0
1 195 7
2 1 5 3 5 5 5
2 5
1 2
4 1 0
1 15 8 9 2 1 5 3 5 5 5
2 5
1 2
4 8 1 0
1 195
In s ert 5 Sp lit 4 -n o d e (5 , 8 , 9 )
Insert 5, 7
Advanced Trees (Part III) 10
A RED-BLACK TREE IS ANOTHER
KIND OF BALANCED BINARY SEARCH
TREE.
Advanced Trees (Part III) 11
A RED-BLACK TREE IS A BINARY SEARCH TREE
IN WHICH THE ROOT ITEM IS COLORED
BLACK, EVERY OTHER ITEM IS COLORED
RED OR BLACK, AND
1. (RED RULE) A RED ITEM CANNOT HAVE
ANY RED CHILDREN;
2. (PATH RULE) THE NUMBER OF BLACK
ITEMS IS THE SAME IN ANY PATH FROM
THE ROOT ITEM TO AN ITEM WITH
NO CHILDREN OR WITH ONE CHILD.
Advanced Trees (Part III) 12
Red-Black Tree
A red-black tree can also be defined as a binary search tree that satisfies the following properties: Root Property: the root is black External Property: every leaf is black Internal Property: the children of a red node are black Depth Property: all the leaves have the same black depth
9
154
62 12
7
21
Advanced Trees (Part III) 13
Binary Search Tree and Red/Black Tree
1
2
5
4
3
2
4
53
1
D egen erat e s earch t ree red -b lack t ree
Advanced Trees (Part III) 14
THE FOLLOWING ARE RED-BLACK
TREES:
Advanced Trees (Part III) 15
60
30 80
20 50 90
Advanced Trees (Part III) 16
60
30 80
20 50 90
40
Advanced Trees (Part III) 17
WHAT ABOUT THIS?
60
30 80
20 50 90
40
THIS IS NOT A RED-BLACK TREE: THE PATH FROM60 TO 80 HAS ONLY ONE BLACK ELEMENT!
Advanced Trees (Part III) 18
50
30 90
20 40 80 131
60 85 100 150
140 160
180
THIS RED-BLACK TREE IS NOT AN AVL TREE. WHY?
Advanced Trees (Part III) 19
Red Black Trees as B-Trees of Order 4 Start with a B-tree of order 4 (so each node contains
1, 2, or 3 entries) Convert a node with 3 entries into a binary search
tree by:
Advanced Trees (Part III) 20
Red-Black Trees
B
CA
A B C
S T U VS T
R ep res en t at io n w it h a b lackp aren t an d t w o red ch ild ren
4 -n o d e (A , B , C )in a 2 -3 -4 T ree
U V
A B
ST
U
3 -n o d e (A , B )in a 2 -3 -4 T ree
A
BS
R ep res en t at io n w it h a b lackp aren t an d a red righ t ch ild
T U
B
A
S T
R ep res en t at io n w it h a b lackp aren t an d a red left ch ild
U
Advanced Trees (Part III) 21
From (2,4) to Red-Black Trees A red-black tree is a representation of a (2,4) tree by means of a
binary tree whose nodes are colored red or black In comparison with its associated (2,4) tree, a red-black tree has
same logarithmic time performance simpler implementation with a single node type
2 6 73 54
4 6
2 7
5
3
3
5OR
Advanced Trees (Part III) 22
Converting a 2-3-4 Tree to Red-Black Tree Example
1 2 1 5
8 1 0 2 0
9 3 0 4 01 3 4 1 2 1 59 3 0 4 01 3 4
1 0
8 2 0
1 2 1 5 3 0 4 0
1 0
8 2 0
3 9
1 4
1 0
8 2 0
3 9
1 4 3 0
1 2 4 0
1 5
Advanced Trees (Part III) 23
CLAIM: THE HEIGHT OF ANY RED-
BLACK TREE IS LOGARITHMIC IN n.
Advanced Trees (Part III) 24
WHAT IS THE MINIMUM HEIGHT OF A
RED-BLACK TREE?
75
40 90
20 60 80 100
10 30 50
Advanced Trees (Part III) 25
SUPPOSE A RED-BLACK TREE IS
COMPLETE, WITH ALL BLACK
ELEMENTS, EXCEPT FOR RED
LEAVES AT THE LOWEST LEVEL.
THEN THE HEIGHT OF THAT TREE IS,
APPROXIMATELY, log2n.
Advanced Trees (Part III) 26
WHAT IS THE MAXIMUM HEIGHT? 50 30 90 20 40 80 131 60 85 100 150 140 160 185
Advanced Trees (Part III) 27
SUPPOSE A RED-BLACK TREE HAS ALL BLACK
ELEMENTS, EXCEPT THAT ONE PATH FROM THE
ROOT TO A LEAF HAS AS MANY RED ELEMENTS AS
POSSIBLE. THEN THE LENGTH OF THAT PATH, AND
THE HEIGHT OF THE TREE, WILL BE MAXIMAL.
THE LENGTH OF THAT PATH IS APPROXIMATELY
TWICE THE MINIMAL HEIGHT, SO THE MAXIMUM
HEIGHT OF A RED-BLACK TREE WILL BE,
ROUGHLY, 2log2 (n).
Advanced Trees (Part III) 28
THERE ARE FOUR ASSOCIATIVECONTAINER CLASSES IN THESTANDARD TEMPLATE LIBRARY:
set: ITEM = KEY; NO DUPLICATES ALLOWED
multiset: ITEM = KEY; DUPLICATES ALLOWED
map: KEY IS PART OF ITEM; NO DUPLICATES ALLOWED
multimap: KEY IS PART OF ITEM;DUPLICATES ALLOWED
Advanced Trees (Part III) 29
Red-Black Tree Algorithms
Find – just like binary search tree Performance?
Insertion Deletion
Advanced Trees (Part III) 30
THERE ARE FIVE STEPS TO ACCOMPLISH THE INSERTION OF THE ITEM v INTO AN rb_tree OBJECT: 1. CREATE A NODE POINTED TO BY x;
2. SET value (x) TO v;
3. INSERT *x AS A LEAF (WITH A BinSearchTree-STYLE INSERTION), AND SET color (x) TO red;
4. FIX UP THE TREE BY RE-COLORING AND RESTRUCTURING, IF THE RED RULE IS VIOLATED (PATH RULE OK: RED INSERTION);
5. SET color (root( )) TO black.
Advanced Trees (Part III) 31
60
30 80
20 50 70 90
ADD 40?
Advanced Trees (Part III) 32
60 30 80 20 50 70 90
40
VIOLATES RED RULE,NEED TO FLIP SOME COLORS (50)
Advanced Trees (Part III) 33
60 30 80 20 50 70 90
40
VIOLATES PATH RULE, NEED TO FLIP SOME COLORS (30)
Advanced Trees (Part III) 34
60 30 80 20 50 70 90
40
VIOLATES RED RULE,NEED TO FLIP SOME COLORS (20)
Advanced Trees (Part III) 35
60 30 80 20 50 70 90
40
ADD 35?
Advanced Trees (Part III) 36
60 30 80 20 50 70 90
40 35
THIS TREE VIOLATES THE RED RULE.
Advanced Trees (Part III) 37
60 30 80 20 50 70 90
40 35
THIS TREE VIOLATES THE PATH RULE.
Advanced Trees (Part III) 38
ROTATION TO THE RESCUE!
Advanced Trees (Part III) 39
60
30 80
20 40 70 90
35 50
Advanced Trees (Part III) 40
WHEN WILL STEP 4 BE NEEDED?
NOT IF (THE NODE POINTED TO BY) x
IS THE ROOT, BECAUSE THEN STEP 5
WILL SET x’S COLOR TO black.
ALSO, IF color (parent (x)) IS black, THEN
NO RESTRUCTURING IS NEEDED
BECAUSE THE RED RULE IS NOT
VIOLATED.
Advanced Trees (Part III) 41
STEP 4 IS ACCOMPLISHED IN A LOOP:
while (x != root() && color(parent(x)) == red){
} // while
color (root( )) = black; // STEP 5
Advanced Trees (Part III) 42
THE LOOP CONSISTS OF THREE CASES
WHEN parent (x) IS A LEFT CHILD, AND
A SYMMETRIC THREE CASES WHEN
parent (x) IS A RIGHT CHILD. IN ALL
CASES, THE ACTION TAKEN DEPENDS
ON THE SIBLING OF parent (x).
Advanced Trees (Part III) 43
ASSUME THAT parent (x) IS A LEFT
CHILD. LET y BE (A POINTER TO) THE
SIBLING OF parent (x). CASE 1: color (y) is red FOR EXAMPLE, SUPPOSE 10 HAS JUST
BEEN INSERTED INTO THE
FOLLOWING TREE:
Advanced Trees (Part III) 44
100
40 120
30 80 135
10 y ACTION: color(parent(x)) = black; x color(y) = black; color(parent(parent(x))) = red; x = parent(parent(x));
Advanced Trees (Part III) 45
100
x 40 120
30 80 135
10 y
THE LOOP NOW TERMINATES BECAUSE
color (parent (x)) IS black. AND THIS IS A RED-BLACK
TREE.
Advanced Trees (Part III) 46
CASE 2: color (y) IS black, AND x IS A RIGHT CHILD RECALL THAT WHEN y IS NULL, y
BECOMES A POINTER TO NIL, WHOSE
COLOR IS black.
FOR EXAMPLE, SUPPOSE 20 HAS JUST
BEEN INSERTED INTO THE
FOLLOWING TREE:
Advanced Trees (Part III) 47
100
40 120
30 80 135
10 y IS NULL ACTION: x = parent (x); 20 x rotate_left (x);
Advanced Trees (Part III) 48
100 40 120 30 80 135 20 y is NULL 10 x
THIS LEADS US TO CASE 3.
Advanced Trees (Part III) 49
CASE 3: color (y) IS black, AND x IS A
LEFT CHILD
FOR EXAMPLE, CASE 3 APPLIES TO
THE TREE IN THE PREVIOUS SLIDE:
Advanced Trees (Part III) 50
100
40 120
30 80 135
20 y is NULL
10 x
Advanced Trees (Part III) 51
100 40 120 30 80 135 20 y is NULL 10 x ACTION:
color (parent (x)) = black; color (parent (parent (x))) = red; rotate_right (parent (parent (x)));
Advanced Trees (Part III) 52
100 40 120 20 80 135 10 30 x
THE PARENT OF x IS NOW black, SO WE ARE DONE.
Advanced Trees (Part III) 53
ADDITIONAL LOOP ITERATIONS
OCCUR ONLY WHEN CASE 1 APPLIES,
AND THEN x IS MOVED TWO LEVELS
UP THE TREE. SO FOR RECOLORING
AND RESTRUCTURING, worstTime(n) IS
LOGARITHMIC IN n. (RECALL THAT
THE HEIGHT OF A RED-BLACK TREE
IS ALWAYS LOGARITHMIC IN n.)
Advanced Trees (Part III) 54
FOR THE ORIGINAL INSERTION,
BinSearchTree-STYLE, worstTime(n) AND
averageTime(n) ARE LOGARITHMIC IN n
FOR THE SAME REASON (THE HEIGHT
OF THE TREE IS LOGARITHMIC IN n).
SO FOR THE insert METHOD,
worstTime(n) AND averageTime(n) ARE
LOGARITHMIC IN n.
Advanced Trees (Part III) 55
Building A Red-Black Tree
Let’s build a red-black tree both ways As a 2-3-4 tree As a red-black tree
Insert the following values 2, 15, 12, 4, 8, 10, 25, 35
Advanced Trees (Part III) 56
Building A Red-Black Tree2 2
In s ert 2m ak e ro o tB L A C K
R ed -B lack T ree 2 - 3 - 4 T r e e
22
R e d- B la c k T r e e
B L A C K
2 1 2 1 51 5
22 -3 -4 T ree
R ed -B lack T ree
1 2
A B
C
DX
G
1 52
1 2
C A B D
D o u b le l eftro tat io n
P
2 4
2 -3 -4 T reeR ed -B lack T ree
1 2
1 51 52
1 2
41 52
1 2
1 52
1 2
C o lo r F l ip
1 52
1 2
M ak e ro o tB L A C K
In s ert 4
4
Insert 15 and 12
Insert 4
Advanced Trees (Part III) 57
2 4 8
1 54
2 -3 -4 T reeR ed -B lack T ree
1 21 2
1 582
1 54
2 -3 -4 T ree
R ed -B lack T ree
1 2
1 5
82
4 1 2
2 8 1 0
1 54
1 2
82
1 54
1 2
82
1 0
C o lo r fl ip In s ert 1 0
8
1 5
1 2
82
1 2
82
2 5
2 -3 -4 T reeR ed -B lack T ree
4 1 2
2 8 1 0
4
1 0
1 5 2 52 5
In s ert 2 5
2 -3 -4 T reeR ed -B lack T ree
4 1 2
2 8 1 01 5
4
2
1 0
1 5 2 5 3 53 5
In s ert 3 5
Insert 8
Insert 10
Insert 25 Insert 35
Advanced Trees (Part III) 58
Insertion
To perform operation insertItem(k, o), we execute the insertion algorithm for binary search trees and color red the newly inserted node z unless it is the root We preserve the root, external, and depth properties If the parent v of z is black, we also preserve the internal property and
we are done Else (v is red ) we have a double red (i.e., a violation of the internal
property), which requires a reorganization of the tree Example where the insertion of 4 causes a double red:
6
3 8
6
3 8
4z
v v
z
Advanced Trees (Part III) 59
Remedying a Double Red
Consider a double red with child z and parent v, and let w be the sibling of v
4
6
7z
vw2
4 6 7
.. 2 ..
Case 1: w is black The double red is an incorrect
replacement of a 4-node Restructuring: we change the 4-
node replacement
Case 2: w is red The double red corresponds
to an overflow Recoloring: we perform the
equivalent of a split
4
6
7z
v
2 4 6 7
2w
Advanced Trees (Part III) 60
Restructuring
A restructuring remedies a child-parent double red when the parent red node has a black sibling
It is equivalent to restoring the correct replacement of a 4-node The internal property is restored and the other properties are
preserved
4
6
7z
vw2
4 6 7
.. 2 ..
4
6
7
z
v
w2
4 6 7
.. 2 ..
Advanced Trees (Part III) 61
Restructuring (cont.)
There are four restructuring configurations depending on whether the double red nodes are left or right children
2
4
6
6
2
4
6
4
2
2
6
4
2 6
4
Advanced Trees (Part III) 62
Recoloring
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
It is equivalent to performing a split on a 5-node The double red violation may propagate to the grandparent u
4
6
7z
v
2 4 6 7
2w
4
6
7z
v
6 7
2w
… 4 …
2
Advanced Trees (Part III) 63
Analysis 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, o)
1. We search for key k to locate the insertion node z
2. We add the new item (k, o) 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)
Advanced Trees (Part III) 64
EXERCISE: INSERT 85 INTO THE FOLLOWING RED-
BLACK TREE: 100 40 120 20 80 135 10 30
Advanced Trees (Part III) 65
EXERCISE: INSERT 15 INTO THE FOLLOWING RED-
BLACK TREE, AND FIX AFTER INSERTION: 100 40 120 20 80 135 10 30
Advanced Trees (Part III) 66
Deletion
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 of 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
3 8
4
v
r w
6
3
4
r
Advanced Trees (Part III) 67
Remedying a Double Black
The algorithm for remedying a double black node w with sibling y considers three casesCase 1: y is black and has a red child We perform a restructuring, equivalent to a transfer , and we are done
Case 2: y 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: y is red We perform an adjustment, equivalent to choosing a different
representation of a 3-node, after which either Case 1 or Case 2 applies Deletion in a red-black tree takes O(log n) time
Advanced Trees (Part III) 68
Red-Black Tree Reorganization
Insertion remedy double red
Red-black tree action (2,4) tree action result
restructuringchange of 4-node representation
double red removed
recoloring splitdouble red removed or propagated up
Deletionremedy double black
Red-black tree action (2,4) tree action result
restructuring transfer double black removed
recoloring fusiondouble black removed or propagated up
adjustmentchange of 3-node representation
restructuring or recoloring follows
Advanced Trees (Part III) 69
Tree Summary
General Trees Multiway search trees B trees 2-3 search trees Tries AVL Trees 2-3-4 trees Red-black trees
Advanced Trees (Part III) 70
BST, Red-Black and AVL Example
B in ary Search T ree (a)
5 0
7 8
7 5
8 0
7 0
9 0
6 0
R ed -B lack T ree (b )
7 8
8 07 05 0
9 06 0
7 5
8 07 5
7 85 0
9 06 0
7 0
A VL T ree (c)
1 0 01 0 0
1 0 0