Kotlinlearncs.online LogoJava
Return to List

Test Writing: BinaryTree to List

Created By: Geoffrey Challen
/ Version: 2020.11.0

Create method toList that accepts a BinaryTree<Any> and returns a List<Any> containing all of the values in the tree, in any order.

Our suggestion is to have toList create the list and then call a private recursive helper method to populate it. You will need to import cs125.trees.BinaryTree. We've provided some code to get you started.

For reference, cs125.trees.BinaryTree is defined like this:

Test Design Challenge

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

  • Provide a public class named TestBinaryTreeToList 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 Kotlin's assert or check methods
import cs125.trees.BinaryTree
fun toList(tree: BinaryTree<*>): List<Any>? {
// Create the list
// Populate it using the helper method
// Return the list
return null
}
// Helper method
private fun toList(
tree: BinaryTree<*>?,
values: MutableList<Any>,
) {
}