Question: Help in JAVA: 1. You have been given the shell of a program. This program builds a small Binary Search Tree of integers. Your mission

Help in JAVA:

1. You have been given the shell of a program. This program builds a small Binary Search Tree of integers. Your mission is to write the method called Accesses that will return the number of accesses to find an element in the tree. The main program calls this method for each element, adding the accesses each time in order to count the total number of accesses. The program then outputs the total number of accesses and the average number.

2. Repeat the above for strings in an input file. The input file is called words2.txt

FOR THIS LAB it is OK if you have to write two TreeInsert methods (one for Integer, one for Strings), and two Accesses methods but if you put template methods in here that is good too. ALSO the integers are in an array, but the Strings are in a file. So after you have built a tree of Strings you either have to close the input file, then reopen it and pick up each word again and calculate the number of accesses to find it, or you have to look for another solution. (Possibilities are to read them into an array at the beginning before building your tree or count accesses in the same loop where you read in your string. Either is fine (for now)).

BuildTreeWithMethod.java:

public class BuildTreeWithMethod {

public static void main(String[] args) { Integer[] arr = {6, 8, 3, 4, 9, 2 }; //data to put in BST BinaryTreeNode root=new BinaryTreeNode(arr[0]);//the root int totalAccesses=1; //# accesses to insert the root //build the tree for (int i = 1; i < arr.length; i++) { TreeInsert(root, arr[i]); } //print out the data - should be sorted inOrder(root); //count access to retrieve all data for (int i = 1; i < arr.length; i++) { //totalAccesses=totalAccesses+Accesses(root, arr[i]); } System.out.println("Number of accesses to find all data in tree is " + totalAccesses); System.out.println("Average number of accesses is " + totalAccesses*1.0/arr.length); } //method to perform- an inorder traversal of a BST public static void inOrder(BinaryTreeNode t) { if (t != null) { inOrder(t.getLeft()); System.out.println(t.getValue()); inOrder(t.getRight());

} } //method to insert an integer (num) into a non-null BST tree public static void TreeInsert(BinaryTreeNode curr, Integer num) { BinaryTreeNode b=new BinaryTreeNode(num); while (curr != null) { int currValue= (int)curr.getValue(); if(num

} else { if (curr.getRight() == null) { curr.setRight(b); break; } else { curr = curr.getRight(); } } } } }

BuildTreeNode.java:

public class BinaryTreeNode { private T element; private BinaryTreeNode left, right;

/** * Creates a new tree node with the specified data. * * @param obj the element that will become a part of the new tree node */ BinaryTreeNode (T obj) { element = obj; left = null; right = null; }

public T getValue(){ return element; }

public T getElement() { return element; }

public void setElement(T element) { this.element = element; }

public BinaryTreeNode getLeft() { return left; }

public void setLeft(BinaryTreeNode left) { this.left = left; }

public BinaryTreeNode getRight() { return right; }

public void setRight(BinaryTreeNode right) { this.right = right; }

}

words2.txt:

Hi

Bye

Monday

April

Donkey

Yellow

Computers

Monkey

Dog

Cat

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!