/
AVL Trees   Knowing More AVL Trees   Knowing More

AVL Trees Knowing More - PowerPoint Presentation

kittie-lecroy
kittie-lecroy . @kittie-lecroy
Follow
342 views
Uploaded On 2020-01-30

AVL Trees Knowing More - PPT Presentation

AVL Trees Knowing More How balanced are we Knowing More How balanced are we How many nodes off each side 3 7 Knowing More How many nodes Determine on demand Knowing More How many nodes Determine on demand ID: 774214

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "AVL Trees Knowing More" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

AVL Trees

Knowing MoreHow balanced are we?

Knowing MoreHow balanced are we? How many nodes off each side? 3 7

Knowing MoreHow many nodes? Determine on demand

Knowing MoreHow many nodes? Determine on demand int size(Node curNode ) if curNode is null, return 0 else return 1 + size( leftChild ) + size( rightChild)

Knowing MoreHow many nodes? Determine on demand T(n) = 2T(n/2) + 1 O(n) int size(Node curNode ) if curNode is null, return 0 else return 1 + size( leftChild) + size(rightChild)

Knowing MoreHow many nodes? Store in tree Update on insert / delete O(1)

Knowing More How balanced are we? How many nodes off each side????? 7 7

Knowing MoreHow balanced are we? What is the longest path in each direction? 3 3

Knowing MoreHow balanced are we? What is the longest path in each direction? Tree ops dependent on longest path Consistent height is key 3 3

Knowing MoreHeight of node Determine on demand int height(Node curNode ) if curNode is null, return -1 else return 1 + max( height(leftChild), height( rightChild ))

Knowing MoreHeight of node Determine on demand T(n) = 2T(n/2) + 1 O(n) per node int height(Node curNode ) if curNode is null, return -1 else return 1 + max( height( leftChild), height( rightChild ))

Knowing MoreHeight of node Store in each node Update on insert / delete O(1) per node 0 0 0 1 1 2 3

AVL TreesGeorgii Adelson-Velsky Evgenii Mikhailovich Landis

AVL Rules Every node has balance factor Balance = Height(left child) – Height(right child) Height of Null = -1 0 0 0 1 2 3 -1 -1 -1 -1 -1 -1 -1

AVL RulesNode must have balance factor of -1, 0 or 1 Rotate to fix bad nodes

AVL RulesWorst case: Height 0 1 2 3 4 Nodes 1

AVL RulesWorst case: Height 0 1 2 3 4 Nodes 1 2

AVL RulesWorst case: Height 0 1 2 3 4 Nodes 1 2 4

AVL RulesWorst case: Height 0 1 2 3 4 Nodes 1 2 4 7

AVL RulesWorst case: Height 0 1 2 3 4 Nodes 1 2 4 7 12

AVL RulesWorst case: Height 0 1 2 3 4 Nodes 1 2 4 7 12

AVL RulesWorst case: Height 0 1 2 3 4 Nodes 1 2 4 7 12

AVL RulesWorst case: Height 0 1 2 3 4 Nodes 1 2 4 7 12

AVL RulesWorst case: N odes at height h N h = Nh-1 + Nh-2 + 1 Height 0 1 2 3 4 Nodes 1 2 4 7 12

AVL RulesWorst case proof + 1  

AVL RulesWorst case proof N(h - 1) is > N(h -2) + 1 + + 1  

AVL RulesWorst case proof + 1 + + 1  

AVL RulesRecurrence relationship:  

AVL RulesRecurrence relationship:  

AVL RulesRecurrence relationship:  

AVL RulesRecurrence relationship: N(0) = 1 Need h - 2k = 0 Solve for k: k = h/2  

AVL RulesRecurrence relationship: k = h/2  

AVL RulesRecurrence relationship: N(0) = 1  

AVL RulesWorst case proof  

AVL RulesWorst case proof  

AVL RulesWorst case proof  

AVL RulesWorst case proof  

So…. Guaranteed log(n) height O( logn ) Insert/Delete/Remove But… Higher constant factors for insert/remove  

Maintaining BalanceInserting/deleting node changes balance by 1 Affects whole chain

Maintaining Balance-2 or +2 needs to be balanced 2 cases Outside Cases (single rotation) : Longest chain is left-left or right-right of unbalanced Inside Cases (double rotation) : Longest chain is left-right or right-left of unbalanced

Case 1A valid AVL subtree f A D G h h h b h h+1

Case 1Insertion creates problem on left-left or right-right f A D G h h+1 h b h+2

Rotate away from problemCase 1 f A D G h h+1 h b h+1

Case 2Problem created on left-right or right-left path f A G h h h b C E d h - 1 h - 1 h+1

Case 2Problem created on left-right or right-left path f A G h h h+1 b C E d h h - 1 h+2

Case 2Do Zig- Zag Rotation f A G h h h+2 b C E d h h - 1 h+1

Case 2Do Zig- Zag Rotation f A G h h h+2 b C E d h h - 1 h+1 h+1

DeletionsDeletions like rotations – rotate to restore balance

DeletionsDeletions like rotations – rotate to restore balance May unbalance multiple nodes

DeletionsDeletions like rotations – rotate to restore balance May unbalance multiple nodes

DeletionsDeletions like rotations – rotate to restore balance May unbalance multiple nodes