Question

You are given a reference to the root node of a binary search tree, that implements...

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;
}
}

Homework Answers

Answer #1

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU

class TreeNode {
  public int key;
  public TreeNode left;
  public TreeNode right;

  public TreeNode(int _key) {
    key = _key;
    left = null;
    right = null;
  }
}

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 < 100; ++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;
  }
}

class DepthPrint {

  // we are using inorder traversal technique as we have to print in sorted order , and printing only those values whose depth is inside the desired range.
  public static void traverse_tree(
    TreeNode node,
    int depth,
    int min_depth,
    int max_depth
  ) {
    if (node == null) {
      return;
    }
    traverse_tree(node.left, depth + 1, min_depth, max_depth);
    if (depth >= min_depth && depth <= max_depth) {
      System.out.print(node.key + " ");
    }
    traverse_tree(node.right, depth + 1, min_depth, max_depth);
  }

  public static void main(String[] args) {
    MyTree T = new MyTree();
    TreeNode root = T.getRoot();
    int min_depth = 0;
    int max_depth = 10;
    System.out.print(
      "Printing Tree structure in sorted manner from depth " +
      min_depth +
      " to " +
      max_depth +
      " : "
    );
    traverse_tree(root, 0, min_depth, max_depth);
    System.out.println();
  }
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In this lab, you will write a program that creates a binary search tree based on...
In this lab, you will write a program that creates a binary search tree based on user input. Then, the user will indicate what order to print the values in. **Please write in C code** Start with the bst.h and bst.c base code provided to you. You will need to modify the source and header file to complete this lab. bst.h: #ifndef BST_H #define BST_H typedef struct BSTNode { int value; struct BSTNode* left; struct BSTNode* right; } BSTNode; BSTNode*...
Here is a picture of a Binary Search Tree. First, construct the Binary Search Tree using...
Here is a picture of a Binary Search Tree. First, construct the Binary Search Tree using the following BinaryNode as we discussed in class. public class BinaryNode { private int value; private BinaryNode leftChild; private BinaryNode rightChild; public BinaryNode(int value) { this.value = value; leftChild = null; rightChild = null; } public BinaryNode(int value, BinaryNode leftChild, BinaryNode rightChild) { this.value = value; this.leftChild = leftChild; this.rightChild = rightChild; } public int getValue() { return value; } public void setValue(int value)...
1. Assume the key of the right child below the root node of a binary search...
1. Assume the key of the right child below the root node of a binary search tree is 40. The value in the root node could be ________. 2. On average, searching for an item in a balanced binary search tree is _________ operation. 3. Where is the inorder successor of a node A in a binary search tree?
(TCO 6) In the following binary tree, the root node is _____. 24 32 17 37...
(TCO 6) In the following binary tree, the root node is _____. 24 32 17 37 (TCO 6) In the following binary tree, the height of the node with value 39 is _____. Group of answer choices 1 2 3 4 (TCO 6) In a binary search tree, the key in the _____ node is larger than the key in the root node. Group of answer choices right left root header
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
do a code trace on how a key gets deleted package interview; import java.util.*; public class...
do a code trace on how a key gets deleted package interview; import java.util.*; public class binarySearchTree { Node root; void insert(int key) { root = insertRec(root,key); } void delete(int key) { root = deleteRec(root,key); } Node insertRec(Node root, int key) { if(root == null) { root = new Node(key); return root; } if(key < root.key) { root.leftChild = insertRec(root.leftChild,key); } else if(key > root.key){ root.rightChild = insertRec(root.rightChild,key); } return root; } Node deleteRec(Node root, int key) { if(root ==...
How can I prove that any node of a binary search tree of n nodes can...
How can I prove that any node of a binary search tree of n nodes can be made the root in at most n − 1 rotations?
You will be traversing through an integer tree to print the data. Given main(), write the...
You will be traversing through an integer tree to print the data. Given main(), write the methods in the 'IntegerBinaryTree' class specified by the // TODO: sections. There are 6 methods in all to write. Ex: If the input is 70 86 60 90 49 62 81 85 38 -1 the output should be: Enter whole numbers to insert into the tree, -1 to stop Inorder: 38 - 49 - 60 - 62 - 70 - 81 - 85 -...
Java Program: You will be traversing through an integer tree to print the data. Given main(),...
Java Program: You will be traversing through an integer tree to print the data. Given main(), write the methods in the 'IntegerBinaryTree' class specified by the // TODO: sections. There are 6 methods in all to write. Ex: If the input is: 70 86 60 90 49 62 81 85 38 -1 the output should be: Enter whole numbers to insert into the tree, -1 to stop Inorder: 38 - 49 - 60 - 62 - 70 - 81 -...
Here is a modification of the BST program that includes a recursive find method: BinarySearchTree2C.java (posted...
Here is a modification of the BST program that includes a recursive find method: BinarySearchTree2C.java (posted below) Implement the following methods using recursion: int depth() // returns the length of the deepest path from root to any leaf int node_count() // returns the number of nodes in the tree void insert(int n) // inserts value n into the tree BinarySearchTree2C clone() // returns a clone (deep copy) of the tree Add code to the main method to test these methods....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT