Sub topic 14
Counting Edges or Counting Nodes
This chapter assumes you know the terms root, leaf, depth, and height, and can read the recursive height and depths functions from earlier chapters. It covers why two correct sources can give different numbers for the same tree, and how to convert between them.
Two conventions
Depth and height both measure a distance, and a distance along a path can be counted in two ways:
- Edge counting counts the connections (edges) between nodes. A path from A to E through B has 2 edges.
- Node counting counts the nodes on the path, including both ends. The same path has 3 nodes.
Both are valid. The problem is that sources rarely say which one they use.
| Quantity in the example tree | Edge counting | Node counting |
|---|---|---|
| Depth of root A | 0 | 1 |
| Depth of B | 1 | 2 |
| Depth of E | 2 | 3 |
| Height of leaf C | 0 | 1 |
| Height of B | 1 | 2 |
| Height of tree (root A) | 2 | 3 |
The node-counting value is always the edge-counting value plus 1. Under node counting, depth 1 is the same thing as level 1, so depth and level coincide.
The path above has 3 nodes and 2 edges. Edge counting reports 2. Node counting reports 3.
Where each convention appears
Edge counting is common in textbooks and in definitions based on paths. It gives a leaf height 0 and the root depth 0. Node counting is common in some course materials and interview problems. It gives a leaf height 1 and the root depth 1.
The empty tree shows the difference most clearly. With node counting, an empty tree has height 0 and a single node has height 1. With edge counting, a single node has height 0, so the empty tree needs the value -1 to keep the recursion consistent. Later chapters cover this edge case in detail.
Converting in code
The height function from the previous chapter uses edge counting. To switch to node counting, change the base value:
class Node:
def __init__(self, value, children=None):
self.value = value
self.children = children or []
def height_edges(node):
# A leaf has height 0.
return 1 + max((height_edges(c) for c in node.children), default=-1)
def height_nodes(node):
# A leaf has height 1.
return 1 + max((height_nodes(c) for c in node.children), default=0)
tree = Node("A", [
Node("B", [Node("E"), Node("F")]),
Node("C"),
Node("D", [Node("G")]),
])
print(height_edges(tree)) # 2
print(height_nodes(tree)) # 3Only the default value differs. It is the height contributed by "no children", and it sets the value for every leaf.
Common errors
- Mixing conventions inside one program, for example computing height with edges and comparing it to a depth counted with nodes. The off-by-one then appears in comparisons such as
depth == height. - Copying a formula from a source without checking its convention. A bound like "at most 2^h leaves" depends on whether h counts edges.
- Assuming a leaf's height is always 0. That is true only for edge counting.
Exercise
A source says a tree has height 4 and counts nodes. What is the height of the same tree when counting edges, and how many nodes are on its longest root-to-leaf path?
Show answer
The height is 3 when counting edges, because node counting is edges plus 1. The longest root-to-leaf path has 4 nodes, which is the node-counted height.