Repository navigation
Expand file tree
/
Copy pathSimpleTreeMap.java
More file actions
185 lines (154 loc) · 5 KB
/
Copy pathSimpleTreeMap.java
File metadata and controls
185 lines (154 loc) · 5 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
package map;
import java.util.Comparator;
import java.util.List;
public class SimpleTreeMap<K extends Comparable, V> implements SimpleMap<K, V> {
Node<K, V> root = null;
Comparator<? extends K> comparator;
int size;
SimpleTreeMap() {
}
SimpleTreeMap(Comparator comparator) {
this.comparator = comparator;
}
@Override
public V get(K key) {
if (root == null) {
throw new NullPointerException();
}
return getByTheKey(key, root);
}
public V getByTheKey(K key, Node<K, V> nodeCursor) {
Node<K,V> newCursor = nodeCursor;
K keyCursor = newCursor.getKey();
// while (keyCursor != key){
// int cmd = keyCursor.compareTo(key);
// if(cmd > 0)
// getByTheKey(key,newCursor.getLeft()) ;
//
// if(cmd < 0)
// getByTheKey(key,newCursor.getRight());
//
// else
// return newCursor.getValue();
//
// }
Node<K, V> cursorNode = nodeCursor;
if (keyCursor.compareTo(key) == 0) {
return cursorNode.getValue(); // Mr. 0: при наступлении этого событи он прыгает
}
if (keyCursor.compareTo(key) > 0) {
Node<K, V> newNodeCursor = cursorNode.getLeft();
return getByTheKey(key, newNodeCursor); // Mr. 0: суда
}
if (keyCursor.compareTo(key) < 0) { // Mr. 1: суда, суда он прыгает. а с какой стати он суда прыгать не должен?
Node<K, V> newNodeCursor = cursorNode.getRight();
return getByTheKey(key, newNodeCursor);
}
return null; // Mr. 0: потом суда, почему мне вообще не ясно
// Mr. 1: и таки чего не ясно? таки все ясно. всегда сюда.
// в любом случае сюда. куда ж еще?
}
@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){
if(root == null){
throw new NullPointerException();
}
K cursorKey = cursorNode.getKey();
if(cursorKey.compareTo(key) == 0){
if(cursorNode.getRight() == null && cursorNode.getLeft() == null){
}
return true;
}
return false;
}
@Override
public List keys() {
return null;
}
@Override
public List values() {
return null;
}
@Override
public boolean ontainsKey(K key) {
return false;
}
@Override
public int getSize() {
return size;
}
private 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;
}
}
}