beyondgrader.com Logo
DemoBrowseAboutTeamLogin

BinaryTree to List

Geoffrey Challen // 2020.11.0

Create a public class BinaryTreeToList that provides a single static method toList. toList accepts a BinaryTree and returns a List<Object> 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 the list. If the tree passed to toList is null you should throw an IllegalArgumentException. You will need to import cs125.trees.BinaryTree, as well as List and a List implementation (probably ArrayList) from java.util. We've provided some code to get you started. For reference, cs125.trees.BinaryTree has the following public properties:

import cs125.trees.BinaryTree;
import java.util.List;
public class BinaryTreeToList {
public static List<Object> toList(BinaryTree tree) {
// Check for null
// Create your list
// Call the helper method to populate the list
// Return the list
return null;
}
// Helper method
private static void toList(BinaryTree tree, List<Object> values) {}
}