360 likes | 473 Vues
AVL Trees. When bad trees happen to good programmers. Good tree. Bad tree. Bad tree. Left and right subtrees have the same height!. For every node in the tree, the left and right subtrees of that node have the same height. Too rigid. AVL Trees. (Adelson-Velskii and Landis)
E N D
AVL Trees When bad trees happen to good programmers.
Bad tree. Left and right subtrees have the same height!
For everynode in the tree, the left and right subtrees of that node have the same height. Too rigid.
AVL Trees • (Adelson-Velskii and Landis) • Binary search trees with an additional property. • For every node in the tree, the height of the left and right subtrees differ by at most 1.
AVL Property • If N is a node in a binary tree, node N has AVL property if the heights of the left sub-tree and right sub-tree are equal or if they differ by 1. Key Point
Height of a Tree • Definition is same as level. Height of a tree is the length of the longest path from root to some leaf node. • Height of an empty tree is -1. • Height of a single node tree is 0. • Recursive definition: • height(t) = 0 if number of nodes = 1 = -1 if T is empty = 1+ max(height(LT), height(RT)) otherwise
5 3 4 1 2 2 3 1 1 0 0 2 0 1 1 0 0 0 1 0 0 0 0 Checking Balance
7 4 3 9 2 6 5 8 3 5 8 4 6 7 9 3 8 2 5 6 12 4 7 4 5 9 18 6 8 16 23 Examples
Computing Height height( T:bin_tree ) { if T = null then return -1 HL = height( T->Left) HR = height( T->Right) H = max( HL, HR ) + 1 return H }
Closer Inspection of “Balance” • A tree is balancedif for every node in the tree, the height of the left and right subtrees differ by at most 1. • A node is balanced if • The left and right subtrees are balanced and • The height of the left and right subtrees differ by at most 1 • A tree is balanced if its root node is balanced.
Checking Balance ( Height, Boolean ) balance_test( T:bin_tree ) { if T = null then return (-1, true) ( HL, BL ) = balance_test( T->Left) ( HR, BR ) = balance_test( T->Right) H = max( H_L, H_R ) + 1 if (abs(HL-HR)<=1 && BL=true && BR=true) then B=true else B=false return ( H, B ) }
Operations • Find, FindMin, FindMax • O(log N) time • Insert • Need to maintain balance • O(log N) time • Delete • a little Complicated
For starters… binary search tree permutations of 1, 2, 3
Details • Balance Factor • the difference between the height of a node’s left subtree and the height of its right subtree • Height-Balanced Trees – all of its nodes have a balance factor of 1, 0, or -1.
Examples BF = -2 BF = -1 BF = 0
Examples BF = 2 BF = 1 BF = 0
Inserting a value into an AVL tree 1. Follow the insertion path (if < go left, otherwise go right). • Remember the deepest node with a balance factor of +1 or -1. (This is called the pivot node.) 3. Insert the node at the appropriate point.
Inserting a value into an AVL tree 4. Recompute balance factors from the pivot node on down, including the pivot node. 5. Has the absolute value of the pivot node’s balance factor increased from 1 to 2?
Rebalancing the tree • This has the visual effect of rotating the subtree of the pivot node. • This is also called an AVL rotation. • (Betcha didn’t see that one coming! ☺)
3 2 1 AVL Balancing : Four Rotations Single right
AVL Balancing : Four Rotations 1 1 3 Double right 2 2 2 2 1 Single right 3 3 3 1 1 1 2 3 Single left Double left 2 2 1 3 1 2 3 3
Let’s take a closer look at the cases The one selected depends on the direction of the “Guilty” insertion, relative to the pivot node.
pivot node 1 Case 1 • The insertion that unbalanced the tree occurred in the left subtree of the left child of the pivot node. BF=1 insert 10 BF=0 12 BF=0 5 BF=0 7 BF=0 3
1 Case 1 BF=1 BF=2 10 BF=0 BF=1 12 BF=0 5 BF=0 BF=1 7 BF=0 3 BF=0
Case 1 steps for AVL rotation • pivot’s left child becomes new pivot • pivots left child retains its left subtree • old pivot becomes the right child of new pivot (it’s old left child) • old pivot takes the right subtree of its old left child (the new pivot now) and makes it the left subtree • old pivot retains its right subtree
pivot node 5 1 Case 1 10 12 7 3 pivot’s left child becomes new pivot
new pivot node 5 1 3 Case 1 pivots left child retains its left subtree pivot’s left child becomes new pivot
new pivot node 5 1 10 3 Case 1 old pivot becomes the right child of new pivot (it’s old left child) pivots left child retains its left subtree
new pivot node 5 1 10 3 7 Case 1 old pivot becomes the right child of new pivot (it’s old left child) old pivot takes the right subtree of its left child (the new pivot now) and makes it the left subtree
new pivot node 12 5 1 10 3 7 Case 1 old pivot takes the right subtree of its left child (the new pivot now) and makes it the left subtree old pivot retains its right subtree
Case 2 • The insertion that unbalanced the tree occurred in the right subtree of the right child of the pivot node. Case 3 • The insertion that unbalanced the tree occurred in the right subtree of the left child of the pivot node. 3 subcases
Case 4 • The insertion that unbalanced the tree occurred in the left subtree of the right child of the pivot node. 3 subcases
Parting thoughts on AVL trees • Performance really depends on the unreliability of the input data. • i.e., you need almost sorted data to make it worth it! • A lot of design time is involved. • A lot of work, too! • Make sure you need it!