Sub topic 1
What a Tree Is: Vocabulary
Prerequisites
This chapter assumes you can write basic Python classes, understand recursion and the call stack, know what stacks and queues are, and can read simple Big-O notation. No tree knowledge is needed. This chapter only defines the words that later chapters use.
Trees as hierarchical data
A tree is a collection of nodes connected by edges. It has one starting node and no cycles. Every node except the starting node has exactly one parent. File systems, HTML documents and organization charts all have this shape.
A binary tree is a tree where each node has at most two children. They are called the left child and the right child. Left and right are distinct positions. A node with only a right child is a different tree from a node with only a left child.
The core vocabulary
Here is a five-node binary tree. The rest of the tutorial reuses it.
| Term | Meaning | In the example |
|---|---|---|
| Root | The one node with no parent | 1 |
| Parent | A node directly above another node | 2 is the parent of 4 |
| Child | A node directly below another node | 4 and 5 are children of 2 |
| Siblings | Nodes that share a parent | 4 and 5 |
| Leaf | A node with no children | 3, 4, 5 |
| Internal node | A node with at least one child | 1, 2 |
| Edge | A link from a parent to a child | 1 to 2 |
| Ancestor | Any node on the path from the root to a node | 1 and 2 are ancestors of 5 |
| Descendant | Any node below a node | 4 and 5 are descendants of 1 |
| Subtree | A node together with all its descendants | the subtree rooted at 2 is 2, 4, 5 |
| Depth | Number of edges from the root to a node | depth of 5 is 2 |
| Height | Number of edges on the longest path down to a leaf | height of 2 is 1 |
Depth is measured from the root down. Height is measured from a node down to its deepest leaf. The height of the whole tree is the height of the root, which here is 2.
Why subtrees matter
Every child of a node is itself the root of a smaller tree. This is why tree code is usually recursive. A tree is either empty, or a root with a left subtree and a right subtree. Each of those is again a tree.
# A tree is empty (None) or a node with two subtrees.
def count_nodes(node):
if node is None:
return 0
return 1 + count_nodes(node.left) + count_nodes(node.right)The function assumes a node class with left and right attributes. The next chapter builds that class.
Common errors
- Mixing up depth and height. Depth counts up to the root. Height counts down to the deepest leaf.
- Counting nodes instead of edges. This tutorial counts edges, so a single node has height 0 and depth 0.
- Treating "empty tree" and "leaf" as the same thing. A leaf is a node. An empty tree has no nodes.
Exercise
In the example tree, what are the depth of node 4, the height of node 2, and the leaves of the subtree rooted at 2?
Show answer
Node 4 has depth 2, because the path 1 to 2 to 4 has two edges. Node 2 has height 1, because its longest downward path (2 to 4) has one edge. The leaves of the subtree rooted at 2 are 4 and 5.