Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA JAVA JAVA PLEASE SHOW COMMENTS AND OUTPUT Trees sure are neat. Let’s take t

ID: 3673908 • Letter: J

Question

JAVA JAVA JAVA

PLEASE SHOW COMMENTS AND OUTPUT

Trees sure are neat. Let’s take this slow and simple and make a tree of integers. Download this file, the driver file, and fill in the code to create a tree of integers.

1st FILE

public class IntBSTree{
private class Node
{
  private int data;
  private Node leftChild;
  private Node rightChild;
  public Node(int aData)
  {
   this.data = aData;
   this.leftChild = null;
   this.rightChild = null;
  }
}
private Node root;

public IntBSTree()
{
  root = null;
}

public void insert(int data)
{
  //Code this
}

public void printInorder()
{
  //Code this
}

public int getDepth(int value)
{
  //Code this
}

}

Driver File

import java.util.*;
public class IntBSTreeTester {

public static void main(String[] args) {
  // TODO Auto-generated method stub
  System.out.println("Int BST Tester!");
  System.out.println("Creating tree");
  IntBSTree testTree = new IntBSTree();
  System.out.println("Populating Tree with values");
  int[] valArr = {4,8,10,2,1,7,3,5,9,6};
  for(int i : valArr)
  {
   testTree.insert(i);
  }
  System.out.println("Testing insertion by in-order traversal");
  testTree.printInorder();
  System.out.println("Getting depth of 6");
  System.out.println(testTree.getDepth(6));
  System.out.println("Getting depth of 14");
  System.out.println(testTree.getDepth(14));
  
}

}

The class IntBSTree is given fill in the following methods

insert: This method returns nothing and takes in an integer value that is then placed as a new node in the tree based on the binary tree properties. A reminder values greater than the parent go to the right subtree and values smaller go to the left subtree. Also it may be a good idea to use a recursive method in order to place these values.

printInorder: This method which returns nothing and has no parameters prints the in order traversal of the tree. For in order traversal each left subtree must be visited, then the value is printed out, and finally each of the right subtrees is visited. It is a good idea to make a recursive method to assist with this. Also if done correctly in-order traversals print out each of the integers in ascending order.

getDepth: The depth of a node is the number of edges from the root to that number. This method returns nothing and takes in a parameter corresponding to the integer value of a node whose depth is returned. If the value is not in the tree a -1 should be returned. Again a recursive helper method may be useful to solve this.

Correct Print Out:

Int BST Tester!

Creating tree

Populating Tree with values

Testing insertion by in-order traversal

1

2

3

4

5

6

7

8

9

10

Getting depth of 6

4

Getting depth of 14

-1

IF you can answer these GREAT!   But not important

Lab Report Questions

1. Demonstrate one-by-one how the insertion process work for the following number 54, 90, 72, 59, 87, 15, 16, 83, 25, and 27.

2. Though it wasn’t asked to be programmed in this lab, now demonstrate how to remove the root two times.

Explanation / Answer

IntBSTree.java

IntBSTreeTester.java

import java.util.*;
public class IntBSTreeTester {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Int BST Tester!");
System.out.println("Creating tree");
IntBSTree testTree = new IntBSTree();
System.out.println("Populating Tree with values");
int[] valArr = {4,8,10,2,1,7,3,5,9,6};
for(int i : valArr) {
testTree.insert(i);
}
System.out.println("Testing insertion by in-order traversal");
testTree.printInorder();
System.out.println("Getting depth of 6");
System.out.println(testTree.getDepth(6));
System.out.println("Getting depth of 14");
System.out.println(testTree.getDepth(14));
}
}

1. This link will help you create step by step BST, this is your answer https://www.cs.usfca.edu/~galles/visualization/BST.html

2.

public void removeRoot() {
if (this.root == null) return;
Node rightChild = this.root.getRightChild();
if (rightChild != null) {
Node parent = null;
while (rightChild.getLeftChild() != null) {
parent = rightChild;
rightChild = rightChild.getLeftChild();
}
if (parent == null) {
rightChild.setLeftChild(this.root.getLeftChild());
this.root = rightChild;
} else {
parent.setLeftChild(null);
rightChild.setLeftChild(this.root.getLeftChild());
rightChild.setRightChild(this.root.getRightChild());
this.root = rightChild;
}
} else {
this.root = this.root.getLeftChild();
}
}