Sub topic 16
Edge cases: one node and the empty tree
This chapter assumes you know the vocabulary from the earlier chapters (root, leaf, depth, height) and the two counting conventions. It also assumes you can read recursive Python. Here you check that the definitions still hold at the smallest possible inputs.
A tree with one node
Take a tree that contains only node A. No other node points to A, so A is the root. A has no children, so it is also a leaf. A node can be both at once, and this is the only case where that happens.
The values follow from the definitions:
- The depth of the root is 0, so the depth of
Ais 0. - A leaf has height 0 when you count edges, so the height of
Ais 0. - The tree's height is the root's height, so it is also 0.
- With the node-counting convention, every value above is one larger. The height is 1.
The empty tree
An empty tree has no nodes, so it has no root, no leaves, and no depths to report. Height is the only value that still needs an answer, and the answer depends on the convention:
- Counting edges, the height is -1. This keeps the rule "height = 1 + the largest child height" consistent. A leaf's children are all empty, so the leaf gets 1 + (-1) = 0.
- Counting nodes, the height is 0. A single node then has height 1, which matches the number of nodes on its longest path.
| Tree | Root | Height (edges) | Height (nodes) |
|---|---|---|---|
| Empty | none | -1 | 0 |
| One node | the node | 0 | 1 |
| Root with one child | the root | 1 | 2 |
Handling both cases in code
The height function from the previous chapter fails on an empty tree, because None has no children. Adding a guard fixes this. Return -1 for None, and the leaf case then works without special handling.
class Node:
def __init__(self, value, children=None):
self.value = value
self.children = children or []
def height(node):
# Edge-counting convention: empty tree is -1, a leaf is 0.
if node is None:
return -1
return 1 + max((height(child) for child in node.children), default=-1)
def depths(node, depth=0, result=None):
if result is None:
result = {}
if node is None:
return result # An empty tree has no nodes to report.
result[node.value] = depth
for child in node.children:
depths(child, depth + 1, result)
return result
single = Node("A")
empty = None
print(height(single)) # 0
print(height(single) + 1) # 1 (node-counting convention)
print(height(empty)) # -1
print(height(empty) + 1) # 0 (node-counting convention)
print(depths(single)) # {'A': 0}
print(depths(empty)) # {}Why the guard matters
Recursive code often reaches None through a missing child, as in a binary tree with an empty left side. If the base case returns -1 for None, a node with one child gets 1 + max(-1, height of that child), so the empty side never wins. The same function then works on every tree without a separate leaf check.
Exercise
A tree has a root R with a single child S, and S has no children. Using the edge-counting convention, what are the height of the tree, the depth of S, and the height of S? What is the tree's height under the node-counting convention?
Show answer
The height of the tree is 1, because the longest path from R has one edge. The depth of S is 1, because it is one edge below the root. The height of S is 0, because S is a leaf. Under the node-counting convention the tree's height is 2.