Hi, Everyone. I am a newer at java programming. Now i have a assignment question is about:
Look at the file: BreadthFirstTraverse.java. It contains one static method, called trav that must traverse a input BinTree.java object in breadth-first order, adding it's values to an ArrayList which is then returned as a result.
All of the code in trav is missing. For this question, you must write this code. As an example of how the code should work when it is finished, the driver program: BreadthFirstDriver.java will output:
[on, the, first, day, of, christmas, my, true, love]
[the, day, of]
when compiled and run with a properly implemented verion of trav in BreadthFirstTraverse.java.
Random note about generics
Note that the signature to trav has a formal type parameter immediately after the static keyword. Static methods can exist independently of instances of their encapsulating classes. Because of this independence, generic static methods have their own type parameters.
Anyone can give me some tips where i can start with this question? Thanks very much!
--- Update ---
BreadthFirstDriver.java:
import java.util.ArrayList; public class BreadthFirstDriver{ public static void main(String [] args){ BinTree<String> carol9 = new BinTree<String>(null,null,"love"); BinTree<String> carol8 = new BinTree<String>(null,null,"true"); BinTree<String> carol7 = new BinTree<String>(carol8,carol9,"my"); BinTree<String> carol6 = new BinTree<String>(null,null,"christmas"); BinTree<String> carol5 = new BinTree<String>(null,null,"of"); BinTree<String> carol4 = new BinTree<String>(null,null,"day"); BinTree<String> carol3 = new BinTree<String>(carol6,carol7,"first"); BinTree<String> carol2 = new BinTree<String>(carol4,carol5,"the"); BinTree<String> carol1 = new BinTree<String>(carol2,carol3,"on"); System.out.println(BreadthFirstTraverse.trav(carol1)); System.out.println(BreadthFirstTraverse.trav(carol2)); } }
BreadthFirstTraverse.java:
import java.util.ArrayList; public class BreadthFirstTraverse{ /** * a method to traverse a BinTree in breadth-first order adding it's contents to an {@link ArrayList}. @param a binTree. @return an array list of the input tree's values. */ public static <T> ArrayList<T> trav(BinTree<T> t){ // fill in the missing code } }