Implement Search (T data) to search for a node having value of key = data in a B
ID: 3594867 • Letter: I
Question
Implement Search (T data) to search for a node having value of key = data in a Binary Search Tree. Use the Binary Search Tree to build a search tree using the given input file that consists of three fields: a UPC key and the corresponding description. Use the search tree created to find the description associated with a given set of UPC keys. The input file (input1.txt) the key and corresponding descriptions in a comma separated file. test the program for the given search keys and determine the total time taken to complete the search. Wanted code in java.
Clues:
You have to create nodes only for the key values, not the other fields,(Test file looks like this)
72, ,outdoor bag 95, ,café 123, ,employee coupon
Theses are the 3 key values: 72, 95, 123, these 3 values need to store in BST.
Similarly with the input file, you read the first line and only search for the key value. 72 not the other fields after the key.
**** clue2:
The data given in the comma separated file is in the following format:
[Product ID],[Product Qty./Weight],[Product Description]
You need to parse the data from the file and store as objects in BST form.
The search method should take input the Product ID as the search key and should return Product Description if found in the BST.
Explanation / Answer
import java.io.IOException;
import java.util.Timer.*;
import java.io.BufferedReader;
import java.io.FileReader;
public class Search {
public static int SearchBinary(int[] Arr, int k) {
int begin = 0;
int end = Arr.length - 1;
while (begin <= end) {
int middle = (begin + end) / 2;
if (k == Arr[middle]) {
return middle;
}
if (k < Arr[middle]) {
end = middle - 1;
} else {
begin = middle + 1;
}
}
return -1;
}
public static void main(String[] args) {
String file = "UPC.csv";
String file2 = "input.dat";
String line = "";
String Splitsym= ",";
int count=0;
try (BufferedReader br = new BufferedReader(new FileReader(file)))
{
while ((line = br.readLine()) != null) {
count=count+1;
String[] input = line.split(Splitsym);
}
} catch (IOException e)
{ e.printStackTrace(); }
int Arr[] = new int[count];
try (BufferedReader br = new BufferedReader(new FileReader(file)))
{
for(int i=0;i<3;i++)
{
line = br.readLine();
String[] input = line.split(Splitsym);
Arr[i]=Integer.parseInt(input[0]);
}
} catch (IOException e)
{
e.printStackTrace();
}
try (BufferedReader br = new BufferedReader(new FileReader(file2)))
{
while ((line = br.readLine()) != null)
{
int input = Integer.parseInt(line);
long beginTime = System.nanoTime();
System.out.println("k "+input+" Fount at :"+ SearchBinary(Arr,input));
long endTime = System.nanoTime();
long totalTime = endTime - beginTime;
System.out.println("Time take for search :" + totalTime + " Neno Seconds");
}
} catch (IOException e)
{ e.printStackTrace(); }
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.