Guess what? In this lesson we’ll be doing more practice with binary trees! And recursion! What could be more fun?
But, would we want to miss out on another debugging challenge? No way!
Create a public class BinaryTreeCounter that provides a single class method named countEqualToEitherChild that
accepts a single BinaryTree and counts the number of nodes in the tree where the value at that node is equal to
either the value at its right child or the value at its left child. Keep in mind that not every node has a
right or left child, so you'll need to check for null carefully. (Or use try-catch!) However, you can assume
that all of the values in the tree are non-null.
Your solution must be recursive: countEqualToEitherChild should call itself on the left and right subtrees, or call
a private recursive helper method that does. A loop that walks the tree won't pass.
For reference, cs125.trees.BinaryTree has the following public properties:
As a warm up let’s write a recursive function to determine the depth or height of a tree. As a reminder, the depth is defined as the distance from the root node to the farthest leaf node. (The depth is not defined for an empty tree, since it has no root.)
Next, let’s look at an example of a recursive function that passes another data structure around. We’ll write a recursive method that returns an array with counts of the number of nodes that have zero, one, or two children. This will also prepare you for this lesson’s homework problem—which is a tricky one!
Write a method sumEvenDepths that accepts a BinaryTree<Integer> and returns an int: the sum of the values in
all of the nodes that are at an even depth in the tree.
The tree passed to sumEvenDepths will not be null.
The root node is at depth 0, its children are at depth 1, their children are at depth 2, and so on. So the values that you should add up are the root's value, the values of its grandchildren, the values of their grandchildren, and so on.
Our suggestion is to have sumEvenDepths call a private recursive helper method that accepts both a tree and the
depth of that tree's root node.
Then each recursive call can pass its children a depth that is one larger.
Your solution must be recursive: sumEvenDepths should call itself on the left and right subtrees, or call a private
recursive helper method that does. A loop that walks the tree won't pass.
For reference, cs125.trees.BinaryTree has the following public methods:
Create a public class BinaryTreeToMap that provides a single static method toMap.
toMap accepts a BinaryTree<?> and returns a Map<Object, Integer> mapping the values in the tree to the
count of the times that the value appears.
Our suggestion is to have toMap create the map and then call a private recursive helper method to populate it.
If the tree passed to toMap is null you should throw an IllegalArgumentException.
You will need to import cs125.trees.BinaryTree, as well as Map and a Map implementation (probably HashMap)
from java.util.
We've provided some code to get you started.
Your solution must be recursive: toMap should call itself on the left and right subtrees, or call a private
recursive helper method that does. A loop that walks the tree won't pass.
For reference, cs125.trees.BinaryTree has the following public properties:
Need more practice? Head over to the practice page.