460 likes | 488 Views
AVL Trees. CSE 373 Data Structures Winter 2006. Binary Search Tree - Best Time. All BST operations are O(d), where d is tree depth minimum d is for a binary tree with N nodes What is the best case tree? What is the worst case tree?
E N D
AVL Trees CSE 373 Data Structures Winter 2006
Binary Search Tree - Best Time • All BST operations are O(d), where d is tree depth • minimum d is for a binary tree with N nodes • What is the best case tree? • What is the worst case tree? • So, best case running time of BST operations is O(log N) CSE 373 - AU 06 -- AVL Trees
Binary Search Tree - Worst Time • Worst case running time is O(N) • What happens when you Insert elements in ascending order? • Insert: 2, 4, 6, 8, 10, 12 into an empty BST • Problem: Lack of “balance”: • compare depths of left and right subtree • Unbalanced degenerate tree CSE 373 - AU 06 -- AVL Trees
Balanced and unbalanced BST 1 4 2 2 5 3 1 3 4 4 Is this “balanced”? 5 2 6 6 1 3 5 7 7 CSE 373 - AU 06 -- AVL Trees
Approaches to balancing trees • Don't balance • May end up with some nodes very deep • Strict balance • The tree must always be balanced perfectly • Pretty good balance • Only allow a little out of balance • Adjust on access • Self-adjusting CSE 373 - AU 06 -- AVL Trees
Balancing Binary Search Trees • Many algorithms exist for keeping binary search trees balanced • Adelson-Velskii and Landis (AVL) trees (height-balanced trees) • Splay trees and other self-adjusting trees • B-trees and other multiway search trees CSE 373 - AU 06 -- AVL Trees
Perfect Balance • Want a complete tree after every operation • tree is full except possibly in the lower right • This is expensive • For example, insert 2 in the tree on the left and then rebuild as a complete tree 6 5 Insert 2 & complete tree 4 9 2 8 1 5 8 1 4 6 9 CSE 373 - AU 06 -- AVL Trees
AVL - Good but not Perfect Balance • AVL trees are height-balanced binary search trees • Balance factor of a node • height(left subtree) - height(right subtree) • An AVL tree has balance factor calculated at every node • For every node, heights of left and right subtree can differ by no more than 1 • Store current heights in each node CSE 373 - AU 06 -- AVL Trees
Height of an AVL Tree • N(h) = minimum number of nodes in an AVL tree of height h. • Basis • N(0) = 1, N(1) = 2 • Induction • N(h) = N(h-1) + N(h-2) + 1 • Solution(Fibonacci) • N(h) >h ( 1.62) h h-2 h-1 CSE 373 - AU 06 -- AVL Trees
Height of an AVL Tree • N(h) >h ( 1.62) • Suppose we have n nodes in an AVL tree of height h. • n >N(h) (because N(h) was the minimum) • n >h hence log n > h (relatively well balanced tree!!) • h < 1.44 log2n (i.e., O(height) takes O(log n)) CSE 373 - AU 06 -- AVL Trees
Node Heights Tree A (AVL) Tree B (AVL) height=3 balance factor=2-1=1 3 6 6 2 1 2 2 4 9 4 9 1 1 1 1 1 1 5 1 5 8 CSE 373 - AU 06 -- AVL Trees
Worst-case AVL Trees 4 nodes: h = 3 vs 3 7 nodes: h = 4 vs 3 12 nodes: h = 5 vs 4 20 nodes: h = 6 vs 5 33 nodes: h = 7 vs 6 54 nodes: h = 8 vs 6 88 nodes: h = 9 vs 7 143 nodes: h = 10 vs 8 232 nodes: h = 11 vs 8 376 nodes: h = 12 vs 9 609 nodes: h = 13 vs 10 986 nodes: h = 14 vs 10 1596 nodes: h = 15 vs 11 CSE 373 - AU 06 -- AVL Trees
Node Heights after Insert 7 Tree A (AVL) Tree B (not AVL) balance factor 2-0 = 2 3 4 6 6 2 2 2 3 4 9 4 9 0 1 1 1 1 1 2 7 1 5 1 5 8 1 7 CSE 373 - AU 06 -- AVL Trees
Insert and Rotation in AVL Trees • Insert operation may cause balance factor to become 2 or –2 for some node • only nodes on the path from insertion point to root node have possibly changed in height • So after the Insert, go back up to the root node by node, updating heights • If a new balance factor (the difference hleft-hright) is 2 or –2, adjust tree by rotationaround the node CSE 373 - AU 06 -- AVL Trees
Single Rotation in an AVL Tree 3 3 6 6 2 3 2 2 4 9 4 8 1 1 1 1 2 1 1 7 9 1 5 8 1 5 1 7 CSE 373 - AU 06 -- AVL Trees
Insertions in AVL Trees Let the node that needs rebalancing be . There are 4 cases: Outside Cases (require single rotation) : 1. Insertion into left subtree of left child of . 2. Insertion into right subtree of right child of . Inside Cases (require double rotation) : 3. Insertion into right subtree of left child of . 4. Insertion into left subtree of right child of . The rebalancing is performed through four separate rotation algorithms. CSE 373 - AU 06 -- AVL Trees
AVL Insertion: Outside Case f Consider a valid AVL subtree b h G h h A D CSE 373 - AU 06 -- AVL Trees
AVL Insertion: Outside Case f Inserting into A destroys the AVL property at node f b h G h+1 h D A CSE 373 - AU 06 -- AVL Trees
AVL Insertion: Outside Case f Do a “right rotation” b h G h+1 h D A CSE 373 - AU 06 -- AVL Trees
Single right rotation f Do a “right rotation” b h G h+1 h D A CSE 373 - AU 06 -- AVL Trees
Outside Case Completed “Right rotation” done (“Left rotation” is mirror symmetric) b f h+1 h h A G D AVL property has been restored CSE 373 - AU 06 -- AVL Trees
AVL Insertion: Inside Case f Consider a valid AVL subtree b h G h h A D CSE 373 - AU 06 -- AVL Trees
AVL Insertion: Inside Case f Inserting into D destroys the AVL property at node f Does “right rotation” restore balance? b h G h h+1 A D CSE 373 - AU 06 -- AVL Trees
AVL Insertion: Inside Case b “Right rotation” does not restore balance… now b is out of balance f h A h h+1 G D CSE 373 - AU 06 -- AVL Trees
AVL Insertion: Inside Case f Consider the structure of subtree Y… b h G h h+1 A D CSE 373 - AU 06 -- AVL Trees
AVL Insertion: Inside Case f Y = node i and subtrees V and W b h G d h h+1 A h or h-1 E C CSE 373 - AU 06 -- AVL Trees
AVL Insertion: Inside Case f We will do a left-right “double rotation” . . . b G d A E C CSE 373 - AU 06 -- AVL Trees
Double rotation : first rotation f left rotation complete d G b E C A CSE 373 - AU 06 -- AVL Trees
Double rotation : second rotation f Now do a right rotation d G b E C A CSE 373 - AU 06 -- AVL Trees
Double rotation : second rotation right rotation complete Balance has been restored d f b h h h or h-1 C G E A CSE 373 - AU 06 -- AVL Trees
Example of Insertions in an AVL Tree 3 20 Insert 5, 40 1 2 10 30 1 1 35 25 CSE 373 - AU 06 -- AVL Trees
Example of Insertions in an AVL Tree 3 4 20 20 2 2 2 3 10 30 10 30 1 1 1 2 1 1 5 35 5 35 25 25 1 40 Now Insert 45 CSE 373 - AU 06 -- AVL Trees
Single rotation (outside case) 5 4 20 20 2 4 2 3 10 30 10 30 1 3 1 1 1 2 5 35 5 40 25 25 1 1 35 45 40 2 Imbalance 1 45 Now Insert 34 CSE 373 - AU 06 -- AVL Trees
Double rotation (inside case) 5 4 20 20 4 4 2 3 10 30 10 35 1 3 3 1 2 2 5 Imbalance 40 5 40 25 30 1 25 34 2 1 45 35 45 1 Insertion of 34 1 34 CSE 373 - AU 06 -- AVL Trees
AVL Tree Deletion • Similar but more complex than insertion • Rotations and double rotations needed to rebalance • Imbalance may propagate upward so that many rotations may be needed. CSE 373 - AU 06 -- AVL Trees
Pros and Cons of AVL Trees • Arguments for AVL trees: • Search is O(log N) since AVL trees are always balanced. • Insertion and deletions are also O(logn) • The height balancing adds no more than a constant factor to the speed of insertion. • Arguments against using AVL trees: • Difficult to program & debug; more space for balance factor. • Asymptotically faster but rebalancing costs time. • Most large searches are done in database systems on disk and use other structures (e.g. B-trees). • May be OK to have O(N) for a single operation if total run time for many consecutive operations is fast (e.g. Splay trees). CSE 373 - AU 06 -- AVL Trees
Deletion 6 100 5 4 144 44 3 2 3 4 117 162 17 62 1 2 2 1 2 3 2 132 178 32 78 111 150 50 1 1 1 2 1 1 1 172 72 101 128 132 147 181 1 129 CSE 373 - AU 06 -- AVL Trees
Deletion 6 100 5 4 144 44 3 1 3 4 117 162 17 62 1 2 2 1 2 3 2 132 178 32 78 111 150 50 1 1 1 2 1 1 1 172 72 101 128 132 147 181 1 129 CSE 373 - AU 06 -- AVL Trees
Deletion 6 100 5 144 44 3 4 117 162 17 62 2 2 3 2 132 178 78 111 150 50 1 1 2 1 1 1 172 72 101 128 132 147 181 1 129 CSE 373 - AU 06 -- AVL Trees
Deletion 6 100 5 144 62 3 4 117 162 44 78 2 2 3 2 132 178 17 72 111 150 50 1 1 2 1 1 1 172 101 128 132 147 181 1 129 CSE 373 - AU 06 -- AVL Trees
Deletion 6 100 3 5 144 62 3 2 4 2 117 162 44 78 2 1 1 1 2 3 2 132 178 17 72 111 150 50 1 1 2 1 1 1 172 101 128 132 147 181 1 129 CSE 373 - AU 06 -- AVL Trees
Deletion 6 100 3 5 144 62 3 2 4 2 117 162 44 78 1 1 2 1 2 3 2 132 178 17 72 111 150 50 1 1 2 1 1 1 172 101 128 132 147 181 1 129 CSE 373 - AU 06 -- AVL Trees
Deletion 100 144 62 117 162 44 78 132 178 17 72 111 150 50 172 101 128 132 147 181 129 CSE 373 - AU 06 -- AVL Trees
Deletion 100 117 62 44 144 78 111 132 17 72 101 162 50 128 132 178 150 129 172 147 181 CSE 373 - AU 06 -- AVL Trees
Deletion 117 144 100 132 62 111 162 44 178 78 101 128 132 150 172 17 72 129 147 181 50 CSE 373 - AU 06 -- AVL Trees
Deletion 5 117 4 4 144 100 3 2 3 3 132 62 111 162 2 1 2 2 2 1 2 44 78 101 128 132 178 150 1 1 1 1 1 1 1 17 72 129 50 172 147 181 CSE 373 - AU 06 -- AVL Trees