Download presentation
Presentation is loading. Please wait.
Published byEdwina Daniels Modified over 8 years ago
1
AVL Trees
2
Knowing More How many nodes? – Determine on demand
3
Knowing More How many nodes? – Determine on demand int size(Node curNode) if curNode is null, return 0 else return 1 + size(leftChild) + size(rightChild)
4
Knowing More How 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)
5
Knowing More How many nodes? – Store in tree Update on insert / delete O(1)
6
Knowing More How balanced are we?
7
Knowing More How balanced are we? – How many nodes off each side? 3 7
8
Knowing More How balanced are we? – How many nodes off each side????? 7 7
9
Knowing More How balanced are we? – What is the longest path in each direction? 3 3
10
Knowing More How balanced are we? – What is the longest path in each direction? Tree ops dependent on longest path – Consistent depth is key 3 3
11
Knowing More Height of node – Determine on demand int depth(Node curNode) if curNode is null, return -1 else return 1 + max( depth(leftChild), depth(rightChild))
12
Knowing More Height of node – Determine on demand – T(n) = 2T(n/2) + 1 O(n) per node int depth(Node curNode) if curNode is null, return -1 else return 1 + max( depth(leftChild), depth(rightChild))
13
Knowing More Height of node – Store in each node Update on insert / delete O(1) 0 0 0 1 1 2 3
14
AVL Trees Georgii Adelson-Velsky Evgenii Mikhailovich Landis
15
AVL Rules Every node has balance factor Balance = Height(left child) – Height(right child) Height of Null = -1 0 0 0 1 2 3
16
AVL Rules Node must have balance factor of -1, 0 or 1 – Rotate to fix bad nodes
17
AVL Rules Worst case: Height01234 Nodes124712
18
AVL Rules Worst case: Height01234 Nodes124712
19
AVL Rules Worst case: Height01234 Nodes124712
20
AVL Rules Worst case: Height01234 Nodes124712
21
AVL Rules Worst case: Nodes at height h N h = N h-1 + N h-2 + 1 Height01234 Nodes124712
22
AVL Rules Worst case:
23
So…. Guaranteed log(n) height – O(logn) Insert/Delete/Remove – But… Higher constant factors for insert/remove
24
Maintaining Balance Inserting/deleting node changes balance by 1 – Affects whole chain
25
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
26
Case 1 A valid AVL subtree f AD G h h h b h h+1
27
Case 1 Insertion creates problem on left-left or right- right f A D G h h+1 h b h+2
28
Rotate away from problem Case 1 f A D G h h+1 h b
29
Case 2 Problem created on left-right or right-left path f A G h h h b C E d h - 1 h+1
30
Case 2 Problem created on left-right or right-left path f A G h h h+1 b C E d h h - 1 h+2
31
Case 2 Do Zig-Zag Rotation f A G h h h+2 b C E d h h - 1 h+1
32
Case 2 Do Zig-Zag Rotation f A G h h h+2 b C E d h h - 1 h+1
33
Deletions Deletions like rotations – rotate to restore balance
34
Deletions Deletions like rotations – rotate to restore balance – May unbalance multiple nodes
35
Deletions Deletions like rotations – rotate to restore balance – May unbalance multiple nodes
36
Deletions Deletions like rotations – rotate to restore balance – May unbalance multiple nodes
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.