Question: //Q3.java import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; public class Q3 { // Instance variables for Q3 static Node root; public static void main(String[] args) {

![// Instance variables for Q3 static Node root; public static void main(String[]](https://s3.amazonaws.com/si.experts.images/answers/2024/08/66c88b1eaf52b_84666c88b1e4a4bb.jpg)
![args) { // Test code for readTokens char[] letters = {'c','a','o','u','d','n','r','g','p','w','b','r','t'}; System.out.println("Letters:](https://s3.amazonaws.com/si.experts.images/answers/2024/08/66c88b1f4c76e_84666c88b1ede79a.jpg)
//Q3.java
import java.util.Arrays; import java.util.LinkedList; import java.util.Queue;
public class Q3 {
// Instance variables for Q3 static Node root;
public static void main(String[] args) {
// Test code for readTokens char[] letters = {'c','a','o','u','d','n','r','g','p','w','b','r','t'}; System.out.println("Letters: " + Arrays.toString(letters));
// Test code for buildTree buildTree(letters);
// Test code for traverseTree System.out.println("Words: "); printTree(root, "");
}
/** * Add letters to ternary tree using a level-order traversal (breadth first search). *
* Take a moment to look over the setup code and implement the while loop. *
- *
- Remove a node, curr, from the {@code Queue} and assign it into a temporary variable *
- Assign the next three letters to curr's left, center, and right children. *
- Add the children into the queue in the same order as above. *
* You can assume that {@code char[] letters} will always contain {@code i * 3 + 1} letters. * * @param letters an array of letters */ static void buildTree(char[] letters) { int index = 0; root = new Node(letters[index++]); Queue /** * Traverse the tree using recursion and print the letters contained in * every path from the root to a leaf node. * * Follow this algorithm: * } } //Node.java public class Node { // Letter value, package private char letter; // Child nodes (left, center, right) // Make sure these references are package private (like the letter value, above) // YOUR CODE HERE public Node(char letter) { this.letter = letter; } } thanks! *
* @param current a reference to the tree node * @param answer a string */ static void printTree(Node current, String answer) { // YOUR CODE HERE
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
