-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionary.java
More file actions
101 lines (76 loc) · 2.89 KB
/
Dictionary.java
File metadata and controls
101 lines (76 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* Searches for words in the dictionary through recursive implementation of binary search
* loads the dictionary that user enters in the command line and into binary search tree
* checks whether a word exists in the dictionary
* @author Adisa Narula
* @version 1.0
*
*/
public class Dictionary {
private AVLString dictionaryWordTree;
/**
* Constructor creates a new string array and stores the list of
* words from the dictionary file into AVL tree structure
* @param dictionary string array of words in the dictionary file
*/
public Dictionary(String[] dictionary) {
dictionaryWordTree = new AVLString();
// add all the words to binary search tree
// in balance tree
// for(int i = 0; i <dictionary.length; i++){
//
// dictionaryWordTree.add(dictionary[i]);
// }
insertBalanceWords(dictionary, 0, dictionary.length-1);
//System.out.println(dictionaryWordTree.toString());
}
/**
* Inserts the words in the dictionary into tree structure and makes sure that
* the tree is not stored as a linked list because the words in the dictionary
* are passed in in alphabetical order.
* @param dictionary array list containing words found the in dictionary
* @param first position of the first word in the dictionary
* @param last position of the last word in the dictionary
*/
private void insertBalanceWords(String[] dictionary, int first, int last) {
// set middle element to one
// method is similar to binary search method
int mid = 0;
// one element to add
if (first == last) {
dictionaryWordTree.add(dictionary[first]);
}
// two elements
else if (first + 1 == last) {
dictionaryWordTree.add(dictionary[first]);
dictionaryWordTree.add(dictionary[last]);
}
else {
// keep updating mid and recursively add data to the tree
mid = (first + last) / 2;
dictionaryWordTree.add(dictionary[mid]);
insertBalanceWords(dictionary, first, mid - 1);
insertBalanceWords(dictionary, mid + 1 , last);
}
}
/**
* Searches the AVL string tree structure that contains the dictionary
* to see whether the tree contains specific prefix that is passed in to the method.
* @param wordToSearch prefix that is searched for in the dictionary.
* @return true if dictionary contains the prefix.
*/
public boolean prefixDictionary(String wordToSearch) {
// true if word is in the dictionary
return(dictionaryWordTree.containsPrefix(wordToSearch));
}
/**
* Searches the AVL string tree structure that contains the dictionary
* to see whether the tree contains specific word that is passed in to the method.
* @param wordToSearch word that is searched for in the dictionary.
* @return true if dictionary contains the word.
*/
public boolean checkEquals(String wordToSearch) {
// true if word is in the dictionary
return (dictionaryWordTree.contains(wordToSearch));
}
}