-
Notifications
You must be signed in to change notification settings - Fork 58
/
bst.go
468 lines (375 loc) · 7.16 KB
/
bst.go
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
package hftorderbook
import (
"fmt"
)
// Simple Binary Search Tree, not self-balancing, good for random input
type nodeBST struct {
Key float64
Value *LimitOrder
Next *nodeBST
Prev *nodeBST
left *nodeBST
right *nodeBST
size int
}
type bst struct {
root *nodeBST
minC *nodeBST // cached min/max keys for O(1) access
maxC *nodeBST
}
func NewBST() bst {
return bst{}
}
func (t *bst) Size() int {
return t.size(t.root)
}
func (t *bst) size(n *nodeBST) int {
if n == nil {
return 0
}
return n.size
}
func (t *bst) IsEmpty() bool {
return t.size(t.root) == 0
}
func (t *bst) panicIfEmpty() {
if t.IsEmpty() {
panic("BST is empty")
}
}
func (t *bst) Contains(key float64) bool {
return t.get(t.root, key) != nil
}
func (t *bst) Get(key float64) *LimitOrder {
t.panicIfEmpty()
x := t.get(t.root, key)
if x == nil {
panic(fmt.Sprintf("key %0.8f does not exist", key))
}
return x.Value
}
func (t *bst) get(n *nodeBST, key float64) *nodeBST {
if n == nil {
return nil
}
if n.Key == key {
return n
}
if n.Key > key {
return t.get(n.left, key)
} else {
return t.get(n.right, key)
}
}
func (t *bst) Put(key float64, value *LimitOrder) {
t.root = t.put(t.root, key, value)
}
func (t *bst) put(n *nodeBST, key float64, value *LimitOrder) *nodeBST {
if n == nil {
// search miss, creating a new node
n := &nodeBST{
Value: value,
Key: key,
size: 1,
}
if t.minC == nil || key < t.minC.Key {
// new min
t.minC = n
}
if t.maxC == nil || key > t.maxC.Key {
// new max
t.maxC = n
}
return n
}
if n.Key == key {
// search hit, updating the value
n.Value = value
return n
}
if n.Key > key {
left := n.left
n.left = t.put(n.left, key, value)
if left == nil {
// new node has been just inserted to the left
prev := n.Prev
if prev != nil {
prev.Next = n.left
}
n.left.Prev = prev
n.left.Next = n
n.Prev = n.left
}
} else {
right := n.right
n.right = t.put(n.right, key, value)
if right == nil {
// new node has been just inserted to the right
next := n.Next
if next != nil {
next.Prev = n.right
}
n.right.Next = next
n.right.Prev = n
n.Next = n.right
}
}
// re-calc size
n.size = t.size(n.left) + 1 + t.size(n.right)
return n
}
func (t *bst) Height() int {
if t.IsEmpty() {
return 0
}
return t.height(t.root)
}
func (t *bst) height(n *nodeBST) int {
if n == nil {
return 0
}
lheight := t.height(n.left)
rheight := t.height(n.right)
height := lheight
if rheight > lheight {
height = rheight
}
return height + 1
}
func (t *bst) Min() float64 {
t.panicIfEmpty()
return t.minC.Key
}
func (t *bst) MinValue() *LimitOrder {
t.panicIfEmpty()
return t.minC.Value
}
func (t *bst) MinPointer() *nodeBST {
t.panicIfEmpty()
return t.minC
}
func (t *bst) min(n *nodeBST) *nodeBST {
if n.left == nil {
return n
}
return t.min(n.left)
}
func (t *bst) Max() float64 {
t.panicIfEmpty()
return t.maxC.Key
}
func (t *bst) MaxValue() *LimitOrder {
t.panicIfEmpty()
return t.maxC.Value
}
func (t *bst) MaxPointer() *nodeBST {
t.panicIfEmpty()
return t.maxC
}
func (t *bst) max(n *nodeBST) *nodeBST {
if n.right == nil {
return n
}
return t.max(n.right)
}
func (t *bst) Floor(key float64) float64 {
t.panicIfEmpty()
floor := t.floor(t.root, key)
if floor == nil {
panic(fmt.Sprintf("there are no keys <= %0.8f", key))
}
return floor.Key
}
func (t *bst) floor(n *nodeBST, key float64) *nodeBST {
if n == nil {
// search miss
return nil
}
if n.Key == key {
// search hit
return n
}
if n.Key > key {
// floor must be in the left sub-tree
return t.floor(n.left, key)
}
// key could be in the right sub-tree, if not, using current root
floor := t.floor(n.right, key)
if floor != nil {
return floor
}
return n
}
func (t *bst) Ceiling(key float64) float64 {
t.panicIfEmpty()
ceiling := t.ceiling(t.root, key)
if ceiling == nil {
panic(fmt.Sprintf("there are no keys >= %0.8f", key))
}
return ceiling.Key
}
func (t *bst) ceiling(n *nodeBST, key float64) *nodeBST {
if n == nil {
// search miss
return nil
}
if n.Key == key {
// search hit
return n
}
if n.Key < key {
// ceiling must be in the right sub-tree
return t.ceiling(n.right, key)
}
// the key could be in the left sub-tree, if not, using current root
ceiling := t.ceiling(n.left, key)
if ceiling != nil {
return ceiling
}
return n
}
func (t *bst) Select(k int) float64 {
if k < 0 || k >= t.Size() {
panic("index out of range")
}
return t.selectNode(t.root, k).Key
}
func (t *bst) selectNode(n *nodeBST, k int) *nodeBST {
if t.size(n.left) == k {
return n
}
if t.size(n.left) > k {
return t.selectNode(n.left, k)
}
k = k - t.size(n.left) - 1
return t.selectNode(n.right, k)
}
func (t *bst) Rank(key float64) int {
t.panicIfEmpty()
return t.rank(t.root, key)
}
func (t *bst) rank(n *nodeBST, key float64) int {
if n == nil {
return 0
}
if n.Key == key {
return t.size(n.left)
}
if n.Key > key {
return t.rank(n.left, key)
}
return t.size(n.left) + 1 + t.rank(n.right, key)
}
func (t *bst) deleteMin(n *nodeBST) *nodeBST {
if n == nil {
return nil
}
if n.left == nil {
// we've reached the least leave of the tree
next := n.Next
prev := n.Prev
if prev != nil {
prev.Next = next
}
if next != nil {
next.Prev = prev
}
n.Next = nil
n.Prev = nil
// updating global min
if t.minC == n {
t.minC = next
}
return n.right
}
n.left = t.deleteMin(n.left)
// update size
n.size = t.size(n.left) + 1 + t.size(n.right)
return n
}
func (t *bst) Delete(key float64) {
t.panicIfEmpty()
t.root = t.delete(t.root, key)
}
func (t *bst) delete(n *nodeBST, key float64) *nodeBST {
if n == nil {
return nil
}
if n.Key == key {
// search hit
// updating linked list
next := n.Next
prev := n.Prev
if prev != nil {
prev.Next = next
}
if next != nil {
next.Prev = prev
}
n.Next = nil
n.Prev = nil
// updating global min and max
if t.minC == n {
t.minC = next
}
if t.maxC == n {
t.maxC = prev
}
// replacing by successor (we can do similar with precedessor)
if n.left == nil {
return n.right
} else if n.right == nil {
return n.left
}
newn := t.min(n.right)
newn.right = t.deleteMin(n.right)
newn.left = n.left
n = newn
} else if n.Key > key {
n.left = t.delete(n.left, key)
} else {
n.right = t.delete(n.right, key)
}
n.size = t.size(n.left) + 1 + t.size(n.right)
return n
}
func (t *bst) Keys(lo, hi float64) []float64 {
if lo < t.Min() || hi > t.Max() {
panic("keys out of range")
}
return t.keys(t.root, lo, hi)
}
func (t *bst) keys(n *nodeBST, lo, hi float64) []float64 {
if n == nil {
return nil
}
if n.Key < lo {
return t.keys(n.right, lo, hi)
} else if n.Key > hi {
return t.keys(n.left, lo, hi)
}
l := t.keys(n.left, lo, hi)
r := t.keys(n.right, lo, hi)
keys := make([]float64, 0)
if l != nil {
keys = append(keys, l...)
}
keys = append(keys, n.Key)
if r != nil {
keys = append(keys, r...)
}
return keys
}
func (t *bst) Print() {
fmt.Println()
t.print(t.root)
fmt.Println()
}
func (t *bst) print(n *nodeBST) {
if n == nil {
return
}
fmt.Printf("%0.8f ", n.Key)
t.print(n.left)
t.print(n.right)
}