1 unbalanced binary search trees balanced search trees 2-3-4 trees red-black trees cse 30331 lecture...
TRANSCRIPT
1
Unbalanced binary search trees Balanced search trees
2-3-4 trees Red-black trees
CSE 30331Lecture 17 – Balanced trees
2
Unbalanced Binary Search Tree and balanced alternatives
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
3
Two Binary Search Tree Examples
Insertion sequence for tree (a): 5, 15, 20, 3, 9, 7, 12, 17, 6, 75, 100, 18, 25, 35, 40
5
18
35
25
17 75
6
127
209
153
40
5
18
35
25
17
75
6 12
7
20
9
153
40
Depth = 6Average comparisons per search = 4.0
Depth = 4Average comparisons per search = 3.47
(a) unbalanced (b) Same data, but more balanced
100
100
4
What is a 2-3-4 Tree?
Each node has 1, 2 or 3 values Each node has 2, 3 or 4 children The tree is ALWAYS perfectly balanced
Depth of all subtrees of a node are equal
5
2-3-4 Tree Example
2 1 5 3 5 5 5
1 2
2 54 8 1 0
1 15 7 9
6
2-3-4 Tree (node types)
A
2-node
(value < A) (value > A)
A B
3-node
A < B
(value < A) (A < value < B) (value > B)
A B C
4-node
A < B < C
(value < A) (A < value < B) (value > C)(B < value < C)
7
2-3-4 Tree InsertionSplits 4-nodes top-down
3 6 9
2 4 108
Inserting 7 requires splitting of the 4-node before adding 7 as a child (leaf or part of existing leaf)
93
6
2 107 84
After splitting, 7 is added to a leaf following normal search tree order
8
2-3-4 Tree Insertion Details 4-nodes must be split on the way down Split involves “promotion” of middle value into parent
node New value is always added to existing 2-node or 3-
node LEAF
2-3-4 tree is always perfectly balanced Implementation is wasteful of space
Each node must have space for 3 values, 1 parent pointer and 4 child pointers, even if not always used
There is always an equivalent red-black binary tree
9
Building 2-3-4 Tree by Series of Insertions
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
Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7
To insert 4, first split the root (a 4-node) and then add value to leaf
10
Building 2-3-4 Tree by Series of Insertions
1 5
1 2
2 4 8
Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7
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
Insert 8
To insert 10, first split the root (a 4-node) and then add value to leaf
11
Building 2-3-4 Tree by Series of Insertions
Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7
2
4 12
8 10 15 25
Insert 25
2
4 12
15 25 358 10
Insert 35
2
4 12 25
8 10 15 35 2
4 12 25
8 10 15 35 55
Insert 55Split 4-node (15, 25, 35)
To insert 55, first split the leaf (a 4-node) and then add value to leaf
12
Building 2-3-4 Tree by Series of Insertions
Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 To insert 11, again requires top-down split of each 4-node
encountered, so root is split and new value is inserted in leaf
2 8 10 15 35 55
254
12
Split 4-node (4, 12, 25)
2 8 10 11 15 35 55
254
12
Insert 11
13
Building 2-3-4 Tree by Series of Insertions
Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 To insert 9, split the leaf (a 4-node) and insert new value
2 15 35 55
25
12
4 10
118
Split 4-node (8, 10, 11)
2 15 35 55
25
12
4 10
118 9
Insert 9
14
Building 2-3-4 Tree by Series of Insertions
Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7
2 15 35 55
25
12
4 10
115 8 9
Insert 5
2 15 35 55
25
12
4 8 10
1195
Split 4-node (5, 8, 9)
2 15 35 55
25
12
4 8 10
1195 7
And finally insert 7 in a leaf
15
Inefficiencies
2-3-4 trees require space for up to ... 3 values in every node Pointers to 4 children from every node
Even if they are not used in all cases So, we can use a Red Black tree instead
A (colored) form of binary search tree Always relatively balanced Nodes have no more storage required than
normal binary search tree
16
Red Black Tree Properties
The root is always BLACK
A RED parent never has a RED child
Every path from ROOT to an EMPTY subtree has the same black height (# of BLACK nodes)
Black Height of tree is O(log2n)
17
Red Black Tree Node Types BLACK parent with two RED children is the
equivalent of a 4-node
A 4-node split is accomplished by flipping the colors to RED parent & two BLACK children, with possible rotation if colors conflict
BLACK parent with single RED child is equivalent of a 3-node
New nodes are always inserted a RED leaves
18
Red-Black subtree equivalents of 2-3-4 tree nodes
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
19
Converting a 2-3-4 Tree to Red-Black Tree (top-down)
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
First, convert the root
(note: shaded nodes are RED)
Original
20
Converting a 2-3-4 Tree to Red-Black Tree (top-down)
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
Then convert the right subtree and we’re done
Next, convert left subtree
21
Building red black tree by repeated insertions
Insert root and color it BLACK Each successive insertion point is found in normal
search tree order Any 4-nodes (black parent with two red children) is
split going down into tree Requires color changes and possible rotations of subtrees
New value is added as RED leaf If result is RED child of RED parent, then rotations
are required to correct
22
Situations in the Splitting of a 4-Node (A X B)
P a r e n t P is B L A C KX is a le f t - c h ild
X
A B
P
C
P a r e n t P is R E D X is a r igh t - c h ild
P a r e n t P is R E D X is a le f t - c h ild
X
A B
P
C
P a r e n t P is B L A C KX is a r igh t - c h ild
X
A B
P
C X
A B
P
C
23
Required actions to split a 4-node (A X B) with parent P & grandparent G
4-node is either child of black parent Flip subtree colors ONLY (X red, A & B black)
4-node is child of red parent (left-left) Flip subtree colors, single right rotation about P & recolor
4-node is child of red parent (right-right) Flip subtree colors, single left rotation about P & recolor
4-node is child of red parent (left-right) Flip subtree colors, then double rotation (first left, then
right) about X & then color X black, G & P red 4-node is child of red parent (right-left)
Flip subtree colors, then double rotation (first right, then left) about X & then color X black, G & P red
24
Left child of a Black parent P:Split requires only a Color Flip
2 - 3 - 4 t r e e v ie wR e d- bla c k t r e e
be f o r e t h e c o lo r f lip
A X B
P
C X P
BA
C
X
A B
P
C
R e d- bla c k t r e ea f t e r t h e c o lo r f lip 2 - 3 - 4 t r e e v ie w
X
A B
P
C
25
Example split prior to inserting 55: flip colors (40,50,60) & insert leaf
2 -3 -4 t ree v iew
5 0
4 0 6 0
3 0
2 04 0 5 0 6 0 5 0
4 0 6 0
3 0
2 0
5 5
5 5 6 0
2 0 3 0 5 0
2 -3 -4 t ree v iew
4 -n o d eb efo re co lo r-fl ip
4 -n o d e after co lo r-fl ipan d in s ert io n o f 5 5
2 0 3 0
C D
C D C D
C D 4 0
26
Required actions to split a 4-node (A X B) with parent P & grandparent G
4-node is either child of black parent Flip subtree colors ONLY (X red, A & B black)
4-node is child of red parent (left-left) Flip subtree colors, single right rotation about P & recolor
4-node is child of red parent (right-right) Flip subtree colors, single left rotation about P & recolor
4-node is child of red parent (left-right) Flip subtree colors, then double rotation (first left, then
right) about X & then color X black, G & P red 4-node is child of red parent (right-left)
Flip subtree colors, then double rotation (first right, then left) about X & then color X black, G & P red
27
RED parent, 4-node oriented left-left from G (grandparent)
S in g le rig h t ro tat io n w i thp iv o t P an d co lo r ch an g es 2 -3 -4 - t ree v iew
2 -3 -4 t ree v iew R ed -b lack t reeb efo re co lo r fl ip
R ed -b lack t reeafter co lo r fl ip
P G
A X B C D
G
BA
X
P
C
D
G
BA
X
P
C
D
G
BA
X
P
C D
X P G
A B C D
28
Required actions to split a 4-node (A X B) with parent P & grandparent G
4-node is either child of black parent Flip subtree colors ONLY (X red, A & B black)
4-node is child of red parent (left-left) Flip subtree colors, single right rotation about P & recolor
4-node is child of red parent (right-right) Flip subtree colors, single left rotation about P & recolor
4-node is child of red parent (left-right) Flip subtree colors, then double rotation (first left, then
right) about X & then color X black, G & P red 4-node is child of red parent (right-left)
Flip subtree colors, then double rotation (first right, then left) about X & then color X black, G & P red
29
Red Parent : Oriented Left-Right From G
P G
A X B
BA
X
G
P
C
DCD
BA
X
G
P
C
D
2 -3 -4 t ree v iew R ed -b lack t reeb efo re co lo r flip
R ed -b lack t reeaft er co lo r flip
30
Red Parent : Oriented Left-Right From G
B
A
X
G
P
C
D
Red-black tree after first (left) rotation (about X)
BA
X
G
P
C
D
Red-black treeafter color flip
31
Red Parent : Oriented Left-Right From G
B
A
X
G
P
C
D
Red-black tree after first (left) rotation (about X)
After second (right) rotation about X and coloring X black, G & P red
BA
G
X
P
C D
32
Building A Red-Black Tree Insertions: 2, 15, 12, 4, 8, 10, 25, 35
2 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
2 2
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
33
Building A Red-Black Tree Insertions: 2, 15, 12, 4, 8, 10, 25, 35
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
34
Example on blackboard
Insert: 2, 12, 15, 7, 5, 8, 10
35
Deleting nodes from a Red-Black Tree
If node has 2 children Swap value with that of inorder successor and Delete the successor node (it has at most 1 child)
If node has 1 or 0 children If it is RED
Delete it and replace with its BLACK child If it is BLACK and its child is RED
Delete it, replace with its RED child and recolor it BLACK If it is BLACK and its child is BLACK
Yikes!!!!
36
Deleting nodes from a Red-Black Tree
If node has 1 or 0 children If it is BLACK and its child is BLACK
Delete the node and replace with its child BUT ... Now we have a situation. We have removed a BLACK
node from some paths and thus the BLACK height may differ in this part of the tree from others
If the node deleted was the root, we are done All paths are shorter by 1 black node
If not the root, then there are 5 other cases These involve bottom-up rebalancing of the tree using
recoloring and various rotations See http://en.wikipedia.org/wiki/Red-black_tree
37
Representing a Red-Black Tree
3 5
8 02 5
R ed -B lack T ree rb n o d e R ep res en t at io n o f R ed -B lack T ree
2 5 R E D 8 0 R E D
5 0 B L A C K
The rbNode class from Ford & Topp
38
Summary
2-3-4 tree Each node has either …
1 value and 2 children 2 values and 3 children 3 values and 4 children
Construction of 2-3-4 trees is complex, so we build an equivalent binary tree known as a red-black tree