Question

Write an efficient java program to implement BST. Your java program should read n words and...

Write an efficient java program to implement BST. Your java program should read n 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 engtofren.java file

To compile: javac engtofren.java

To execute: java engtofren< any data file name

Your main method should be as follow:

public static void main(String args[]) {

engtofren bst = new engtofren (); //

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");}

}//

Read input from System.in. i.e. command prompt.

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

The algorithm to insert data into BST is:

  1. Start from root.
  2. If root is null create a new node and assign it to root. Else compare the inserting element with root, if less than root, then recurse for left, else recurse for right.
  3. After reaching the end, just insert that node at left(if less than current) else right.

The algorithm to search is:

  1. start from root.
  2. first compare the key with root, if the key is present at root or root is null, return root.
  3. If key is greater than root’s key, we recur for right subtree of root node. Otherwise we recur for left subtree.

The java code is as follows:

import java.util.Scanner;

public class engtofren
{
// Root of BST
   private Node root;
  
// Constructor
engtofren() {
root = null;
}
  
// This method mainly calls insertRecord()
void insert(String word, String meaning) {
root = insertRecord(root, word, meaning);
}
  
// A recursive function to insert a new key in BST
Node insertRecord(Node root, String word, String meaning) {
  
//   If the tree is empty, return a new node
if (root == null) {
root = new Node(word, meaning);
return root;
}
  
//   Otherwise, recur down the tree */
if (word.compareToIgnoreCase(root.word) < 0)
root.left = insertRecord(root.left, word, meaning);
else if (word.compareToIgnoreCase(root.word) > 0)
root.right = insertRecord(root.right, word, meaning);
  
//return the (unchanged) node pointer
return root;
}
  
   public Node search(Node root, String key)
   {
//   root is null or key is present at root
   if (root==null || root.word.equalsIgnoreCase(key))
{
       return root;
}
  
//   val is greater than root's key
   if (root.word.compareToIgnoreCase(key)>0)
   return search(root.left, key);
  
//   val is less than root's key
   return search(root.right, key);
   }
  
  
   public static void main(String args[]) {

       engtofren bst = new engtofren (); //

       try{

       Scanner inf = new Scanner (System.in);

// Read input from KB/ File

// read no. of words
       System.out.println("Enter no of words");
        int n = inf.nextInt();
        System.out.println("Enter words");

       for (int i = 1; i <= n; i++){

       // read English word

           String english = inf.next();

       // read corresponding word

       String french = inf.next();

       bst.insert(english, french);

       }

      
       while(inf.hasNext()){

//   read next English word
       String english = inf.next();

       Node searchedNode=bst.search( bst.root, english);
       System.out.println(searchedNode.word + " : " + searchedNode.meaning);
//   search in BST and print its corresponding French if exist.

       }

       inf.close();// close input file

       }catch (Exception e) {System.out.println("\nException " + e + "\n");}

       }
}


class Node
{
String word;
String meaning;
Node left, right;

public Node(String word, String meaning) {

this.word= word;
this.meaning=meaning;
left = right = null;
}
}

Add a comment
Know the answer?
Add Answer to:
Write an efficient java program to implement BST. Your java program should read n words and...
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
  • 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...

  • 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 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...

  • 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...

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

    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...

  • Today you are to write a Java program that will prompt for and read 2 words...

    Today you are to write a Java program that will prompt for and read 2 words of equal length entered by the user, and create a new word which contains the last letter of the 1st word, the last letter of the 2nd word, followed by the second to last character of the 1st word, followed by the second to last character of the 2nd word and so on. Be sure to use the same format and wording as in...

  • 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...

  • I cant get this python program to read all the unique words in a file. It...

    I cant get this python program to read all the unique words in a file. It can only read the number of the unique words in a line. import re import string from collections import Counter # opens user inputted filename ".txt" and (w+) makes new and writes def main(): textname = input("Enter the file to search: ") fh = open(textname, 'r', encoding='utf-8' ) linecount = 0 wordcount = 0 count = {} print("Sumary of the", fh) for line in...

  • Write a program IN PYTHON that checks the spelling of all words in a file. It...

    Write a program IN PYTHON that checks the spelling of all words in a file. It should read each word of a file and check whether it is contained in a word list. A word list available below, called words.txt. The program should print out all words that it cannot find in the word list. Requirements Your program should implement the follow functions: main() The main function should prompt the user for a path to the dictionary file and a...

  • read the code and comments, and fix the program by INSERTING the missing code in Java...

    read the code and comments, and fix the program by INSERTING the missing code in Java THank you please import java.util.Scanner; public class ExtractNames { // Extract (and print to standard output) the first and last names in "Last, First" (read from standard input). public static void main(String[] args) { // Set up a Scanner object for reading input from the user (keyboard). Scanner scan = new Scanner (System.in); // Read a full name from the user as "Last, First"....

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