Debian Codegraph @

splay/splay.go

raw symbols SCIP
1 package splay
2
3 import (
4 "fmt"
5 )
6
7 type (
8 Any interface{}
9 LessFunc func(interface{}, interface{}) bool
10 VisitFunc func(interface{}) bool
11
12 node struct {
13 value Any
14 parent, left, right *node
15 }
16 nodei struct {
17 step int
18 node *node
19 prev *nodei
20 }
21
22 SplayTree struct {
23 length int
24 root *node
25 less LessFunc
26 }
27 )
28
29 // Create a new splay tree, using the less function to determine the order.
30 func New(less LessFunc) *SplayTree {
31 return &SplayTree{0, nil, less}
32 }
33
34 // Get the first value from the collection. Returns nil if empty.
35 func (this *SplayTree) First() Any {
36 if this.length == 0 {
37 return nil
38 }
39
40 n := this.root
41 for n.left != nil {
42 n = n.left
43 }
44 return n.value
45 }
46
47 // Get the last value from the collection. Returns nil if empty.
48 func (this *SplayTree) Last() Any {
49 if this.length == 0 {
50 return nil
51 }
52 n := this.root
53 for n.right != nil {
54 n = n.right
55 }
56 return n.value
57 }
58
59 // Get an item from the splay tree
60 func (this *SplayTree) Get(item Any) Any {
61 if this.length == 0 {
62 return nil
63 }
64
65 n := this.root
66 for n != nil {
67 if this.less(item, n.value) {
68 n = n.left
69 continue
70 }
71
72 if this.less(n.value, item) {
73 n = n.right
74 continue
75 }
76
77 this.splay(n)
78 return n.value
79 }
80 return nil
81 }
82 func (this *SplayTree) Has(value Any) bool {
83 return this.Get(value) != nil
84 }
85 func (this *SplayTree) Init() {
86 this.length = 0
87 this.root = nil
88 }
89 func (this *SplayTree) Add(value Any) {
90 if this.length == 0 {
91 this.root = &node{value, nil, nil, nil}
92 this.length = 1
93 return
94 }
95
96 n := this.root
97 for {
98 if this.less(value, n.value) {
99 if n.left == nil {
100 n.left = &node{value, n, nil, nil}
101 this.length++
102 n = n.left
103 break
104 }
105 n = n.left
106 continue
107 }
108
109 if this.less(n.value, value) {
110 if n.right == nil {
111 n.right = &node{value, n, nil, nil}
112 this.length++
113 n = n.right
114 break
115 }
116 n = n.right
117 continue
118 }
119
120 n.value = value
121 break
122 }
123 this.splay(n)
124 }
125 func (this *SplayTree) PreOrder(visit VisitFunc) {
126 if this.length == 1 {
127 return
128 }
129 i := &nodei{0, this.root, nil}
130 for i != nil {
131 switch i.step {
132 // Value
133 case 0:
134 i.step++
135 if !visit(i.node.value) {
136 break
137 }
138 // Left
139 case 1:
140 i.step++
141 if i.node.left != nil {
142 i = &nodei{0, i.node.left, i}
143 }
144 // Right
145 case 2:
146 i.step++
147 if i.node.right != nil {
148 i = &nodei{0, i.node.right, i}
149 }
150 default:
151 i = i.prev
152 }
153 }
154 }
155 func (this *SplayTree) InOrder(visit VisitFunc) {
156 if this.length == 1 {
157 return
158 }
159 i := &nodei{0, this.root, nil}
160 for i != nil {
161 switch i.step {
162 // Left
163 case 0:
164 i.step++
165 if i.node.left != nil {
166 i = &nodei{0, i.node.left, i}
167 }
168 // Value
169 case 1:
170 i.step++
171 if !visit(i.node.value) {
172 break
173 }
174 // Right
175 case 2:
176 i.step++
177 if i.node.right != nil {
178 i = &nodei{0, i.node.right, i}
179 }
180 default:
181 i = i.prev
182 }
183 }
184 }
185 func (this *SplayTree) PostOrder(visit VisitFunc) {
186 if this.length == 1 {
187 return
188 }
189 i := &nodei{0, this.root, nil}
190 for i != nil {
191 switch i.step {
192 // Left
193 case 0:
194 i.step++
195 if i.node.left != nil {
196 i = &nodei{0, i.node.left, i}
197 }
198 // Right
199 case 1:
200 i.step++
201 if i.node.right != nil {
202 i = &nodei{0, i.node.right, i}
203 }
204 // Value
205 case 2:
206 i.step++
207 if !visit(i.node.value) {
208 break
209 }
210 default:
211 i = i.prev
212 }
213 }
214 }
215 func (this *SplayTree) Do(visit VisitFunc) {
216 this.InOrder(visit)
217 }
218 func (this *SplayTree) Len() int {
219 return this.length
220 }
221 func (this *SplayTree) Remove(value Any) {
222 if this.length == 0 {
223 return
224 }
225
226 n := this.root
227 for n != nil {
228 if this.less(value, n.value) {
229 n = n.left
230 continue
231 }
232 if this.less(n.value, value) {
233 n = n.right
234 continue
235 }
236
237 // First splay the parent node
238 if n.parent != nil {
239 this.splay(n.parent)
240 }
241
242 // No children
243 if n.left == nil && n.right == nil {
244 // guess we're the root node
245 if n.parent == nil {
246 this.root = nil
247 break
248 }
249 if n.parent.left == n {
250 n.parent.left = nil
251 } else {
252 n.parent.right = nil
253 }
254 } else if n.left == nil {
255 // root node
256 if n.parent == nil {
257 this.root = n.right
258 break
259 }
260 if n.parent.left == n {
261 n.parent.left = n.right
262 } else {
263 n.parent.right = n.right
264 }
265 } else if n.right == nil {
266 // root node
267 if n.parent == nil {
268 this.root = n.left
269 break
270 }
271 if n.parent.left == n {
272 n.parent.left = n.left
273 } else {
274 n.parent.right = n.left
275 }
276 } else {
277 // find the successor
278 s := n.right
279 for s.left != nil {
280 s = s.left
281 }
282
283 np := n.parent
284 nl := n.left
285 nr := n.right
286
287 sp := s.parent
288 sr := s.right
289
290 // Update parent
291 s.parent = np
292 if np == nil {
293 this.root = s
294 } else {
295 if np.left == n {
296 np.left = s
297 } else {
298 np.right = s
299 }
300 }
301
302 // Update left
303 s.left = nl
304 s.left.parent = s
305
306 // Update right
307 if nr != s {
308 s.right = nr
309 s.right.parent = s
310 }
311
312 // Update successor parent
313 if sp.left == s {
314 sp.left = sr
315 } else {
316 sp.right = sr
317 }
318 }
319
320 break
321 }
322
323 if n != nil {
324 this.length--
325 }
326 }
327 func (this *SplayTree) String() string {
328 if this.length == 0 {
329 return "{}"
330 }
331 return this.root.String()
332 }
333
334 // Splay a node in the tree (send it to the top)
335 func (this *SplayTree) splay(n *node) {
336 // Already root, nothing to do
337 if n.parent == nil {
338 this.root = n
339 return
340 }
341
342 p := n.parent
343 g := p.parent
344
345 // Zig
346 if p == this.root {
347 if n == p.left {
348 p.rotateRight()
349 } else {
350 p.rotateLeft()
351 }
352 } else {
353 // Zig-zig
354 if n == p.left && p == g.left {
355 g.rotateRight()
356 p.rotateRight()
357 } else if n == p.right && p == g.right {
358 g.rotateLeft()
359 p.rotateLeft()
360 // Zig-zag
361 } else if n == p.right && p == g.left {
362 p.rotateLeft()
363 g.rotateRight()
364 } else if n == p.left && p == g.right {
365 p.rotateRight()
366 g.rotateLeft()
367 }
368 }
369 this.splay(n)
370 }
371
372 // Swap two nodes in the tree
373 func (this *SplayTree) swap(n1, n2 *node) {
374 p1 := n1.parent
375 l1 := n1.left
376 r1 := n1.right
377
378 p2 := n2.parent
379 l2 := n2.left
380 r2 := n2.right
381
382 // Update node links
383 n1.parent = p2
384 n1.left = l2
385 n1.right = r2
386
387 n2.parent = p1
388 n2.left = l1
389 n2.right = r1
390
391 // Update parent links
392 if p1 != nil {
393 if p1.left == n1 {
394 p1.left = n2
395 } else {
396 p1.right = n2
397 }
398 }
399 if p2 != nil {
400 if p2.left == n2 {
401 p2.left = n1
402 } else {
403 p2.right = n1
404 }
405 }
406
407 if n1 == this.root {
408 this.root = n2
409 } else if n2 == this.root {
410 this.root = n1
411 }
412 }
413
414 // Node methods
415 func (this *node) String() string {
416 str := "{" + fmt.Sprint(this.value) + "|"
417 if this.left != nil {
418 str += this.left.String()
419 }
420 str += "|"
421 if this.right != nil {
422 str += this.right.String()
423 }
424 str += "}"
425 return str
426 }
427 func (this *node) rotateLeft() {
428 parent := this.parent
429 pivot := this.right
430 child := pivot.left
431
432 if pivot == nil {
433 return
434 }
435
436 // Update the parent
437 if parent != nil {
438 if parent.left == this {
439 parent.left = pivot
440 } else {
441 parent.right = pivot
442 }
443 }
444
445 // Update the pivot
446 pivot.parent = parent
447 pivot.left = this
448
449 // Update the child
450 if child != nil {
451 child.parent = this
452 }
453
454 // Update this
455 this.parent = pivot
456 this.right = child
457 }
458 func (this *node) rotateRight() {
459 parent := this.parent
460 pivot := this.left
461 child := pivot.right
462
463 if pivot == nil {
464 return
465 }
466
467 // Update the parent
468 if parent != nil {
469 if parent.left == this {
470 parent.left = pivot
471 } else {
472 parent.right = pivot
473 }
474 }
475
476 // Update the pivot
477 pivot.parent = parent
478 pivot.right = this
479
480 if child != nil {
481 child.parent = this
482 }
483
484 // Update this
485 this.parent = pivot
486 this.left = child
487 }