Kotlinlearncs.online LogoJava
Return to List

Test Writing: Binary Tree Any Same Shape

Created By: Geoffrey Challen
/ Version: 2023.11.0

Create a public class named BinaryTreeSameShape with a static method sameShape that accepts two BinaryTree<?> arguments and returns true if they have the same shape and false otherwise. Two binary trees have the same shape if they have the same nodes in the same positions, regardless of the values that each node contains.

sameShape should throw an IllegalArgumentException if either of its arguments are null. As a result, you probably want to set up a private helper function to actually perform the recursion, allowing you to handle null as a base case.

In this problem you'll perform recursion on both trees at the same time. But in every other way it's a typical tree recursion problem. Consider your base case: if you reach a null subtree for both trees in the same step, then they have the same shape at that point in the tree. Otherwise, consider the other cases and how to handle them, as well as how to continue the recursion and combine the results from your recursive steps together.

For reference, cs125.trees.BinaryTree has the following public properties:

Test Design Challenge

You're challenge is to write tests for this problem described above.

  • Provide a public class named TestBinaryTreeSameShape with a single non-private class method named test that accepts no arguments and does not return a value.
  • If the implementation of the class described above is incorrect, your test method should throw an exception.
  • If it is correct, do not throw an exception.
  • You may want to use Java's assert method