Advanced Data StructuresMphil AVL TreesJibrael Jos : Nov2009
AVLRotate Right / LeftSimple DoubleComplex RotateRotate AlgoDouble RotateAvoid Taking Printout : Use RTF Outline in case needed2Agenda
AVL Trees1962 – Two Russian MathematiciansAdelson-Velskii  and LandisHeights of Subtree do not defer by more than oneAvoid Taking Printout : Use RTF Outline in case needed3
AVL Trees - WhyAvoid Taking Printout : Use RTF Outline in case needed41814Unbalanced  BST Tree128812441418AVL
AVL – Rotate RightPlease Do Not Take Printout : Use RTF Outline in case needed51284
AVL – Rotate LeftPlease Do Not Take Printout : Use RTF Outline in case needed64812
Complex RightPlease Do Not Take Printout : Use RTF Outline in case needed71820128144
Complex LeftPlease Do Not Take Printout : Use RTF Outline in case needed88144181220
Complex Right - AlgoPlease Do Not Take Printout : Use RTF Outline in case needed918RootResult Should Be12tempPtr1218881414
Complex Right - AlgoPlease Do Not Take Printout : Use RTF Outline in case needed10tempPtr = root -> leftRoot -> left = tempPtr -> righttempPtr -> right = rootRoot = tempPtrreturn18Root12tempPtr814
Simple Double Rotate RightPlease Do Not Take Printout : Use RTF Outline in case needed11124Step 1 : 8
Simple Double Rotate RightPlease Do Not Take Printout : Use RTF Outline in case needed12128Step 2 : 4
AVL NodeNodeKeyDataleftRightBal (RH , LH , EH)End NodeAvoid Taking Printout : Use RTF Outline in case needed13
AVL Insert / TranscriptIn linked list we did search and then insertBut here we need to adjust balance and may have to adjust tree not only at the point of  insertion but all the way upWe set taller flagLet prev module decide as we back out of recursionsAvoid Taking Printout : Use RTF Outline in case needed14
AVL InsertPlease Do Not Take Printout : Use RTF Outline in case needed156050EHOther Node already inserted13EH18LH1220EHEH814EHEHDifferent stages of a node visit ?
Flow ChartPlease Do Not Take Printout : Use RTF Outline in case needed16RecursionIf tree is tallerIf LH and we inserted on leftYesYesBalanceNoNoYesIf RH and we inserted on right
AVL INSERT ALGOIf root is null       Taller = true       Root = newPtrelse   If insert on left “ < “         AVL Insert (root -> left)        if taller             if  LH do left balance            else if root is RH                    taller=false            adjust balance factorAvoid Taking Printout : Use RTF Outline in case needed17When will it not be taller ?
AVL left BalanceleftTree= root - >leftIf leftTree is LH            Case 1: left of left : single rotateElseCase 2: right of left : Double RotationrightTree=leftTree->rightAdjust balance for Right TreerotateLeft(leftTree)rotateRight(root)Taller = falsereturnAvoid Taking Printout : Use RTF Outline in case needed18
FlowPlease Do Not Take Printout : Use RTF Outline in case needed19
ReferenceMy CourseFurzonPlease Do Not Take Printout : Use RTF Outline in case needed20Action ItemSelf Learning

Data Structures : AVL Trees

  • 1.
    Advanced Data StructuresMphilAVL TreesJibrael Jos : Nov2009
  • 2.
    AVLRotate Right /LeftSimple DoubleComplex RotateRotate AlgoDouble RotateAvoid Taking Printout : Use RTF Outline in case needed2Agenda
  • 3.
    AVL Trees1962 –Two Russian MathematiciansAdelson-Velskii and LandisHeights of Subtree do not defer by more than oneAvoid Taking Printout : Use RTF Outline in case needed3
  • 4.
    AVL Trees -WhyAvoid Taking Printout : Use RTF Outline in case needed41814Unbalanced BST Tree128812441418AVL
  • 5.
    AVL – RotateRightPlease Do Not Take Printout : Use RTF Outline in case needed51284
  • 6.
    AVL – RotateLeftPlease Do Not Take Printout : Use RTF Outline in case needed64812
  • 7.
    Complex RightPlease DoNot Take Printout : Use RTF Outline in case needed71820128144
  • 8.
    Complex LeftPlease DoNot Take Printout : Use RTF Outline in case needed88144181220
  • 9.
    Complex Right -AlgoPlease Do Not Take Printout : Use RTF Outline in case needed918RootResult Should Be12tempPtr1218881414
  • 10.
    Complex Right -AlgoPlease Do Not Take Printout : Use RTF Outline in case needed10tempPtr = root -> leftRoot -> left = tempPtr -> righttempPtr -> right = rootRoot = tempPtrreturn18Root12tempPtr814
  • 11.
    Simple Double RotateRightPlease Do Not Take Printout : Use RTF Outline in case needed11124Step 1 : 8
  • 12.
    Simple Double RotateRightPlease Do Not Take Printout : Use RTF Outline in case needed12128Step 2 : 4
  • 13.
    AVL NodeNodeKeyDataleftRightBal (RH, LH , EH)End NodeAvoid Taking Printout : Use RTF Outline in case needed13
  • 14.
    AVL Insert /TranscriptIn linked list we did search and then insertBut here we need to adjust balance and may have to adjust tree not only at the point of insertion but all the way upWe set taller flagLet prev module decide as we back out of recursionsAvoid Taking Printout : Use RTF Outline in case needed14
  • 15.
    AVL InsertPlease DoNot Take Printout : Use RTF Outline in case needed156050EHOther Node already inserted13EH18LH1220EHEH814EHEHDifferent stages of a node visit ?
  • 16.
    Flow ChartPlease DoNot Take Printout : Use RTF Outline in case needed16RecursionIf tree is tallerIf LH and we inserted on leftYesYesBalanceNoNoYesIf RH and we inserted on right
  • 17.
    AVL INSERT ALGOIfroot is null Taller = true Root = newPtrelse If insert on left “ < “ AVL Insert (root -> left) if taller if LH do left balance else if root is RH taller=false adjust balance factorAvoid Taking Printout : Use RTF Outline in case needed17When will it not be taller ?
  • 18.
    AVL left BalanceleftTree=root - >leftIf leftTree is LH Case 1: left of left : single rotateElseCase 2: right of left : Double RotationrightTree=leftTree->rightAdjust balance for Right TreerotateLeft(leftTree)rotateRight(root)Taller = falsereturnAvoid Taking Printout : Use RTF Outline in case needed18
  • 19.
    FlowPlease Do NotTake Printout : Use RTF Outline in case needed19
  • 20.
    ReferenceMy CourseFurzonPlease DoNot Take Printout : Use RTF Outline in case needed20Action ItemSelf Learning