Download presentation
Presentation is loading. Please wait.
1
TCSS 342, Winter 2006 Lecture 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 1 -1 -1 -1 1 -1 TCSS 342B v1.0
9
Not AVL Trees -1 -2 -2 -1 2 -1 2 1 -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 to fix Case 1
right rotation (clockwise): left child becomes parent; original parent demoted to right TCSS 342B v1.0
19
Left rotation to fix Case 4
left rotation (counter-clockwise): right child becomes parent; original parent demoted to left 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 rotation after 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 85 10 5 60 65 50 40 90 80 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
Find proper external node. Insert and color node red. No black depth violation but may violate the red-black parent-child relationship. Fix red-red violation on current level, then move upwards and repeat Let: 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 Color root black. TCSS 342B v1.0
34
Fixing Tree, Case one Red child, red parent. Parent has a black sibling a b u w v z Vl Zr Zl 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 u v w Zr Zl Vl TCSS 342B v1.0
36
Still Case One a b u w v Vr z Zr Zl TCSS 342B v1.0
37
Case I: Rotate and recolor
v-middle key a b v u z w Zl Zr Vr TCSS 342B v1.0
38
Case II Red child, red parent. Parent has a red sibling. a b u w v z
Vl Zr TCSS 342B v1.0
39
Case II: Recolor Red-red may move up… a b u w v z Vl Zr Zl
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) 15 10 85 70 20
TCSS 342B v1.0
49
Red Black Tree Rotate 15 10 70 85 20 TCSS 342B v1.0
50
Red Black Tree Insert 60 (sibling of parent is red) 15 10 70 85 20 60
TCSS 342B v1.0
51
Red Black Tree Change Color 15 10 70 85 20 60 TCSS 342B v1.0
52
Red Black Tree Insert 30 (sibling of parent is black) 15 10 70 85 20
60 30 TCSS 342B v1.0
53
Red Black Tree Rotate 15 10 70 85 30 20 60 TCSS 342B v1.0
54
Red Black Tree Insert 50 (sibling ?) 15 10 70 85 30 20 60 50
TCSS 342B v1.0
55
Red Black Tree Insert 50 (sibling of 70 is black!) gramps 15 15
Parent 70 10 70 85 30 Child 30 20 60 Oops, red-red. ROTATE! 50 TCSS 342B v1.0
56
Red Black Tree Double Rotate – Adjust colors 30 15 70 10 20 85 60
Child-Parent-Gramps Middle goes to “top” Previous top becomes child. Its right child is middles left child. 50 TCSS 342B v1.0
57
Red Black Tree Insert 65 30 15 70 10 20 85 60 50 65 TCSS 342B v1.0
58
Red Black Tree Insert 80 30 15 70 10 20 85 60 80 50 65 TCSS 342B v1.0
59
Red Black Tree Insert 90 30 15 70 10 20 85 60 80 90 50 65 TCSS 342B v1.0
60
Red Black Tree Insert 40 30 15 70 10 20 85 60 80 90 50 65 40 TCSS 342B v1.0
61
Red Black Tree Adjust color 30 15 70 10 20 85 60 80 90 50 65 40
TCSS 342B v1.0
62
Red Black Tree Adjust color 30 15 70 10 20 85 60 80 90 50 65 40
TCSS 342B v1.0
63
Red Black Tree Insert 5 30 15 70 10 20 85 60 5 80 90 50 65 40 TCSS 342B v1.0
64
Red Black Tree Insert 55 30 15 70 10 20 85 60 5 80 90 50 65 40 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 10
20 85 60 5 80 90 50 65 40 55 TCSS 342B v1.0
67
40 15 70 10 20 85 60 5 80 90 50 65 55 TCSS 342B v1.0
68
Inorder successor is Black
Change colors along the traverse path so that the leaf to be deleted is RED. Delete 15. 30 15 70 10 20 85 60 5 80 90 50 65 40 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
Both children of X and T are black: P Black X Red, T Red
TCSS 342B v1.0
71
If X is a leaf we are done. Recall: x is the inorder successor!
P X T A B If X is a leaf we are done. Recall: x is the inorder successor! TCSS 342B v1.0
72
P X T C1 A D B C Zig-Zag, C1 Middle key.
Even though we want to proceed with X we have a red-red violation that needs to be fixed. T has a red child. P X T C1 A D B C Zig-Zag, C1 Middle key. TCSS 342B v1.0
73
Note: black depth remains unchanged!
X B C D A TCSS 342B v1.0
74
Third case P X T C1 A B D C T middle key.
B will become P’s right child. No change in depth. Third case P X T C1 A B D C T middle key. TCSS 342B v1.0
75
T P C1 X B C D A 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
P X T C1 E C B Y A B Root of C is black Otherwise, continue
X has a red child P X T C1 E C B Y A B TCSS 342B v1.0
78
P C1 T X E Y A C B TCSS 342B v1.0
79
30 Delete 15 15 70 10 20 85 60 5 80 90 50 65 40 55 TCSS 342B v1.0
80
60 Delete 15 30 70 15 85 50 65 10 20 80 90 40 55 60 5 Swap (15, 20) 30 70 20 85 50 65 10 15 80 90 40 55 5 TCSS 342B v1.0
81
Third case: (mirror image) X (15) has two black children (Nulls)
60 Delete 15 30 70 20 85 50 65 10 15 80 90 40 55 5 Third case: (mirror image) X (15) has two black children (Nulls) Sibling has one red and one black child. TCSS 342B v1.0
82
60 Delete 15 30 70 10 85 50 65 5 20 80 90 40 55 TCSS 342B v1.0
83
References Lewis & Chase book, Chapter 13. TCSS 342B v1.0
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.