Sub topic 7
Inorder: left, node, right
This chapter assumes you can read recursive Python, know how the call stack works, and have the Node class from the earlier chapter on representing a binary tree. Inorder is the second of the three depth-first orders.
The rule
An inorder traversal visits a node between its two subtrees. At every node it does three things in order:
- Traverse the left subtree completely.
- Visit the node.
- Traverse the right subtree completely.
Preorder visits the node first, and postorder visits it last. Inorder puts it in the middle.
Recursive implementation
The tree used here is the five-node tree from earlier in the tutorial.
class Node:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
# 1
# / \
# 2 3
# / \
# 4 5
root = Node(1, Node(2, Node(4), Node(5)), Node(3))
def inorder(node):
if node is None:
return []
return inorder(node.left) + [node.value] + inorder(node.right)
print(inorder(root)) # [4, 2, 5, 1, 3]The base case returns an empty list for None, so a missing child contributes nothing. Each call returns the values of its own subtree, and the + operators join the left values, the node and the right values in that order.
Tracing the call order
Node 1 cannot be visited until everything in its left subtree is done. That subtree is rooted at 2, and 2 in turn waits for node 4. The first value produced is therefore 4, the leftmost node of the tree.
The trace for the left side of the tree goes like this:
inorder(1)callsinorder(2), which callsinorder(4).inorder(4)gets two empty lists from itsNonechildren and returns[4].- Back in
inorder(2), the node 2 is added, theninorder(5)returns[5]. The result is[4, 2, 5]. - Back in
inorder(1), the node 1 is added, theninorder(3)returns[3]. The final result is[4, 2, 5, 1, 3].
Common errors
- Moving the node term. Putting
[node.value]first gives preorder, and putting it last gives postorder. The only difference between the three functions is the position of that term. - Forgetting the base case. Without the
Nonecheck, the function fails with anAttributeErrorat the first leaf. - Reading the output as a layout. Inorder
[4, 2, 5, 1, 3]matches a left-to-right reading of the tree only when you project each node straight down onto a line. That is a useful picture, but it is not a general rule about node positions. - Building lists with
+on large trees. Each+copies lists, so this version can cost more than O(n) on skewed trees. Appending to one shared list avoids this. A later chapter covers the cost of traversals.
Why inorder matters
Inorder is the order that matches the left-to-right structure of the tree. In a binary search tree, where smaller keys sit on the left and larger keys on the right, this order produces the keys in sorted order. A later chapter explains why.
Exercise
A tree has root 10, left child 5 and right child 15. Node 5 has only a right child, 7. What is the inorder traversal?
Show answer
[5, 7, 10, 15]. Inorder on the left subtree visits 5 (its left is empty), then its right child 7. Then it visits the root 10, and finally the right subtree, which is 15.