4/38
Part 1 of 7 · TreesLearn and do

A tree has one way down to each region, so a walk down needs no seen-set

Count the rows and the regions, and a rule appears.

Two ways round the rock on a grid
yard
Saab#abbE

From S to E through the a cells or the b cells. In a tree, Sintra has one way down: Europe, Portugal, Lisbon District, Sintra.

There are 13 regions and 12 pairs, one pair for each region except the top. That's always so: a tree with nn nodes has n−1n - 1 edges, the links between a parent and a child. So there's exactly one way down from the top to any region.

A walk that starts at the top and goes into every child reaches each region once, by that one way. It never comes back to a region. That's why tree code has no seen-set.

On the grid, E can be reached around either side of the rock, so the search had to remember what it had already queued. A tree has no second way round. A general graph does, and the seen-set comes back with it near the end of this lesson.

The practice problem writes the tree a second way, and this task uses it. There's one list for each region that holds others: the region first, then the regions directly inside it. So ["Portugal", "Lisbon District", "Porto District"] says Portugal directly holds those two districts. Porto holds nothing, so it gets no list of its own, and the lists come in any order.

In the editor, finish build_tree(lists). It builds a small tree whose top's list isn't first.