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
} } //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 /** * 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 public void setLeft(BinaryTreeNode public BinaryTreeNode public void setRight(BinaryTreeNode } words2.txt: Hi Bye Monday April Donkey Yellow Computers Monkey Dog Cat
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started