Theory

Six titles, one tree

There is no code here. What you have here is what a search tree does — step by step, on the same six titles you put into the hash table in lesson 10:

Hamlet   Dune   Ulysses   Dune   Emma   Beloved   Lolita

This time they arrive in a different order, and Dune repeats. You write nothing in this lesson yet. Read the tables top to bottom and check at least one row with a pencil.

How we count

Every Put and Get counts comparisons: how many nodes you stepped on while going down. In code that is c.Hit(), once per node. Arriving at an empty place compares nothing, so the first node — the root — costs zero.

There is one rule: a smaller key goes left, a larger key goes right.

The tree grows

Seven Puts. Each row shows the path down and where the title ended up:

insertion         path down                             where it landed   cmps
put Hamlet        —                                     the root             0
put Dune          Hamlet: Dune smaller → left           left of Hamlet       1
put Ulysses       Hamlet: Ulysses larger → right        right of Hamlet      1
put Dune (again)  Hamlet → left; Dune = Dune            replaced             2
put Emma          Hamlet → left; Dune → right           right of Dune        2
put Beloved       Hamlet → left; Dune → left            left of Dune         2
put Lolita        Hamlet → right; Ulysses → left        left of Ulysses      2
                                                              total:        10

At the end the tree looks like this (six nodes, height 3):

                 Hamlet
               /        \
           Dune          Ulysses
          /    \         /
    Beloved    Emma   Lolita

And the lookups in this tree — one Get per title:

Get Hamlet      1 comparison     (the root)
Get Dune        2
Get Ulysses     2
Get Beloved     3                (Hamlet → Dune → Beloved)
Get Emma        3
Get Lolita      3
                       total:    14   (2.33 on average)
Get Zorba       2, and not found (Hamlet → Ulysses → empty on the right)

The in-order walk

Now the same tree hands over the whole library in alphabetical order. The rule: first the left subtree, then the node itself, then the right subtree.

walk(Hamlet)
    walk(Dune)
        walk(Beloved)        →  output: Beloved
        output: Dune
        walk(Emma)           →  output: Emma
    output: Hamlet
    walk(Ulysses)
        walk(Lolita)         →  output: Lolita
        output: Ulysses

The result: Beloved Dune Emma Hamlet Lolita Ulysses. Zero comparisons — no key was compared with another, because the order was already in the tree.

Two neighbouring questions for the same tree:

Min   Hamlet → Dune → Beloved      (go left while you can)    Beloved
Max   Hamlet → Ulysses             (go right while you can)   Ulysses

A range query: Emma .. Hamlet

The same walk, but some subtrees are skipped because they cannot hold an answer:

node      visited?   decision
Hamlet    yes        Emma < Hamlet → the left may hold some; Hamlet is in range
Dune      yes        Emma < Dune? no → its left (Beloved) is SKIPPED;
                     Dune < Emma → the right may hold some
Emma      yes        in range
Beloved   no         skipped
Ulysses   no         skipped: Hamlet < Hamlet? no → the right is not even touched
Lolita    no         skipped together with Ulysses
                     result: [Emma Hamlet],  visited 3 of 6

And if you ask for everything (A .. z), it visits all 6 of 6 — not one subtree can be skipped.

Four observations

Do not skip them — the following steps are built from nothing else.

1. The shape comes only from the insertion order. Every new node became a leaf — nothing was moved around. The same titles in another order would give another tree. Carry that thought with you to step 8.

2. An equal key replaces, it does not add. Dune the second time cost 2 comparisons (it found itself), and 6 nodes remain. Exactly like in lesson 10.

3. The sorted list cost zero. Not because the walk is "clever", but because the left always holds smaller keys and the right larger ones. So "left, me, right" cannot produce any other order. In the next step you will write that down as a rule — the BST invariant.

4. A range visits fewer nodes than the whole tree. 3 of 6 — a small tree, a small saving. The range in step 1 visited 11 of 1,000. The saving grows with the tree.

What next

Now you know what the tree does. In the next step you write Put — the main method — as Go code: the descent is given, two places are missing.