-
Notifications
You must be signed in to change notification settings - Fork 0
/
ABTreeMap.java
240 lines (214 loc) · 5.53 KB
/
ABTreeMap.java
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
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import ABTreeSet.Node;
/**
* @author Stephanie Engelhardt
*/
public class ABTreeMap<K extends Comparable<? super K>, V> {
final class Entry implements Comparable<Entry>, Map.Entry<K, V> {
private final K key; // immutable
private V value; // mutable
Entry(K key, V value) {
this.key = key;
this.value = value;
}
@Override
public int compareTo(Entry o) {
if((int) o.getKey()>(int)key)
return 1;
else if((int)o.getKey()<(int)key)
return -1;
else
return 0;
}
@Override
public K getKey() {
return key;
}
@Override
public V getValue() {
return value;
}
@Override
public V setValue(V newValue) {
value=newValue;
return newValue;
}
}
private ABTreeSet<Entry> entrySet;
private int size;
private boolean selfbalance;
private int top;
private int bottom;
private Node root;
/**
* Default constructor. Builds a map that uses a non-self-balancing tree.
*/
public ABTreeMap() {
entrySet=new ABTreeSet<Entry>();
size=0;
selfbalance=false;
root=null;
}
/**
* If isSelfBalancing is true, <br>
* builds a map that uses self-balancing tree with alpha = 2/3. <br>
* If isSelfBalancing is false, <br>
* builds a map that uses a non-self-balancing tree.
*
* @param isSelfBalancing
*/
public ABTreeMap(boolean isSelfBalancing) {
entrySet=new ABTreeSet<Entry>();
selfbalance=isSelfBalancing;
if(selfbalance){
top=2;
bottom=3;
}
size=0;
root=null;
}
/**
* If isSelfBalancing is true, <br>
* builds a map that uses a self-balancing tree with alpha = top/bottom.
* <br>
* If isSelfBalancing is false, <br>
* builds a map that uses a non-self-balancing tree.
*
* @param isSelfBalancing
* @param top
* @param bottom
* @throws IllegalArgumentException
* if (1/2 < alpha < 1) is false
*/
public ABTreeMap(boolean isSelfBalancing, int top, int bottom) {
entrySet=new ABTreeSet<Entry>();
selfbalance=isSelfBalancing;
if(selfbalance){
this.top=top;
this.bottom=bottom;
}
size=0;
root=null;
}
/**
* Returns true if this map contains a mapping for key; <br>
* otherwise, it returns false.
*
* @param key
* @return true if this map contains a mapping for key; <br>
* otherwise, it returns false.
*/
public boolean containsKey(K key) {
return entrySet.getBSTNode(new Entry(key,null))!=null;
}
/**
* Returns the value to which key is mapped, <br>
* or null if this map contains no mapping for key.
*
* @param key
* @return
*/
public V get(K key) {
ABTreeSet<Entry>.Node n= (ABTreeSet<ABTreeMap<K, V>.Entry>.Node) entrySet.getBSTNode(new Entry(key,null));
if(n!=null)
return n.data().value;
return null;
}
/**
* Returns a ABTreeSet storing the keys (not the values)
*
* Example. Suppose this map consists of the following (key, value) pairs:
* (10, Carol), (21, Bill), (45, Carol), (81, Alice), (95, Bill). <br>
* Then, the ABTreeSet returned should consist of 10, 21, 45, 81, 91.
* <p>
* The keySet should have the same tree structure as entrySet.
*
* @return a ABTreeSet storing the keys (not the values)
*/
public ABTreeSet<K> keySet() {
ABTreeSet<K> result=new ABTreeSet<K>();
Iterator iter= entrySet.iterator();
while(iter.hasNext()){
result.add(((Entry) iter.next()).getKey());
}
return result;
}
/**
* Associates value with key in this map. <br>
* Returns the previous value associated with key, <br>
* or null if there was no mapping for key.
*
* @param key
* @param value
* @return the previous value associated with key, <br>
* or null if there was no mapping for key.
* @throws NullPointerException
* if key or value is null.
*/
public V put(K key, V value) {
ABTreeSet<Entry>.Node n=(ABTreeSet<ABTreeMap<K, V>.Entry>.Node) entrySet.getBSTNode(new Entry(key,null));
V result=null;
if(n!=null){
result=n.data().value;
n.data().value=value;
}
else{
entrySet.add(new Entry(key, value));
size++;
}
return result;
}
/**
* Removes the mapping for key from this map if it is present. <br>
* Returns the previous value associated with key, <br>
* or null if there was no mapping for key.
*
* @param key
* @return the previous value associated with key, <br>
* or null if there was no mapping for key.
*/
public V remove(K key) {
ABTreeSet<Entry>.Node n=(ABTreeSet<ABTreeMap<K, V>.Entry>.Node) entrySet.getBSTNode(new Entry(key, null));
V result=null;
if(n!=null){
result=n.data().value;
entrySet.unlinkNode(n);
}
return result;
}
/**
* Returns the number of key-value mappings in this map.
*
* @return the number of key-value mappings in this map.
*/
public int size() {
return size;
}
@Override
public String toString() {
return entrySet.toString();
}
/**
* Returns an ArrayList storing the values contained in this map. <br>
* Note that there may be duplicate values. <br>
* The ArrayList should contain the values in ascending order of keys.
* <p>
* Example. <br>
* Suppose this map consists of the following (key, value) pairs: <br>
* (10, Carol), (21, Bill), (45, Carol), (81, Alice), (95, Bill). <br>
* Then, the ArrayList returned should consist of the strings <br>
* Carol, Bill, Carol, Alice, Bill, in that order.
*
* @return
*/
public ArrayList<V> values() {
ArrayList<V> result=new ArrayList<V>();
Iterator iter= entrySet.iterator();
while(iter.hasNext()){
result.add(((Entry) iter.next()).getValue());
}
return result;
}
}