Code

Lookup, iteration, height — write them yourself

Insertion works: the tree balances itself. The remaining functions come with no holes this time — empty skeletons that you fill in completely.

Nearly all of them are familiar. Most are lesson 11's code carried across unchanged: *node becomes *avlNode and nothing else. Rotations change the shape, not the meaning — that is the observation that an AVL tree is a BST. Copy them and do not feel like you are cheating.

Pseudocode

Get(title):
    n ← root
    while n ≠ nil:
        c.Hit()
        if title < n.key:   n ← n.left
        if title > n.key:   n ← n.right
        else:               return n.item, true
    return nothing, false

Keys():                              // sorted, with no sorting
    walk(n):  walk(n.left); emit n.key; walk(n.right)

Range(lo, hi):                       // only subtrees that can hold an answer
    walk(n):
        if n = nil: return
        c.Hit()
        if lo < n.key:         walk(n.left)
        if lo ≤ n.key ≤ hi:    emit n.key
        if n.key < hi:         walk(n.right)

Min:  go left as far as you can.      Max:  go right as far as you can.

Height():        return height(root)           // O(1)
WorstBalance():  the largest |balance(n)| over every node in the tree

Height() is O(1) here: it is root.h. In lesson 11 it had to walk the whole tree. That is a free dividend from the field you just added.

WorstBalance() is the only one of these that lesson 11 did not have. It buys the algorithm nothing; it is for you: if the invariant holds it returns at most 1, and the test requires exactly that.

Your job

avl.go (in the panel) has grown: seven new skeletons, each with panic("not implemented").

rotateLeft and rebalance are still shown with their holes — the panel shows the skeleton, not your file. In your copy they are already filled in; leave them that way.

Read the contracts, not just the signatures — the doc comments say everything the test will check.

avl_test.go has grown too: five new tests, while the first two are unchanged. The new ones need all seven functions, so for now they will fall on a panic.

The test checks a property, not an output

That distinction matters enough to say out loud.

Every earlier lesson's tests checked an output: is it sorted, was the key found, were there fewer than so many comparisons. A test like that can in principle be passed by an algorithm other than the one asked for.

TestHeightStaysBoundedOnSortedInput checks the shape:

bound := 1.4405*math.Log2(float64(n)+2) - 0.3277
if float64(avl.Height()) > bound {
	t.Errorf("n=%d: height %d exceeds the AVL bound %.1f", n, avl.Height(), bound)
}

The input is strictly ascending — the same one that destroyed lesson 11's tree.

Lesson 11's BST returns every key correctly here. Keys() is sorted. Get never errs. And still:

n=  1000: height 1000 exceeds the AVL bound 14.0  (71x over)
n= 10000: height 10000 exceeds the AVL bound 18.8  (532x over)

532×. Correct answers do not come close to being enough here. Only the shape passes.

test what it requires
TestTraceReproducesTheLessonTable (last step) the same table
TestEveryInsertKeepsTheInvariant (last step) the invariant after every insert
TestAVLStoresAndFinds finds everything stored, misses what was not
TestRotationsPreserveTheOrdering the BST invariant and a balance of −1..1 during the build, not only at the end
TestHeightStaysBoundedOnSortedInput height and comparisons stay under the AVL bound
TestHeightStaysBoundedOnEveryAdversarialOrder the same for ascending, descending and zigzag
TestRotationsAreAmortizedConstant rotations per insert do not grow with n

TestRotationsPreserveTheOrdering checks every 97th insertion, not only the end, and does it through the public interface — the Keys() and WorstBalance() you have just written. A wrong rotation often corrupts the tree temporarily and later insertions happen to "repair" it; a check only at the end lets that through.

TestHeightStaysBoundedOnEveryAdversarialOrder takes the third worst-case order from lesson 11's homework — the zigzag, which also produced height n.

Run go test ./.... While you write, the test tells you which function and where it falls short.