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

Monday

The command tree
// The fleet command tree
⏱ about 15 min

Monday: The command tree

The fleet is growing, and new crew keep asking the same thing: who do I report to?

Rocket tries a list of everyone. "Ada, Bo, Cy, Dee, Eve," he reads. "But that does not show who leads who."

Raven sketches Ada at the top, with lines down to her two leads. "What do you notice about Bo's part of the drawing?"

"It looks like a small copy of the whole thing," Rocket says. "Bo at the top, with crew below."

Nova projects the drawing. "Here is a hint. This shape has a name. It is a tree, drawn upside down."

Parts of a tree

Each person in the tree is a node. Ada is the root, at the top. Lines go down from each node to its children.

NodeLeads (children)
Ada (captain)Bo, Eve
Bo (pilot lead)Cy, Dee
Eve (science lead)Fen, Gil, Hal
Cy, Dee, Fen, Gil, Halnobody
  • The root is the node at the top: Ada.
  • A child is a node directly below another node. Bo and Eve are Ada's children.
  • A leaf is a node with no children. Cy, Dee, Fen, Gil and Hal are leaves.
  • Every child is the root of its own smaller tree. Eve's smaller tree has Eve, Fen, Gil and Hal.
READ THE COMMAND TREE.
  • Read the question.
  • Tap your answer.
Who is the root?
Who are Bo's children?
Is Eve a leaf?
How many nodes are in the whole tree?
How many leaves are in the tree?
How many nodes are in Eve's smaller tree, counting Eve?
Draw the command tree. Put Ada at the top and draw lines down to each child.

Practice lab: the ship tree

Trees can hold places, not just people. Here is the ship, drawn as a tree of decks and rooms.

NodeChildren
ShipDeck A, Deck B
Deck ALab, Dock
Deck BBridge, Hold
Lab, Dock, Bridge, Holdnone
Which node is the root?
How many nodes are in the whole tree?
How many leaves are in the tree?
How many nodes are in Deck B's smaller tree, counting Deck B?
SHIP TREE CHECK
  • Read the question.
  • Tap your answer.
Is Lab a leaf?
Which node has Lab as a child?
Which of these is not a leaf?
StatementTrue or false?
Ship has two children.?
Bridge is the root.?
Deck A is the root of its own smaller tree.?
A leaf has an empty list of children.?
WHY THIS EXERCISEEach child is the root of a smaller tree.
A new room called Scope is added inside Lab. How many nodes are in the tree now?
WHY THIS EXERCISEScope is one more node. Lab is no longer a leaf.

Great work! Trees are everywhere in computing, from folders on a computer to the shape of a web page. Tomorrow we store the tree in Python.