-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBST.java
More file actions
253 lines (223 loc) · 5.09 KB
/
Copy pathBST.java
File metadata and controls
253 lines (223 loc) · 5.09 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
public class BST<Key extends Comparable<Key>, Value>
{
/* inner class for BST node type
* insert keys in random order to avoid worst case */
private class Node
{
private Key key;
private Value val;
Node left, right;
private int count;
public Node(Key key, Value val)
{
this.key = key;
this.val = val;
}
// accessors
public Key getKey() { return key; }
public Value getValue() { return val; }
public int getCount() { return count; }
// mutators
public void setKey(Key key) { this.key = key; }
public void setValue(Value val) { this.val = val; }
public void setCount(int count) { this.count = count; }
}
/* BST is just a reference to the root node */
private Node root;
/* insertion */
public void put(Key key, Value val)
{
// use recursion to insert (O(lgN))
root = put(root, key, val);
}
/* insertion recursive helper */
private Node put(Node x, Key key, Value val)
{
if (x == null) return new Node(key, val);
int cmp = key.compareTo(x.getKey());
if (cmp < 0) x.left = put(x.left, key, val);
else if (cmp > 0) x.right = put(x.right, key, val);
else x.setValue(val);
// set the count variable
x.setCount(1 + size(x.left) + size(x.right));
return x;
}
/* searching the BST */
public Value get(Key key)
{
// O(lgN)
Node x = root;
while (x != null)
{
int cmp = x.getKey().compareTo(key);
if (cmp < 0) x = x.right;
else if (cmp > 0) x = x.left;
else return x.getValue();
}
// not found
return null;
}
/* Ordered operations */
public Key min()
{
return min(root).getKey();
}
private Node min(Node x)
{
// return minimum key
while (x.left != null)
x = x.left;
return x;
}
public Key max()
{
return max(root).getKey();
}
private Node max(Node x)
{
// return the maximum key
while (x.right != null)
x = x.right;
return x;
}
public Key floor(Key key)
{
// return the floor of key
Node x = floor(root, key);
if (x == null)
return null;
return x.getKey();
}
/* helper to floor function */
private Node floor(Node x, Key key)
{
if (x == null) return null;
int cmp = key.compareTo(x.getKey());
if (cmp == 0) return x;
else if (cmp < 0) return floor(x.left, key);
// if key > x.key check right sub tree also
Node tmp = floor(x.right, key);
if (tmp == null) return x;
else return tmp;
}
public Key ceil(Key key)
{
// return the ceiling of key
Node x = ceil(root, key);
if (x == null)
return null;
return x.getKey();
}
/* helper method for ceil */
private Node ceil(Node x, Key key)
{
if (x == null) return null;
int cmp = key.compareTo(x.getKey());
if (cmp == 0) return x;
else if (cmp > 0) return ceil(x.right, key);
// if key < x.key check left sub tree also
Node tmp = ceil(x.left, key);
if (tmp == null) return x;
else return tmp;
}
/* get size of BST */
public int size()
{
return size(root);
}
/* helper to get size of node */
private int size(Node x)
{
if (x == null) return 0;
return x.getCount();
}
/* ranking method */
public int rank(Key key)
{
return rank(root, key);
}
/* helper to rank */
private int rank(Node x, Key key)
{
if (x == null) return 0;
int cmp = key.compareTo(x.getKey());
if (cmp == 0) return size(x.left);
else if (cmp < 0) return rank(x.left, key);
else return 1 + size(x.left) + rank(x.right, key);
}
/* traversals */
public Iterable<Key> keys()
{
QueueLL<Key> q = new QueueLL<Key>();
inorder(root, q);
return q;
}
private void inorder(Node x, QueueLL<Key> q)
{
if (x == null) return;
// traverse left subtree
inorder(x.left, q);
// enqueue current node
q.enqueue(x.getKey());
// traverse right subtree
inorder(x.right, q);
}
/* nice to have pre-order and post-order implemented */
private void preorder(Node x, QueueLL<Key> q)
{
if (x == null) return;
q.enqueue(x.getKey());
preorder(x.left, q);
preorder(x.right, q);
}
private void postorder(Node x, QueueLL<Key> q)
{
if (x == null) return;
postorder(x.left, q);
postorder(x.right, q);
q.enqueue(x.getKey());
}
/* Hibbard deletion */
public void delete(Key key)
{
root = delete(root, key);
}
/* helper to delete */
private Node delete(Node x, Key key)
{
if (x == null) return null;
int cmp = key.compareTo(x.getKey());
if (cmp < 0) x.left = delete(x.left, key);
else if (cmp > 0) x.right = delete(x.right, key);
else
{
if (x.right == null) return x.left;
if (x.left == null) return x.right;
// keep backup of current node
Node tmp = x;
// make x point to minimum node of right subtree
x = min(tmp.right);
// re-route right link to point to tmp's right subtree without the minimum
x.right = deleteMin(tmp.right);
// re-route left link to point to tmp's left subtree
x.left = tmp.left;
}
// update counts
x.setCount(1 + size(x.left) + size(x.right));
return x;
}
private void deleteMin()
{
// use recursive helper
root = deleteMin(root);
}
/* helper to hibbard deletion */
private Node deleteMin(Node x)
{
if (x.left == null) return x.right;
x.left = deleteMin(x.left);
// update counts
x.setCount(1 + size(x.left) + size(x.right));
return x;
}
}