Day 22 - Intro to Trees
NOTE: this is way too short -- should actually do more work with the table, or get to the tree here.
1. Introduction to Trees
- Overview:
- Explain that trees are hierarchical data—data that nest sub-entities within a larger entity.
- Introduce a real-world scenario: an organization’s hierarchy, where each employee (or manager) has a name, title, and (possibly empty) list of direct reports.
- Why Tables Fall Short:
- A table represents flat rows. While you can include a “manager” field in each row, you lose the nesting of subordinates.
- Example questions that are hard to answer with a table:
- “Who are all the subordinates reporting (directly or indirectly) to a given manager?”
- “What is the depth (number of levels) of the organization?”
- Do Now:
- Ask: “Imagine you have a table with columns: name, title, manager. How would you list the direct reports of a manager? How would you compute the full chain of command?”
- Brief discussion to surface difficulties (e.g., repeated table scans, inability to naturally express nested structure).
2. An Example Organization Table (15 minutes)
- Present a Table Example:
- Show a simplified organization table:
org-table = table: name, title, manager
row: "Alice", "CEO", ""
row: "Bob", "CTO", "Alice"
row: "Carol", "CFO", "Alice"
row: "Dave", "Engineer", "Bob"
row: "Eve", "Accountant", "Carol"
row: "Frank", "Engineer", "Bob"
end
- Show a simplified organization table:
- Interactive Discussion:
- Ask: “How would you compute, using table functions, the list of direct reports for Bob?”
- (Students may suggest filtering rows where manager equals "Bob.")
- Next, ask: “How would you compute all reports (not just direct) for Bob?”
- Emphasize that while it is possible, it becomes clumsy and inefficient when the organization has many levels.
- Critique:
- Summarize the pitfalls:
- Repeated filtering across generations.
- Loss of inherent tree structure.
- Harder to compose recursive operations (e.g., computing organizational depth).
- Summarize the pitfalls:
3. Conclusion and Reflection (10 minutes)
- Summary:
- Explain that while tables are great for flat data, they make representing and processing hierarchical (tree) data cumbersome.