Lecture 12 - Balanced Trees
Contents
- A tree is weight balanced if: (size(l) - height(r)| < 2
- A tree is height balanced if: |height(l) - height(r)| < 2
Tree Rotation
Operations that change the balance of a tree whilst maintaining a search tree
Left Rotate
left subtree moves down
right subtree promoted
left child of right subtree becomes right child of left subtree
|
|
Right Rotate
right subtree moves down
left subtree promoted
right child of left subtree becomes left child of right subtree
1 2 | B -> A A C -> C B |
|
|
Rebalance cost
Many O(n)
Degenerate O(n logn)
When to rebalance
Rebalance on every inseration
or rebalance every k values
or rebalance when a threshold is passed
// Amortisation - more work now less work later