= Solution
Give the visiting procedure a <procedure type>:
``
TYPE Visit = PROCEDURE(x: INTEGER);
PROCEDURE Inorder(t: Tree; p: Visit);
BEGIN
IF t # NIL THEN
Inorder(t.left, p);
p(t.label);
Inorder(t.right, p)
END
END Inorder;
``
<Structural induction> establishes <algorithm correctness>: an empty <binary tree> makes no calls; otherwise the first <recursive call> visits precisely the left <subtree>, then the root is visited once, then the second <recursive call> visits precisely the right <subtree>. Assume `p` does not modify the <binary tree>. With constant-cost `p`, there is constant work per node and empty child occurrence:
$$
\boxed{T(n)=\Theta(n+1),\qquad\text{auxiliary storage }O(h+1),}
$$
where $h$ is the <height of a binary tree> and storage is the <call stack>.
Back to article page