Kotlinlearncs.online LogoJava
Return to List

Test Writing: Binary Tree Combine Even Strings

Created By: Pranav Narayanan
/ Version: 2025.2.0

Create a public class TreeCombineStrings that provides a single class method named combineEvenStrings. combineEvenStrings accepts a single BinaryTree<String>. If the passed tree is null, throw an IllegalArgumentException. Otherwise, return the concatenation of all even length Strings in this tree, in the order current + left + right (post-order traversal).

For example, given the tree:

      "12"
     /    \
  "cs"    "4"

You would return "12cs", by concatenating the current node "12", the left subtree "cs", and the right subtree "" (the right subtree has no even length Strings). If the tree was:

      "24"
     /
  "1"
     \
      "cs"

You would return "24cs", by concatenating the current node "24", and the left subtree "cs" (you ignore "1" because it is not even length). You may need to write a private helper method to solve this problem.

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 TestTreeCombineStrings 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
Report a Problem

Attribution must link to this page: https://www.learncs.online/testtesting/java/binary-tree-combine-even-strings/[email protected]