-
Count number of words This method returns the number of words in the dictionary. The suggested signature is
public int countWords()
-
Print all words This method prints all the words in the dictionary. The suggested signature is
public void printAllWords()
To implement the dictionary, we can use a hash table or a tree data structure. If we choose to use a hash table, we can use chaining to handle collisions. Each bucket in the hash table will store a linked list of words.
Here is an implementation of the Dictionary class using a hash table:
public class Dictionary {
private LinkedList[] buckets;
private int size;
public Dictionary() {
this.buckets = new LinkedList[26];
this.size = 0;
}
public Dictionary(String s) {
this();
addWord(s);
}
public Dictionary(File f) throws IOException {
this();
BufferedReader reader = new BufferedReader(new FileReader(f));
String line;
while ((line = reader.readLine()) != null) {
addWord(line);
}
reader.close();
}
public void addWord(String s) throws WordAlreadyExistsException {
if (findWord(s)) {
throw new WordAlreadyExistsException("Word already exists in dictionary.");
}
int index = hash(s);
if (buckets[index] == null) {
buckets[index] = new LinkedList<String>();
}
buckets[index].add(s);
size++;
}
public boolean findWord(String s) {
int index = hash(s);
if (buckets[index] == null) {
return false;
}
return buckets[index].contains(s);
}
public void deleteWord(String s) throws WordNotFoundException {
if (!findWord(s)) {
throw new WordNotFoundException("Word not found in dictionary.");
}
int index = hash(s);
buckets[index].remove(s);
size--;
}
public String[] findSimilar(String s) {
List<String> similarWords = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
for (char c = 'a'; c <= 'z'; c++) {
String newWord = s.substring(0, i) + c + s.substring(i+1);
if (findWord(newWord)) {
similarWords.add(newWord);
}
}
}
return similarWords.toArray(new String[similarWords.size()]);
}
public int countWords() {
return size;
}
public void printAllWords() {
for (LinkedList<String> list : buckets) {
if (list != null) {
for (String word : list) {
System.out.println(word);
}
}
}
}
private int hash(String s) {
return s.charAt(0) - 'a';
}
}
Note that the hash function simply takes the first character of the word and maps it to an index in the hash table. This is a simple hash function that works well for small dictionaries. In real-world scenarios, we would need a more sophisticated hash function that can handle collisions effectively.
The addWord and deleteWord methods throw exceptions when the word already exists or cannot be found in the dictionary, respectively. This is to handle errors that may occur when using the dictionary.
The findSimilar method implements a simple algorithm that generates all words that differ by exactly one letter from the given word s. It then checks if each of these words exists in the dictionary and returns a list of all similar words found.
The printAllWords method simply prints all the words in the dictionary by iterating over each bucket in the hash table and printing all the words in each linked list (if it exists).
In summary, a dictionary is a useful data structure for storing and searching a list of words. It can be implemented using a hash table or a tree, and supports operations such as adding, searching, deleting, and finding similar words.