Question

Could you guys write an efficient java program to implement BST. Your java program should read...

Could you guys write an efficient java program to implement BST. Your java program should read words and its corresponding French and store it in a BST. Then read every English word
and print its corresponding French by searching in BST. Assume your java program is in
xxxxx5.java file (5th java project), where xxxxx is the first 5 characters of your last name.
To compile: javac xxxxx5.java
To execute: java xxxxx5 < any data file name
Your main method should be as follow:


public static void main(String args[]) {
xxxxx5 bst = new xxxxx5 (); // xxxxx is the first 5 characters of your last name
try{
Scanner inf = new Scanner (System.in);
// Read input from KB/ File
// read no. of words
n = inf.nextInt();
for (i = 1; i <= n; i++){
// read English word
english = inf.next();
// read corresponding word
french = inf.next();
bst.insert(english, french);
}
while(inf.hasNext()){
// read next English word
english = inf.next();
search in BST and print its corresponding French if exist.
}// end while
inf.close();// close input file
}catch (Exception e) {prt("\nException " + e + "\n");}
}// end main method

Sample data file can be as follow:
5 student etudiant book livre this cette is est a une this is a book

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

Please find below code for your reference. Also I have attached Program and Output screenshots.

Code:-

import java.util.Scanner;

public class TreeNode {
  
   private TreeNode right;
   private TreeNode left;
   private String english;
   private String french;
  
   public TreeNode(String s, String d) {
       english = s;
       french = d;
   }
   public String find (String s) {
       if (s.equals(english)) {
           return french;
       }
       if (s.compareTo(english) < 0) {
           if (right != null)
               return right.find(s);
           return "";
       }
       else {
       if (left != null)
           return left.find(s);
           return "";
       }
   }
   public void insert (String s, String d) {
  
       if (s.equals(english)) {
           french = d;
           return;
       }
       if (s.compareTo(english) < 0) {
      
           if (right != null)
               right.insert(s,d);
           else
               right = new TreeNode(s,d);
           return;
       }
       if (left != null)
           left.insert(s,d);
       else
           left = new TreeNode(s,d);
   }
  
   public static void main(String args[])
   {
   TreeNode tn = new TreeNode("English","French");
   Scanner inf = new Scanner (System.in);
   int n = inf.nextInt();
   for(int i=1;i<=n;i++)
   {
   String english = inf.next();
   String french = inf.next();
   tn.insert(english,french);
   }
   while (inf.hasNext())
   {
   String english = inf.next();
   System.out.println("English->"+english);
   String french = tn.find(english);
   System.out.println("French->"+french);
   }
   inf.close();
  
   }
}

Add a comment
Know the answer?
Add Answer to:
Could you guys write an efficient java program to implement BST. Your java program should read...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a java program to convert and print an infix expression to postfix expression. You can...

    Write a java program to convert and print an infix expression to postfix expression. You can use Java stack methods. (Must read input from System.in) Your main method should be as follow: public static void main(String args[]) { intopost p = new intopost (); String iexp, pexp; //infix postfix expression             try{ Scanner inf = new Scanner (System.in);                     // Read input from KB/ File while(inf.hasNext()){ // read next infix expression                                 iexp = inf.next(); // Assume method name to convert infix...

  • Write a Python program to read lines of text from a file. For each word (i.e,...

    Write a Python program to read lines of text from a file. For each word (i.e, a group of characters separated by one or more whitespace characters), keep track of how many times that word appears in the file. In the end, print out the top twenty counts and the corresponding words for each count. Print each value and the corresponding words, in alphabetical order, on one line. Print this in reverse sorted order by word count. You can assume...

  • Could I get some help with this assignment In this assignment, you'll write a program which...

    Could I get some help with this assignment In this assignment, you'll write a program which reads a text file, and prints a histogram of its word sizes. So, for example, the historgram should show the number of words of length 1, of length 2, etc., up to the number of words of length n, where n is some constant defined in your program. To obtain text files that are suitably long, you could try Project Gutenberg, a site containing...

  • In this assignment you will implement the second version of your spell checker. Using the randomi...

    In this assignment you will implement the second version of your spell checker. Using the randomized dictionary (random_dictionary.txt) given, read in the dictionary into an array of 26 Binary Search Trees (BST) , one for each letter of the alphabet. Thus the first BST would contain only those words starting with letter 'a', while the last would contain only those words starting with letter 'z'. Then, when you read in the book (oliver.txt), you examine the first character of each...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

  • Capitalization JAVA In this program, you will read a file line-by-line. For each line of data...

    Capitalization JAVA In this program, you will read a file line-by-line. For each line of data (a string), you will process the words (or tokens) of that line one at a time. Your program will capitalize each word and print them to the screen separated by a single space. You will then print a single linefeed (i.e., newline character) after processing each line – thus your program will maintain the same line breaks as the input file. Your program should...

  • This program is in python and thanks fro whoever help me. In this program, you will...

    This program is in python and thanks fro whoever help me. In this program, you will build an English to Hmong translator program. Hmong is a language widely spoken by most Southeast Asian living in the twin cities. The program lets the user type in a sentence in English and then translate it to a Hmong sentence. The program does not care about grammar or punctuation marks. That means your program should remove punctuation marks from the English words before...

  • Write a French/English dictionary lookup program. Read a list of pairs of English and French words...

    Write a French/English dictionary lookup program. Read a list of pairs of English and French words from a file specified by the user. English/French words should be exact matches (don't try to find partial matches). Use the supplied EnglishFrenchDictionary.java class as your main class. Fill in the missing code in the DictionaryTable.java class to read the input file and perform the searches. Add code to the DictionaryTable read() method to: read pairs of lines (English word is on the first...

  • Goal: design and implement a dictionary. implement your dictionary using AVL tree . Problem​: Each entry...

    Goal: design and implement a dictionary. implement your dictionary using AVL tree . Problem​: Each entry in the dictionary is a pair: (word, meaning). Word is a one-word string, meaning can be a string of one or more words (it’s your choice of implementation, you can restrict the meaning to one-word strings). The dictionary is case-insensitive. It means “Book”, “BOOK”, “book” are all the same . Your dictionary application must provide its operations through the following menu (make sure that...

  • 4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java...

    4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43). Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT