← Back to course
Code Crew G5 / Week 10 / Crew Quest
6/6
Week 10 Β· Trees

Crew Quest

Crew Quest: Tree of things
// The fleet command tree
⏱ about 15 min

Crew Quest: Tree of things

Rocket wants to see trees off the ship too. He looks around the galley.

"The galley has cupboards. Each cupboard has shelves. Each shelf holds things," he says.

Raven grabs a pencil. "What do you notice? That is a tree!"

"The galley is the root," Rocket says. "The things on the shelves are the leaves."

Nova hovers over the sketch. "Here is a hint. Count the leaves, then count every node."

  1. A tree has a root at the top. Each node can have children below it.
  2. A leaf has no children.
  3. In Python, a node can be a dictionary with a name and a list of children.
  4. Every child is the root of a smaller tree, so recursion fits.
  5. count, show, count_leaves and has_member all visit each node.
β—‡ TREE OF THINGS
Pick a room or a shelf at home. Make it the root of a tree.
Draw its parts as children, like shelves or boxes.
Add the things inside each part as leaves.
Count the leaves. Then count every node.
Point to one child and say what its smaller tree holds.
For a grown-up
A bookshelf, toy bin or kitchen cupboard works well. Keep it to objects, not people.
RECAP QUESTION
  • Read the question.
  • Tap your answer.
A node has an empty list of children. What is it called?
Bonus recap statementTrue or false?
The root is at the top of a tree.?
A leaf has many children.?
Every child is the root of a smaller tree.?
count visits every node once.?
WHY THIS EXERCISEThese four ideas make recursion fit trees.
How many nodes are in the fleet tree?
How many leaves are in the fleet tree?
How many spaces does show print before a node at depth 3?

Quest complete! Next week you choose and plan your capstone project.

← Friday