Trees sure are neat. Lets take this slow and simple and make a tree of integers. Download
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:
importjava.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");
IntBSTreetestTree = new IntBSTree();
System.out.println("Populating Tree with values");
int[] valArr = {4,8,10,2,1,7,3,5,9,6};
for(inti : 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));
}
}
FIRST FILE:
public class IntBSTree{
private class Node
{
privateint data;
private Node leftChild;
private Node rightChild;
public Node(intaData)
{
this.data = aData;
this.leftChild = null;
this.rightChild = null;
}
}
private Node root;
publicIntBSTree()
{
root = null;
}
public void insert(int data)
{
//Code this
}
public void printInorder()
{
//Code this
}
publicintgetDepth(int value)
{
//Code this
}
}