Height
Definition |
The heightof a node is the maximum depth of its subtree. |
AVL Property
AVL trees maintain the following property:
For all nodesN, |N.Left.Height− N.Right.Height| ≤ 1
We claim that this ensures balance.
Conclusion
AVL Property |
If you can maintain the AVL property, you can perform operations inO(log(n))time. |
AVLInsert(k, R) |
Insert(k, R) Rebalance(N) |
Rebalance(N) |
P ←N.Parent RebalanceRight(N) RebalanceLeft(N) AdjustHeight(N) Rebalance(P) |
AdjustHeight(N) |
N.Height← 1+ max( N .Left.Height, N.Right.Height) |
AVLDelete(N) |
Delete(N) Rebalance(M) |
Summary |
AVL trees can implement all of the basic operations inO(log(n))time per operation. |
MergeWithRoot(R1,R2,T ) |
T.Left← R1 R1.Parent← T R2.Parent← T return T |
Time O(1).
Merge(R1,R2) |
T ←Find(∞,R1) Delete(T) MergeWithRoot(R1,R2,T ) return T |
Time O(h).
AVLTreeMergeWithRoot(R1,R2,T ) |
if |R1.Height− R2.Height| ≤1: MergeWithRoot(R1,R2,T ) return T |
Split(R, x) |
if R =null: return (null,null) if x ≤R.Key: return (R1,R3) if x >R.Key: ... |
Summary |
Merge combines trees. Both can be implemented inO(log(n))time for AVL trees. |