Re-think about the complexity
loopup has complexity of O(h) where h is the height of the tree
h can be in O(n) or in O(log n)
Balanced Tree: h \in O(\log n)
lookup, insert, find_min = O(log n)
Self-balancing Tree: a tree remains balanced as we insert new nodes
AVL trees
Red-black trees
Splay trees
B-trees
Goal: additional representation invariants
Height invariant: at every node, the height of the left and right sub-trees differ by at most 1
How to identify AVL Trees
count from root to leaf
count from left to right
every path should have length <=1 in differ
Approach: two invariant
insert one but break one of invariant
patch the broken invariant
If a operation requires insertion and rotation, then the height of a tree never change
These rotation requires the heaviest tree is from outside nodes
each type of rotation only cost O(1)
at most 1 (double) rotation for each insertion
you use double rotate when the heaviest tree in the middle
you should fix the bottom violation first
the heaviest tree is determined by the following diagram (notice the end note don't get counted, because we only trace through 2 segments)
Table of Content