Maintaining AVL Property
What happens if we add(11)
?
No programming assignment this week!
Instead: short take-home quiz on binary search trees
add
/remvoe
Implement a sorted set (SimpleSSet
) with efficient operations:
find
add
remove
Previous best: sorted array with binary search
find
in $O(\log n)$ timeadd
/remove
in $O(n)$ timeIntroduced AVL trees
$T$ has AVL property if for every node $v$ with children $u$ and $w$, we have
$\vert h(u) - h(w) \vert \leq 1$
We showed:
add
/remove
/find
take $O(\log n)$ timeBut:
add
/remove
as previously implemented may destroy AVL propertyadd
/remove
as beforeWhat happens if we add(11)
?
If we add
a new node as before, it is always a leaf.
Which nodes could become unbalanced?
How can we check for unbalance?
All this takes $O(\log n)$ time if $T$ is AVL tree (before add
)
add
Suppose $T$ becomes unbalanced after add
Note: 4 possibilities of relative order of $x, y, z$
So: restructure tree to move $x$ up
Suppose $z$ became unbalanced after add(w)
What is root’s height after restructuring?
Why does restructuring restore balance of subtree?
Why does restructuring maintain BST property?
Does restructuring make tree balanced?
After add(w)
, iterate over $w$’s ancestors from $w$ upwards:
What is running time?
How do we remove
nodes?
Case 1: Leaf
Case 2: Single-child
Case 3: Two-child
Which cases modify tree structure?
$x, y, z$ redefined; same restructuring
What is height of root after restructuring?
Restructuring may again cause imblance! Must continue upwards.
After remove(x)
, iterate over removed node’s ancestors upwards:
What is running time?
We can modify add
/remove
such that
So
add
/remove
/find
operations happen in $O(\log n)$ time!Compare performance of BST (as in Assignment 06) and AVL tree implementation
Tests:
Time to add elements (ms):
BST: 770
AVL: 601
Heights:
BST: 45
AVL: 22
Time to find elements (ms):
BST: 120
AVL: 88
Time to remove elements (ms):
BST: 89
AVL: 137
Time to add Shakespeare's vocabulary (ms):
BST: 287
AVL: 246
Heights:
BST: 36
AVL: 17
Time to add words from dictionary (ms):
BST: 639
AVL: 2
Heights:
BST: 9999
AVL: 13
(Dis)advantages of BST vs AVL tree?
2 Ingredients:
AVL trees give efficient worst-case performance
add
, remove
, find
all in $O(\log n)$ timePerformance between BST vs AVL trees depends on usage
Another Tree Representation:
Goal. Implement a priority queue with $O(\log n)$-time operations
Exercise. How could this goal be achieved with an AVL tree?
comparable
elemementsGoal. Use & maintain these properties for an efficient implementation of a priority queue:
add(x)
min()
removeMin()
$T$ a binary tree
$T$ has the heap property if for every node storing value $v$ with children $u$ and $w$, we have $v \leq u$ and $v \leq w$.
If $T$ has the heap property, what can we say about the value of the root?
$T$ is a complete binary tree of depth $D$ if:
If $T$ is a complete binary tree, where can we add a node to maintain completeness?
If $T$ is a complete binary tree, what nodes can we remove and maintain completeness?
A binary tree $T$ storing comparable elements is a binary heap if:
Given a binary heap $T$, how can we implement min()
?
Given a binary heap $T$, how can we add
an element?
Given a binary heap $T$, how can we removeMin
?