Presentation is loading. Please wait.

Presentation is loading. Please wait.

AVL Trees When bad trees happen to good programmers.

Similar presentations


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

1 AVL Trees When bad trees happen to good programmers.

2 Good tree.

3 Bad tree.

4 Bad tree. Left and right subtrees have the same height!

5 For every node in the tree, the left and right subtrees of that node have the same height. Too rigid.

6 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.

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

8 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

9 Checking Balance

10 Examples

11 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 }

12 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.

13 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 ) }

14 Operations Find, FindMin, FindMax O(log N) time Insert Need to maintain balance O(log N) time Delete a little Complicated

15 For starters… binary search tree permutations of 1, 2, 3

16 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.

17 Examples BF = 0 BF = -1 BF = -2

18 BF = 0 BF = 1 BF = 2

19 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.

20 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

21 If yes, rebalance the tree!!!

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

23 AVL Balancing : Four Rotations Single right 1 2 3

24 AVL Balancing : Four Rotations Single right Single left Double right Double left

25 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.

26 Case 1 The insertion that unbalanced the tree occurred in the left subtree of the left child of the pivot node insert 1 BF=1 BF=0 pivot node

27 Case BF=1 BF=0 BF=2 BF=1 BF=0

28 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

29 Case pivot node pivot’s left child becomes new pivot

30 Case new pivot node pivots left child retains its left subtreepivot’s left child becomes new pivot

31 Case 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

32 Case 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

33 Case 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

34 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

35 Case 4 The insertion that unbalanced the tree occurred in the left subtree of the right child of the pivot node. 3 subcases

36 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!


Download ppt "AVL Trees When bad trees happen to good programmers."

Similar presentations


Ads by Google