AVL Trees
Knowing More How many nodes? – Determine on demand
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)
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)
Knowing More How many nodes? – Store in tree Update on insert / delete O(1)
Knowing More How balanced are we?
Knowing More How balanced are we? – How many nodes off each side? 3 7
Knowing More How balanced are we? – How many nodes off each side????? 7 7
Knowing More How balanced are we? – What is the longest path in each direction? 3 3
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
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))
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))
Knowing More Height of node – Store in each node Update on insert / delete O(1)
AVL Trees Georgii Adelson-Velsky Evgenii Mikhailovich Landis
AVL Rules Every node has balance factor Balance = Height(left child) – Height(right child) Height of Null =
AVL Rules Node must have balance factor of -1, 0 or 1 – Rotate to fix bad nodes
AVL Rules Worst case: Height01234 Nodes124712
AVL Rules Worst case: Height01234 Nodes124712
AVL Rules Worst case: Height01234 Nodes124712
AVL Rules Worst case: Height01234 Nodes124712
AVL Rules Worst case: Nodes at height h N h = N h-1 + N h Height01234 Nodes124712
AVL Rules Worst case:
So…. Guaranteed log(n) height – O(logn) Insert/Delete/Remove – But… Higher constant factors for insert/remove
Maintaining Balance Inserting/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 1 A valid AVL subtree f AD G h h h b h h+1
Case 1 Insertion creates problem on left-left or right- right f A D G h h+1 h b h+2
Rotate away from problem Case 1 f A D G h h+1 h b
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
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
Case 2 Do Zig-Zag Rotation f A G h h h+2 b C E d h h - 1 h+1
Case 2 Do Zig-Zag Rotation f A G h h h+2 b C E d h h - 1 h+1
Deletions Deletions like rotations – rotate to restore balance
Deletions Deletions like rotations – rotate to restore balance – May unbalance multiple nodes
Deletions Deletions like rotations – rotate to restore balance – May unbalance multiple nodes
Deletions Deletions like rotations – rotate to restore balance – May unbalance multiple nodes