830 likes | 980 Views
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
E N D
TCSS 342, Winter 2006Lecture Notes Balanced Binary Search Trees TCSS 342B v1.0
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
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
Balanced AVL Tree TCSS 342B v1.0
extremely unbalanced tree TCSS 342B v1.0
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
Which are AVL Trees? TCSS 342B v1.0
Examples of AVL Trees 1 0 -1 0 0 0 -1 0 -1 1 -1 0 0 0 0 TCSS 342B v1.0
Not AVL Trees -1 -2 -2 -1 2 -1 2 0 0 1 0 -1 0 0 TCSS 342B v1.0
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
balancing a tree with a right rotation TCSS 342B v1.0
A right rotation in an AVL tree TCSS 342B v1.0
FIGURE 13.11 Unbalanced tree and balanced tree after a left rotation TCSS 342B v1.0
figure 10.12 A right-left rotation TCSS 342B v1.0
figure 10.13 A left-right rotation TCSS 342B v1.0
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
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
Right rotation to fix Case 1 • right rotation (clockwise): left child becomes parent; original parent demoted to right TCSS 342B v1.0
Left rotation to fix Case 4 • left rotation (counter-clockwise): right child becomes parent; original parent demoted to left TCSS 342B v1.0
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
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
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
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
a right-left rotationafter removal TCSS 342B v1.0
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
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
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
30 15 70 10 20 85 60 5 80 90 50 65 40 55 A RedBlack tree.Black depth 3. TCSS 342B v1.0
figure 10.16 Valid red/black trees TCSS 342B v1.0
red/black tree after insertion TCSS 342B v1.0
red/black tree after removal TCSS 342B v1.0
RedBlack Insertion TCSS 342B v1.0
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
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
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
Still Case One a b u w v Vr z Zr Zl TCSS 342B v1.0
Case I: Rotate and recolor a • v-middle key b v u z w Zl Zr Vr TCSS 342B v1.0
Case II • Red child, red parent. Parent has a red sibling. a b u w v z Vl Zr TCSS 342B v1.0
Case II: Recolor • Red-red may move up… a b u w v z Vl Zr Zl TCSS 342B v1.0
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
Red Black Tree • Insert 10 – root 10 TCSS 342B v1.0
Red Black Tree • Insert 10 – root (external nodes not shown) 10 TCSS 342B v1.0
Red Black Tree • Insert 85 10 85 TCSS 342B v1.0
Red Black Tree • Insert 15 10 85 15 TCSS 342B v1.0
Red Black Tree • Rotate – Change colors 15 10 85 TCSS 342B v1.0
Red Black Tree • Insert 70 15 10 85 70 TCSS 342B v1.0
Red Black Tree • Change Color 15 10 85 70 TCSS 342B v1.0
Red Black Tree • Insert 20 (sibling of parent is black) 15 10 85 70 20 TCSS 342B v1.0
Red Black Tree • Rotate 15 10 70 85 20 TCSS 342B v1.0
Red Black Tree • Insert 60 (sibling of parent is red) 15 10 70 85 20 60 TCSS 342B v1.0