forked from srdelisser/Assignment-5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeFrequencyTable.java
More file actions
123 lines (86 loc) · 2.82 KB
/
TreeFrequencyTable.java
File metadata and controls
123 lines (86 loc) · 2.82 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
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) {
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) {
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) {
throw new UnsupportedOperationException("IMPLEMENT THIS METHOD");
}
/** 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() {
throw new UnsupportedOperationException("IMPLEMENT THIS METHOD");
}
/** Returns the values in the order specified by the method compareTo of the key
* objects.
*
* @return the values
*/
public long[] values() {
throw new UnsupportedOperationException("IMPLEMENT THIS METHOD");
}
/** 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) + "}";
}
}