This repository was archived by the owner on Sep 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeMap.java
More file actions
207 lines (179 loc) · 4.21 KB
/
Copy pathTreeMap.java
File metadata and controls
207 lines (179 loc) · 4.21 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
package com.hkt.tutorial.algorithms.ds;
import java.util.NoSuchElementException;
@SuppressWarnings({ "rawtypes", "unchecked", "hiding" })
public class TreeMap<K extends Comparable, V> implements Map<K, V> {
private int size;
private Node<K> root;
@Override
public int size() {
return size;
}
public boolean isEmpty() {
return root == null;
}
@Override
public void clear() {
root = null;
size = 0;
}
@Override
public V get(K key) {
if (root == null) {
return null;
}
Node<K> values = find(key, false);
System.out.println();
if (values == null)
return null;
return values.value;
}
@Override
public V put(K key, V value) {
if (root == null) {
root = new Node<K>(key);
size = 1;
return null;
}
Node<K> location = find(key, true);
int cp = key.compareTo(location.key);
if (cp == 0) {
V oldValue = location.value;
location.value = value;
return oldValue;
} else {
if (cp < 0)
location.left = new Node<K>(key);
else
location.right = new Node<K>(key);
size++;
return null;
}
}
@Override
public boolean contains(K key) {
V value = get(key);
if (value == null) {
return false;
}
return true;
}
public K firstKey() {
if (root == null)
throw new NoSuchElementException("Root Null");
return (K) firstNode().key;
}
public K lastKey() {
if (root == null)
throw new NoSuchElementException();
return (K) lastNode().key;
}
public Node firstNode() {
Node node = root;
while (node.right != null)
node = node.right;
return node;
}
public Node lastNode() {
Node node = root;
while (node.left != null)
node = node.left;
return node;
}
@Override
public V remove(K key) {
if (root == null)
return null;
root = splay(root, key);
int cmp = key.compareTo(root.key);
if (cmp == 0) {
if (root.left == null) {
root = root.right;
} else {
Node x = root.right;
root = root.left;
splay(root, key);
root.right = x;
}
}
return null;
}
private Node splay(Node h, K key) {
if (h == null)
return null;
int cmp1 = key.compareTo(h.key);
if (cmp1 < 0) {
if (h.left == null) {
return h;
}
int cmp2 = key.compareTo(h.left.key);
if (cmp2 < 0) {
h.left.left = splay(h.left.left, key);
h = rotateRight(h);
} else if (cmp2 > 0) {
h.left.right = splay(h.left.right, key);
if (h.left.right != null)
h.left = rotateLeft(h.left);
}
if (h.left == null)
return h;
else
return rotateRight(h);
} else if (cmp1 > 0) {
if (h.right == null) {
return h;
}
int cmp2 = key.compareTo(h.right.key);
if (cmp2 < 0) {
h.right.left = splay(h.right.left, key);
if (h.right.left != null)
h.right = rotateRight(h.right);
} else if (cmp2 > 0) {
h.right.right = splay(h.right.right, key);
h = rotateLeft(h);
}
if (h.right == null)
return h;
else
return rotateLeft(h);
}
else
return h;
}
private Node rotateRight(Node h) {
Node x = h.left;
h.left = x.right;
x.right = h;
return x;
}
private Node rotateLeft(Node h) {
Node x = h.right;
h.right = x.left;
x.left = h;
return x;
}
private Node<K> find(K key, boolean b) {
Node<K> current = root;
Node<K> parent = null;
do {
int cp = key.compareTo(current.key);
if (cp == 0)
break;
parent = current;
if (cp < 0)
current = current.left;
else
current = current.right;
} while (current != null);
if (current == null && b == true)
return parent;
return current;
}
private class Node<K extends Comparable> {
Node<K> left, right;
K key;
V value;
public Node(K k) {
key = k;
}
}
}