# AVL Trees When bad trees happen to good programmers.

## Presentation on theme: "AVL Trees When bad trees happen to good programmers."— Presentation transcript:

AVL Trees When bad trees happen to good programmers.

Good 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) 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.

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

Checking Balance 0 00 00 0 000 01 11 1 1 1 2 22 3 3 4 5

Examples 4 6 58 79 3 2 7 9 8 46 5 3 3 2 68 7 5 4 8 6 1623 18 12 945

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 balanced if 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 = 0 BF = -1 BF = -2

BF = 0 BF = 1 BF = 2

Inserting a value into an AVL tree 1. Follow the insertion path (if < go left, otherwise go right). 2. 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.

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? Inserting a value into an AVL tree

If yes, rebalance the tree!!!

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! ☺)

AVL Balancing : Four Rotations Single right 1 2 3

AVL Balancing : Four Rotations Single right 3 2 1 2 13 1 2 3 Single left 2 31 3 2 1 Double right 3 21 3 12 2 3 1 Double left

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.

Case 1 The insertion that unbalanced the tree occurred in the left subtree of the left child of the pivot node. 10 5 12 3 7 insert 1 BF=1 BF=0 pivot node

Case 1 10 5 12 3 7 1 BF=1 BF=0 BF=2 BF=1 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

Case 1 10 5 12 3 7 1 pivot node pivot’s left child becomes new pivot

Case 1 5 3 1 new pivot node pivots left child retains its left subtreepivot’s left child becomes new pivot

Case 1 5 3 1 new pivot node pivots left child retains its left subtree old pivot becomes the right child of new pivot (it’s old left child) 10

Case 1 5 73 1 new pivot node old pivot becomes the right child of new pivot (it’s old left child) 10 old pivot takes the right subtree of its left child (the new pivot now) and makes it the left subtree

Case 1 512 73 1 new pivot node 10 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!

Similar presentations