-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathST.java
More file actions
95 lines (78 loc) · 3.06 KB
/
ST.java
File metadata and controls
95 lines (78 loc) · 3.06 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
//Symbol Table API
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.TreeMap;
public class ST<Key extends Comparable<Key>,Value> implements Iterable<Key> {
private TreeMap<Key,Value> st;
//Initializes an empty symbol table
public ST() {
st = new TreeMap<Key,Value>();
}
//Returns the value associated with the given key in this symbol table
public Value get(Key key) {
if (key==null) throw new IllegalArgumentException("called get() with null key");
return st.get(key);
}
//Inserts the specified key-value pair into the symbol table, overwriting the old value with the new value
//if the symbol table already contains the specified key.
public void put(Key key, Value val) {
if (key==null) throw new IllegalArgumentException("called put() with null key");
if (val == null) st.remove(key);
else st.put(key, val);
}
//Removes the specified key and its associated value from this symbol table(if the key is in this symbol table)
public void delete(Key key) {
if (key == null) throw new IllegalArgumentException("called delete() with null key");
st.remove(key);
}
//Removes the specified key and its associated value from this symbol table(if the key is in this symbol table)
public void remove(Key key) {
if (key == null) throw new IllegalArgumentException("called delete() with null key");
st.remove(key);
}
//Returns true if this symbol table contain the given key
public boolean contains(Key key) {
if(key==null) throw new IllegalArgumentException("called contains() with null key");
return st.containsKey(key);
}
//Returns the number of key-value pairs in the symbol table
public int size() {
return st.size();
}
//Returns true if the symbol table is empty
public boolean isEmpty() {
return size()==0;
}
//Returns all the keys in the symbol table
public Iterable<Key> keys(){
return st.keySet();
}
@Override
public Iterator<Key> iterator() {
return st.keySet().iterator();
}
//Returns the smallest key in the symbol table
public Key min() {
if(isEmpty()) throw new NoSuchElementException("called min() with empty symbol table");
return st.firstKey();
}
//Returns the largest key in the symbol table
public Key max() {
if(isEmpty()) throw new NoSuchElementException("called max() with empty symbol table");
return st.lastKey();
}
//Returns the smallest key in the symbol table greater than or equal to
public Key ceiling(Key key) {
if (key == null) throw new IllegalArgumentException("called ceiling() with null key");
Key k = st.ceilingKey(key);
if (k == null) throw new NoSuchElementException("all keys are less than " + key);
return k;
}
//Returns the largest key in the symbol table less than or equal to
public Key floor(Key key) {
if (key == null) throw new IllegalArgumentException("called floor() with null key");
Key k = st.floorKey(key);
if (k == null) throw new NoSuchElementException("all keys are greater than " + key);
return k;
}
}