CS 240 Fall 2015 Mike Lam, Professor I find your lack of balance disturbing. Balanced (AVL) Trees
Review ● Binary Search Trees (BSTs) – Ordered binary tree – Operations are O(h) where h is the height of the tree – For mostly-random insertions and deletions, h ≈ log n ● As in the last part of Monday's lab – For other situations, we need to use a more "balanced" binary tree implementation ● General idea: restore balance after every add or remove ● However, this will take extra work! ● Tradeoff between cost and benefit of balancing
Issues ● How much should we re-balance? – How often? How many nodes? How strict? – How do we measure balance? ● We could re-balance the entire tree after every insertion – This would lead to O(n log n) insertion times – Essentially re-build the tree every time – This seems quite excessive! ● Goal: faster insertions and "good enough" balance – AVL trees (easiest to understand) – Red-Black trees – Many others...
AVL Trees ● AVL Trees – Inventors G. M. A delson- V elsky and E. M. L andis (1962) – Balance factor : height(node->left) – height(node->right) – Height-balance property ● Heights of children differ by at most one ● Balance factor is -1, 0, or 1 – Enforce this property using tree rotations Image from: ht tp://www.geeksforgeeks.org/how-to-determine-if-a-binary-tree-is-balanced/
Tree Height ● Height: # of edges from root to furthest leaf – Minor extension: height of an empty tree will be -1 – Applies to whole trees as well as subtrees Caveat : Heights can't really be negative (what would it mean to have negative edges?), but it does make the math work better...
Binary Search Tree 44 17 88 8 32 65 97 28 54 82 93 29 76 80
Binary Search Tree 44 17 88 h=0 8 32 65 97 h=0 28 h=0 54 82 93 h=0 29 76 h=0 80
Binary Search Tree 44 17 88 h=0 8 32 65 97 h=1 h=1 h=0 28 h=0 54 82 93 h=0 29 76 h=1 h=0 80
Binary Search Tree 44 17 88 h=0 8 32 65 97 h=1 h=-1 h=1 h=0 28 h=0 54 82 93 h=-1 h=0 29 76 h=1 h=0 80
Binary Search Tree 44 17 88 h=0 8 32 65 97 h=1 h=1 h=0 28 h=0 54 82 93 h=0 29 76 h=1 h=0 80
Imbalances ● How do we fix imbalances? – Need to re-arrange tree ● Solution: Rotations! ● Example:
Imbalances ● How do we fix imbalances? – Need to re-arrange tree ● Solution: Rotations! ● Example:
Rotations ● Rotations – Single rotation (below) – Single/double rotations (right) ● Four cases of trinode restructuring x->right = y->left y->left = x "left rotation" x y y x "right rotation" y->left = x->right x->right = y
AVL Trees ● Insertion – Insert into BST as usual ● Binary search until empty subtree is found, then add a new node – Check ancestors of new node for imbalances – Fix imbalances via trinode restructuring ● Removal – Remove from BST as usual – Check ancestors of removed node for imbalances – Fix imbalances via trinode restructuring
AVL Tree h=3 44 h=2 17 78 h=1 32 50 88 h=0 h=1 h=0 h=0 48 62 h=0 Insert 54
AVL Tree h=3 44 h=2 17 78 h=1 32 50 88 h=0 h=1 h=0 h=0 48 62 h=0 Update heights of 54 ancestors and check h=0 for imbalances
AVL Tree h=3 44 h=2 17 78 h=1 32 50 88 h=0 h=1 h=0 h=0 48 62 h=1 Update heights of 54 ancestors and check h=0 for imbalances
AVL Tree h=3 44 h=2 17 78 h=1 32 50 88 h=0 h=2 h=0 h=0 48 62 h=1 Update heights of 54 ancestors and check h=0 for imbalances
AVL Tree h=4 44 h=3 17 78 h=1 32 50 88 h=0 h=2 h=0 h=0 48 62 h=1 Imbalance 54 h=0 detected
AVL Tree h=4 44 h=3 17 78 h=1 32 50 88 h=0 h=2 h=0 h=0 48 62 h=1 54 Nodes involved h=0
AVL Tree h=4 44 h=3 17 78 h=1 32 50 88 h=0 h=2 h=0 h=0 48 62 h=1 54 Rebalance h=0
AVL Tree h=4 44 h=3 17 78 h=1 32 62 88 h=0 h=2 h=0 h=1 50 h=0 54 48 Rebalance h=0
AVL Tree h=3 44 h=2 17 62 h=1 32 50 78 h=0 h=1 h=1 88 h=0 h=0 48 54 h=0 Balanced subtree; continue up tree to root
AVL Tree h=3 44 h=2 17 62 h=1 32 50 78 h=0 h=1 h=1 88 h=0 h=0 48 54 h=0 Done!
Exercises ● 3, 9, 12, 5, 4, 1 ● 10, 5, 7, 15, 9, 25 ● Show every step! – Mark rotations as L or R To check your answers: http://www.qmatica.com/DataStructures/Trees/AVL/AVLTree.html
AVL Tree Analysis ● In general: n ( h ) = 1 + n ( h-1 ) + n ( h-2 ) – n ( h ) = number of nodes in AVL tree of height h – Reasoning: AVL tree with minimal number of nodes has one node and two subtrees: one with height h-1 and one with height h-2 ● (They differ by at most one b/c of the balance property, and you can't skip heights) ● This is a Fibonacci progression ⌊ √ 5 ⌋ h Φ – n(h) >= Fib(h) = (Φ = golden ratio) – Exponential w.r.t. height: n ( h ) is Ω(Φ h ) – Thus, h is O ( log n )
Alternative: Red-Black Trees ● Coloring scheme – Root is colored black – All children of a red node must be colored black (no "double reds") – All nodes with zero or one children have the same number of black-colored ancestors ● Path from root to furthest leaf is no more than twice as long as the path from root to nearest leaf ● Less-strictly balanced ● Faster insertion/removal but slower lookups
Recommend
More recommend