-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTreeMapCustom.java
179 lines (153 loc) · 4.47 KB
/
TreeMapCustom.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
package Maps;
import java.util.*;
import java.util.Comparator;
public class TreeMapCustom<K extends Comparable<K>, V> {
private Node root;
private int size = 0;
private class Node<K, V> {
K key;
V val;
Node left, right;
public Node(K key, V val, Node<K, V> left, Node<K, V> right) {
this.key = key;
this.val = val;
this.left = left;
this.right = right;
}
public Node(K key, V val) {
this.key = key;
this.val = val;
}
}
Comparator<K> comparator = (o1, o2) -> {
return o1.compareTo(o2);
};
public TreeMapCustom(Comparator<? extends K> comparator) {
this.comparator = (Comparator<K>) comparator;
}
public TreeMapCustom() {
}
public boolean put(K key, V val) {
Node node = put(key, val, root);
root = node;
return node != null;
}
private Node put(K key, V val, Node node) {
if (node == null) {
node = new Node(key, val);
size++;
} else {
if (comparator.compare(key, (K) node.key) < 0) {
node.left = put(key, val, node.left);
} else if (comparator.compare(key, (K) node.key) > 0) {
node.right = put(key, val, node.right);
} else {
node.val = val;
}
}
return node;
}
public boolean containsKey(K key) {
return get(key, root) != null;
}
public V get(K key) {
return get(key, root);
}
public int size() {
return size;
}
private V get(K key, Node node) {
if (node == null) {
return null;
}
if (comparator.compare(key, (K) node.key) == 0) {
return (V) node.val;
} else if (comparator.compare(key, (K) node.key) > 0) {
return get(key, node.right);
}
return get(key, node.left);
}
boolean that = false;
public V remove(K key) {
V val = get(key);
if (val == null) {
return null;
}
that = false;
root = remove(key, root);
if (that) {
size++;
}
return val;
}
private Node remove(K key, Node node) {
if (node == null) {
return null;
}
if (comparator.compare(key, (K) node.key) > 0) {
node.right = remove(key, node.right);
} else if (comparator.compare(key, (K) node.key) < 0) {
node.left = remove(key, node.left);
} else {
if (node.left == null && node.right == null) {
size--;
return null;
} else if (node.left == null && node.right != null) {
size--;
return node.right;
} else if (node.left != null && node.right == null) {
size--;
return node.left;
} else {
size--;
that = true;
Node minKey = getMin(node.right);
node.right = remove((K) minKey.key, node.right);
node.key = minKey.key;
node.val = minKey.val;
return node;
}
}
return node;
}
private Node getMin(Node node) {
if (node.left == null) {
return node;
}
return getMin(node.left);
}
public void clear() {
root = null;
}
private void display(Node node, StringBuilder helper) {
if (node == null) {
return;
}
display(node.left, helper);
helper.append(node.key + "=" + node.val + ",");
display(node.right, helper);
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder("{");
StringBuilder temp = new StringBuilder();
display(root, temp);
builder.append(temp);
if(builder.length()==1) builder.append(" ");
String t = builder.substring(0, builder.length() - 1);
return (new StringBuilder(t)).append("}").toString();
}
public ArrayList<K> toKeyArray() {
ArrayList<K> list = new ArrayList<>();
helperTraverse(list, root);
return list;
}
private void helperTraverse(ArrayList<K> list, Node node) {
if (node == null) {
return;
}
helperTraverse(list, node.left);
list.add((K) node.key);
helperTraverse(list, node.right);
}
}