Sub topic 6
Ancestors and Descendants
This chapter assumes you can read basic Python, follow recursion, and already know the terms root, parent, child, and leaf.
The idea
Parent and child describe a single edge. Ancestor and descendant extend that idea along a whole path.
- An ancestor of a node is any node on the path from that node up to the root, excluding the node itself. This includes the parent, the grandparent, and so on up to the root.
- A descendant of a node is any node reachable by going down through its children, excluding the node itself. This includes the children, the grandchildren, and so on down to the leaves.
The two terms are mirror images. If X is an ancestor of Y, then Y is a descendant of X.
In the example tree from earlier chapters, A is the root, B, C and D are its children, E and F are children of B, and G is the child of D.
For node E, the ancestors are B and A. For node B, the descendants are E and F. The root A is an ancestor of every other node. Every node other than A is a descendant of A.
| Node | Ancestors | Descendants |
|---|---|---|
| A | none | B, C, D, E, F, G |
| B | A | E, F |
| D | A | G |
| E | B, A | none |
| C | A | none |
The root has no ancestors, and a leaf has no descendants.
Proper and inclusive versions
The definition above is the proper one, which excludes the node itself. Some textbooks use an inclusive version, where every node counts as its own ancestor and its own descendant. Check which one your source uses. It matters for statements such as "the root is an ancestor of all nodes", which holds in both versions only if the root is allowed to count as its own ancestor.
Finding descendants in code
Descendants are easy to collect with recursion, because the descendants of a node are its children plus the descendants of each child.
class Node:
def __init__(self, value, children=None):
self.value = value
self.children = children or []
def descendants(node):
result = []
for child in node.children:
result.append(child.value)
result.extend(descendants(child))
return result
tree = Node("A", [
Node("B", [Node("E"), Node("F")]),
Node("C"),
Node("D", [Node("G")]),
])
print(descendants(tree)) # ['B', 'E', 'F', 'C', 'D', 'G']
print(descendants(tree.children[0])) # ['E', 'F']
print(descendants(tree.children[1])) # []Nodes in this class do not store a reference to their parent, so you cannot walk upward to find ancestors from a node alone. You can find them by searching down from the root and recording the path, or by adding a parent attribute.
Common errors
- Listing a node as its own ancestor or descendant while using the proper definition.
- Treating siblings as related by ancestry. B and C share the ancestor A, but neither is an ancestor of the other.
- Forgetting that the root is an ancestor of every other node, not only of its children.
Exercise
In the example tree, list the ancestors of G and the descendants of D. Is C a descendant of B?
Show answer
The ancestors of G are D and A. The descendants of D are only G. C is not a descendant of B. C is a sibling of B, and no downward path leads from B to C.