Question
QUESTION:
Each unit in a Latin textbook contains a Latin-English vocabulary of words that have been used for the first time in a particular time. Write a program that converts a set of such vocabularies stored in file Latin in to a set of English-Latin vocabularies. Make the following assumptions:
a. Unit names are preceded by a percentage symbol.
b. There is only one entry per line.
c. A Latin word is separated by a colon from its English equivalent(s); if there is more than one equivalent, they are separated by a comma.
To output English words in alphabetical order, create a binary search tree for each unit containing English words and linked lists of Latin equivalents. Make sure that there is only one node for each English word in the tree. For example, there is only one node for and, although and is used twice in unit 6: with words ac and atque. After the task has been completed for a given unit (that is, the content of the tree has been stored in an output file), delete the tree along with all linked lists from computer memory before creating a tree for next unit.
Here is an example of a file containing Latin-English vocabularies:
%Unit 5
ante : before, in front of, previously
antiques : ancient
ardeo : burn, be on fire, desire
arma : arms, weapons
aurum : gold
aureus : golden, of gold
%Unit 6
animal : animal
Athenae : Athens
atque : and
ac : and
aurora : dawn
%Unit 7
amo : love
amor : love
annus : year
Asia : Asia
From these units, the program should generate the following output:
%Unit5
ancient : antiques
arms : arma
be on fire : ardeo
before : ante
burn : ardeo
desire : ardeo
gols : aurum
golden : aureus
in front of : ante
of gold : aureus
previously : ante
weapons : arma
%Unit 6
Athens : Athenae
and : ac, atque
animal : animal
dawn : aurora
%Unit 7
Asia : Asia
love : amor, amo
year : annus
Explanation / Answer
import java.io.*; import java.util.*; public class LatinSort { static class TreeNode { // An object of type TreeNode represents one node // in a binary tree of strings. String item; // The data in this node. TreeNode left; // Pointer to left subtree. TreeNode right; // Pointer to right subtree. TreeNode lat; // Pointer to Latin words. TreeNode(String str) { // Constructor. Make a node containing the specified string. item = str; } } // end nested class TreeNode static TreeNode root; // Pointer to the root node in a binary tree. This // tree is used in this program as a binary sort tree. // The tree is not allowed to contain duplicate // items. When the tree is empty, root is null. public static void main(String[] args) throws IOException{ String filename = "Latin.txt", latin, english; BufferedReader fin = new BufferedReader(new FileReader(filename)); BufferedWriter out = new BufferedWriter(new FileWriter("Output.txt")); BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); System.out.println("This program will read from a file called 'Latin.txt' containing a"); System.out.println("Latin-English word list and write to a file named 'output.txt' with the"); System.out.println("computed data in alphabetical order. "); System.out.println("To change the results of this program, open 'Latin.txt' and add what you want."); System.out.println("Directions: "); System.out.println("1) Unit names are preceded by a percentage symbol."); System.out.println("2) There is only one entry per line."); System.out.println("3) A Latin word is seperated by a colon from its English quivalent(s);"); System.out.println(" if there is more than one equivalent, they are seperated by a comma."); String item = fin.readLine().trim().toLowerCase(); while (item != null) { item = item.toLowerCase(); int ind = item.indexOf("%"); if(ind != -1){ //new TreeNode; System.out.println(item +" not part of tree"); } if(item.length()!=0){ StringTokenizer stok = new StringTokenizer(item,":,"); latin = stok.nextToken(); while(stok.hasMoreTokens()){ english = stok.nextToken(); //System.out.println("? "); //String item; // The user's input. if (english.length() == 0){ break; } english.trim(); System.out.println("ENGLISH IS" + english); if (treeContains(root,english)) { english = english + ", " + latin; System.out.println(" That item is already in the tree."); } else { english = english + " : " + latin; treeInsert(english.trim()); // Add user's string to the tree. //treeInsert(latin.trim()); System.out.println(" The tree contains " + countNodes(root) + " items."); System.out.println(" Contents of tree: "); treeList(root); } } } item = fin.readLine(); } // end while System.out.println(" Exiting program."); fin.close(); out.close(); } // end main() static void treeInsert(String newItem) { // Add the item to the binary sort tree to which the global // variable "root" refers. (Note that root can't be passed as // a parameter to this routine because the value of root might // change, and a change in the value of a formal parameter does // not change the actual parameter.) //if(newItem.indexOf(",")==-1){ //System.out.println("Latin word!"); //} if ( root == null ) { // The tree is empty. Set root to point to a new node containing // the new item. This becomes the only node in the tree. root = new TreeNode( newItem ); return; } TreeNode runner; // Runs down the tree to find a place for newItem. runner = root; // Start at the root. while (true) { if ( newItem.compareTo(runner.item) < 0 ) { // Since the new item is less than the item in runner, // it belongs in the left subtree of runner. If there // is an open space at runner.left, add a new node there. // Otherwise, advance runner down one level to the left. if ( runner.left == null ) { runner.left = new TreeNode( newItem ); runner.lat = new TreeNode( newItem ); // runner.left = new TreeNode( latin ); return; // New item has been added to the tree. } else runner = runner.left; } else { // Since the new item is greater than or equal to the item in // runner it belongs in the right subtree of runner. If there // is an open space at runner.right, add a new node there. // Otherwise, advance runner down one level to the right. if ( runner.right == null ) { runner.right = new TreeNode( newItem ); return; // New item has been added to the tree. } else runner = runner.right; } } // end while } // end treeInsert() static boolean treeContains( TreeNode root, String english) { // Return true if item is one of the items in the binary // sort tree to which root points. Return false if not. if ( root == null ) { // Tree is empty, so it certainly doesn't contain item. return false; } else if ( english.equals(root.item)) { // Yes, the item has been found in the root node. return true; } else if ( english.compareTo(root.item) < 0 ) { // If the item occurs, it must be in the left subtree. return treeContains( root.left, english); } else { // If the item occurs, it must be in the right subtree. return treeContains( root.right, english); } } // end treeContains() static void treeList(TreeNode node) throws IOException{ // Print the items in the tree in postorder, one item // to a line. Since the tree is a sort tree, the output // will be in increasing order. String outputfile = "Outagain.txt"; BufferedWriter out = new BufferedWriter(new FileWriter("outagain.txt")); if ( node != null ) { treeList(node.left); // Print items in left subtree. System.out.println(" " + node.item); // Print item in the node. treeList(node.right); // Print items in the right subtree. } } // end treeList() static int countNodes(TreeNode node) { // Count the nodes in the binary tree to which node // points. Return the answer. if ( node == null ) { // Tree is empty, so it contains no nodes. return 0; } else { // Add up the root node and the nodes in its two subtrees. int leftCount = countNodes( node.left ); int rightCount = countNodes( node.right ); return 1 + leftCount + rightCount; } } // end countNodes()