Sub topic 4
Leaves and Internal Nodes
This chapter assumes you can write recursive functions and use classes that hold references to other objects. It builds on the node class from the earlier chapters, where each node stores a value and a list of children.
Two kinds of non-root nodes
Every node in a tree falls into one of two groups, based on its children.
- A leaf is a node with no children. It is also called an external node.
- An internal node is a node with at least one child. It is also called a non-leaf node.
The root counts as an internal node whenever the tree has more than one node. The root is not a special third category here. It is a node, and it has either children or none.
In the example tree below, A is the root. B and D are internal nodes. C, E, F, and G are leaves.
Checking in code
The Node class from the previous chapter already contains the test for leaves. A node is a leaf when its children list is empty.
class Node:
def __init__(self, value, children=None):
self.value = value
self.children = children or []
def is_leaf(self):
return not self.children
tree = Node("A", [
Node("B", [Node("E"), Node("F")]),
Node("C"),
Node("D", [Node("G")]),
])
def leaves(node):
if node.is_leaf():
return [node.value]
result = []
for child in node.children:
result.extend(leaves(child))
return result
def internal_nodes(node):
if node.is_leaf():
return []
result = [node.value]
for child in node.children:
result.extend(internal_nodes(child))
return result
print(leaves(tree)) # ['E', 'F', 'C', 'G']
print(internal_nodes(tree)) # ['A', 'B', 'D']Both functions use the same recursion. The leaf case is the base case that stops it. The lists are built in left-to-right order, which is why E and F come before C.
Common errors
- Calling the root a leaf. A root with children is internal. Only a tree with a single node has a root that is also a leaf.
- Treating "last in the list" as "leaf". C is the second child of A, yet it is a leaf. D is the last child of A, yet it is internal. Leaf status depends on a node's own children, not its position among its siblings.
- Testing
children is None. The constructor turns a missing argument into[]. A leaf therefore has an empty list, andis Nonewould never be true. Usenot self.children.
Why the distinction matters
Many measurements are defined through leaves. The height of a node, covered later, is the distance down to its deepest leaf. A leaf always has height 0, which gives the height function a base case. Internal nodes are where branching happens, so they decide how many leaves a tree can have.
Exercise
In a tree with root R, where R has one child S and S has two children T and U, which nodes are leaves and which are internal? What changes if you remove T and U?
Show answer
T and U are leaves. R and S are internal. If you remove T and U, S has no children, so S becomes a leaf and R is the only internal node.
Exercise
Why does internal_nodes(Node("X")) return an empty list?
Show answer
A single node with no children is a leaf, so the base case returns []. The root of a one-node tree is a leaf, not an internal node.