-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.java
More file actions
254 lines (221 loc) · 5.03 KB
/
Copy pathBinarySearchTree.java
File metadata and controls
254 lines (221 loc) · 5.03 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
/*
Program #4
CS310
Hashim Abdirahim
cssc1449
*/
package data_structures;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.ConcurrentModificationException;
public class BinarySearchTree<K extends Comparable<K>, V> implements DictionaryADT<K, V> {
private Node<K, V> root;
private int currentSize, modCounter;
private K k;
public BinarySearchTree() {
root = null;
currentSize = 0;
modCounter = 0;
k = null;
}
private class Node<K, V> {
private K key;
private V value;
private Node<K, V> leftChild, rightChild;
public Node(K k, V v) {
key = k;
value = v;
leftChild = rightChild = null;
}
}
public boolean contains(K key) {
return findValue(key, root) != null;
}
// refrenced Riggins reader
public boolean add(K key, V value) {
if (contains(key))
return false;
if (root == null)
root = new Node<K, V>(key, value);
else
insert(key, value, root, null, false);
currentSize++;
modCounter++;
return true;
}
public boolean delete(K key) {
if (isEmpty())
return false;
if(!remove(key,root,null,false))
return false;
currentSize--;
modCounter++;
return true;
}
public V getValue(K key) {
if (isEmpty())
return null;
return findValue(key, root);
}
public K getKey(V value) {
if (isEmpty())
return null;
k = null;
findKey(root, value);
return k;
}
public int size() {
return currentSize;
}
public boolean isFull() {
return false;
}
public boolean isEmpty() {
return currentSize == 0;
}
public void clear() {
root = null;
currentSize = 0;
modCounter++;
}
/////// Iterator Helper
abstract class IteratorHelper<E> implements Iterator<E>{
protected int i;
protected int j;
private int modCount;
Node<K,V>[] orderedArray;
public IteratorHelper(){
i = j = 0;
modCount = modCounter;
orderedArray = new Node[currentSize];
inOrder(root);
}
public void inOrder(Node<K, V> n) {
if(n == null)
return;
inOrder(n.leftChild);
orderedArray[j++] = root;
inOrder(n.rightChild);
}
public boolean hasNext(){
if(modCount != modCounter)
throw new ConcurrentModificationException();
return i < currentSize;
}
public void remove(){
throw new UnsupportedOperationException();
}
}
class KeyIteratorHelper<K> extends IteratorHelper<K>{
public KeyIteratorHelper() {
super();
}
public K next() {
if(!hasNext())
throw new NoSuchElementException();
return (K) orderedArray[i++].key;
}
}
class ValueIteratorHelper<V> extends IteratorHelper<V>{
public ValueIteratorHelper(){
super();
}
public V next() {
if(!hasNext())
throw new NoSuchElementException();
return (V) orderedArray[i++].value;
}
}
public Iterator<K> keys() {
return new KeyIteratorHelper();
}
public Iterator<V> values() {
return new ValueIteratorHelper();
}
////////HELPER METHODS
//Refrenced Rigins
private void insert(K k, V v, Node<K, V> n, Node<K, V> parent, boolean wasLeft) {
if (n == null) {
if (wasLeft)
parent.leftChild = new Node<K, V>(k, v);
else
parent.rightChild = new Node<K, V>(k, v);
} else if (((Comparable<K>) k).compareTo((K) n.key) < 0)
insert(k, v, n.leftChild, n, true);
else
insert(k, v, n.rightChild, n, false);
}
// Worked From riggins reader
private V findValue(K key, Node<K, V> n) {
if (n == null)
return null;
if (((Comparable<K>) key).compareTo(n.key) < 0)
return findValue(key, n.leftChild);
else if (((Comparable<K>) key).compareTo(n.key) > 0)
return findValue(key, n.rightChild);
else
return (V) n.value;
}
private void findKey(Node<K,V> n, V value){
if(n==null) return;
if(((Comparable<V>)value).compareTo(n.value) == 0){
k=n.key;
return;
}
findKey(n.leftChild,value);
findKey(n.rightChild,value);
}
private boolean remove(K key, Node<K,V> n, Node<K,V> parent, boolean wasLeft) {
if(n == null)
return false;
int compare = 0;
compare = ((Comparable<K>)key).compareTo(n.key);
if(compare < 0)
return remove(k, n.leftChild, n, true);
else if(compare >0)
return remove(k, n.rightChild, n, false);
else {
if((n.leftChild == null) && n.rightChild == null) {
if(parent == null)
root = null;
else if(wasLeft)
parent.leftChild = null;
else
parent.rightChild = null;
}
else if(n.leftChild == null) {
if (parent == null)
root = n.rightChild;
else if(wasLeft)
parent.leftChild = n.leftChild;
else
parent.rightChild = n.leftChild;
}
else {
Node<K,V> tmp = Successor(n.rightChild);
if(tmp == null) {
n.key = n.rightChild.key;
n.value = n.rightChild.value;
n.rightChild = n.rightChild.rightChild;
}
else {
n.key = tmp.key;
n.value = tmp.value;
}
}
}
return true;
}
private Node<K,V> Successor(Node<K,V> n){
Node<K,V> parent = null;
while(n.leftChild != null){
parent = n;
n = n.leftChild;
}
if(parent == null)
return null;
else
parent.leftChild = n.rightChild;
return n;
}
}