tcss 342, winter 2006 lecture notes
DESCRIPTION
TCSS 342, Winter 2006 Lecture Notes. Balanced Binary Search Trees. Objectives. Learn about balanced binary search trees AVL trees red-black trees Talk about why they work Talk about their run-time performance. AVL Trees. In 1960 two Russian mathematicians - PowerPoint PPT PresentationTRANSCRIPT
TCSS 342B v1.0
1
TCSS 342, Winter 2006Lecture Notes
Balanced Binary Search Trees
TCSS 342B v1.0
2
Objectives• Learn about balanced binary search trees
– AVL trees– red-black trees
• Talk about why they work
• Talk about their run-time performance.
TCSS 342B v1.0
3
AVL Trees
• In 1960 two Russian mathematicians
• Georgii Maksimovich Adel'son-Vel'skii
• Evgenii Mikhailovich Landis
• developed a technique for keeping a binary search tree balanced as items are inserted into it.
• called AVL trees.
TCSS 342B v1.0
4
Balanced AVL Tree
TCSS 342B v1.0
5
extremely unbalanced tree
TCSS 342B v1.0
6
AVL Trees• The balance factor of node x =
• height of x's right subtree minus height of x's left subtree – define height of empty tree to be -1.
• binary search tree is an AVL tree if– balance factor of each node is 0, 1 or -1
• Are AVL trees always completely balanced?
TCSS 342B v1.0
7
Which are AVL Trees?
TCSS 342B v1.0
8
Examples of AVL Trees
0
0 0 -1
-1 0
0
1
-1 0
0 -1 1
0 0
TCSS 342B v1.0
9
Not AVL Trees
-2
-1
-2
2 0
0
-1
-1 2
0 1
0
0 -1
TCSS 342B v1.0
10
AVL Tree Data Structure
• extension of binary search tree where trees are always AVL trees.
• Maintain AVL property using rotations• Rotations occur when tree becomes
unbalanced from insertion or deletion• At each node, keep track of height of
subtree rooted at node (for balance factor computations)
TCSS 342B v1.0
11
balancing a tree with a right rotation
TCSS 342B v1.0
12
A right rotation in an AVL tree
TCSS 342B v1.0
13
FIGURE 13.11 Unbalanced tree and balanced tree after a left rotation
TCSS 342B v1.0
14
figure 10.12 A right-left rotation
TCSS 342B v1.0
15
figure 10.13 A left-right rotation
TCSS 342B v1.0
16
AVL insertion• After normal BST insert, update heights from new leaf up
towards root. If balance factor changes to +2 or -2, then use rotation(s) to rebalance.
• Let A be the first unbalanced node found. (This is the current lowest depth node that is a non-AVL subtree). Four cases: 1. A has balance factor -2 and A’s left child has balance factor –1.
Then perform right rotation around A. (right rotation)
2. A has and balance factor -2 and A’s left child has balance factor 1. Then perform left rotation around A’s left child, followed by right rotation around A. (left-right rotation)
TCSS 342B v1.0
17
AVL insertion (2)• Let A be the unbalanced node. Remaining 2 cases are
mirror image of the two before. 3. A has balance factor 2 and A’s right child has balance factor –1.
Then perform right rotation around A’s right child, followed by left rotation around A. (right-left rotation)
4. A has balance factor 2 and A’s right child has balance factor 1. Then perform left rotation around A. (left rotation)
• After rebalancing, continue up the tree updating heights. (and checking for imbalances in balance factor)
• Are all cases handled?
• What if new item added was A’s left child or right child?
• What if A’s child has balance factor 0?
TCSS 342B v1.0
18
right rotation (clockwise): left child becomes parent; original parent demoted to right
Right rotation to fix Case 1
TCSS 342B v1.0
19
left rotation (counter-clockwise): right child becomes parent; original parent demoted to left
Left rotation to fix Case 4
TCSS 342B v1.0
20
Problem: Cases 2, 3 a single right rotation does not fix Case 2! a single left rotation also does not fix Case 3
TCSS 342B v1.0
21
Left-right rotation to fix Case 2 left-right double rotation: a left rotation of
the left child, followed by a right rotation at the parent
TCSS 342B v1.0
22
Right-left rotation to fix Case 3 right-left double rotation: a right rotation of
the right child, followed by a left rotation at the parent
TCSS 342B v1.0
23
AVL rotation notes• right-left and left-right rotation sometimes called double
rotation• If subtree rooted at A was an AVL tree before the
insertion, then– subtree rooted at A is always an AVL tree after applying the
rotation(s) specified for each case. [Correctness]– no further rotations will be made for ancestors of A. – If A has balance factor –2, then
• A’s left child cannot have balance factor 0. • A’s left child has balance factor is –1 if and only if new node was
added to subtree rooted at A’s leftmost grandchild.• A’s left child has balance factor is 1 if and only if new node was
added to subtree rooted at A’s left child’s right child. • new node must be added at grandchild level of A or deeper.
TCSS 342B v1.0
24
a right-left rotationafter removal
TCSS 342B v1.0
25
AVL deletion• Perform normal BST remove (with replacement of node to
be removed with its successor). Then update heights from replacement node location upwards towards root. If balance factor changes to +2 or -2, then use rotation(s) to rebalance.
• Let A be the first unbalanced node found. (This is the current lowest depth node that is a non-AVL subtree). At least four cases: 1. A has balance factor -2 and A’s left child has balance factor –1.
Then perform right rotation around A. (right rotation)2. A has and balance factor -2 and A’s left child has balance factor 1.
Then perform left rotation around A’s left child, followed by right rotation around A. (left-right rotation)
TCSS 342B v1.0
26
AVL deletion (2)• Let A be the unbalanced node. Additional 2 mirror image
cases:3. A has balance factor 2 and A’s right child has balance factor –1.
Then perform right rotation around A’s right child, followed by left rotation around A. (right-left rotation)
4. A has balance factor 2 and A’s right child has balance factor 1. Then perform left rotation around A. (left rotation)
5. Additional cases?
• After rebalancing, continue up the tree updating heights. Must continue checking for imbalances in balance factor, and rebalancing if necessary.
• Are all cases handled? • What if item removed was A’s left child or right child? • What if A’s child has balance factor 0?
TCSS 342B v1.0
27
Red-Black Trees
• Root property: Root is BLACK.• External Property: Every external node is BLACK
(external nodes: null nodes) – external nodes not drawn in pictures..
• Internal property: Children of a RED node are BLACK.
• Depth property: All external nodes have the same BLACK depth. – (BLACK depth = depth counting just black nodes)
TCSS 342B v1.0
28
A RedBlack tree.Black depth 3.
30
15 70
20 8510
5
60
6550
40
9080
55
TCSS 342B v1.0
29
figure 10.16 Valid red/black trees
TCSS 342B v1.0
30
red/black tree after insertion
TCSS 342B v1.0
31
red/black tree after removal
TCSS 342B v1.0
32
RedBlack
Insertion
TCSS 342B v1.0
33
Red Black Trees, Insertion
1. Find proper external node.2. Insert and color node red.3. No black depth violation but may violate the
red-black parent-child relationship.4. Fix red-red violation on current level, then move
upwards and repeatLet: z be the current red node with a red parent v. (z may
have been just inserted). Let u be its grandparent. u must be black.Two cases, discussed next
5. Color root black.
TCSS 342B v1.0
34
Fixing Tree, Case one• Red child, red parent. Parent has a black
sibling
a
b u
v w
zVl
ZlZr
TCSS 342B v1.0
35
Case I: Rotate and recolor• Z-middle key. Black height does not
change! No more red-red.
a
b z
v
w
u
Vl Zl Zr
TCSS 342B v1.0
36
Still Case One
a
b u
v w
z
Vr
ZlZr
TCSS 342B v1.0
37
Case I: Rotate and recolor• v-middle key a
b v
z u
ZrZlw
Vr
TCSS 342B v1.0
38
Case II
• Red child, red parent. Parent has a red sibling.
a
b u
v w
zVl
Zr
TCSS 342B v1.0
39
Case II: Recolor
• Red-red may move up…
a
b u
v w
zVl
ZrZl
TCSS 342B v1.0
40
Red Black Trees, Insertion
4. Fix red-red violation on current level, then move upwards (by two levels) and repeat
Let: z, v, u be current red node, red parent, and black grandparent,
Case one: v has black sibling. Then rotate middle key of (z,v,u) up to the grandparent position. Color middle key (in former grandparent position) black, and its children red.
Case two: v has red sibling. Then recolor grandparent red and its children black.
TCSS 342B v1.0
41
Red Black Tree
• Insert 10 – root
10
TCSS 342B v1.0
42
Red Black Tree
• Insert 10 – root (external nodes not shown)
10
TCSS 342B v1.0
43
Red Black Tree
• Insert 85
10
85
TCSS 342B v1.0
44
Red Black Tree
• Insert 15
10
85
15
TCSS 342B v1.0
45
Red Black Tree
• Rotate – Change colors
15
10 85
TCSS 342B v1.0
46
Red Black Tree
• Insert 70
15
10 85
70
TCSS 342B v1.0
47
Red Black Tree
• Change Color
15
10 85
70
TCSS 342B v1.0
48
Red Black Tree
• Insert 20 (sibling of parent is black)
20
15
10 85
70
TCSS 342B v1.0
49
Red Black Tree
• Rotate
15
10 70
20 85
TCSS 342B v1.0
50
Red Black Tree
• Insert 60 (sibling of parent is red)
15
10 70
20 85
60
TCSS 342B v1.0
51
Red Black Tree
• Change Color
15
10 70
20 85
60
TCSS 342B v1.0
52
Red Black Tree
• Insert 30 (sibling of parent is black)
15
10 70
20 85
60
30
TCSS 342B v1.0
53
Red Black Tree
• Rotate
15
10 70
30 85
6020
TCSS 342B v1.0
54
Red Black Tree
• Insert 50 (sibling ?)
15
10 70
30 85
6020
50
TCSS 342B v1.0
55
Red Black Tree
• Insert 50 (sibling of 70 is black!)
15
10 70
30 85
6020
50Oops, red-red. ROTATE!
Child 30
Parent 70
gramps 15
TCSS 342B v1.0
56
Red Black Tree
• Double Rotate – Adjust colors
30
15 70
20 8510 60
50Child-Parent-Gramps
Middle goes to “top”
Previous top becomes child. Its right child is middles left child.
TCSS 342B v1.0
57
Red Black Tree
• Insert 65
30
15 70
20 8510 60
6550
TCSS 342B v1.0
58
Red Black Tree
• Insert 80
30
15 70
20 8510 60
6550 80
TCSS 342B v1.0
59
Red Black Tree
• Insert 90
30
15 70
20 8510 60
6550 9080
TCSS 342B v1.0
60
Red Black Tree
• Insert 40
30
15 70
20 8510 60
6550
40
9080
TCSS 342B v1.0
61
Red Black Tree
• Adjust color
30
15 70
20 8510 60
6550
40
9080
TCSS 342B v1.0
62
Red Black Tree
• Adjust color
30
15 70
20 8510 60
6550
40
9080
TCSS 342B v1.0
63
Red Black Tree
• Insert 5
30
15 70
20 8510
5
60
6550
40
9080
TCSS 342B v1.0
64
Red Black Tree
• Insert 55
30
15 70
20 8510
5
60
6550
40
9080
55
TCSS 342B v1.0
65
Delete
• We first note that a red node is either a leaf or must have two children.
• Also, if a black node has a single child it must be a red leaf.
• Swap X with inorder successor.• If inorder successor is red, (must be a leaf) delete.
If it is a single child parent, delete and change its child color to black. In both cases the resulting tree is a legit red-black tree.
TCSS 342B v1.0
66
Delete demo
• Delete 30: Swap with 40 and delete red leaf.
30
15 70
20 8510
5
60
6550
40
9080
55
TCSS 342B v1.0
67
40
15 70
20 8510
5
60
6550 9080
55
TCSS 342B v1.0
68
Inorder successor is BlackChange colors along the traverse path so that the leaf to be deleted is RED.
Delete 15.30
15 70
20 8510
5
60
6550
40
9080
55
TCSS 342B v1.0
69
General strategy
• As you traverse the tree to locate the inorder successor let X be the current node, T its sibling and P the parent.
• Color the root red.
• Retain: “the color of P is red.”
• If all children of X and T are black:
• P Black, X Red, T Red
TCSS 342B v1.0
70
X T
P
Both children of X and T are black:
P Black X Red, T Red
A B
TCSS 342B v1.0
71
X T
P
If X is a leaf we are done. Recall: x is the inorder successor!
A B
TCSS 342B v1.0
72
X T
P
C1
B C
D
Zig-Zag, C1 Middle key.
A
Even though we want to proceed with X we have a red-red violation that needs to be fixed.
T has a red child.
Even though we want to proceed with X we have a red-red violation that needs to be fixed.
T has a red child.
TCSS 342B v1.0
73
P T
C1
B C D
X
A
Note: black depth remains unchanged!
Note: black depth remains unchanged!
TCSS 342B v1.0
74
Third case
X T
P
C1
B
C D
A
T middle key.
B will become P’s right child. No change in depth.
B will become P’s right child. No change in depth.
TCSS 342B v1.0
75
P C1
T
B C DA
X
TCSS 342B v1.0
76
• If both children of T are red select one of the two rotations.
• If the right child of X is red make it the new parent (it is on the inorder-successor path).
• If the left child of X is red:
TCSS 342B v1.0
77
T
P
X
EC1
B
Y
C
AB
Root of C is black
Otherwise, continue
X has a red child
Root of C is black
Otherwise, continue
X has a red child
TCSS 342B v1.0
78
T
P
XE
C1
Y
CA
B
TCSS 342B v1.0
79
30
15 70
20 8510
5
60
6550
40
9080
55
Delete 15
TCSS 342B v1.0
80
30
20
70
10
8515
5
60
6550
40 908055
Delete 15
30
15
70
10
8520
5
60
6550
40 908055
Swap (15, 20)
TCSS 342B v1.0
81
30
15
70
10
8520
5
60
6550
40 908055
Delete 15
Third case: (mirror image) X (15) has two black children (Nulls)
Sibling has one red and one black child.
TCSS 342B v1.0
82
30 70
10 85
205
60
6550
40 908055
Delete 15
TCSS 342B v1.0
83
References
• Lewis & Chase book, Chapter 13.