Kotlinlearncs.online LogoJava
Return to List

Test Writing: BinaryTree to Map

Created By: Geoffrey Challen
/ Version: 2021.4.0

Create a method toMap that accepts a BinaryTree<*> and returns a Map<Any, Int> mapping the values in the tree to the count of the times that the value appears.

Our suggestion is to have toMap create the map 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:

Note that you may need to cast tree.value to Any so that you can add it to your map.

Test Design Challenge

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

  • Provide a public class named TestBinaryTreeToMap 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 toMap(tree: BinaryTree<*>): Map<Any, Int>? {
// Create your map
// Call the helper method to populate the map
// Return the map
return null
}
// Helper method
private fun toMap(
tree: BinaryTree<*>?,
values: MutableMap<Any, Int>,
) {
// Add to mutableMap<Any, Int> by casting to Any
}