-
Notifications
You must be signed in to change notification settings - Fork 0
/
bst.go
336 lines (269 loc) · 5.43 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
package consistenthashing
type node struct {
key uint32
val interface{}
parent *node
left *node
right *node
}
type bst struct {
root *node
}
func (b *bst) insert(key uint32, val interface{}) {
newNode := &node{key: key, val: val, parent: nil, left: nil, right: nil}
if b.root == nil {
b.root = newNode
} else {
insertNode(b.root, newNode)
}
}
func insertNode(root, newNode *node) {
if newNode.key <= root.key {
if root.left != nil {
insertNode(root.left, newNode)
} else {
root.left = newNode
root.left.parent = root
}
} else {
if root.right != nil {
insertNode(root.right, newNode)
} else {
root.right = newNode
root.right.parent = root
}
}
}
func (b *bst) find(key uint32) *node {
return findNode(b.root, key)
}
func findNode(root *node, key uint32) *node {
if root == nil {
return nil
}
if key == root.key {
return root
}
if key < root.key {
return findNode(root.left, key)
}
if key > root.key {
return findNode(root.right, key)
}
return root
}
func (b *bst) min(node *node) *node {
lnode := node
if lnode == nil {
return nil
}
for lnode.left != nil {
lnode = lnode.left
}
return lnode
}
func (b *bst) max(node *node) *node {
lnode := node
if lnode == nil {
return nil
}
for lnode.right != nil {
lnode = lnode.right
}
return lnode
}
func (b *bst) remove(key uint32) {
b.removeNode(key)
}
func (b *bst) findPrevSmaller(key uint32) *node {
node := b.find(key)
if node == nil {
return b.prevClosest(b.root, key)
}
smallerNode := b.predecessor(node)
if smallerNode != nil {
return smallerNode
}
return node
}
func (b *bst) prevClosest(node *node, key uint32) *node {
if node == nil {
return nil
}
prevNode := node
for node != nil {
prevNode = node
if node.key < key {
node = node.right
} else if node.key > key {
node = node.left
} else {
return b.predecessor(node)
}
}
if key > prevNode.key {
return prevNode
}
return b.predecessor(prevNode)
}
func (b *bst) findNextGreater(key uint32) *node {
node := b.find(key)
if node == nil {
return b.nextClosesElmt(b.root, key)
}
sNode := b.successor(node)
if node != nil {
return sNode
}
return node
}
func (b *bst) nextClosesElmt(root *node, key uint32) *node {
if root == nil {
return nil
}
prevNode := root
for root != nil {
prevNode = root
if root.key < key {
root = root.right
} else if root.key > key {
root = root.left
} else {
return b.successor(root)
}
}
if key < prevNode.key {
return prevNode
}
return b.successor(prevNode)
}
func (b *bst) removeNode(key uint32) bool {
node := b.find(key)
if node == nil {
return false
}
// node does not have child
if node.left == nil && node.right == nil {
if node.parent != nil {
if node.parent.left == node {
node.parent.left = nil
} else if node.parent.right == node {
node.parent.right = nil
}
} else {
b.root = nil
}
} else if node.left == nil || node.right == nil {
// node have only one child
childNode := node.right
if node.right == nil {
childNode = node.left
}
parent := node.parent
if parent != nil {
if parent.left == node {
parent.left = childNode
} else if parent.right == node {
parent.right = childNode
}
} else {
childNode.parent = parent
b.root = childNode
}
} else {
// have both child
successor := b.successor(node)
// remove successor from it's parent
successorParent := successor.parent
if successorParent.left == successor {
successorParent.left = nil
} else {
successorParent.right = nil
}
node.key = successor.key
node.val = successor.val
}
return true
}
// the predecessor of node x is node with greatest key smaller than x
func (b *bst) predecessor(node *node) *node {
if node.left != nil {
return b.max(node.left)
}
currentNode := node
parent := currentNode.parent
if parent != nil && parent.left == currentNode {
currentNode = parent
parent = currentNode.parent
}
return parent
}
// the successor of node x is node with minimum key greater than x
func (b *bst) successor(node *node) *node {
if node.right != nil {
return b.min(node.right)
}
if node.parent == nil {
return node
}
currentNode := node
parent := currentNode.parent
for parent != nil && currentNode == parent.right {
currentNode = parent
parent = currentNode.parent
}
return parent
}
// left root right
func (b *bst) inorder() []*node {
var res []*node
return iterateInorder(b.root, res)
}
func iterateInorder(root *node, res []*node) []*node {
if root == nil {
return res
}
if root.left != nil {
res = iterateInorder(root.left, res)
}
res = append(res, root)
if root.right != nil {
res = iterateInorder(root.right, res)
}
return res
}
// root, left , right
func (b *bst) preorder() []*node {
var res []*node
return iteratePreorder(b.root, res)
}
func iteratePreorder(root *node, res []*node) []*node {
if root == nil {
return res
}
res = append(res, root)
if root.left != nil {
res = iteratePreorder(root.left, res)
}
if root.right != nil {
res = iteratePreorder(root.right, res)
}
return res
}
func (b *bst) postorder() []*node {
var res []*node
return iteratePostorder(b.root, res)
}
// left, right , root
func iteratePostorder(root *node, res []*node) []*node {
if root == nil {
return res
}
if root.left != nil {
res = iteratePostorder(root.left, res)
}
if root.right != nil {
res = iteratePostorder(root.right, res)
}
return append(res, root)
}