2. AVL Rotate Right / Left Simple Double Complex Rotate Rotate Algo Double Rotate Avoid Taking Printout : Use RTF Outline in case needed 2 Agenda
3. AVL Trees 1962 – Two Russian Mathematicians Adelson-Velskii and Landis Heights of Subtree do not defer by more than one Avoid Taking Printout : Use RTF Outline in case needed 3
4. AVL Trees - Why Avoid Taking Printout : Use RTF Outline in case needed 4 18 14 Unbalanced BST Tree 12 8 8 12 4 4 14 18 AVL
5. AVL – Rotate Right Please Do Not Take Printout : Use RTF Outline in case needed 5 12 8 4
6. AVL – Rotate Left Please Do Not Take Printout : Use RTF Outline in case needed 6 4 8 12
7. Complex Right Please Do Not Take Printout : Use RTF Outline in case needed 7 18 20 12 8 14 4
8. Complex Left Please Do Not Take Printout : Use RTF Outline in case needed 8 8 14 4 18 12 20
9. Complex Right - Algo Please Do Not Take Printout : Use RTF Outline in case needed 9 18 Root Result Should Be 12 tempPtr 12 18 8 8 14 14
10. Complex Right - Algo Please Do Not Take Printout : Use RTF Outline in case needed 10 tempPtr = root -> left Root -> left = tempPtr -> right tempPtr -> right = root Root = tempPtr return 18 Root 12 tempPtr 8 14
11. Simple Double Rotate Right Please Do Not Take Printout : Use RTF Outline in case needed 11 12 4 Step 1 : 8
12. Simple Double Rotate Right Please Do Not Take Printout : Use RTF Outline in case needed 12 12 8 Step 2 : 4
13. AVL Node Node Key Data left Right Bal (RH , LH , EH) End Node Avoid Taking Printout : Use RTF Outline in case needed 13
14. AVL Insert / Transcript In linked list we did search and then insert But here we need to adjust balance and may have to adjust tree not only at the point of insertion but all the way up We set taller flag Let prev module decide as we back out of recursions Avoid Taking Printout : Use RTF Outline in case needed 14
15. AVL Insert Please Do Not Take Printout : Use RTF Outline in case needed 15 60 50 EH Other Node already inserted 13 EH 18 LH 12 20 EH EH 8 14 EH EH Different stages of a node visit ?
16. Flow Chart Please Do Not Take Printout : Use RTF Outline in case needed 16 Recursion If tree is taller If LH and we inserted on left Yes Yes Balance No No Yes If RH and we inserted on right
17. AVL INSERT ALGO If root is null Taller = true Root = newPtr else If insert on left “ < “ AVL Insert (root -> left) if taller if LH do left balance else if root is RH taller=false adjust balance factor Avoid Taking Printout : Use RTF Outline in case needed 17 When will it not be taller ?
18. AVL left Balance leftTree= root - >left If leftTree is LH Case 1: left of left : single rotate Else Case 2: right of left : Double Rotation rightTree=leftTree->right Adjust balance for Right Tree rotateLeft(leftTree) rotateRight(root) Taller = false return Avoid Taking Printout : Use RTF Outline in case needed 18
19. Flow Please Do Not Take Printout : Use RTF Outline in case needed 19