liveBook · Manning

Tree

As explained in Software Design for Python Programmers

A tree is a hierarchical data structure composed of nodes connected in parent-child relationships.

A tree is a hierarchical data structure whose nodes connect through parent-child relationships. The top node is the root; nodes at the bottom are leaves. Nodes may come from different classes and hold different datatypes. section 11.2

A tree supports several practical operations:

  • Organize related objects into nested levels, such as store, department, category, and product. section 11.2
  • Traverse from the root downward, visiting every node during a pass. section 11.2
  • Apply different algorithms to the same collection, such as printing hierarchy, counting activities, or calculating revenue. section 11.2
  • Represent a hierarchy with aggregation: Intramural contains sports, sports contain games, and games refer to hall leaves. section 11.2.2

Intramural sports reports

An Intramural root contains baseball, football, and volleyball Sport nodes. Each Sport contains Game nodes, while each Game refers to winning and losing Hall leaf nodes. Traversing the tree produces scores and activity counts; a separate hall list supports the winnings report. section 11.2.2

Figure 11.5 An example tree data structure of hierarchical data for a store. Store is the root node. The product nodes at the bottom (shaded in gray) are the leaf nodes.
A picture containing diagram, sketch, line, plan

Description automatically generated

Use the store hierarchy to inspect root, intermediate nodes, categories, and leaf products. section 11.2

How Software Design for Python Programmers covers it

How deep it goes
Explains roots, leaves, heterogeneous nodes, aggregation, and traversal, then demonstrates the structure with Python classes.
Examples
Uses store products and intramural sports reports to illustrate roots, departments, sports, games, halls, and leaves.
What it assumes
Assumes familiarity with classes and objects while demonstrating hierarchical aggregation through concrete domain examples.
What you can do after
Apply it via the Visitor Design Pattern to run multiple reports over one hierarchical data collection.
How it connects
Connects trees with parent-child relationships, aggregation, traversal, and the Visitor Design Pattern.

Also explained in

Compare how 11 books explain tree →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Design for Python Programmers. The section links point to the parts of the book it draws on.