Question: Create a public class BinaryTreeCounter that provides a single public class method named countParentLeftRightInOrder. Your method should accept a single BinaryTree and return a count
Create a public class BinaryTreeCounter that provides a single public class method named countParentLeftRightInOrder. Your method should accept a single BinaryTree and return a count of all nodes where the following three values are in ascending order:
The node's parent's value
The node's left child's value
The node's right child's value
You can ignore node's that don't have a parent and a left and right child. Which includes the root, which has no parent.
For example, consider the following tree:
A
B C
D E
Nodes A C D and E will not be counted, since they don't have a parent and both children. If the values of A D and E are and respectively, then we count Node B If the values of A D and E are and respectively, then we do not count Node B
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 countParentLeftRightInOrder being only a quick check and then starting the recursion on the left and right tree using 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
