Question: public void printTree() { if(root == null) { System.out.println(Tree is empty); return; } LinkedList queue = new LinkedList(); Vector nodes = new Vector(); int level

 public void printTree() { if(root == null) { System.out.println("Tree is empty");

public void printTree() { if(root == null) { System.out.println("Tree is empty"); return; } LinkedList queue = new LinkedList(); Vector nodes = new Vector(); int level = 0;

// enqueue the root queue.add(root);

// now print out each level - if a node is null, just enqueue null while(level

/* * printLevel */ private void printLevel(Vector nodes, int level) { // tab over for the first node in the level //for(int i=0; i

// printing out the node - no sibling if(nodes.size() == 1) { TreeNode tmp = (TreeNode)nodes.get(0); System.out.print(tmp.key + "(" + tmp.balFactor + ")"); }

// every other node has or could have a sibling else { for(int i=0; i

// print out the first node if it exists if(tmp != null) System.out.print(tmp.key + "(" + tmp.balFactor + ")"); else System.out.print("\t");

// tab over to it's sibling for(int j=0; j

// now print out the sibling if it exists tmp = (TreeNode)nodes.get(i+1); if(tmp != null) System.out.print(tmp.key + "(" + tmp.balFactor + ")"); else System.out.print("\t");

// tab over to it's cousin for(int j=0; j

// done with this level System.out.print(" "); }

/* * minValue */ private int minValue(int x, int y) { return (x y) ? x : y; } }

public void printTree() This method prints the first 4 levels of the tree. It prints it in a way that show the parent, child relationship. For each node, it shows the key and the balance factor. private void This method is called by printTree method. printLevel(Vector nodes, It is used to print the nodes at a particular int level) level. private int minValue(int x, You may need to call this method when inty) recalculating the balance factor of a node. It returns the minimum value of x and y. private int maxValue(int x, You may need to call this method when inty) recalculating the balance factor of a node. It returns the maximum value of x and y. public void printTree() This method prints the first 4 levels of the tree. It prints it in a way that show the parent, child relationship. For each node, it shows the key and the balance factor. private void This method is called by printTree method. printLevel(Vector nodes, It is used to print the nodes at a particular int level) level. private int minValue(int x, You may need to call this method when inty) recalculating the balance factor of a node. It returns the minimum value of x and y. private int maxValue(int x, You may need to call this method when inty) recalculating the balance factor of a node. It returns the maximum value of x and y

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!