-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbtree.go
338 lines (307 loc) · 5.51 KB
/
rbtree.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
package treemap
const (
red = 0 // red
black = 1 // black
)
// Keytype the key type
type Keytype interface {
LessThan(interface{}) bool
Equal(interface{}) bool
}
type valuetype interface{}
type Node struct {
left, right, parent *Node
color int
Key Keytype
Value valuetype
}
// Tree tree
type Tree struct {
root *Node
size int
}
// NewTree return a pointer to Tree
func NewTree() *Tree {
return &Tree{}
}
// Find ...
func (t *Tree) Find(key Keytype) interface{} {
n := t.findnode(key)
if n != nil {
return n.Value
}
return nil
}
// FindIter ...
func (t *Tree) FindIter(key Keytype) *Node {
return t.findnode(key)
}
// Empty ...
func (t *Tree) Empty() bool {
if t.root == nil {
return true
}
return false
}
// Iterator ...
func (t *Tree) Iterator() *Node {
return minimum(t.root)
}
// Size ...
func (t *Tree) Size() int {
return t.size
}
// Clear ...
func (t *Tree) Clear() {
t.root = nil
t.size = 0
}
// Insert ...
func (t *Tree) Insert(key Keytype, value valuetype) {
x := t.root
var y *Node
for x != nil {
y = x
if key.LessThan(x.Key) {
x = x.left
} else {
x = x.right
}
}
z := &Node{parent: y, color: red, Key: key, Value: value}
t.size++
if y == nil {
z.color = black
t.root = z
return
} else if z.Key.LessThan(y.Key) {
y.left = z
} else {
y.right = z
}
t.rbInsertFixup(z)
}
// Delete ...
func (t *Tree) Delete(key Keytype) {
z := t.findnode(key)
if z == nil {
return
}
var x, y *Node
if z.left != nil && z.right != nil {
y = successor(z)
} else {
y = z
}
if y.left != nil {
x = y.left
} else {
x = y.right
}
xparent := y.parent
if x != nil {
x.parent = xparent
}
if y.parent == nil {
t.root = x
} else if y == y.parent.left {
y.parent.left = x
} else {
y.parent.right = x
}
if y != z {
z.Key = y.Key
z.Value = y.Value
}
if y.color == black {
t.rbDeleteFixup(x, xparent)
}
t.size--
}
func (t *Tree) rbInsertFixup(z *Node) {
var y *Node
for z.parent != nil && z.parent.color == red {
if z.parent == z.parent.parent.left {
y = z.parent.parent.right
if y != nil && y.color == red {
z.parent.color = black
y.color = black
z.parent.parent.color = red
z = z.parent.parent
} else {
if z == z.parent.right {
z = z.parent
t.leftRotate(z)
}
z.parent.color = black
z.parent.parent.color = red
t.rightRotate(z.parent.parent)
}
} else {
y = z.parent.parent.left
if y != nil && y.color == red {
z.parent.color = black
y.color = black
z.parent.parent.color = red
z = z.parent.parent
} else {
if z == z.parent.left {
z = z.parent
t.rightRotate(z)
}
z.parent.color = black
z.parent.parent.color = red
t.leftRotate(z.parent.parent)
}
}
}
t.root.color = black
}
func (t *Tree) rbDeleteFixup(x, parent *Node) {
var w *Node
for x != t.root && getColor(x) == black {
if x != nil {
parent = x.parent
}
if x == parent.left {
w = parent.right
if w.color == red {
w.color = black
parent.color = red
t.leftRotate(parent)
w = parent.right
}
if getColor(w.left) == black && getColor(w.right) == black {
w.color = red
x = parent
} else {
if getColor(w.right) == black {
if w.left != nil {
w.left.color = black
}
w.color = red
t.rightRotate(w)
w = parent.right
}
w.color = parent.color
parent.color = black
if w.right != nil {
w.right.color = black
}
t.leftRotate(parent)
x = t.root
}
} else {
w = parent.left
if w.color == red {
w.color = black
parent.color = red
t.rightRotate(parent)
w = parent.left
}
if getColor(w.left) == black && getColor(w.right) == black {
w.color = red
x = parent
} else {
if getColor(w.left) == black {
if w.right != nil {
w.right.color = black
}
w.color = red
t.leftRotate(w)
w = parent.left
}
w.color = parent.color
parent.color = black
if w.left != nil {
w.left.color = black
}
t.rightRotate(parent)
x = t.root
}
}
}
if x != nil {
x.color = black
}
}
func (t *Tree) leftRotate(x *Node) {
y := x.right
x.right = y.left
if y.left != nil {
y.left.parent = x
}
y.parent = x.parent
if x.parent == nil {
t.root = y
} else if x == x.parent.left {
x.parent.left = y
} else {
x.parent.right = y
}
y.left = x
x.parent = y
}
func (t *Tree) rightRotate(x *Node) {
y := x.left
x.left = y.right
if y.right != nil {
y.right.parent = x
}
y.parent = x.parent
if x.parent == nil {
t.root = y
} else if x == x.parent.right {
x.parent.right = y
} else {
x.parent.left = y
}
y.right = x
x.parent = y
}
// findnode finds the node by key and return it, if not exists return nil.
func (t *Tree) findnode(key Keytype) *Node {
x := t.root
for x != nil {
if key.LessThan(x.Key) {
x = x.left
} else {
//if key == x.Key {
if key.Equal(x.Key) {
return x
}
x = x.right
}
}
return nil
}
// Next returns the node's successor as an iterator.
func (n *Node) Next() *Node {
return successor(n)
}
// successor returns the successor of the node
func successor(x *Node) *Node {
if x.right != nil {
return minimum(x.right)
}
y := x.parent
for y != nil && x == y.right {
x = y
y = x.parent
}
return y
}
// getColor gets color of the node.
func getColor(n *Node) int {
if n == nil {
return black
}
return n.color
}
// minimum finds the minimum node of subtree n.
func minimum(n *Node) *Node {
for n.left != nil {
n = n.left
}
return n
}