-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTreeFrequencyTable.java
More file actions
276 lines (216 loc) · 7.77 KB
/
TreeFrequencyTable.java
File metadata and controls
276 lines (216 loc) · 7.77 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import java.util.NoSuchElementException;
/** Implements the interface <code>FrequencyTable</code> using a
* binary search tree.
*
* @author Marcel Turcotte (turcott@eecs.uottawa.ca)
*/
public class TreeFrequencyTable implements FrequencyTable {
// Stores the elements of this binary search tree (frequency
// table)
private static class Elem {
private String key;
private long count;
private Elem left;
private Elem right;
private Elem(String key) {
this.key = key;
this.count = 0;
left = null;
right = null;
}
}
private Elem root = null; // A reference to the root element
private int size = 0; // The size of the tree
/** The size of the frequency table.
*
* @return the size of the frequency table
*/
public int size() {
return size;
}
/** Creates an entry in the frequency table and initializes its
* count to zero.
*
* @param key key with which the specified value is to be associated
*/
public void init(String key) {
if (key == null){
throw new IllegalArgumentException();
}
Elem current = root;
if ( current == null ) {
root = new Elem( key);
}
boolean init = false;
while(!init){
int test = key.compareTo(current.key); //compare key with current key
if (test==0){//the same
init = true;
}else if(test<0){//less then
if (current.left==null){ //if left is empty
current.left = new Elem(key);
init = true;
}else{
current = current.left;
}
}else{//greater then
if (current.right==null){//if right is empty
current.right = new Elem(key);
init = true;
}else{
current = current.right;
}
}
}
//throw new UnsupportedOperationException("IMPLEMENT THIS METHOD");
}
/** The method updates the frequency associed with the key by one.
*
* @param key key with which the specified value is to be associated
*/
public void update(String key) {
if (key == null){
throw new IllegalArgumentException("null key");
}
Elem current = root;
if ( current == null ) {
root = new Elem( key);
}
boolean updated = false;
while(!updated){
int test = key.compareTo(current.key); //compare key with current key
if (test==0){//the same
current.count++; //updates the counter if its the same
updated=true;
}else if(test<0){//less then
if (current.left==null){ //if left is empty
throw new NoSuchElementException("Element not found");//if it hit the end and its null
//updated=true;
}else{
current = current.left;
}
}else{//greater then
if (current.right==null){//if right is empty
throw new NoSuchElementException("Element not found");//if it hits the end of the rright and it is null
//updated=true;
}else{
current = current.right;
}
}
}
//throw new UnsupportedOperationException("IMPLEMENT THIS METHOD");
}
/**
* Looks up for key in this TreeFrequencyTable, returns associated value.
*
* @param key value to look for
* @return value the value associated with this key
* @throws NoSuchElementException if the key is not found
*/
public long get(String key) {
if (key == null){
throw new IllegalArgumentException("null key");
}
Elem current = root;
boolean get = false;
if ( current == null ) {
throw new NoSuchElementException("Key not found");
//get=true;
//root = new Elem<E>( key);
}
while(!get){
int test = key.compareTo(current.key); //compare key with current key
if (test==0){//the same
return current.count; //updates the counter if its the same
//get=true;
}else if(test<0){//less then
if (current.left==null){ //if left is empty
throw new NoSuchElementException("Key not found");//if it hit the end and its null
//get=true;
}else{
current = current.left;
}
}else{//greater then
if (current.right==null){//if right is empty
throw new NoSuchElementException("Key not found");//if it hits the end of the rright and it is null
//get=true;
}else{
current = current.right;
}
}
}
//throw new UnsupportedOperationException("IMPLEMENT THIS METHOD");
throw new NoSuchElementException("Key not found");
}
/** Returns the list of keys in order, according to the method compareTo of the key
* objects.
*
* @return the list of keys in order
*/
public LinkedList<String> keys() {
LinkedList<String> listyList;
listyList=new LinkedList<String>();
return inOrderList(root,listyList);
//throw new UnsupportedOperationException("IMPLEMENT THIS METHOD");
}
/** Returns a LinkedList with the keys
*
* @param list a LinkedList<String>
* @param current an Elem
* @return a LinkedList with the keys
*/
private LinkedList<String> inOrderList(Elem current, LinkedList<String> list){
LinkedList<String> newList;
newList=list;
if(current!=null){
inOrderList(current.left, newList);
newList.addLast(current.key);
inOrderList(current.right, newList);
}
return newList;
}
/** Returns the values in the order specified by the method compareTo of the key
* objects.
*
* @return the values
*/
public long[] values() {
long[] countArray;
countArray=new long[size];
int counter=0;
return inOrderValue(root, countArray, counter);
// throw new UnsupportedOperationException("IMPLEMENT THIS METHOD");
}
/** Returns a array with long values
* @param counter, then counter of the whole thing
* @param array an array on long numbers
* @param current an Elem
* @return an array with long values
*/
private long[] inOrderValue(Elem current, long[] array, int counter){
long[] countArray;
countArray=new long[size];
//int counter=0;
if(current!=null){
inOrderValue(current.left, countArray, counter);
countArray[counter]=current.count;
counter++;
inOrderValue(current.right, countArray, counter);
}
return countArray;
}
/** Returns a String representation of the tree.
*
* @return a String representation of the tree.
*/
public String toString() {
return toString( root );
}
// Helper method.
private String toString(Elem current) {
if (current == null) {
return "{}";
}
return "{" + toString(current.left) + "[" + current.key + "," + current.count + "]" + toString(current.right) + "}";
}
}