-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleTreeMap.java
More file actions
315 lines (271 loc) · 9.78 KB
/
Copy pathSimpleTreeMap.java
File metadata and controls
315 lines (271 loc) · 9.78 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package map;
import java.util.*;
public class SimpleTreeMap<K extends Comparable, V> implements SimpleMap<K, V>, Iterable {
Node<K, V> parent = null;
List listValue;
List listKey;
Node<K, V> root = null;
int size;
@Override
public V get(K key) {
if (root == null) {
throw new NullPointerException();
}
return getByTheKey(key, root);
}
private V getByTheKey(K key, Node<K, V> nodeCursor) {
Node<K, V> newCursor = nodeCursor;
K keyCursor = newCursor.getKey();
if (keyCursor.compareTo(key) == 0)
return newCursor.getValue();
else {
if (keyCursor.compareTo(key) > 0) {
Node<K, V> newNodeCursor = newCursor.getLeft();
return getByTheKey(key, newNodeCursor);
}
if (keyCursor.compareTo(key) < 0) {
Node<K, V> newNodeCursor = newCursor.getRight();
return getByTheKey(key, newNodeCursor);
}
}
return null;
}
@Override
public boolean put(K key, V value) {
return addNode(key, value, root);
}
private boolean addNode(K key, V value, Node<K, V> nodeCursor) {
if (root == null) {
root = new Node(value, key, null, null);
size++;
return true;
}
K keyCursor = nodeCursor.getKey();
if (keyCursor.compareTo(key) > 0) {
Node<K, V> left = nodeCursor.getLeft();
if (left == null) {
nodeCursor.setLeft(new Node(value, key, null, null));
size++;
return true;
} else {
Node<K, V> newNodeCursor = nodeCursor.getLeft();
addNode(key, value, newNodeCursor);
}
}
if (keyCursor.compareTo(key) == 0) {
nodeCursor.setValue(value);
return true;
}
if (keyCursor.compareTo(key) < 0) {
Node<K, V> right = nodeCursor.getRight();
if (right == null) {
nodeCursor.setRight(new Node(value, key, null, null));
size++;
} else {
Node<K, V> newNodeCursor = nodeCursor.getRight();
addNode(key, value, newNodeCursor);
}
}
return false;
}
@Override
public boolean remove(K key) {
return removeByTheKey(key, root);
}
private boolean removeByTheKey(K key, Node<K, V> cursorNode) {
K cursorKey = cursorNode.getKey();
if (root == null) {
throw new NullPointerException();
}
if (key.compareTo(cursorKey) < 0) { // 1-ый случай когда искомая
parent = cursorNode;
removeByTheKey(key, cursorNode.getLeft());
return true;//нода лежит слева
}
if (key.compareTo(cursorKey) > 0) { // 2-ой случай когда искомая
parent = cursorNode; // нода лежит справа
removeByTheKey(key, cursorNode.getRight());
return true;
}
if (key.compareTo(cursorKey) == 0) { // 3 случай когда нашли ноду
if (key == root.getKey()) { //3.0 когда искомый ключ = корню
if (root.getLeft() == null && root.getRight() == null) {
root = null;
size--;
return true;
}
if(root.getLeft() != null && root.getRight() == null){
root = root.getLeft();
size--;
return true;
}
if(root.getLeft() == null && root.getRight() != null){
root = root.getRight();
size--;
return true;
}
}
if (cursorNode.getLeft() == null && cursorNode.getRight() == null) { //3.1 случай когда у удаляемого
if (parent.getRight() == cursorNode) { // элемента левая и правая нода
parent.setRight(null); // нул
size--;
return true;
}
if (parent.getLeft() == cursorNode) {
parent.setLeft(null);
size--;
return true;
}
}
if (cursorNode.getLeft() != null && cursorNode.getRight() == null) { //3.2 случай когда у удаляемого
if (parent.getRight() == cursorNode) { //3.2.1элемента левая нода не нул
parent.setRight(cursorNode.getLeft()); //а правая нода нул
size--;
return true;
}
if (parent.getLeft() == cursorNode) {
parent.setLeft(cursorNode.getLeft());
size--;
return true;
}
}
if (cursorNode.getLeft() == null && cursorNode.getRight() != null) { //3.2.2случай когда у удаляемого
if (parent.getRight() == cursorNode) { //элемента левая нода нул а
parent.setRight(cursorNode.getRight()); //правая нода нет
size--;
return true;
}
if (parent.getLeft() == cursorNode) {
parent.setLeft(cursorNode.getRight());
size--;
return true;
}
}
if (cursorNode.getLeft() != null && cursorNode.getRight() != null) { //3.2.3 когда оба листа не нул
if (parent.getRight() == cursorNode) { //тогда берем и подставляем левый лист или узел
parent.setRight(cursorNode.getLeft());
size--;
return true;
}
if (parent.getLeft() == cursorNode) {
parent.setLeft(cursorNode.getLeft());
size--;
return true;
}
}
}
return false;
}
@Override
public List<K> keys() {
listKey = new LinkedList();
return getAllKeys(root);
}
public List getAllKeys(Node node) {
if (node != null) {
listKey.add(node.getKey());
getAllKeys(node.getLeft());
getAllKeys(node.getRight());
}
return listKey;
}
@Override
public List values() {
listValue = new LinkedList();
return getAllValues(root);
}
private List getAllValues(Node node) {
if (node != null) {
listValue.add(node.getValue());
getAllValues(node.getLeft());
getAllValues(node.getRight());
}
return listValue;
}
@Override
public boolean ontainsKey(K key) {
Iterator test = this.iterator();
while (test.hasNext() == true) {
Node<K, V> node = (Node<K, V>) test.next();
if (node.getKey() == key) {
return true;
}
}
return false;
}
@Override
public int getSize() {
return size;
}
@Override
public Iterator iterator() {
return new IteratorForTreeMap(root);
}
public static class Node<K, V> {
V value;
K key;
Node<K, V> left;
Node<K, V> right;
Node(V value, K key, Node left, Node right) {
this.value = value;
this.key = key;
this.left = left;
this.right = right;
}
public V getValue() {
return value;
}
public K getKey() {
return key;
}
public Node<K, V> getLeft() {
return left;
}
public Node<K, V> getRight() {
return right;
}
private void setValue(V value) {
this.value = value;
}
private void setLeft(Node left) {
this.left = left;
}
private void setRight(Node right) {
this.right = right;
}
public String toString() {
return "Key = " + key + " Value = " + value;
}
}
public class IteratorForTreeMap<K, V> implements Iterator {
SimpleTreeMap.Node<K, V> root;
Stack<SimpleTreeMap.Node> stack;
boolean firstPass = true;
IteratorForTreeMap(SimpleTreeMap.Node<K, V> root) {
stack = new Stack<>();
this.root = root;
}
@Override
public boolean hasNext() {
if (stack.isEmpty() && firstPass == true) {
getAllNodes(root);
firstPass = false;
}
return (!stack.isEmpty());
}
@Override
public Object next() {
if (!stack.isEmpty()) {
return stack.pop();
}
return null;
}
private void getAllNodes(SimpleTreeMap.Node<K, V> node) {
if (node != null) {
stack.add(node);
getAllNodes(node.getLeft());
getAllNodes(node.getRight());
}
}
}
}