Kotlinlearncs.online LogoJava
Return to List

Test Writing: Binary Tree Size

Created By: Geoffrey Challen
/ Version: 2020.11.0

Create a public class BinaryTreeSize that provides a single static method size. size accepts a cs125.trees.BinaryTree<?>, a BinaryTree that can contain any value, and returns the number of nodes it contains. You'll want to count recursively, identifying both a base case and a recursive step.

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

Don't overthink this! Like many recursive algorithms, the solution is elegant and simple: 4 lines total if you do it right. You'll also need to import cs125.trees.BinaryTree for this and similar problems.

Test Design Challenge

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

  • Provide a public class named TestBinaryTreeSize 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