Maintaining AVL Property
What happens if we add(11)?
Implement a sorted set (SimpleSSet) with efficient operations:
findaddremovePrevious best: sorted array with binary search
find in $O(\log n)$ timeadd/remove in $O(n)$ timefindaddremoveadd/remove ops determines heightSequence of operations determines height!
If:
Then:
add, remove, find are $O(n)$ in the worst caseThis is worse than a sorted array (find is $O(\log n)$)
What can we do about it?
Idea. When we modify the tree (add or remove), restructure the tree to maintain balance
Challenges.
A binary tree $T$ is height balanced or an AVL tree (Adelson-Valsky & Landis) if for every node $v$ with children $u$ and $v$, we have $\vert h(u) - h(v)\vert \leq 1$.
We’ll show:
As a result
add, remove, and find for sorted sets all in time $O(\log n)$Which nodes are height balanced?
$T$ is an AVL tree if every node is height balanced
Proposition. If $T$ is an AVL tree with $n$ nodes, then $h(T) = O(\log n)$.
add/remove/find run in time $O(\log n)$ on $T$Argument. Roundabout method
Claim. Suppose $T$ is an AVL tree with height $h$. Then $T$ contains at least $2^{h/2}$ nodes.
Claim $\implies$ Proposition:
Claim. Suppose $T$ is an AVL tree with height $h$. Then $T$ contains at least $2^{h/2}$ nodes.
Idea. For a given height $h$, define $m(h)$ to be the minimum number nodes of any AVL tree with height $h$
Question. What is the structure of AVL tree with $m(h)$ nodes?
Claim. Suppose $T$ is an AVL tree with height $h$. Then $T$ contains at least $2^{h/2}$ nodes.
Symbolically. $m$ satisfies:
Can use this to compute:
Claim. Suppose $T$ is an AVL tree with height $h$. Then $T$ contains at least $2^{h/2}$ nodes.
Using $m(h) = m(h-1) + m(h-2)$, derive a bound on $m(h)$:
Since Claim $\implies$ Proposition we can conclude:
find/add/remove take time $O(\log n)$However calling add/remove with previous implementation may destroy AVL property
Questions.
How much damage can a single add/remove do to balance?
Can balanced be restored efficiently?
Perform add/remove as before, then
check if AVL property is maintained,
if not, restructure graph to restore balance
What happens if we add(11)?
If we add a new node as before, it is always a leaf.
add
Suppose $T$ becomes unbalanced after add
Note: 4 possibilities of relative order of $x, y, z$