Question: Write a method called printLevel that accepts an integer parameter n and prints the values at level n from left to right, one per line.
Write a method called printLevel that accepts an integer parameter n and prints the values at level n from left to right, one per line. We will use the convention that the overall root is at level 1, its children are at level 2, and so on. If there are no values at the level, your method should produce no output. Your method should throw an IllegalArgumentException if it is passed a value for a level that is less than 1. For example, if a variable t refers to reference tree #2, then the call of t.printLevel(3); would produce the following output: 0 7 6
please also create a main program to test the code.

Provided Java Files
**************
IntTree.java
**************
} }
// post: prints the tree contents using a preorder traversal public void printPreorder() { System.out.print("preorder:"); printPreorder(overallRoot); System.out.println(); }
// post: prints the tree contents using a preorder traversal // post: prints in preorder the tree with given root private void printPreorder(IntTreeNode root) { if (root != null) { System.out.print(" " + root.data); printPreorder(root.left); printPreorder(root.right); } }
// post: prints the tree contents using a inorder traversal public void printInorder() { System.out.print("inorder:"); printInorder(overallRoot); System.out.println(); }
// post: prints in inorder the tree with given root private void printInorder(IntTreeNode root) { if (root != null) { printInorder(root.left); System.out.print(" " + root.data); printInorder(root.right); } }
// post: prints the tree contents using a postorder traversal public void printPostorder() { System.out.print("postorder:"); printPostorder(overallRoot); System.out.println(); }
// post: prints in postorder the tree with given root private void printPostorder(IntTreeNode root) { if (root != null) { printPostorder(root.left); printPostorder(root.right); System.out.print(" " + root.data); } }
// post: prints the tree contents, one per line, following an // inorder traversal and using indentation to indicate // node depth; prints right to left so that it looks // correct when the output is rotated. public void printSideways() { printSideways(overallRoot, 0); }
// post: prints in reversed preorder the tree with given // root, indenting each line to the given level private void printSideways(IntTreeNode root, int level) { if (root != null) { printSideways(root.right, level + 1); for (int i = 0; i
}
********************
IntTreeNode.java
********************
// Class for storing a single node of a binary tree of ints
public class IntTreeNode { public int data; public IntTreeNode left; public IntTreeNode right; // constructs a leaf node with given data public IntTreeNode(int data) { this(data, null, null); } // constructs a branch node with given data, left subtree, // right subtree public IntTreeNode(int data, IntTreeNode left, IntTreeNode right) { this.data = data; this.left = left; this.right = right; } }
Reference Tree #2 Reference Tree #2
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
