Question: Create a public class BinaryTreeCounter that provides a single public class method named countAnyEqualToEight. Your method should accept a single BinaryTree and return a count
Create a public class BinaryTreeCounter that provides a single public class method named countAnyEqualToEight. Your method should accept a single BinaryTree and return a count of all nodes where any of the following values are equal to :
That node's value
Its right child's value
Its left child's value
Its parent's value
This problem is somewhat more complicated than previous binary tree problems that we have solved. This is because the binary tree data structure that we have provided does not provide access to a node's parent! It could, but it doesn't. Sorry.
As a result, you will need to pass the parent's value to the recursive calls on its children. Doing this will result in your public method countAnyEqualToEight being only a quick check and then starting the recursion on another method, which should be marked private. We've provided you with some starter code reflecting this design pattern, but you are welcome to modify the name and method signature of the private method as needed.
If the tree passed to your public method is null, you can return since an empty tree has no children. You will probably want to do this to avoid calling getValue on a null tree.
For reference, cstrees.BinaryTree has the following public properties:
public class BinaryTree
public Object getValue; returns the value
public BinaryTree getRight; returns the right node
public BinaryTree getLeft; returns the left node
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
