Class: Xunch::RBTree

Inherits:
Object
  • Object
show all
Defined in:
lib/xunch/utils/rb_tree.rb,
lib/xunch/utils/rb_tree_node.rb

Defined Under Namespace

Classes: ImmutableNode, Node

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&compare_proc) ⇒ RBTree

Creates a new tree.



15
16
17
18
19
# File 'lib/xunch/utils/rb_tree.rb', line 15

def initialize(&compare_proc)
  @size = 0
  @root = nil
  @compare_proc = compare_proc
end

Instance Attribute Details

#compare_procObject (readonly)

The key compare proc



12
13
14
# File 'lib/xunch/utils/rb_tree.rb', line 12

def compare_proc
  @compare_proc
end

#rootObject (readonly)

The tree’s root node.



6
7
8
# File 'lib/xunch/utils/rb_tree.rb', line 6

def root
  @root
end

#sizeObject (readonly)

The number of nodes in the tree.



9
10
11
# File 'lib/xunch/utils/rb_tree.rb', line 9

def size
  @size
end

Instance Method Details

#ceiling_key(key) ⇒ Object



299
300
301
302
303
304
305
306
# File 'lib/xunch/utils/rb_tree.rb', line 299

def ceiling_key(key)
  node = ceiling_node(key)
  if node == nil
    return nil
  else
    return node.key
  end
end

#ceiling_node(key) ⇒ Object



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
# File 'lib/xunch/utils/rb_tree.rb', line 257

def ceiling_node(key)
  p = @root;
  while p != nil
    cmp = compare_key(key, p.key)
    if cmp < 0
      if p.left != nil
        p = p.left
      else
        if p == nil
          return nil
        else
          return RBTree::ImmutableNode.new(p.key,p.value)
        end
      end
    elsif cmp > 0
      if p.right != nil
        p = p.right
      else
        parent = p.parent
        ch = p
        while parent != nil && ch == parent.right
          ch = parent
          parent = parent.parent;
        end
        if parent == nil
          return nil
        else
          return RBTree::ImmutableNode.new(parent.key,parent.value)
        end
      end
    else
      if p == nil
        return nil
      else
        return RBTree::ImmutableNode.new(p.key,p.value)
      end
    end
  end

  return nil
end

#ceiling_value(key) ⇒ Object



308
309
310
311
312
313
314
315
# File 'lib/xunch/utils/rb_tree.rb', line 308

def ceiling_value(key)
  node = ceiling_node(key)
  if node == nil
    return nil
  else
    return node.value
  end
end

#clearObject



75
76
77
78
# File 'lib/xunch/utils/rb_tree.rb', line 75

def clear
  @size = 0
  @root = nil
end

#colorof(node) ⇒ Object



604
605
606
607
608
# File 'lib/xunch/utils/rb_tree.rb', line 604

def colorof(node)
  if node != nil
    node.color
  end
end

#compare_key(key1, key2) ⇒ Object



616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
# File 'lib/xunch/utils/rb_tree.rb', line 616

def compare_key(key1,key2)
  if !key1.integer? || !key2.integer?
    raise "#{key1} type is #{key1.class}, #{key2} type is #{key2.class}, but need type Integer"
  end
  if @compare_proc != nil
    return @cmp_proc.call(key1, key2)
  else
    if key1 > key2
      return 1
    elsif key1 < key2
      return -1
    elsif key1 == key2
      return 0
    end
  end
end

#deletenode(p) ⇒ Object



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
# File 'lib/xunch/utils/rb_tree.rb', line 391

def deletenode(p)
  @size-=1

  if p.left != nil && p.right != nil
    s = successor(p)
    p.key = s.key
    p.value = s.value
    p = s
  end

  replacement = (p.left != nil ? p.left : p.right)

  if replacement != nil
    replacement.parent = p.parent;
    if p.parent == nil
      @root = replacement
    elsif p == p.parent.left
      p.parent.left  = replacement
    else
      p.parent.right = replacement
    end

    p.left = p.right = p.parent = nil

    if p.color == :black
      fixafterdeletion(replacement)
    end
  elsif p.parent == nil
    @root = nil
  else
    if p.color == :black
      fixdfterdeletion(p)
    end
    if p.parent != nil
      if p == p.parent.left
        p.parent.left = nil
      elsif p == p.parent.right
        p.parent.right = nil
      end
      p.parent = nil
    end
  end
end

#first_keyObject



94
95
96
97
98
99
100
101
102
# File 'lib/xunch/utils/rb_tree.rb', line 94

def first_key
  p = @root;
  if p != nil
    while p.left != nil
      p = p.left
    end
  end
  p.key
end

#first_nodeObject



84
85
86
87
88
89
90
91
92
# File 'lib/xunch/utils/rb_tree.rb', line 84

def first_node
  p = @root;
  if p != nil
    while p.left != nil
      p = p.left
    end
  end
  RBTree::ImmutableNode.new(p.key,p.value)
end

#first_valueObject



104
105
106
107
108
109
110
111
112
# File 'lib/xunch/utils/rb_tree.rb', line 104

def first_value
  p = @root;
  if p != nil
    while p.left != nil
      p = p.left
    end
  end
  p.value
end

#fixafterdeletion(x) ⇒ Object



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/xunch/utils/rb_tree.rb', line 451

def fixafterdeletion(x)
  while x != @root && colorof(x) == :black
    if x == leftof(parentof(x))
      sib = rightof(parentof(x))

      if colorof(sib) == :red
        setcolor(sib, :black)
        setcolor(parentof(x), :red)
        rotateleft(parentof(x))
        sib = rightof(parentof(x))
      end

      if colorof(leftof(sib))  == :black && colorof(rightof(sib)) == :black
        setcolor(sib, :red)
        x = parentof(x)
      else
        if colorof(rightof(sib)) == :black
          setcolor(leftof(sib), :black)
          setcolor(sib, :red)
          rotateright(sib)
          sib = rightof(parentof(x))
        end
        setcolor(sib, colorof(parentof(x)))
        setcolor(parentof(x), :black)
        setcolor(rightof(sib), :black)
        rotateleft(parentof(x))
        x = @root
      end
    else
      sib = leftof(parentof(x))

      if colorof(sib) == :red
        setcolor(sib, :black)
        setcolor(parentof(x), :red)
        rotateright(parentof(x))
        sib = leftof(parentof(x))
      end

      if colorof(rightof(sib)) == :black && colorof(leftof(sib)) == :black
        setcolor(sib, :red)
        x = parentof(x)
      else
        if colorof(leftof(sib)) == :black
          setcolor(rightof(sib), :black)
          setcolor(sib, :red)
          rotateleft(sib)
          sib = leftof(parentof(x))
        end
        setcolor(sib, colorof(parentof(x)))
        setcolor(parentof(x), :black)
        setcolor(leftof(sib), :black)
        rotateright(parentof(x))
        x = @root;
      end
    end
  end
  setcolor(x, :black);
end

#fixafterinsertion(x) ⇒ Object



510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
# File 'lib/xunch/utils/rb_tree.rb', line 510

def fixafterinsertion(x)
  x.color = :red

  while x != nil && x != @root && x.parent.color == :red 
    if parentof(x) == leftof(parentof(parentof(x)))
      y = rightof(parentof(parentof(x)))
      if colorof(y) == :red
        setcolor(parentof(x), :black)
        setcolor(y, :black)
        setcolor(parentof(parentof(x)), :red)
        x = parentof(parentof(x))
      else
        if x == rightof(parentof(x)) 
          x = parentof(x)
          rotateleft(x)
        end
        setcolor(parentof(x), :black);
        setcolor(parentof(parentof(x)), :red);
        rotateright(parentof(parentof(x)));
      end
    else
      y = leftof(parentof(parentof(x)))
      if colorof(y) == :red
        setcolor(parentof(x), :black)
        setcolor(y, :black)
        setcolor(parentof(parentof(x)), :red)
        x = parentof(parentof(x))
      else
        if x == leftof(parentof(x))
          x = parentof(x)
          rotateright(x)
        end
        setcolor(parentof(x), :black);
        setcolor(parentof(parentof(x)), :red);
        rotateleft(parentof(parentof(x)));
      end
    end
  end

  @root.color = :black;
end

#floor_key(key) ⇒ Object



186
187
188
189
190
191
192
193
# File 'lib/xunch/utils/rb_tree.rb', line 186

def floor_key(key)
  node = floor_node(key)
  if node == nil
    return nil
  else
    return node.key
  end
end

#floor_node(key) ⇒ Object



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
# File 'lib/xunch/utils/rb_tree.rb', line 144

def floor_node(key)
  p = @root;
  while p != nil
    cmp = compare_key(key, p.key)
    if cmp > 0
      if p.right != nil
        p = p.right
      else
        if p == nil
          return nil
        else
          return RBTree::ImmutableNode.new(p.key,p.value)
        end
      end
    elsif cmp < 0
      if p.left != nil
        p = p.left
      else
        parent = p.parent
        ch = p
        while parent != nil && ch == parent.left
          ch = parent
          parent = parent.parent;
        end
        if parent == nil
          return nil
        else
          return RBTree::ImmutableNode.new(parent.key,parent.value)
        end
      end
    else
      if p == nil
        return nil
      else
        return RBTree::ImmutableNode.new(p.key,p.value)
      end
    end
  end

  return nil
end

#floor_value(key) ⇒ Object



195
196
197
198
199
200
201
202
# File 'lib/xunch/utils/rb_tree.rb', line 195

def floor_value(key)
  node = floor_node(key)
  if node == nil
    return nil
  else
    return node.value
  end
end

#get(key) ⇒ Object



70
71
72
73
# File 'lib/xunch/utils/rb_tree.rb', line 70

def get(key)
  p = getnode(key);
  return (p == nil ? nil : p.value)
end

#getnode(key) ⇒ Object



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/xunch/utils/rb_tree.rb', line 435

def getnode(key)
  raise "key is nil." unless key != nil 
  p = @root;
  while p != nil
    cmp = compare_key(key, p.key)
    if cmp < 0
      p = p.left
    elsif cmp > 0
      p = p.right
    else
      return p
    end
  end
  return nil
end

#higher_key(key) ⇒ Object



353
354
355
356
357
358
359
360
# File 'lib/xunch/utils/rb_tree.rb', line 353

def higher_key(key)
  node = higher_node(key)
  if node == nil
    return nil
  else
    return node.key
  end
end

#higher_node(key) ⇒ Object



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
# File 'lib/xunch/utils/rb_tree.rb', line 317

def higher_node(key)
  p = @root;
  while p != nil
    cmp = compare_key(key, p.key)
    if cmp < 0
      if p.left != nil
        p = p.left
      else
        if p == nil
          return nil
        else
          return RBTree::ImmutableNode.new(p.key,p.value)
        end
      end
    else
      if p.right != nil
        p = p.right
      else
        parent = p.parent
        ch = p
        while parent != nil && ch == parent.right
          ch = parent
          parent = parent.parent;
        end
        if parent == nil
          return nil
        else
          return RBTree::ImmutableNode.new(parent.key,parent.value)
        end
      end
    end
  end

  return nil
end

#higher_value(key) ⇒ Object



362
363
364
365
366
367
368
369
# File 'lib/xunch/utils/rb_tree.rb', line 362

def higher_value(key)
  node = higher_node(key)
  if node == nil
    return nil
  else
    return node.value
  end
end

#last_keyObject



124
125
126
127
128
129
130
131
132
# File 'lib/xunch/utils/rb_tree.rb', line 124

def last_key
  p = @root;
  if p != nil
    while p.right != nil
      p = p.right
    end
  end
  p.key
end

#last_nodeObject



114
115
116
117
118
119
120
121
122
# File 'lib/xunch/utils/rb_tree.rb', line 114

def last_node
  p = @root;
  if p != nil
    while p.right != nil
      p = p.right
    end
  end
  RBTree::ImmutableNode.new(p.key,p.value)
end

#last_valueObject



134
135
136
137
138
139
140
141
142
# File 'lib/xunch/utils/rb_tree.rb', line 134

def last_value
  p = @root;
  if p != nil
    while p.right != nil
      p = p.right
    end
  end
  p.value
end

#leftof(node) ⇒ Object



596
597
598
# File 'lib/xunch/utils/rb_tree.rb', line 596

def leftof(node)
  return (node == nil ? nil : node.left)
end

#lower_key(key) ⇒ Object



239
240
241
242
243
244
245
246
# File 'lib/xunch/utils/rb_tree.rb', line 239

def lower_key(key)
  node = lower_node(key)
  if node == nil
    return nil
  else
    return node.key
  end
end

#lower_node(key) ⇒ Object



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
# File 'lib/xunch/utils/rb_tree.rb', line 204

def lower_node(key)
  p = @root;
  while p != nil
    cmp = compare_key(key, p.key)
    if cmp > 0
      if p.right != nil
        p = p.right
      else
        if p == nil
          return nil
        else
          return RBTree::ImmutableNode.new(p.key,p.value)
        end
      end
    else
      if p.left != nil
        p = p.left
      else
        parent = p.parent
        ch = p
        while parent != nil && ch == parent.left
          ch = parent
          parent = parent.parent;
        end
        if parent == nil
          return nil
        else
          return RBTree::ImmutableNode.new(parent.key,parent.value)
        end
      end
    end
  end
  return nil
end

#lower_value(key) ⇒ Object



248
249
250
251
252
253
254
255
# File 'lib/xunch/utils/rb_tree.rb', line 248

def lower_value(key)
  node = lower_node(key)
  if node == nil
    return nil
  else
    return node.value
  end
end

#parentof(node) ⇒ Object



592
593
594
# File 'lib/xunch/utils/rb_tree.rb', line 592

def parentof(node)
  return (node == nil ? nil : node.parent)
end

#put(key, value) ⇒ Object



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
# File 'lib/xunch/utils/rb_tree.rb', line 21

def put(key,value)
  tmp_node = @root
  if(tmp_node == nil)
    compare_key(key,key)
    @root = RBTree::Node.new(key,value,nil)
    @size = 1
    return value
  end
  cmp = 0
  parent = nil

  begin
    parent = tmp_node
    cmp = compare_key(key,parent.key)
    if cmp < 0
      tmp_node = tmp_node.left;
    elsif cmp > 0
      tmp_node = tmp_node.right;
    else
      return tmp_node.value = value;
    end
  end while tmp_node != nil

  node = RBTree::Node.new(key,value,parent)

  if cmp < 0
    parent.left = node
  else
    parent.right = node
  end
  
  fixafterinsertion(node)    
  @size+=1

  return value
  
end

#remove(key) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/xunch/utils/rb_tree.rb', line 59

def remove(key)
  p = getnode(key)
  if (p == nil)
    return nil
  end

  oldValue = p.value
  deletenode(p)
  return oldValue
end

#rightof(node) ⇒ Object



600
601
602
# File 'lib/xunch/utils/rb_tree.rb', line 600

def rightof(node)
  return (node == nil ? nil : node.right)
end

#rotateleft(p) ⇒ Object



552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# File 'lib/xunch/utils/rb_tree.rb', line 552

def rotateleft(p)
  if p != nil
    r = p.right
    p.right = r.left
    if r.left != nil
      r.left.parent = p
    end
    r.parent = p.parent
    if p.parent == nil
      @root = r
    elsif p.parent.left == p
      p.parent.left = r
    else
      p.parent.right = r
    end
    r.left = p
    p.parent = r
  end
end

#rotateright(p) ⇒ Object



572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
# File 'lib/xunch/utils/rb_tree.rb', line 572

def rotateright(p)
  if p != nil
    l = p.left
    p.left = l.right
    if l.right != nil
      l.right.parent = p
    end
    l.parent = p.parent
    if p.parent == nil
      @root = l
    elsif p.parent.right == p
      p.parent.right = l
    else 
      p.parent.left = l
    end
    l.right = p
    p.parent = l
  end
end

#setcolor(node, color) ⇒ Object



610
611
612
613
614
# File 'lib/xunch/utils/rb_tree.rb', line 610

def setcolor(node, color)
  if node != nil
    node.color = color
  end 
end

#successor(t) ⇒ Object



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/xunch/utils/rb_tree.rb', line 371

def successor(t)
  if t == nil
    return nil
  elsif t.right != nil
    p = t.right
    while p.left != nil
        p = p.left
    end
    return p
  else
    p = t.parent
    ch = t
    while p != nil && ch == p.right
      ch = p
      p = p.parent
    end
    return p
  end
end