import java.io.*; import java.util.*; /* Copyright (c) 1999 Nathan Meyers $Id: Dictionary.java,v 1.3 1999/11/10 17:48:17 nathanm Exp $ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ public class Dictionary { public Node root = null; // Constructor: Build a tree by parsing words from a reader public Dictionary(Reader r) throws IOException { // The reader classes don't know how to extract words from // input, so we'll build our own word extractor BufferedReader reader = new BufferedReader(r); String currentLine; // Read a line while ((currentLine = reader.readLine()) != null) { // Build a string tokenizer StringTokenizer tokenizer = new StringTokenizer(currentLine); while (tokenizer.hasMoreTokens()) { String word = tokenizer.nextToken(); Node newnode; if (root == null) newnode = root = new Node(word); else { // Build a key Node key = new Node(word); // Start at root newnode = root; // Continue until we find a matching node int compare; while ((compare = key.compareTo(newnode)) != 0) { if (compare < 0) { if (newnode.left == null) newnode.left = new Node(word); newnode = newnode.left; } else { if (newnode.right == null) newnode.right = new Node(word); newnode = newnode.right; } } } newnode.inputCount++; } } } // Traverser public void traverse(TraverseFunc tf) { root.traverse(tf); } // Look for word and increment count public void countWord(String word) throws NoSuchEntryException { root.countWord(new Node(word)); } }