Sub topic 9
Depth: Distance from the Root
This chapter assumes you can write recursive functions and know what a node object with a list of children looks like. It covers depth, the first of the two measurements that people often mix up. Height comes in a later chapter.
What depth measures
The depth of a node is the number of edges on the path from the root down to that node. Each tree has exactly one such path to each node, so depth is well defined.
- The root has depth 0, because the path from the root to itself has no edges.
- A child has depth equal to its parent's depth plus one.
- Siblings always share the same depth.
Depth looks upward. To find it, you ask how far a node is from the top.
Depth in the example tree
Here is the tree used in this tutorial.
To get the depth of G, follow the path A, D, G. That path has two edges, so G has depth 2.
| Node | Path from root | Depth |
|---|---|---|
| A | A | 0 |
| B | A, B | 1 |
| C | A, C | 1 |
| D | A, D | 1 |
| E, F | A, B, E (or F) | 2 |
| G | A, D, G | 2 |
Depth and level
A level is the set of all nodes that have the same depth. Level 0 holds the root, level 1 holds B, C, and D, and level 2 holds E, F, and G. Some sources start counting levels at 1, so check which convention a source uses. A later chapter covers this in detail.
Computing depth in code
Depth is computed top-down. You pass the parent's depth to each child and add one. This code reuses the Node class from the earlier chapter.
class Node:
def __init__(self, value, children=None):
self.value = value
self.children = children or []
tree = Node("A", [
Node("B", [Node("E"), Node("F")]),
Node("C"),
Node("D", [Node("G")]),
])
def depths(node, depth=0, result=None):
# The root has depth 0. Each step down adds one edge.
if result is None:
result = {}
result[node.value] = depth
for child in node.children:
depths(child, depth + 1, result)
return result
print(depths(tree))
# {'A': 0, 'B': 1, 'E': 2, 'F': 2, 'C': 1, 'D': 1, 'G': 2}The function visits each node once, so it takes time proportional to the number of nodes. The result dictionary is created on the first call and shared by all recursive calls. Using result=None as the default avoids the common Python bug where a mutable default argument is shared between separate calls.
Common errors
- Starting the root at 1. With the edge-counting convention, the root is 0. If a source says the root is at depth 1, it counts nodes instead of edges.
- Mixing up depth and height. Depth counts down from the root to a node. Height counts down from a node to its deepest leaf. In the example, node B has depth 1 but height 1, and node C has depth 1 but height 0.
- Forgetting to increase the depth. Passing
depthinstead ofdepth + 1to the recursive call gives every node depth 0.
Exercise
A node X has parent Y, and Y has parent Z. Z is the root. What is the depth of X, and which other nodes must have the same depth as Y?
Show answer
X has depth 2, because the path Z, Y, X has two edges. Every sibling of Y has the same depth as Y, which is 1. Other nodes at depth 1 are the children of the root Z.