Question
Java - Do #5. You have been provided two files: input.dat = includes UPC key and the corresponding description. The UPC keys are randomly chosen
Java -
Do #5.
You have been provided two files: input.dat = includes UPC key and the corresponding description. The UPC keys are randomly chosen from the main file(UPC.csv). You will use this file to select the search key. upc.csv = It is the main file that consists of UPC keys and descriptions.
Implement your method to create a search tree from upc.csv file. Then test your search implementation by entering a few keys manually and print the description. Finally, run your code to search all of the keys from the input.dat file in the binary search tree and determine the total time taken to complete the search.
Screenshot of file input.dat and UPC.csv
1-4 has been done:
Here's the code :
//Java code
class binary{
int data;
binary left,right;
}
public class bst {
static binary createnode(int data) {
binary newnode=new binary();
newnode.data=data;
newnode.left=null;
newnode.right=null;
return newnode;
}
static binary insertnode(binary root,int data) {
if(root==null)return createnode(data);
if(root.data>data)
root.left=insertnode(root.left,data);
if(root.data root.right=insertnode(root.right,data); return root; } static void inorder(binary root) { if(root==null)return; inorder(root.left); System.out.print(root.data+" "); inorder(root.right); } static binary deletenode(binary root,int key) { if(root==null)return null; else if(root.data>key) root.left=deletenode(root.left,key); else if(root.data root.right=deletenode(root.right,key); else { if(root.left==null) return root.right; if(root.right==null) return root.left; binary min=findmin(root.right); root.data=min.data; deletenode(root.right,min.data); } return root; } static public binary search(binary root, int key) { // Base Cases: root is null or key is present at root if (root==null || root.data==key) return root; // val is greater than root's key if (root.data > key) return search(root.left, key); // val is less than root's key return search(root.right, key); } static binary findmin(binary root) { while(root.left!=null) root=root.left; return root; } public static void main(String[]str) { binary root=null; root = insertnode(root, 50); root = insertnode(root, 30); root = insertnode(root, 20); root = insertnode(root, 40); root = insertnode(root, 70); root = insertnode(root, 60); root = insertnode(root, 80); System.out.println("inorder traversal of tree"); inorder(root); System.out.print(" Delete 20 "); root = deletenode(root, 20); System.out.println("inorder traversal of tree"); inorder(root); System.out.print(" Delete 30 "); root = deletenode(root, 30); System.out.println("inorder traversal of tree"); inorder(root); System.out.print(" Delete 50 "); root = deletenode(root, 50); System.out.println("inorder traversal of tree"); inorder(root); } } ------------------------------------------
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