In: Computer Science
You are given a reference to the root node of a binary search tree, that implements a dictionary data structure. Please print all the elements in depths 500 through 510, all in sorted order. A node in a binary search tree is at depth x, if it takes x hops to get from the root. So the root is at depth 0, the children of the root are at depth 1, and so on. The class TreeNode defines a single node in the binary search tree, and the file MyTree.class is a pre-compiled Java code that defines a binary search tree. The file DepthPrint.java creates an instance of MyTree, and gets the root node of the tree. Please write code that will print elements in depths 500 through 510 in sorted order. Please feel free to add new methods, instance fields and classes that will help your implementation. Your output should match the file depthprint.out.
DepthPrint.java:
public class DepthPrint {
public static void main(String [] args) {
// Create an instance of MyTree.
MyTree T = new MyTree();
// Get the root node of my tree.
TreeNode root = T.getRoot();
// TODO: Code for printing the tree
from depth 500 through 510 in sorted order.
// Feel free to define recursive
methods to traverse the tree and print.
// Please print each key separated
by spaces.
// Printing a new line in the
end.
System.out.println();
}
}
TreeNode.java:
public class TreeNode
{
public int key;
public TreeNode left;
public TreeNode right;
public TreeNode(int _key)
{
key = _key;
left = null;
right = null;
}
}
MyTree.java:
public class MyTree
{
private TreeNode root;
private TreeNode insert(final TreeNode treeNode, final int
n)
{
if (treeNode == null)
{
return new TreeNode(n);
}
if (n <= treeNode.key)
{
treeNode.left = this.insert(treeNode.left, n);
}
else
{
treeNode.right = this.insert(treeNode.right, n);
}
return treeNode;
}
public MyTree()
{
this.root = null;
System.out.println("Loading my Tree.");
for (int i = 0; i < 10000; ++i)
{
if ((i & 0x1) == 0x1)
{
this.root = this.insert(this.root, -i);
}
else
{
this.root = this.insert(this.root, i);
}
}
System.out.println("My Tree is loaded.");
}
public TreeNode getRoot()
{
return this.root;
}
}
Hi PFA code for DepthPrint.java. I have added the function printdepth500to510
public class DepthPrint {
public static void main(String [] args) {
// Create an instance of MyTree.
MyTree T = new MyTree();
// Get the root node of my tree.
int temp=10;
TreeNode root = T.getRoot();
// TODO: Code for printing the tree from depth 500 through 510 in sorted order.
// Feel free to define recursive methods to traverse the tree and print.
// Please print each key separated by spaces.
DepthPrint depth_print = new DepthPrint();
depth_print.printdepth500to510(root,0);
// Printing a new line in the end.
System.out.println();
}
void printdepth500to510(TreeNode node,int depth){
if(node == null)
return;
int curr_depth = depth + 1;
if(curr_depth>510)
return;
int val;
printdepth500to510(node.left,curr_depth);
if(curr_depth>500)
System.out.print(String.valueOf(node.key)+" ");
printdepth500to510(node.right,curr_depth);
}
}