-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbnode_mod.f90
1668 lines (1369 loc) · 52.6 KB
/
rbnode_mod.f90
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
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
509
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
! Red-black tree implementation
! -----------------------------------------------------
! Algorithms based on this Wikipedia article:
! https://en.wikipedia.org/wiki/Red%E2%80%93black_tree
!
module rbnode_mod
!*
!
!
use common_mod, only : DATA_KIND, mold, compare_fun, get_node_label_fun
use iso_fortran_env, only : int8
implicit none
private
type, public :: rbnode_t
!! Red-black tree node
private
integer(kind=DATA_KIND), allocatable :: dat(:)
logical(int8) :: isblack = .false.
type(rbnode_t), pointer :: left => null()
type(rbnode_t), pointer :: right => null()
type(rbnode_t), pointer :: parent => null()
contains
procedure :: is_node_black
procedure :: leftnode, rightnode, upnode
procedure :: nextnode => nextnode_tbp
end type rbnode_t
interface rbnode_t
module procedure rbnode_new, rbnode_newroot
!module procedure rbnode_import
!module procedure rbnode_copy
end interface
type, public :: rbbasetree_t
type(rbnode_t), pointer :: root => null()
contains
procedure :: isvalid => rbbasetree_isvalid
procedure :: blackheight => rbbasetree_blackheight
procedure :: size => rbbasetree_size
procedure :: graphviz => rbbasetree_graphviz
procedure :: leftmost => rbbasetree_leftmost
end type rbbasetree_t
integer, parameter :: LEFT_CHILD=1, RIGHT_CHILD=2, NO_PARENT=0
logical(int8), parameter :: RED_COLOUR=.false.,BLACK_COLOUR=.true.
integer, parameter :: MAX_SAFE_DEPTH=1000000
!! Defensive test to avoid infinite loops in the code
public rbnode_update, rbnode_read, rbnode_free
public rbnode_find
public rbnode_leftmost, rbnode_nextnode, rbnode_prevnode
public rbnode_insert, rbnode_delete
public rbnode_validate, rbnode_blackheight
public join, split, union
public join2, split2
integer, public, save :: allocation_counter = 0
!! temporary, just for mem.leakage debuging TODO
contains
! ==============
! Simple getters
! ==============
logical function is_node_black(this)
class(rbnode_t), intent(in) :: this
is_node_black = this%isblack
end function
function leftnode(this) result(ln)
class(rbnode_t), intent(in) :: this
type(rbnode_t), pointer :: ln
ln => this%left
end function
function rightnode(this) result(rn)
class(rbnode_t), intent(in) :: this
type(rbnode_t), pointer :: rn
rn => this%right
end function
function upnode(this) result(un)
class(rbnode_t), intent(in) :: this
type(rbnode_t), pointer :: un
un => this%parent
end function
function nextnode_tbp(this) result(nn)
class(rbnode_t), intent(in), target :: this
type(rbnode_t), pointer :: nn
nn => this
nn => rbnode_nextnode(nn)
end function
! TODO prevnode, leftmost?, rightmost?
! ===========================
! Rbbasetree_t basic routines
! ===========================
function rbbasetree_leftmost(this) result(ln)
class(rbbasetree_t), intent(in) :: this
type(rbnode_t), pointer :: ln
ln => null()
if (associated(this%root)) ln => rbnode_leftmost(this%root)
end function
! ================================
! Allocate new node (CONSTRUCTOR)
! Update node data (???)
! Read data from node
! Deallocate node
! ================================
function rbnode_new(dat) result(new)
!! Allocate new node, fill it with data, result pointer
integer(DATA_KIND), intent(in) :: dat(:)
type(rbnode_t), pointer :: new
integer :: ierr
allocation_counter=allocation_counter+1
allocate(new, stat=ierr)
if (ierr /= 0) &
error stop 'could not allocate new rbtr node'
allocate(new%dat(size(dat)), stat=ierr)
if (ierr /= 0) &
error stop 'could not allocate data in new rbtr node'
new%dat = dat
new%left => null()
new%right => null()
new%parent => null()
end function rbnode_new
function rbnode_newroot(left, dat, colour, right) result(new)
!! Allocate new node, fill it with given data and colour, and make
!! it a root of two given sub-trees
type(rbnode_t), intent(in), pointer :: left, right
integer(DATA_KIND), intent(in) :: dat(:)
logical(int8), intent(in) :: colour
type(rbnode_t), pointer :: new
integer :: ierr
allocation_counter=allocation_counter+1
allocate(new, stat=ierr)
if (ierr /= 0) &
error stop 'could not allocate new rbtr node'
allocate(new%dat(size(dat)), stat=ierr)
if (ierr /= 0) &
error stop 'could not allocate data in new rbtr node'
new%dat = dat
new%isblack = colour
new%parent => null()
new%left => left
if (associated(new%left)) new%left%parent => new
new%right => right
if (associated(new%right)) new%right%parent => new
end function rbnode_newroot
subroutine rbnode_update(node, newdata)
!! Update the data content of the node by newdata
!! TODO may invalidate the red-black tree!!!
type(rbnode_t), intent(in), pointer :: node
integer(DATA_KIND), intent(in) :: newdata(:)
integer :: ierr
if (.not. associated(node)) &
error stop 'could not update data: node is null'
if (allocated(node%dat)) then
ierr = 0
if (size(newdata) /= size(node%dat)) deallocate(node%dat, stat=ierr)
if (ierr /= 0) &
error stop 'could not deallocate data during update'
end if
ierr = 0
if (.not. allocated(node%dat)) allocate(node%dat(size(newdata)), stat=ierr)
if (ierr /= 0) &
error stop 'could not allocate data during update'
node%dat = newdata
end subroutine rbnode_update
function rbnode_read(node) result(dat)
!! Return node data
type(rbnode_t), intent(in), pointer :: node
integer(DATA_KIND), allocatable :: dat(:)
if (.not. associated(node)) &
error stop 'could not read: node is null'
if (.not. allocated(node%dat)) &
error stop 'could not read: node contains no data'
allocate(dat(size(node%dat)))
dat = node%dat
end function rbnode_read
subroutine rbnode_free(deleted)
!! Deallocae the node from memory
type(rbnode_t), pointer, intent(inout) :: deleted
integer :: ierr
if (.not. associated(deleted)) &
error stop 'could not free node: null pointer'
ierr = 0
if (allocated(deleted%dat)) deallocate(deleted%dat, stat=ierr)
if (ierr /= 0) &
error stop 'could not free node: data deallocation failed'
deallocate(deleted, stat=ierr)
if (ierr /= 0) &
error stop 'could not free node: node deallocation failed'
allocation_counter=allocation_counter-1
end subroutine rbnode_free
! =================================================
! Jump inside tree (grandparent, sibling, uncle)
! Tree rotations
! In-order traversal (nextnode, prevnode)
! =================================================
function rbnode_whichchild(node) result(we)
type(rbnode_t), intent(in), pointer :: node
integer :: we
if (.not. associated(node)) &
error stop 'whichchild: null pointer as input'
if (associated(node%parent)) then
if (associated(node, node%parent%left)) then
we = LEFT_CHILD
else if (associated(node, node%parent%right)) then
we = RIGHT_CHILD
else
error stop 'whichchild: parent not related to node!'
end if
else
we = NO_PARENT
end if
end function rbnode_whichchild
function rbnode_isblack(node) result(isblack)
!! Allow to query also null nodes (they are assumed black)
type(rbnode_t), pointer, intent(in) :: node
logical :: isblack
isblack = .true.
if (associated(node)) isblack = node%isblack
end function rbnode_isblack
function rbnode_grandparent(node) result(grandparent)
type(rbnode_t), intent(in), pointer :: node
type(rbnode_t), pointer :: grandparent
if (.not. associated(node)) &
error stop 'grandparent: null node as input'
if (associated(node%parent)) then
grandparent => node%parent%parent
else
grandparent => null()
end if
end function rbnode_grandparent
function rbnode_sibling(node, node_is_which) result(sibling)
type(rbnode_t), intent(in), pointer :: node
integer, intent(out), optional :: node_is_which
type(rbnode_t), pointer :: sibling
if (.not. associated(node)) &
error stop 'sibling: null node as input'
associate (we => rbnode_whichchild(node))
select case(we)
case(LEFT_CHILD)
sibling => node%parent%right
case(RIGHT_CHILD)
sibling => node%parent%left
case(NO_PARENT)
sibling => null()
case default
error stop 'sibling: invalid output from which_child'
end select
if (present(node_is_which)) node_is_which = we
end associate
end function rbnode_sibling
function rbnode_uncle(node, parent_is_which) result(uncle)
type(rbnode_t), intent(in), pointer :: node
integer, intent(out), optional :: parent_is_which
type(rbnode_t), pointer :: uncle
if (.not. associated(node)) &
error stop 'uncle: null node as input'
if (associated(node%parent)) then
uncle => rbnode_sibling(node%parent, parent_is_which)
else
uncle => null()
if (present(parent_is_which)) parent_is_which=NO_PARENT
end if
end function rbnode_uncle
subroutine rehang_tree(oldchild, newchild, tree)
type(rbnode_t), pointer, intent(in) :: oldchild, newchild
type(rbbasetree_t), intent(inout) :: tree
type(rbnode_t), pointer :: parent
! Old child (pivot) exchanged place with new child (rotator) during the
! rotation. Here, we repair the parent->child link.
! The other way link (child->parent) should have been updates in
! rotate_...()
parent => newchild%parent
if (associated(parent)) then
! Assert old child was really one of parents children
if (associated(parent%left, oldchild)) then
parent%left => newchild
else if (associated(parent%right, oldchild)) then
parent%right => newchild
else
error stop 'rehang_tree - old child not recognized by parent'
end if
else
! Pivot was root, and did not have any parent. Root-pointer must
! be repaired instead
if (.not. associated(tree%root, oldchild)) &
error stop 'rehang_tree - unexpected association of root pointer'
tree%root => newchild
end if
end subroutine rehang_tree
function rotate_left(piv, tree) result(rot)
!! Rotate left.
!! Fails if pivot is leaf or if pivot's right child (rotator) is leaf
!! Remember: pivot's parent must be relinked to rotator outside!
type(rbnode_t), intent(in), pointer :: piv
type(rbbasetree_t), intent(inout) :: tree
type(rbnode_t), pointer :: rot
if (.not. associated(piv)) &
error stop 'rotate_left: pivot is null'
rot => piv%right
if (.not. associated(rot)) &
error stop 'rotate_left: rotator is null'
! rotator is the right child of pivot, it will become the new
! root of the sub-tree after the rotation
if (.not. associated(rot%parent,piv)) &
error stop 'rotate_left: rotator is not linked to pivot'
rot%parent => piv%parent
piv%parent => rot
! pivot becomes rotator's new left child
! rotator's old left child node transfered as the pivot's right child
piv%right => rot%left
if (associated(piv%right)) then
if (.not. associated(piv%right%parent,rot)) &
error stop 'rotate_left: rotator''s left child not linked to rotator'
piv%right%parent => piv
end if
rot%left => piv
call rehang_tree(piv, rot, tree)
end function rotate_left
function rotate_right(piv, tree) result(rot)
!! Rotate right.
!! Fails if pivot is leaf or if pivot's left child (rotator) is leaf
!! Remember: pivot's parent must be relinked to rotator outside!
type(rbnode_t), intent(in), pointer :: piv
type(rbbasetree_t), intent(inout) :: tree
type(rbnode_t), pointer :: rot
if (.not. associated(piv)) &
error stop 'rotate_right: pivot is null'
rot => piv%left
if (.not. associated(rot)) &
error stop 'rotate_right: rotator is null'
! rotator is the left child of pivot, it will become the new
! root of the sub-tree after the rotation
if (.not. associated(rot%parent,piv)) &
error stop 'rotate_right: rotator is not linked to pivot'
rot%parent => piv%parent
piv%parent => rot
! pivot becomes rotator's new right child
! rotator's old right child node transfered as the pivot's left child
piv%left => rot%right
if (associated(piv%left)) then
if (.not. associated(piv%left%parent,rot)) &
error stop 'rotate_right: rotator''s right child not linked to rotator'
piv%left%parent => piv
end if
rot%right => piv
call rehang_tree(piv, rot, tree)
end function rotate_right
function rbnode_leftmost(node) result(leftmost)
type(rbnode_t), intent(in), pointer :: node
type(rbnode_t), pointer :: leftmost
integer :: i
if (.not. associated(node)) &
error stop 'leftmost: null node as input'
leftmost => node
do i=1,MAX_SAFE_DEPTH
if (.not. associated(leftmost%left)) exit
leftmost => leftmost%left
end do
if (i==MAX_SAFE_DEPTH+1) &
error stop 'leftmost: MAX_SAFE_DEPTH reached, increase it if this is not error'
end function rbnode_leftmost
function rbnode_rightmost(node) result(rightmost)
type(rbnode_t), intent(in), pointer :: node
type(rbnode_t), pointer :: rightmost
integer :: i
if (.not. associated(node)) &
error stop 'rightmost: null node as input'
rightmost => node
do i=1,MAX_SAFE_DEPTH
if (.not. associated(rightmost%right)) exit
rightmost => rightmost%right
end do
if (i==MAX_SAFE_DEPTH+1) &
error stop 'rightmost: MAX_SAFE_DEPTH reached, increase it if this is not error'
end function rbnode_rightmost
function rbnode_nextnode(node) result(successor)
!! Return next node, or null pointer if node is the last node
type(rbnode_t), intent(in), pointer :: node
type(rbnode_t), pointer :: successor
type(rbnode_t), pointer :: child
integer :: i
if (.not. associated(node)) &
error stop 'nextnode: input is a null node'
! If node has a right sub-tree, the next node is the leftmost node in this
! sub-tree
if (associated(node%right)) then
successor => rbnode_leftmost(node%right)
return
end if
! Otherwise, the next node is the first parent of a left child that is
! encountered when traversing up the tree. If no such parent exists, it
! means that the node is the last node
child => node
do i=1,MAX_SAFE_DEPTH
successor => child%parent
! no more parents, return null
if (.not. associated(successor)) exit
select case(rbnode_whichchild(child))
case(LEFT_CHILD)
! parent of a left child is the next node and is returned
exit
case(RIGHT_CHILD)
! parent of a right child is not the next node, but its
! grand-parent could be...
child => child%parent
case default
error stop 'nextnode: node has no parent and this is not expected'
end select
end do
if (i==MAX_SAFE_DEPTH+1) &
error stop 'nextnode: MAX_SAFE_DEPTH reached, increase it if this is not error'
end function rbnode_nextnode
function rbnode_prevnode(node) result(predecessor)
!! Return prev node, or null pointer if node is the first node
type(rbnode_t), intent(in), pointer :: node
type(rbnode_t), pointer :: predecessor
type(rbnode_t), pointer :: child
integer :: i
if (.not. associated(node)) &
error stop 'prevnode: input is a null node'
! If node has a left sub-tree, the next node is the rightmost node in this
! sub-tree
if (associated(node%left)) then
predecessor => rbnode_rightmost(node%left)
return
end if
! Otherwise, the prev node is the first parent of a right child that is
! encountered when traversing up the tree. If no such parent exists, it
! means that the node is the first node
child => node
do i=1,MAX_SAFE_DEPTH
predecessor => child%parent
! no more parents, return null
if (.not. associated(predecessor)) exit
select case(rbnode_whichchild(child))
case(RIGHT_CHILD)
! parent of a right child is the prev node and is returned
exit
case(LEFT_CHILD)
! parent of a left child is not the prev node, but its
! grand-parent could be...
child => child%parent
case default
error stop 'prevnode: node has no parent and this is not expected'
end select
end do
if (i==MAX_SAFE_DEPTH+1) &
error stop 'prevnode: MAX_SAFE_DEPTH reached, increase it if this is not error'
end function rbnode_prevnode
function rbnode_find(start, val, cfun) result(found)
type(rbnode_t), intent(in), pointer :: start
integer(DATA_KIND), intent(in) :: val(:)
procedure(compare_fun) :: cfun
type(rbnode_t), pointer :: found
type(rbnode_t), pointer :: finger
integer :: i
found => null()
finger => start
do i=1, MAX_SAFE_DEPTH
if (.not. associated(finger)) exit
select case(cfun(val, finger%dat))
case(-1) ! val < finger
finger => finger%left
case(+1) ! val > finger
finger => finger%right
case(0) ! val == finger
found => finger
exit
case default
error stop 'find: invalid value returned from user function'
end select
end do
if (i==MAX_SAFE_DEPTH+1) &
error stop 'find: MAX_SAFE_DEPTH reached, incr. it if this is not error'
end function rbnode_find
! =========
! Insertion
! =========
subroutine rbnode_insert(tree, new, cfun, ierr, new_output)
!* Insert a new node to the tree.
! Optional pointer `new_output` points to the inserted node
! and `ierr=0` if insertion was sucessful.
!
! If a duplicate is in the tree, there are three posibililties the
! error is handled:
! - Error stop if `ierr` was not provided
! - New node is freed here if `output` was not provided, `ierr=2` returned.
! - User is responsible for freeing node via `output`, `ierr=1` returned.
!
type(rbbasetree_t), intent(inout) :: tree
type(rbnode_t), intent(in), pointer :: new
procedure(compare_fun) :: cfun
!! A < B -> -1, A == B -> 0, A > B -> 1
integer, intent(out), optional :: ierr
!! on exit: 0...insertion ok
!! 1...duplicate in tree, node not inserted
!! 2...duplicate in tree, node was deallocated automatically
type(rbnode_t), pointer, optional :: new_output
!! pointer to the new node so it can be deallocated by user, if inserion
!! failed
integer, parameter :: FLAG_OK=0, FLAG_DUPLICATE=1, FLAG_DUPLICATE_FREED=2
integer :: i, ierr0, which_child
type(rbnode_t), pointer :: new_local, finger
! assert the new node is isolated
if (.not. associated(new)) &
error stop 'insert: new node is null node'
if (associated(new%parent) .or. associated(new%left) .or. associated(new%right))&
error stop 'insert: new node must be alone'
new_local => new
if (present(new_output)) new_output => new
ierr0 = FLAG_OK
if (.not. associated(tree%root)) then
! insert a first node to an empty tree
tree%root => new_local
which_child = NO_PARENT
else
! find place where new node will be inserted
finger => tree%root
DOWN_LOOP: do i=1, MAX_SAFE_DEPTH
select case(cfun(new_local%dat, finger%dat))
case(-1) ! new < root
if (associated(finger%left)) then
finger => finger%left
else
which_child = LEFT_CHILD
exit DOWN_LOOP
end if
case(+1) ! new > root
if (associated(finger%right)) then
finger => finger%right
else
which_child = RIGHT_CHILD
exit DOWN_LOOP
end if
case(0) ! new == root
ierr0 = FLAG_DUPLICATE
exit DOWN_LOOP
case default
error stop 'insert: invalid value returned from user function'
end select
end do DOWN_LOOP
if (i==MAX_SAFE_DEPTH+1) &
error stop 'insert: MAX_SAFE_DEPTH reached, increase it if this is not error'
end if
! Insert node or handle duplicates error
if (ierr0 == FLAG_OK) then
if (which_child == LEFT_CHILD) then
finger%left => new_local
else if (which_child == RIGHT_CHILD) then
finger%right => new_local
end if
if (which_child /= NO_PARENT) new_local%parent => finger
else if (ierr0 == FLAG_DUPLICATE) then
if (.not. present(ierr)) then
! Panic if `ierr` is not provided
error stop 'insert: not sucessfull as the same node is already in tree'
else if (.not. present(new_output)) then
! try to deallocate new node and silently return
call rbnode_free(new_local)
ierr0 = FLAG_DUPLICATE_FREED
end if
else
error stop 'insert: unreachable branch'
end if
if (present(ierr)) ierr = ierr0
! Repair red-black tree
if (ierr0==FLAG_OK) then
new_local%isblack = .false. ! inserted node is red
call insert_repair(new_local, tree)
end if
end subroutine rbnode_insert
recursive subroutine insert_repair(n, tree)
type(rbnode_t), intent(inout), pointer :: n
type(rbbasetree_t), intent(inout) :: tree
type(rbnode_t), pointer :: p, u, g, tmp
logical :: uncle_exists, uncle_is_red
integer :: whichchild_p, whichchild_n
if (.not. associated(n)) &
error stop 'insert_repair: n is null node'
p => n%parent
MAIN: if (.not. associated(p)) then
! CASE I - `n` is root, root should be black
!Allow red roots? TODO
!n%isblack = .true.
if (.not. associated(tree%root,n)) &
error stop 'node n seems to be root, but root points elsewhere'
else if (p%isblack) then MAIN
! CASE II - nothing has to be done
continue
else MAIN
! parent is red
u => rbnode_uncle(n, whichchild_p)
g => rbnode_grandparent(n)
uncle_exists = associated(u)
uncle_is_red = .false.
if (uncle_exists) uncle_is_red = .not. u%isblack
if (uncle_exists .and. uncle_is_red) then
! CASE III - repaint parent and uncle black and repair grandparent
p%isblack = .true.
u%isblack = .true.
g%isblack = .false.
call insert_repair(g, tree)
else
! CASE IV - parent is red and uncle is black
whichchild_n = rbnode_whichchild(n)
! case IV, step 1 (move n to the outer-side of sub-tree if needed)
if (whichchild_n==RIGHT_CHILD .and. whichchild_p==LEFT_CHILD) then
tmp => rotate_left(p, tree)
n => p
else if (whichchild_n==LEFT_CHILD .and. whichchild_p==RIGHT_CHILD) then
tmp => rotate_right(p, tree)
n => p
end if
! case IV, step 2
p => n%parent
g => rbnode_grandparent(n)
if (whichchild_p==LEFT_CHILD) then
tmp => rotate_right(g, tree)
else if (whichchild_p==RIGHT_CHILD) then
tmp => rotate_left(g, tree)
else if (whichchild_p==NO_PARENT) then
! parent is red-root, it can be always relabeled
p%isblack = BLACK_COLOUR
return
else
error stop 'case IV, part 2, unreachable branch'
end if
p%isblack = .true.
g%isblack = .false.
end if
end if MAIN
end subroutine insert_repair
! ========
! Deletion
! ========
!TODO - error treatment in rbnode_delete is not finished
subroutine rbnode_delete(tree, what, ierr, deleted_output)
!* Remove (and maybe free) a node from the tree.
! Optional pointer `deleted_output` points to the deleted node
! in the case user wants to take responsibility for freeing the node.
! If `deleted_output` is not provided, the node is freed here.
!
class(rbbasetree_t), intent(inout) :: tree
type(rbnode_t), pointer, intent(in) :: what
integer, optional, intent(out) :: ierr
type(rbnode_t), pointer, intent(out), optional :: deleted_output
integer :: ierr0
type(rbnode_t), pointer :: n, ch
integer(kind=DATA_KIND), allocatable :: tmp_dat(:)
if (.not. associated(what)) &
error stop 'delete: what is null node'
! TODO for now we panic if the deleted node does not exist
! later allow to exit quietly?
n => what
! CASE I: N has two children
! * find the successor node, move content of that node to the current
! node to be deleted and then delete the successor node
! * continue to the CASE II (one or zero children)
if (associated(n%left) .and. associated(n%right)) then
ch => rbnode_leftmost(n%right)
if (present(deleted_output)) then
! swap n%dat and ch%dat
call move_alloc(n%dat, tmp_dat)
call move_alloc(ch%dat, n%dat)
call move_alloc(tmp_dat, ch%dat)
else
! just move ch%dat, n%dat can be discarded
call move_alloc(ch%dat, n%dat)
end if
n => ch
else
continue
endif
! CASE II: N has one or zero children:
! * If N is red, both its children must be leafs.
! Node can be removed without violating red-black properties.
! * If N is black and its child CH is red, then N can be replaced by
! CH, CH is relabeled black and we are done.
! * If N is black with no children, removing N will break the
! red-black tree properties and it must be rebalanced in
! "delete_case1" subroutines.
if (associated(n%left)) then
ch => n%left
elseif (associated(n%right)) then
ch => n%right
else
ch => null()
endif
if (.not. associated(ch)) then
! N has no children
if (n%isblack) call delete_case1(tree, n)
! N is red
if (.not. associated(n%parent)) then
! N was root
tree%root => null()
elseif (rbnode_whichchild(n)==LEFT_CHILD) then
n%parent%left => null()
elseif (rbnode_whichchild(n)==RIGHT_CHILD) then
n%parent%right => null()
else
error stop 'Delete: impossible branch'
endif
else
! N has one child (N must be black and CH must be red)
ch % parent => n % parent
if (.not. associated(n%parent)) then
! N was root, CH is new root
tree%root => ch
elseif (rbnode_whichchild(n)==LEFT_CHILD) then
n%parent%left => ch
elseif (rbnode_whichchild(n)==RIGHT_CHILD) then
n%parent%right => ch
else
error stop 'Delete: impossible branch2'
endif
! Assert N is black and CH is red
!if (.not. n%isblack .and. .not. ch%isblack) then
if (.not. (n%isblack .and. .not. ch%isblack)) then
error stop "delete: assertion failed"
endif
ch%isblack = .true.
endif
! Now N can be deallocated
if (present(deleted_output)) then
deleted_output => n
else
call rbnode_free(n)
end if
end subroutine rbnode_delete
recursive subroutine delete_case1(tree, m)
class(rbbasetree_t), intent(inout) :: tree
type(rbnode_t), pointer, intent(in) :: m
!
! M is a black node without children.
! If M is the new root, nothing needs to be done.
! Otherwise proceed to case 2.
!
if (associated(m%parent)) call delete_case2(tree, m)
end subroutine delete_case1
recursive subroutine delete_case2(tree, m)
class(rbbasetree_t), intent(inout) :: tree
type(rbnode_t), pointer, intent(in) :: m
!
! If S is red:
! * make S black, make P red and
! * rotate left/right around P so S will become grandparent of M
!
type(rbnode_t), pointer :: s, p, tmp
s => rbnode_sibling(m)
p => m%parent
if (.not. rbnode_isblack(s)) then
p%isblack = .false. ! red_node
s%isblack = .true. ! black_node
if (rbnode_whichchild(m)==LEFT_CHILD) then
tmp => rotate_left(p, tree)
else
tmp => rotate_right(p, tree)
endif
endif
call delete_case34(tree, m)
end subroutine delete_case2
recursive subroutine delete_case34(tree, m)
class(rbbasetree_t), intent(inout) :: tree
type(rbnode_t), pointer, intent(in) :: m
!
! If S, Sleft, Sright and P are black then
! * repaint S red: this compensates the deleted black node in S' subtree
! but the whole P->M and P->S sub-trees are one black node
! less than the remaining branches, therefore ...
! * rebalance up-level: use delete_case1 on P
!
! If S, Sleft and Sright are black but P is red then
! * exchange color of S and P and we are done
!
! Otherwise proceed to delete_case5
!
type(rbnode_t), pointer :: s, p
s => rbnode_sibling(m)
p => m%parent
! assert that sibling is not leaf
if (.not. associated(s)) &
& error stop "delete_case34: defensive check, sibling is a leaf:"
if (p%isblack .and. s%isblack .and. &
& rbnode_isblack(s%left) .and. rbnode_isblack(s%right)) then
s%isblack = .false. ! red_node
call delete_case1(tree, p)
elseif ( .not. p%isblack .and. s%isblack .and. &
& rbnode_isblack(s%left) .and. rbnode_isblack(s%right)) then
s%isblack = .false. ! red_node
p%isblack = .true. ! black_node
else
call delete_case5(tree, m)
endif
end subroutine delete_case34
subroutine delete_case5(tree, m)
class(rbbasetree_t), intent(inout) :: tree
type(rbnode_t), pointer, intent(in) :: m
!
! S is black, S left is red, S right is black and M is the left child
! * rotate right at S so S left is new sibling of M
! * exchange colors of S and its ne parent (it was S left)
!
! Mirrored situation
! S is black, S right is red, S left is black and M is the right child
! * rotate left at S so S right is new sibling of M
! * exchange colort of S and its new parent (it was S right)
!
! At the enf M should have black sibling with red children on the
! outside of the tree and this falls into case 6
!
type(rbnode_t), pointer :: s, tmp
s => rbnode_sibling(m)
! assert that sibling is not leaf
if (.not. associated(s)) &
& error stop "delete_case5: defensive check, sibling is a leaf:"
! assert that sibling is black
if (.not. s%isblack) &
& error stop "delete_case5: sibling is red"
if (rbnode_whichchild(m)==LEFT_CHILD .and. rbnode_isblack(s%right)) then
! assert S left is red
if (rbnode_isblack(s%left)) error stop "delete_case5: assert1"
s%isblack = .false. ! red_node
s%left%isblack = .true. ! black_node
tmp => rotate_right(s, tree)
elseif (rbnode_whichchild(m)==RIGHT_CHILD .and. rbnode_isblack(s%left)) then
! assert S right is red
if (rbnode_isblack(s%right)) error stop "delete_case5: assert2"
s%isblack = .false. ! red_node
s%right%isblack = .true. ! black_node
tmp => rotate_left(s, tree)
endif
call delete_case6(tree, m)
end subroutine delete_case5
subroutine delete_case6(tree, m)
class(rbbasetree_t), intent(inout) :: tree