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

Trees sure are neat. Let’s take this slow and simple and make a tree of integers

ID: 3673924 • Letter: T

Question

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.

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

DRIVER FILE:

FIRST FILE:

Explanation / Answer

import java.util.*;

public class IntBSTreeTester {

public static void main(String[] args) {
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=0; i<10;i++)
{
testTree.insert(valArr[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));
  
  
}

}

import java.util.*;

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)
{
Node newnode=new Node(data);
if(root==null){
           root = newnode;
return;
       }
  
       Node current = root;
       Node parent = null;
       while(true){
           parent = current;
if(data<=current.data){              
               current = current.leftChild;
               if(current==null){
                   parent.leftChild = newnode;

                   return;
               }
  
           }
else{
               current = current.rightChild;
               if(current==null){
                   parent.rightChild = newnode;
                   return;
               }
}
       }
  
}
public void printInorder()
{
Node current=root;
printinorder(current);
}
public void printinorder(Node r)
{
if (r != null)
{
printinorder(r.leftChild);
System.out.print(r.data +" ");
printinorder(r.rightChild);
}
}
  

public void getDepth(int value)
{
Node current=root;
int result=0;
while ((current != null))   
{
int currentvalue = current.data;
if(currentvalue==value)
{
System.out.println(result);

}
else if (value < currentvalue)
{
current = current.leftChild;
result++;
}
else if (value > currentvalue)
{
current = current.rightChild;
result++;
}
getDepth(int value);
}

  
  
  

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote