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

Tuesday

A tree in Python
// The fleet command tree
⏱ about 15 min

Tuesday: A tree in Python

Rocket wants the command tree saved in the fleet computer. "Do I need a new kind of variable?" he asks.

Raven remembers the cargo dictionary. "What do you notice about each person in the tree?"

"Each one has a name and a list of crew," Rocket says. "A dictionary with a list inside!"

"And the list holds more dictionaries," Raven adds.

Nova sparkles. "Here is a hint. Read the brackets from the outside in, one step at a time."

Nodes as dictionaries

Each node is a dictionary with two keys. "name" holds the person. "crew" holds a list of child nodes.

A leaf has an empty crew list. Read the program slowly, and match it to the table from Monday.

fleet = {
    "name": "Ada",
    "crew": [
        {"name": "Bo", "crew": [
            {"name": "Cy", "crew": []},
            {"name": "Dee", "crew": []},
        ]},
        {"name": "Eve", "crew": [
            {"name": "Fen", "crew": []},
            {"name": "Gil", "crew": []},
            {"name": "Hal", "crew": []},
        ]},
    ],
}

print(fleet["name"])
print(fleet["crew"][0]["name"])
print(fleet["crew"][1]["crew"][2]["name"])
print(len(fleet["crew"]))
Output:
Ada
Bo
Hal
2
  1. fleet["crew"] is Ada's list of children.
  2. fleet["crew"][1] is the second child, Eve's node.
  3. fleet["crew"][1]["crew"] is Eve's list of children.
  4. fleet["crew"][1]["crew"][2]["name"] is the name in spot 2 of that list: Hal.
What is fleet["crew"][0]["crew"][1]["name"]?
What is len(fleet["crew"][1]["crew"])?
What is fleet["crew"][1]["crew"][0]["name"]?
StatementTrue or false?
A leaf node has crew equal to [].?
fleet["crew"][0] is a name.?
Every node has the same two keys.?
WHY THIS EXERCISEBecause every node has the same shape, one function can handle any node.

Practice lab: the ship in Python

ship = {
    "name": "Ship",
    "parts": [
        {"name": "Deck A", "parts": [
            {"name": "Lab", "parts": []},
            {"name": "Dock", "parts": []},
        ]},
        {"name": "Deck B", "parts": [
            {"name": "Bridge", "parts": []},
            {"name": "Hold", "parts": []},
        ]},
    ],
}

print(ship["parts"][1]["name"])
print(ship["parts"][0]["parts"][1]["name"])
print(len(ship["parts"][1]["parts"]))
What does the first print line show?
What does the second print line show?
What does the third print line show?
What is ship["parts"][1]["parts"][0]["name"]?
BRACKET CHECK
  • Read the question.
  • Tap your answer.
What is ship["parts"][0]["parts"][0]["parts"]?
Which one is a list?
Which gets the name Hold?
StatementTrue or false?
Every node in ship has a name key and a parts key.?
ship["parts"][0] is the text Deck A.?
Hold is a leaf.?
The ship tree uses parts where the fleet uses crew.?
WHY THIS EXERCISEThe key names can change, but the tree shape stays the same.
What is fleet["crew"][0]["crew"][0]["name"]?
WHY THIS EXERCISETwo steps down from the root lands on Cy.

Super! You stored a whole tree with dictionaries and lists. Tomorrow we count every node with recursion.

← Monday