beyondgrader.com Logo
DemoBrowseAboutTeamLogin

BinaryTree to Map

Geoffrey Challen // 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.