Question: public class TreePrinter { private static final int MAX_LEVELS = 6; private BinarySearchTree tree; // the tree private int height; // its height // Powers

public class TreePrinter { private static final int MAX_LEVELS = 6; private BinarySearchTree tree; // the tree private int height; // its height // Powers of 2 private static int POWERS_OF_2[] = new int[MAX_LEVELS+2]; static { int power = 1; for (int i = 0; i tree) { this.tree = tree; this.height = tree.height(); } /** * Print the tree using a level-order traversal. * @param label the label to print before the tree. */ public void print(String label) { System.out.println(label);

// Queue of nodes at this level. BinaryNode thisLevelNodes[] = (BinaryNode[]) new BinaryNode[1]; int offset = POWERS_OF_2[(height+1)]-1; thisLevelNodes[0] = tree.getRoot(); // Loop to print each level of nodes. for (int level = 0; level MAX_LEVELS) { System.out.println("*** Too many levels to print. ***"); break; } // Print node data. printData(level, offset, thisLevelNodes); if (level != height) { // Print outgoing pointers /\ from each parent node. printOutgoingPointers(level, offset, thisLevelNodes); offset = POWERS_OF_2[height - level] - 1; // Print connecting dashes ---- if (level levelNodes[]) { printSpaces(offset); int k = POWERS_OF_2[level]; for (int i = 0; i node = levelNodes[i]; if (node != null) { System.out.printf("%3d ", node.getData()); } else { System.out.print(" "); } // Space over to the next node in this level. if (i levelNodes[]) { printSpaces(offset); int k = POWERS_OF_2[level]; for (int i = 0; i node = levelNodes[i]; // Has left child: print / if ((node != null) && (node.getLeft() != null)) { System.out.print(" /"); } // No left child: print space else { System.out.print(" "); } // Has right child: print \ if ((node != null) && (node.getRight() != null)) { System.out.print("\\ "); } // No right child: print space else { System.out.print(" "); } // Space over to the next node in this level. if (i levelNodes[]) { if (offset > 1) printSpaces(offset); int k = POWERS_OF_2[level]; for (int i = 0; i node = levelNodes[i]; // Has left child: print dashes if ((node != null) && (node.getLeft() != null)) { printSpaces(3); printDashes(offset-1); } // No left child: print spaces else { printSpaces(offset+2); } // Has right child: print dashes if ((node != null) && (node.getRight() != null)) { printSpaces(2); printDashes(offset-1); } // No right child: print spaces else { printSpaces(offset+1); } // Space over to the next node in this level. if (i levelNodes[]) { printSpaces(offset); int k = POWERS_OF_2[level]; for (int i = 0; i node = levelNodes[i]; // Left child: print / if ((node != null) && (node.getLeft() != null)) { System.out.print(" /"); } // No left child: print spaces else { printSpaces(3); } // Right child: print \ if ((node != null) && (node.getRight() != null)) { printSpaces(2*offset); System.out.print("\\"); } // No right child: print spaces else { printSpaces(2*offset+1); } // Space over to the next node in this level. if (i [] nextLevel(int level, BinaryNode levelNodes[]) { BinaryNode nextLevel[] = (BinaryNode[]) new BinaryNode[POWERS_OF_2[level+1]]; for (int i = 0; i node = levelNodes[i]; // Queue the left child nodes of each non-null parent node. nextLevel[2*i] = (node != null) && (node.getLeft() != null) ? node.getLeft() : null; // Queue the right child nodes of each non-null parent node. nextLevel[2*i+1] = (node != null) && (node.getRight() != null) ? node.getRight() : null; } return nextLevel; } /** * Print spaces. * @param count the number of spaces. */ private void printSpaces(int count) { for (int i = 0; i

public class TreePrinter { private static final int MAX_LEVELS = 6; privateBinarySearchTree tree; // the tree private int height; // its height //Powers of 2 private static int POWERS_OF_2[] = new int[MAX_LEVELS+2]; static {

You are provided a TreePrinter class that has a print) method that will print any arbitrary binary tree. A template for how it prints a tree: K K X KE E KEKEE KE EE EEXEE E E TreePrinter is able to print trees with height up to 5, i.e., 32 node values on the bottom row. An example of an actual printed tree: You are provided a TreePrinter class that has a print) method that will print any arbitrary binary tree. A template for how it prints a tree: K K X KE E KEKEE KE EE EEXEE E E TreePrinter is able to print trees with height up to 5, i.e., 32 node values on the bottom row. An example of an actual printed tree

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!