Close
Register
Close Window

Show Source |    | About   «  7.6. Implementing Tree Traversals   ::   Contents   ::   7.8. Binary Tree Node Implementations  »

7.7. Information Flow in Recursive Functions

7.7.1. Information Flow in Recursive Functions

Handling information flow in a recursive function can be a challenge. In any given function, we might need to be concerned with either or both of:

  1. Passing down the correct information needed by the function to do its work,

  2. Returning (passing up) information to the recursive function’s caller.

Any given problems might need to do either or both. Here are some examples and exercises.

7.7.1.1. Local

Local traversal involves going to each node in the tree to do some operation. Such functions need no information from the parent (other than a pointer to the current node), and pass no information back. Examples include preorder traversal and incrementing the value of every node by one.

7.7.1.2. Passing Down Information

Slightly more complicated is the situation where every node needs the same piece of information to be passed to it. An example would be incrementing the value for all nodes by some amount. In this case, the value parameter is simply passed on unchanged in all recursive calls.

Many functions need information that changes from node to node. A simple example is a function to set the value for each node of the tree to be its depth. In this case, the depth is passed as a parameter to the function, and each recursive call must adjust that value (by adding one).

7.7.2. Binary Tree Set Depth Exercise

7.7.3. Collect-and-return

Collect-and-return requires that we communicate information back up the tree to the caller. Simple examples are to count the number of nodes in a tree, or to sum the values of all the nodes.

Example 7.7.1

Consider the problem of counting (and returning) the number of nodes in a binary tree. The key insight is that the total count for any (non-empty) subtree is one for the root plus the counts for the left and right subtrees. Where do left and right subtree counts come from? Calls to function count on the subtrees will compute this for us. Thus, we can implement count as follows.

The following solution is correct but inefficient as it does redundant checks on the left and the right child of each visited node.

When you write a recursive function that returns a value, such as counting the number of nodes in the subtree, you have to make sure that your function actually returns a value. A common mistake is to make a recursive call and not capture the returned value. Another common mistake is to not return a value.

Settings

Proficient Saving... Error Saving
Server Error
Resubmit

7.7.4. Binary Tree Check Sum Exercise

7.7.5. Binary Tree Leaf Nodes Count Exercise

7.7.6. Binary Tree Sum Nodes Exercise

7.7.7. Combining Information Flows

Many functions require both that information be passed in, and that information be passed back. Let’s start with a relatively simple case. If we want to check if some node in the tree has a particular value, that value has to be passed down, and the count has to be passed back up. The downward flow is simple, as the value being checked for never changes. The information passed up has the simple collect-and-return style: Return True if and only if one of the children returns True.

7.7.8. Binary Tree Check Value Exercise

7.7.9. Combination Problems

Slightly more complicated problems combine what we have seen so far. Information passing down the tree changes from node to node. Data passed back up the tree uses the collect-and-return paradigm.

7.7.10. Binary Tree Height Exercise

7.7.11. Binary Tree Get Difference Exercise

7.7.12. Binary Tree Has Path Sum Exercise

   «  7.6. Implementing Tree Traversals   ::   Contents   ::   7.8. Binary Tree Node Implementations  »

nsf
Close Window