Put — fill in two holes
In the last step you watched the tree in a table. Now the same thing is written down as an algorithm — and you fill in two missing pieces.
The BST invariant
The whole structure rests on one sentence:
For every node: every key in the left subtree is smaller than its key, every key in the right subtree is larger.
Not "the child on the left is smaller" — the entire subtree. Everything else follows: search, the walk, the range.
A node has a key and two pointers:
node:
key the key (the title)
item the record
left pointer to the smaller ones
right pointer to the larger ones
In lesson 10 a node had one pointer (next in the chain). Here there are two
— and step 6 shows what that costs in memory.
Search — already written
Every comparison rules out an entire subtree:
Get(title):
cur ← root
while cur ≠ nil:
c.Hit()
if title < cur.key: cur ← cur.left
if title > cur.key: cur ← cur.right
otherwise: return cur.item, true
return empty, false
This is lesson 6's binary search, with the path written into the pointers instead of computed from indices. The same idea: one comparison halves the space that is left.
"Halves" — if the subtrees are the same size. When they are not, the cost rises, and step 8 shows how far that can go.
Get is already in your bst.go — it is your model.
Put in pseudocode
Insertion is the same search, only it ends not when it finds the key but when it reaches an empty place:
Put(item):
cur ← the address where the pointer to the root is kept
while *cur ≠ nil:
n ← *cur
c.Hit()
if item.Title = n.key:
n.item ← item
return // equal key: replaced
if item.Title < n.key: cur ← the address of n.left
otherwise: cur ← the address of n.right
*cur ← new leaf { key: item.Title, item: item }
count ← count + 1
A new node always becomes a leaf. The tree never reorganises itself — which is why insertion order decides the shape, and why step 8 is possible at all. This is not a simplification or a stopgap: if you "fix" it, there is nothing left to measure.
cur is a pointer to a pointer. It is the same table row you saw in the last
step: "Dune on the left" means that the left field of the Hamlet node was empty
and the new leaf was written into it. To be able to write into that field you
need its address, not a copy of it:
Put has to change the pointer in the node, not a copy of it.
cur := t.root // a copy: link a node to cur and the tree never sees it
cur := &t.root // pointer to a pointer: `*cur = &node{...}` changes the tree
This is the classic mistake of this exercise and it does not show at once: Put
raises no error, Len goes up, and Get finds nothing. The skeleton already uses
the second form — a recursive insert that returns the new subtree would be
equally correct.
Your job
Create bst.go and bst_test.go (see the panel).
bst.go is complete and compiles, but it does not work: the types, the
constructor, Len and Get are written, and Put has the descent — but two
places are missing. They are marked HOLE 1 and HOLE 2:
- HOLE 1 — when to go left. In the pseudocode that is
if item.Title < n.key. Replacefalsewith a real condition. - HOLE 2 — storing the new leaf in the empty place you found, through
cur, and counting it (t.n++).
Nothing else needs changing.
bst_test.go is the verdict — you do not write it, you pass it. For now it
checks only what exists: storing, finding and the last step's table.
The test checks that table itself: seven Puts must cost 0 1 1 2 2 2 2
comparisons, the tree must have exactly that shape (root Hamlet, Dune on the
left, Ulysses on the right, …), and the lookups must cost 1, 2, 2, 3, 3, 3. If
your code counts differently, something is off even if everything is found.
Run the test before you fill anything in
Run it as it stands first:
$ go test ./...
--- FAIL: TestTreeStoresAndFinds (0.00s)
bst_test.go:17: stored 0, want 1000
--- FAIL: TestTreeDuplicateKeysReplace (0.00s)
bst_test.go:39: stored 0 entries for one key, want 1
bst_test.go:42: got ID 0, want 2 — the last Put should win
--- FAIL: TestTreeReproducesTheLessonTrace (0.00s)
bst_test.go:79: Put 2 ("Dune"): 0 comparisons, want 1
bst_test.go:79: Put 3 ("Ulysses"): 0 comparisons, want 1
bst_test.go:79: Put 4 ("Dune"): 0 comparisons, want 2
bst_test.go:79: Put 5 ("Emma"): 0 comparisons, want 2
bst_test.go:79: Put 6 ("Beloved"): 0 comparisons, want 2
bst_test.go:79: Put 7 ("Lolita"): 0 comparisons, want 2
bst_test.go:83: stored 0 keys, want 6 (one of the seven puts was a repeat)
FAIL
That is not an error — it is the starting point. The skeleton stores nothing: the tree stays empty, so no descent costs anything.
Fill in both holes and run it again — you should see ok.
When it does not pass
root.left is Ulysses, want DuneandGet("00919"): ok=false— you flipped the inequality: smaller keys went right. The tree holds together, but its invariant is reversed, andGetwalks the right way and no longer finds them.Put 3 ("Ulysses"): 2 comparisons, want 1androot.left is -, want Dune— the first hole was left asfalse: no key ever went left, and everything piled up to the right in one long path.stored 0, want 1000— you linked the leaf but did not count it. The tree itself is fine; onlyLenis wrong. After linking you needt.n++.