SlideShare uma empresa Scribd logo
1 de 33
Baixar para ler offline
AVL Trees
                     Dynamic Tree Balancing




© Gunnar Gotshalks                            AVLtrees–1
Problems with BST

 • With random insertions and deletions BST has
   Θ ( log N ) times for search, insert and remove
 • But worst case behaviour is Θ ( N )
 • Problem is that BST’s can become unbalanced
 • We need a rebalance operation on a BST to restore the
   balance property and regain Θ ( log N )
 • Rebalancing should be cheap enough that we could do it
   dynamically on every insert and remove
       »   Preference is to have Θ ( 1 ) rebalance time




© Gunnar Gotshalks                                        AVLtrees–2
AVL Balance Definition

 • A good balance conditions ensures the height of a tree
   with N nodes is Θ ( log N )
       »   That gives Θ ( log N ) performance

 • The following balance definition is used
       »   The empty tree is balanced
       »   For every node in a non-empty tree
         height ( left_sub_tree ) – height ( right_sub_tree )  ≤ 1




© Gunnar Gotshalks                                                 AVLtrees–3
Rebalancing

 • Restructure the tree by moving the nodes around while
   preserving the order property
 • The operation is called a rotation
       »   Make use of the property that a node has one parent and
           two direct descendents




© Gunnar Gotshalks                                             AVLtrees–4
Single Rotations




© Gunnar Gotshalks                      AVLtrees–5
Single LL Rotation Pseudocode

          // Return pointer to root after rotation

          rotate_LL ( oldRoot : Node ) : Node is
            Result ← oldRoot . left
            oldRoot . left ← Result . right
            Result . right ← oldRoot
            adjustHeight(old_root)
            adjustHeight(old_root.left)
            adjustHeight(Result)                         Exercise
          end                                            write rotate_RR
          // Example use of rotate_LL

          parent . left ← rotate_LL ( parent . left)
          parent . right ← rotate_LL ( parent . right)

© Gunnar Gotshalks                                               AVLtrees–6
AdjustHeight Pseudocode


       // Assume that every node contains a height attribute
        adjustHeight ( root : Node ) is
         if root ≠ null then
             root . height ← 1 + max ( height ( root . left )
                                      , height ( root . right ) )
         end
        end




© Gunnar Gotshalks                                                  AVLtrees–7
Single Rotations & Height




© Gunnar Gotshalks                               AVLtrees–8
Single Rotations & Height – 2

h (K1) = 1 + max ( h(K2) , h(C) )           h (K2) = 1 + max ( h(K1) , h(A) )
h (K2) = 1 + max ( h(A) , h(B) )            h (K1) = 1 + max ( h(B) , h(C) )

if h(A) > h(B) ∧ h(B) ≥ h(C)
then rotate_LL reduces the height of the root

Proof – before rotation                   – after rotation

h(K2) = 1 + h(A)                          h(K1) = 1 + h(B)
          -- h(A) > h(B)                            -- h(B) ≥ h(C)
h(K1) = 1 + h(K2)                         h(K2) = 1 + h(A)
          -- h(K2) > h(B) ≥ h(C)                    -- h(A) ≥ 1 + h(B) > h(B)
h(K1) = 2 + h(A)
                          Before rotation h(root) = 2 + h(A)
                          After rotation h(root) = 1 + h(A)
                          Height of root has been reduced

  © Gunnar Gotshalks                                                   AVLtrees–9
Single Rotations & Height – 3




                                             Proof (?) by diagram
if h(A) > h(B) ∧ h(B) ≥ h(C)
then rotate_LL reduces the height of the root


 © Gunnar Gotshalks                                          AVLtrees–10
Double Rotation – LR




© Gunnar Gotshalks                          AVLtrees–11
Double Rotation – LR – Height



                          LR rotation




                         If h(K3) > h(A) ∧ h(A) ≥ h(C)
                         then rotate_LR reduces the height of root

© Gunnar Gotshalks                                              AVLtrees–12
Double Rotation – RL




© Gunnar Gotshalks                          AVLtrees–13
Double Rotation – RL – Height




                         if h(K3) > h(C) ∧ h(C) ≥ h(A)
                         then rotate_RL reduces the height of root

© Gunnar Gotshalks                                              AVLtrees–14
Single RL Rotation Pseudocode

// Return pointer to root after rotation
rotate_RL ( oldRoot : Node ) : Node is
  rightChild ← oldRoot . right ; Result ← rightChild . left
  oldRoot . right ← Result . left ; rightChild . left ← Result . right
  Result . left ← oldRoot ; Result . right ← rightChild
  adjustHeight ( oldRoot )
  adjustHeight ( rightChild )
                                                     Exercise
  adjustHeight ( Result )                            write rotate_LR
end
// Example use of rotate_RL
parent . left ← rotate_RL ( parent . left)
parent . right ← rotate_RL ( parent . right)


© Gunnar Gotshalks                                              AVLtrees–15
Insert into AVL Pseudocode

          // Insert will do rotations, which changes the root of
          // sub-trees. As a consequence, the recursive insert must
          // return the root of the resulting sub-tree.
          insert ( key : KeyType , data : ObjectType ) is
            newNode ← new Node ( key , data )
            root ← insertRec ( root , newNode )
            root ← rebalance ( root )     // Insertion may change
            adjustHeight (root)           // height, which may
                                          // cause imbalance
          end

         Only one rebalance will occur but we do not know where



© Gunnar Gotshalks                                              AVLtrees–16
InsertRec Pseudocode

     // Insert may do rotations, which changes the root of
     // sub-trees. As a consequence, the recursive insert must
     // return the root of the resulting sub-tree.
     // Invariant – The tree rooted at root is balanced
     insertRec ( root : Node , newNode : Node ) : Node is
       if root = Void then Result ← newNode
       else if root . key > newNode . key
             then root . left ← insertRec ( root . left , newNode )
             else root . right ← insertRec ( root . right , newNode )
             fi
             Result ← rebalance ( root ) ; adjustHeight ( Result )
       fi
     end


© Gunnar Gotshalks                                              AVLtrees–17
Height Pseudocode


       // Assume that every node contains a height attribute
       // Different definition for height for AVL trees.
       // Height of leaf is 1 (Figure 10.10 p435) not 0 (page 273).
       // By implication height of empty tree is 0 (see slides
       // Tree Algorithms–11..15 on binary tree height).
        height ( root : Node ) : Integer is
         if node = Void then Result ← 0
                          else Result ← node . Height
         fi
         return
        end



© Gunnar Gotshalks                                                AVLtrees–18
Rebalance Pseudocode

 • Define 6 variables that have the height of the sub-trees of
   interest for rotations
       »   If any of the pointers are void, height 0 is returned




© Gunnar Gotshalks                                                 AVLtrees–19
Rebalance Pseudocode – 2

 • Have the symmetric cases for the other 3 height variables




© Gunnar Gotshalks                                       AVLtrees–20
Rebalance Pseudocode – 3


rebalance ( root : Node ) : Node is
  h_AL ← heightLL ( root ) ; h_AR ← heightRR ( root )
  h_BL ← heightLR ( root ) ; h_BR ← heightRL ( root )
  h_CL ← height( root . right) ; h_CR ← height ( root . left)

 if     h_AL = h_BL ∧ h_BL ≥ h_CL then Result ← rotate_LL ( root )
 elseif h_AR = h_BR ∧ h_BR ≥ h_CR then Result ← rotate_RR ( root )
 elseif h_BL = h_AL ∧ h_AL ≥ h_CL then Result ← rotate_LR ( root )
 elseif h_BR = h_AR ∧ h_AR ≥ h_CR then Result ← rotate_RL ( root )
 else Result ← root
 fi
                 This follows the mathematical development in
end              slides 8, 12, 14 and works correctly for insertion
                        where the objective is to reduce the height of
                        a subtree. See slides 29..32 for problems with
                        remove.
   © Gunnar Gotshalks                                                    AVLtrees–21
Remove Difficulties

 • Remove has to do two things
       »   Return the entry corresponding to the key
       »   Rebalance the tree
             > Means adjusting the pointers
             > Without a parent pointer, the path from the root to
                the node is a singly linked list
             > Need to keep track of the parent node of the root of
                the sub-tree to rebalance to adjust the pointer to the
                new sub-tree
             > Consequence is every step we have to look one level
                deeper than BST remove algorithm

 • Rebalancing may occur at all levels


© Gunnar Gotshalks                                                 AVLtrees–22
Remove Pseudocode

        remove ( key : KeyType ) : EntryType is
          if root = Void then Result ← Void // Entry not in tree
          elseif root . key = key then        // Root is a special case
              Result ← root . entry
              root ← removeNode ( root )
          else Result ← removeRec ( root , key ) // Try sub-trees
          fi

        // The following routines need look ahead. They are the
        // main change from BST remove.

          adjustHeight ( root )
          root ← rebalance ( root )
        end

© Gunnar Gotshalks                                             AVLtrees–23
RemoveRec Pseudocode

    // Require root ≠ null ∧ root .key ≠ key
    //         entry ∈ tree → entry ∈ root
    //         balanced ( tree (root ) )
    // Ensure entry ∈ tree → Result = entry
    //        entry ∉ tree → Result = Void
    //        tree ( root ) may be unbalanced

    removeRec ( root : Node , key : KeyType ) : EntryType is
      if root . key > key then // Remove from the left sub-tree
      else // Remove from the right sub-tree
      fi
      return
    end


© Gunnar Gotshalks                                                AVLtrees–24
RemoveRec Pseudocode – 2

    // Remove from the left sub-tree

      if root . left = Void then Result ← Void
      elseif root . left . key = key then
          Result ← root . left . entry
          root . left ← removeNode ( root . left )
      else
          Result ← removeRec ( root . left , key )
          adjustHeight( root . left )
          root . left ← rebalance ( root . left )
      fi
    end



© Gunnar Gotshalks                                   AVLtrees–25
RemoveRec Pseudocode – 3

    // Remove from the right sub-tree

      if root . right = Void then Result ← Void
      elseif root . right . key = key then
          Result ← root . right . entry
          root . right ← removeNode ( root . right )
      else
          Result ← removeRec ( root . right, key )
          adjustHeight ( root . right )
          root . right ← rebalance ( root . right )
      fi
    end



© Gunnar Gotshalks                                     AVLtrees–26
RemoveNode

  // Require root ≠ Void
  // Ensure Result is a balanced tree with root removed
             Result = replacement node
  removeNode ( root : Node ) : Node
    if root . left = Void then Result ← root . right
    elseif root . right = Void then Result ← root . Left
    else child ← root . left
        if child . right = Void then
           root . entry ← child . entry ; root . left ← child . left
        else root . left ←
                  swap_and_remove_left_neighbour ( root , child )
        fi
        adjustHeight ( root )
        Result ← rebalance ( root )
    fi
  end
© Gunnar Gotshalks                                                     AVLtrees–27
Swap and Remove Left Neighbour


// Require child . right ≠ Void
// Ensure Result is a balanced tree with node removed
           Result = replacement node
swap_and_remove_left_neighbour ( parent , child : Node ) : Node
  if child . right . right ≠ Void then
     child . right ←
      swap_and_remove_left_neighbour ( parent , child . right )
  else
     parent . entry ← child . right . entry
     child . right ← child . right . left
  fi
  adjustHeight ( parent )
  Result ← rebalance ( parent )
end
© Gunnar Gotshalks                                       AVLtrees–28
Problem with Rebalance Pseudocode

 • The pseudocode for rebalance in slide 21 is works
   correctly for inserting a node into an AVL tree.
       »   But the pseudocode fails for the following remove
           example




                            Remove 21




© Gunnar Gotshalks                                             AVLtrees–29
Problem with Rebalance Pseudocode

 • What is the problem?
       »   The case cannot occur on insertion – inserting 17 or 19
           invokes a rebalance
       »   Need to rebalance but the height will not change



                                Remove 21




© Gunnar Gotshalks                                             AVLtrees–30
Rebalance Pseudocode Revised – 2

 • Correct removal with rebalance is the following


                       Remove 21
                       rotateLR




                       Remove 21
                       rotateLL




© Gunnar Gotshalks                                   AVLtrees–31
Rebalance Pseudocode Revised – 3

 • Correct rebalance needs to have the following changes
       »   Does the height of left and right sub-trees differ by more
           than 1?
             > If so, then continue rebalance.

       »   The condition h(A) > h(B) does not hold (slide 8)
             > Need to change to h(A) ≥ h(B)
                     – If h(A) = h(B) then either rotateLL or rotateLR will restore
                       balance but not change the height




© Gunnar Gotshalks                                                              AVLtrees–32
Rebalance Pseudocode for Remove


rebalance ( root : Node ) : Node is
  h_AL ← heightLL ( root ) ; h_AR ← heightRR ( root )
  h_BL ← heightLR ( root ) ; h_BR ← heightRL ( root )
  h_CL ← height( root . right) ; h_CR ← height ( root . left)

 if     h_AL ≥ h_BL ∧ h_BL ≥ h_CL then Result ← rotate_LL ( root )
 elseif h_AR ≥ h_BR ∧ h_BR ≥ h_CR then Result ← rotate_RR ( root )
 elseif h_BL ≥ h_AL ∧ h_AL ≥ h_CL then Result ← rotate_LR ( root )
 elseif h_BR ≥ h_AR ∧ h_AR ≥ h_CR then Result ← rotate_RL ( root )
 else Result ← root
 fi
end             Note the ≥ instead of = to handle cases for remove.




   © Gunnar Gotshalks                                           AVLtrees–33

Mais conteúdo relacionado

Mais procurados

Session 1 - Silva, Singh, Richardson at MLconf NYC
Session 1 - Silva, Singh, Richardson at MLconf NYCSession 1 - Silva, Singh, Richardson at MLconf NYC
Session 1 - Silva, Singh, Richardson at MLconf NYCMLconf
 
presentation
presentationpresentation
presentationjie ren
 
Data Mining: Concepts and Techniques (3rd ed.) — Chapter 5
Data Mining:  Concepts and Techniques (3rd ed.)— Chapter 5 Data Mining:  Concepts and Techniques (3rd ed.)— Chapter 5
Data Mining: Concepts and Techniques (3rd ed.) — Chapter 5 Salah Amean
 
Kernel Bayes Rule
Kernel Bayes RuleKernel Bayes Rule
Kernel Bayes RuleYan Xu
 
2014-06-20 Multinomial Logistic Regression with Apache Spark
2014-06-20 Multinomial Logistic Regression with Apache Spark2014-06-20 Multinomial Logistic Regression with Apache Spark
2014-06-20 Multinomial Logistic Regression with Apache SparkDB Tsai
 
Sparse matrix computations in MapReduce
Sparse matrix computations in MapReduceSparse matrix computations in MapReduce
Sparse matrix computations in MapReduceDavid Gleich
 
Spatial functions in MySQL 5.6, MariaDB 5.5, PostGIS 2.0 and others
Spatial functions in  MySQL 5.6, MariaDB 5.5, PostGIS 2.0 and othersSpatial functions in  MySQL 5.6, MariaDB 5.5, PostGIS 2.0 and others
Spatial functions in MySQL 5.6, MariaDB 5.5, PostGIS 2.0 and othersHenrik Ingo
 

Mais procurados (7)

Session 1 - Silva, Singh, Richardson at MLconf NYC
Session 1 - Silva, Singh, Richardson at MLconf NYCSession 1 - Silva, Singh, Richardson at MLconf NYC
Session 1 - Silva, Singh, Richardson at MLconf NYC
 
presentation
presentationpresentation
presentation
 
Data Mining: Concepts and Techniques (3rd ed.) — Chapter 5
Data Mining:  Concepts and Techniques (3rd ed.)— Chapter 5 Data Mining:  Concepts and Techniques (3rd ed.)— Chapter 5
Data Mining: Concepts and Techniques (3rd ed.) — Chapter 5
 
Kernel Bayes Rule
Kernel Bayes RuleKernel Bayes Rule
Kernel Bayes Rule
 
2014-06-20 Multinomial Logistic Regression with Apache Spark
2014-06-20 Multinomial Logistic Regression with Apache Spark2014-06-20 Multinomial Logistic Regression with Apache Spark
2014-06-20 Multinomial Logistic Regression with Apache Spark
 
Sparse matrix computations in MapReduce
Sparse matrix computations in MapReduceSparse matrix computations in MapReduce
Sparse matrix computations in MapReduce
 
Spatial functions in MySQL 5.6, MariaDB 5.5, PostGIS 2.0 and others
Spatial functions in  MySQL 5.6, MariaDB 5.5, PostGIS 2.0 and othersSpatial functions in  MySQL 5.6, MariaDB 5.5, PostGIS 2.0 and others
Spatial functions in MySQL 5.6, MariaDB 5.5, PostGIS 2.0 and others
 

Destaque

Louis Britto profile
Louis Britto profileLouis Britto profile
Louis Britto profileLouis Britto
 
Omay milena rojas martinez2
Omay milena rojas martinez2Omay milena rojas martinez2
Omay milena rojas martinez2klaumilenitha
 
Compressed js with NodeJS & GruntJS
Compressed js with NodeJS & GruntJSCompressed js with NodeJS & GruntJS
Compressed js with NodeJS & GruntJSDavid Nguyen
 
Cli mini Hack!#1 ~Terminalとの親睦を深めよう~
Cli mini Hack!#1 ~Terminalとの親睦を深めよう~Cli mini Hack!#1 ~Terminalとの親睦を深めよう~
Cli mini Hack!#1 ~Terminalとの親睦を深めよう~Kei IWASAKI
 
Skype4 pyで遊んでみた
Skype4 pyで遊んでみたSkype4 pyで遊んでみた
Skype4 pyで遊んでみたDaijirou Yamada
 
moi truong, xu ly rac thai, dich vu moi truong
moi truong, xu ly rac thai, dich vu moi truongmoi truong, xu ly rac thai, dich vu moi truong
moi truong, xu ly rac thai, dich vu moi truongTư vấn môi trường
 
Strategies to Fuel the Energy Workforce
Strategies to Fuel the Energy WorkforceStrategies to Fuel the Energy Workforce
Strategies to Fuel the Energy WorkforceEric Wilkinson
 
INTEGRATE Chicago - Andrés Ordóñez
INTEGRATE Chicago - Andrés OrdóñezINTEGRATE Chicago - Andrés Ordóñez
INTEGRATE Chicago - Andrés OrdóñezIMCWVU
 
Accenture fjord-trends-2015
Accenture fjord-trends-2015Accenture fjord-trends-2015
Accenture fjord-trends-2015Matthew Sikes
 
2 stone-ifa dignified care
2 stone-ifa dignified care2 stone-ifa dignified care
2 stone-ifa dignified careifa2012_2
 

Destaque (15)

Louis Britto profile
Louis Britto profileLouis Britto profile
Louis Britto profile
 
Omay milena rojas martinez2
Omay milena rojas martinez2Omay milena rojas martinez2
Omay milena rojas martinez2
 
Yafo Flea Market
Yafo Flea MarketYafo Flea Market
Yafo Flea Market
 
Compressed js with NodeJS & GruntJS
Compressed js with NodeJS & GruntJSCompressed js with NodeJS & GruntJS
Compressed js with NodeJS & GruntJS
 
Cli mini Hack!#1 ~Terminalとの親睦を深めよう~
Cli mini Hack!#1 ~Terminalとの親睦を深めよう~Cli mini Hack!#1 ~Terminalとの親睦を深めよう~
Cli mini Hack!#1 ~Terminalとの親睦を深めよう~
 
Skype4 pyで遊んでみた
Skype4 pyで遊んでみたSkype4 pyで遊んでみた
Skype4 pyで遊んでみた
 
moi truong, xu ly rac thai, dich vu moi truong
moi truong, xu ly rac thai, dich vu moi truongmoi truong, xu ly rac thai, dich vu moi truong
moi truong, xu ly rac thai, dich vu moi truong
 
6 From Atoms To Color TV
6 From Atoms To Color TV6 From Atoms To Color TV
6 From Atoms To Color TV
 
Bao mat may chu cua mot to chuc
Bao mat may chu cua mot to chucBao mat may chu cua mot to chuc
Bao mat may chu cua mot to chuc
 
Strategies to Fuel the Energy Workforce
Strategies to Fuel the Energy WorkforceStrategies to Fuel the Energy Workforce
Strategies to Fuel the Energy Workforce
 
Bao mat cho cac may tinh cua to chuc
Bao mat cho cac may tinh cua to chucBao mat cho cac may tinh cua to chuc
Bao mat cho cac may tinh cua to chuc
 
INTEGRATE Chicago - Andrés Ordóñez
INTEGRATE Chicago - Andrés OrdóñezINTEGRATE Chicago - Andrés Ordóñez
INTEGRATE Chicago - Andrés Ordóñez
 
Accenture fjord-trends-2015
Accenture fjord-trends-2015Accenture fjord-trends-2015
Accenture fjord-trends-2015
 
Madison Marie
Madison MarieMadison Marie
Madison Marie
 
2 stone-ifa dignified care
2 stone-ifa dignified care2 stone-ifa dignified care
2 stone-ifa dignified care
 

Semelhante a 22 av ltrees

Semelhante a 22 av ltrees (7)

Avl tree
Avl treeAvl tree
Avl tree
 
AVL TREE PREPARED BY M V BRAHMANANDA REDDY
AVL TREE PREPARED BY M V BRAHMANANDA REDDYAVL TREE PREPARED BY M V BRAHMANANDA REDDY
AVL TREE PREPARED BY M V BRAHMANANDA REDDY
 
Ie
IeIe
Ie
 
Lecture3
Lecture3Lecture3
Lecture3
 
4.10.AVLTrees[1].ppt
4.10.AVLTrees[1].ppt4.10.AVLTrees[1].ppt
4.10.AVLTrees[1].ppt
 
Avl trees
Avl treesAvl trees
Avl trees
 
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdfCS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
 

Último

Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...BookNet Canada
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024TopCSSGallery
 
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...amber724300
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Karmanjay Verma
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsYoss Cohen
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxAna-Maria Mihalceanu
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Nikki Chapple
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 

Último (20)

Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024
 
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platforms
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance Toolbox
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 

22 av ltrees

  • 1. AVL Trees Dynamic Tree Balancing © Gunnar Gotshalks AVLtrees–1
  • 2. Problems with BST • With random insertions and deletions BST has Θ ( log N ) times for search, insert and remove • But worst case behaviour is Θ ( N ) • Problem is that BST’s can become unbalanced • We need a rebalance operation on a BST to restore the balance property and regain Θ ( log N ) • Rebalancing should be cheap enough that we could do it dynamically on every insert and remove » Preference is to have Θ ( 1 ) rebalance time © Gunnar Gotshalks AVLtrees–2
  • 3. AVL Balance Definition • A good balance conditions ensures the height of a tree with N nodes is Θ ( log N ) » That gives Θ ( log N ) performance • The following balance definition is used » The empty tree is balanced » For every node in a non-empty tree  height ( left_sub_tree ) – height ( right_sub_tree )  ≤ 1 © Gunnar Gotshalks AVLtrees–3
  • 4. Rebalancing • Restructure the tree by moving the nodes around while preserving the order property • The operation is called a rotation » Make use of the property that a node has one parent and two direct descendents © Gunnar Gotshalks AVLtrees–4
  • 5. Single Rotations © Gunnar Gotshalks AVLtrees–5
  • 6. Single LL Rotation Pseudocode // Return pointer to root after rotation rotate_LL ( oldRoot : Node ) : Node is Result ← oldRoot . left oldRoot . left ← Result . right Result . right ← oldRoot adjustHeight(old_root) adjustHeight(old_root.left) adjustHeight(Result) Exercise end write rotate_RR // Example use of rotate_LL parent . left ← rotate_LL ( parent . left) parent . right ← rotate_LL ( parent . right) © Gunnar Gotshalks AVLtrees–6
  • 7. AdjustHeight Pseudocode // Assume that every node contains a height attribute adjustHeight ( root : Node ) is if root ≠ null then root . height ← 1 + max ( height ( root . left ) , height ( root . right ) ) end end © Gunnar Gotshalks AVLtrees–7
  • 8. Single Rotations & Height © Gunnar Gotshalks AVLtrees–8
  • 9. Single Rotations & Height – 2 h (K1) = 1 + max ( h(K2) , h(C) ) h (K2) = 1 + max ( h(K1) , h(A) ) h (K2) = 1 + max ( h(A) , h(B) ) h (K1) = 1 + max ( h(B) , h(C) ) if h(A) > h(B) ∧ h(B) ≥ h(C) then rotate_LL reduces the height of the root Proof – before rotation – after rotation h(K2) = 1 + h(A) h(K1) = 1 + h(B) -- h(A) > h(B) -- h(B) ≥ h(C) h(K1) = 1 + h(K2) h(K2) = 1 + h(A) -- h(K2) > h(B) ≥ h(C) -- h(A) ≥ 1 + h(B) > h(B) h(K1) = 2 + h(A) Before rotation h(root) = 2 + h(A) After rotation h(root) = 1 + h(A) Height of root has been reduced © Gunnar Gotshalks AVLtrees–9
  • 10. Single Rotations & Height – 3 Proof (?) by diagram if h(A) > h(B) ∧ h(B) ≥ h(C) then rotate_LL reduces the height of the root © Gunnar Gotshalks AVLtrees–10
  • 11. Double Rotation – LR © Gunnar Gotshalks AVLtrees–11
  • 12. Double Rotation – LR – Height LR rotation If h(K3) > h(A) ∧ h(A) ≥ h(C) then rotate_LR reduces the height of root © Gunnar Gotshalks AVLtrees–12
  • 13. Double Rotation – RL © Gunnar Gotshalks AVLtrees–13
  • 14. Double Rotation – RL – Height if h(K3) > h(C) ∧ h(C) ≥ h(A) then rotate_RL reduces the height of root © Gunnar Gotshalks AVLtrees–14
  • 15. Single RL Rotation Pseudocode // Return pointer to root after rotation rotate_RL ( oldRoot : Node ) : Node is rightChild ← oldRoot . right ; Result ← rightChild . left oldRoot . right ← Result . left ; rightChild . left ← Result . right Result . left ← oldRoot ; Result . right ← rightChild adjustHeight ( oldRoot ) adjustHeight ( rightChild ) Exercise adjustHeight ( Result ) write rotate_LR end // Example use of rotate_RL parent . left ← rotate_RL ( parent . left) parent . right ← rotate_RL ( parent . right) © Gunnar Gotshalks AVLtrees–15
  • 16. Insert into AVL Pseudocode // Insert will do rotations, which changes the root of // sub-trees. As a consequence, the recursive insert must // return the root of the resulting sub-tree. insert ( key : KeyType , data : ObjectType ) is newNode ← new Node ( key , data ) root ← insertRec ( root , newNode ) root ← rebalance ( root ) // Insertion may change adjustHeight (root) // height, which may // cause imbalance end Only one rebalance will occur but we do not know where © Gunnar Gotshalks AVLtrees–16
  • 17. InsertRec Pseudocode // Insert may do rotations, which changes the root of // sub-trees. As a consequence, the recursive insert must // return the root of the resulting sub-tree. // Invariant – The tree rooted at root is balanced insertRec ( root : Node , newNode : Node ) : Node is if root = Void then Result ← newNode else if root . key > newNode . key then root . left ← insertRec ( root . left , newNode ) else root . right ← insertRec ( root . right , newNode ) fi Result ← rebalance ( root ) ; adjustHeight ( Result ) fi end © Gunnar Gotshalks AVLtrees–17
  • 18. Height Pseudocode // Assume that every node contains a height attribute // Different definition for height for AVL trees. // Height of leaf is 1 (Figure 10.10 p435) not 0 (page 273). // By implication height of empty tree is 0 (see slides // Tree Algorithms–11..15 on binary tree height). height ( root : Node ) : Integer is if node = Void then Result ← 0 else Result ← node . Height fi return end © Gunnar Gotshalks AVLtrees–18
  • 19. Rebalance Pseudocode • Define 6 variables that have the height of the sub-trees of interest for rotations » If any of the pointers are void, height 0 is returned © Gunnar Gotshalks AVLtrees–19
  • 20. Rebalance Pseudocode – 2 • Have the symmetric cases for the other 3 height variables © Gunnar Gotshalks AVLtrees–20
  • 21. Rebalance Pseudocode – 3 rebalance ( root : Node ) : Node is h_AL ← heightLL ( root ) ; h_AR ← heightRR ( root ) h_BL ← heightLR ( root ) ; h_BR ← heightRL ( root ) h_CL ← height( root . right) ; h_CR ← height ( root . left) if h_AL = h_BL ∧ h_BL ≥ h_CL then Result ← rotate_LL ( root ) elseif h_AR = h_BR ∧ h_BR ≥ h_CR then Result ← rotate_RR ( root ) elseif h_BL = h_AL ∧ h_AL ≥ h_CL then Result ← rotate_LR ( root ) elseif h_BR = h_AR ∧ h_AR ≥ h_CR then Result ← rotate_RL ( root ) else Result ← root fi This follows the mathematical development in end slides 8, 12, 14 and works correctly for insertion where the objective is to reduce the height of a subtree. See slides 29..32 for problems with remove. © Gunnar Gotshalks AVLtrees–21
  • 22. Remove Difficulties • Remove has to do two things » Return the entry corresponding to the key » Rebalance the tree > Means adjusting the pointers > Without a parent pointer, the path from the root to the node is a singly linked list > Need to keep track of the parent node of the root of the sub-tree to rebalance to adjust the pointer to the new sub-tree > Consequence is every step we have to look one level deeper than BST remove algorithm • Rebalancing may occur at all levels © Gunnar Gotshalks AVLtrees–22
  • 23. Remove Pseudocode remove ( key : KeyType ) : EntryType is if root = Void then Result ← Void // Entry not in tree elseif root . key = key then // Root is a special case Result ← root . entry root ← removeNode ( root ) else Result ← removeRec ( root , key ) // Try sub-trees fi // The following routines need look ahead. They are the // main change from BST remove. adjustHeight ( root ) root ← rebalance ( root ) end © Gunnar Gotshalks AVLtrees–23
  • 24. RemoveRec Pseudocode // Require root ≠ null ∧ root .key ≠ key // entry ∈ tree → entry ∈ root // balanced ( tree (root ) ) // Ensure entry ∈ tree → Result = entry // entry ∉ tree → Result = Void // tree ( root ) may be unbalanced removeRec ( root : Node , key : KeyType ) : EntryType is if root . key > key then // Remove from the left sub-tree else // Remove from the right sub-tree fi return end © Gunnar Gotshalks AVLtrees–24
  • 25. RemoveRec Pseudocode – 2 // Remove from the left sub-tree if root . left = Void then Result ← Void elseif root . left . key = key then Result ← root . left . entry root . left ← removeNode ( root . left ) else Result ← removeRec ( root . left , key ) adjustHeight( root . left ) root . left ← rebalance ( root . left ) fi end © Gunnar Gotshalks AVLtrees–25
  • 26. RemoveRec Pseudocode – 3 // Remove from the right sub-tree if root . right = Void then Result ← Void elseif root . right . key = key then Result ← root . right . entry root . right ← removeNode ( root . right ) else Result ← removeRec ( root . right, key ) adjustHeight ( root . right ) root . right ← rebalance ( root . right ) fi end © Gunnar Gotshalks AVLtrees–26
  • 27. RemoveNode // Require root ≠ Void // Ensure Result is a balanced tree with root removed Result = replacement node removeNode ( root : Node ) : Node if root . left = Void then Result ← root . right elseif root . right = Void then Result ← root . Left else child ← root . left if child . right = Void then root . entry ← child . entry ; root . left ← child . left else root . left ← swap_and_remove_left_neighbour ( root , child ) fi adjustHeight ( root ) Result ← rebalance ( root ) fi end © Gunnar Gotshalks AVLtrees–27
  • 28. Swap and Remove Left Neighbour // Require child . right ≠ Void // Ensure Result is a balanced tree with node removed Result = replacement node swap_and_remove_left_neighbour ( parent , child : Node ) : Node if child . right . right ≠ Void then child . right ← swap_and_remove_left_neighbour ( parent , child . right ) else parent . entry ← child . right . entry child . right ← child . right . left fi adjustHeight ( parent ) Result ← rebalance ( parent ) end © Gunnar Gotshalks AVLtrees–28
  • 29. Problem with Rebalance Pseudocode • The pseudocode for rebalance in slide 21 is works correctly for inserting a node into an AVL tree. » But the pseudocode fails for the following remove example Remove 21 © Gunnar Gotshalks AVLtrees–29
  • 30. Problem with Rebalance Pseudocode • What is the problem? » The case cannot occur on insertion – inserting 17 or 19 invokes a rebalance » Need to rebalance but the height will not change Remove 21 © Gunnar Gotshalks AVLtrees–30
  • 31. Rebalance Pseudocode Revised – 2 • Correct removal with rebalance is the following Remove 21 rotateLR Remove 21 rotateLL © Gunnar Gotshalks AVLtrees–31
  • 32. Rebalance Pseudocode Revised – 3 • Correct rebalance needs to have the following changes » Does the height of left and right sub-trees differ by more than 1? > If so, then continue rebalance. » The condition h(A) > h(B) does not hold (slide 8) > Need to change to h(A) ≥ h(B) – If h(A) = h(B) then either rotateLL or rotateLR will restore balance but not change the height © Gunnar Gotshalks AVLtrees–32
  • 33. Rebalance Pseudocode for Remove rebalance ( root : Node ) : Node is h_AL ← heightLL ( root ) ; h_AR ← heightRR ( root ) h_BL ← heightLR ( root ) ; h_BR ← heightRL ( root ) h_CL ← height( root . right) ; h_CR ← height ( root . left) if h_AL ≥ h_BL ∧ h_BL ≥ h_CL then Result ← rotate_LL ( root ) elseif h_AR ≥ h_BR ∧ h_BR ≥ h_CR then Result ← rotate_RR ( root ) elseif h_BL ≥ h_AL ∧ h_AL ≥ h_CL then Result ← rotate_LR ( root ) elseif h_BR ≥ h_AR ∧ h_AR ≥ h_CR then Result ← rotate_RL ( root ) else Result ← root fi end Note the ≥ instead of = to handle cases for remove. © Gunnar Gotshalks AVLtrees–33