-
Notifications
You must be signed in to change notification settings - Fork 1
/
adt23tree_impl.i
2174 lines (1938 loc) · 62.7 KB
/
adt23tree_impl.i
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
{@discard
This file is a part of the PascalAdt library, which provides
commonly used algorithms and data structures for the FPC and Delphi
compilers.
Copyright (C) 2004, 2005 by Lukasz Czajka
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA }
{@discard
adt23tree_impl.i::prefix=&_mcp_prefix&::item_type=&ItemType&
}
&include adt23tree.defs
&include adt23tree_impl.mcp
{$R+}
{ ========================================================================== }
{ Notes on the implementation of T23Tree }
{ -------------------------------------------------------------------------- }
{ T23Tree is implemented as a variant of a 23-tree with items stored
in internal nodes. In ordinary 23-trees items are stored in leaves
and in internal nodes only keys are stored, which enables fast
lookup. Each internal node has two or three children and all leaves
are on the same level. The keys of the smallest items in the 2nd and
3rd sub-tree are stored in each internal node. T23Tree
implementation differs in the way that instead of these keys whole
items are stored, and they are not present in the sub-tree to which
they belong. Therefore, no leaves exist physically. The nodes which
are logically parents of leaves contain in their LowItem fields the
items that should be stored in the leaves, and the Child fields are
all set to nil. The item normally stored in the left-most leaf is
stored in the LowestItem field. This field should be treated as a
node which is the parent of the root. This way, the height of a tree
consisting only of the LowestItem and the root node is 1, whereas
the height of a tree consisting only of the LowestItem node is 0
(this is important in the implementation of the Concatenate and
Split operations). When I mention the existence of some entity I
usually mean its physical existence within the internal structure
used to represent the 23-tree. I also sometimes talk about logical
existence, which should be understood as the existence of an entity
within the logical, implementation-independent structure of a
23-tree (i.e. the keys in iternal nodes, items in leaves, ...). }
{ An example T23Tree graphically: }
{
+----------------+
| LowestItem = 0 |
+----------------+
|
|
+----+----+
+---| 5 | 10 |
| +----+----+
1| | | 3
| 2| +------------+
| | |
+----+----+ +----+----+ +----+----+
+-------| 3 | nil| +-| 7 | nil| +-| 13 | nil|
| +----+----+ | +----+----+ | +----+----+ 2
1| | | | 2 1| +--------------+
| 2| 1| +------+ +-------+ |
| | | | | |
+----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+
| 1 | 2 | | 4 | nil| | 6 | nil| | 8 | 9 | | 11 | 12 | | 14 | 15 |
+----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+
}
{ Boxes represent the nodes. The numbers in the boxes represent
LowItem[2] (the first number) and LowItem[3] (the second
number). The links represent the parentship relation. The number
beside each link tells which child of its parent is the node at the
bottom of the link (i.e. if this number is n then the node is
pointed by (node's parent)^.Child[n]). Note that the nodes at the
lowest level have no physical children, although they are logically
the parents of leaves. They don't need to have children as the items
normally stored in the leaves are stored either in their LowItem[]'s
or in the LowItem[]'s of some higher nodes. }
{ How are repeated items managed? }
{ When one node contains some item in LowItem[n], but not in
LowItem[n-1], an equal item may be also stored in LowItem[n+1], the
sub-tree of Child[n], the sub-tree of Child[n-1] and also the
sub-tree of Child[n+1] if LowItem[n+1] contains an equal item. }
{ How is a position within a 23-tree represented ? }
{ The position of an item is represented by a pair (node,low), where
node is the pointer to the node which has the appropriate LowItem
and low is the number of this LowItem (2 or 3). The end of range is
represented by the (nil,0) pair. The first item (LowestItem) is
represented by the (nil,1) pair. }
{ ========================================================================== }
{ ------------------ test routines ------------------------ }
function NodeConsistent(node : P23TreeNode) : Boolean;
begin
with node^ do
begin
Assert((Child[1] <> nil) or ((Child[2] = nil) and (Child[3] = nil)));
Assert((Child[2] <> nil) or (Child[3] = nil));
Result := true;
end;
end;
{ -------------------------- non-member routines --------------------------- }
{ returns the left-most leaf of the sub-tree of node; if node is nil
then nil is returned }
function LeftMostLeafNode(node : P23TreeNode) : P23TreeNode;
begin
if node <> nil then
begin
while node^.Child[1] <> nil do
node := node^.Child[1];
end;
Result := node;
end;
{ returns the right-most leaf of the sub-tree of node; if node is nil
then nil is returned }
function RightMostLeafNode(node : P23TreeNode) : P23TreeNode;
begin
if node <> nil then
begin
while node^.Child[2] <> nil do
begin
if node^.Child[3] <> nil then
node := node^.Child[3]
else
node := node^.Child[2]
end;
end;
Result := node;
end;
function SubTreeSize(node : P23TreeNode) : SizeType;
var
finish, parent : P23TreeNode;
begin
Result := 0;
if node <> nil then
begin
{ visit all nodes in pre-order counting items in each node }
finish := node^.Parent;
repeat
Inc(Result);
if node^.StoredItems = 2 then
Inc(Result);
if node^.Child[1] <> nil then
node := node^.Child[1]
else begin
parent := node^.Parent;
while (parent <> finish) and
((parent^.Child[3] = node) or
((parent^.Child[2] = node) and
(parent^.Child[3] = nil))) do
begin
node := parent;
parent := parent^.Parent;
end;
if parent <> finish then
begin
if parent^.Child[1] = node then
node := parent^.Child[2]
else
node := parent^.Child[3];
end else
break;
end;
until false;
end;
end;
{ -------------------------- T23Tree --------------------------------- }
constructor T23Tree.Create;
begin
inherited;
InitFields;
end;
constructor T23Tree.CreateCopy(const cont : T23Tree;
const itemCopier : IUnaryFunctor);
var
item2, item3 : ItemType;
pdest : ^P23TreeNode;
src, destparent : P23TreeNode;
storedi : 1..2;
begin
if itemCopier = nil then
CreateCopyWithoutItems(cont)
else begin
inherited CreateCopy(TSetAdt(cont));
FSize := cont.FSize;
FValidSize := cont.FValidSize;
FHeight := cont.FHeight;
LowestItem := DefaultItem; { O.K. }
if cont.LowestItem <> DefaultItem then
LowestItem := itemCopier.Perform(cont.LowestItem);
if cont.FRoot = nil then
begin
FRoot := nil;
Exit;
end;
try
{ copy every node while moving in pre-order }
pdest := @FRoot; { pointer to where to place the newly created node }
destparent := nil;
src := cont.FRoot;
repeat
if src^.StoredItems >= 1 then
begin
item2 := itemCopier.Perform(src^.LowItem[2]); { may raise }
storedi := 1;
end;
if src^.StoredItems = 2 then
begin
item3 := itemCopier.Perform(src^.LowItem[3]); { may raise }
storedi := 2;
end;
NewNode(pdest^); { may raise }
with pdest^^ do
begin
StoredItems := storedi;
LowItem[2] := item2;
LowItem[3] := item3;
Parent := destparent;
Child[1] := nil;
Child[2] := nil;
Child[3] := nil;
end;
if src^.Child[1] <> nil then
begin
src := src^.Child[1];
destparent := pdest^;
pdest := @(pdest^^.Child[1]);
end else
begin
{ we have to go up the tree to the nearest unvisited node }
while src^.Parent <> nil do
begin
if (src^.Parent^.Child[1] = src) and
(src^.Parent^.Child[2] <> nil) then
begin
src := src^.Parent^.Child[2];
pdest := @(destparent^.Child[2]);
break;
end else if (src^.Parent^.Child[2] = src) and
(src^.Parent^.Child[3] <> nil) then
begin
src := src^.Parent^.Child[3];
pdest := @(destparent^.Child[3]);
break;
end else
begin
src := src^.Parent;
destparent := destparent^.Parent;
end;
end;
end;
until src^.Parent = nil;
except
if storedi >= 1 then
DisposeItem(item2);
if storedi = 2 then
DisposeItem(item3);
raise;
end;
end; { end not itemCopier = nil }
end;
constructor T23Tree.CreateCopyWithoutItems(const tree : T23Tree);
begin
inherited CreateCopy(TSetAdt(tree));
InitFields;
end;
destructor T23Tree.Destroy;
begin
Clear;
inherited;
end;
procedure T23Tree.InitFields;
begin
LowestItem := DefaultItem;
FRoot := nil;
FSize := 0;
FValidSize := true;
FHeight := 0;
end;
procedure T23Tree.DeleteSubTree(node : P23TreeNode);
var
parent, fin : P23TreeNode;
child : Integer;
begin
if node <> nil then
begin
{ visit all nodes in post-order destroying every visited node }
fin := node^.Parent;
node := LeftMostLeafNode(node);
parent := node^.Parent;
while parent <> fin do
begin
if parent^.Child[1] = node then
child := 1
else if parent^.Child[2] = node then
child := 2
else
child := 3;
if node^.StoredItems >= 1 then
begin
DisposeItem(node^.LowItem[2]);
Dec(FSize);
end;
if node^.StoredItems = 2 then
begin
DisposeItem(node^.LowItem[3]);
Dec(FSize);
end;
DisposeNode(node);
if (child = 1) or ((child = 2) and (parent^.Child[3] <> nil)) then
node := LeftMostLeafNode(parent^.Child[child + 1])
else { all children of parent visited - go to parent }
node := parent;
parent := node^.Parent;
end;
if node^.StoredItems >= 1 then
begin
DisposeItem(node^.LowItem[2]);
Dec(FSize);
end;
if node^.StoredItems = 2 then
begin
DisposeItem(node^.LowItem[3]);
Dec(FSize);
end;
DisposeNode(node);
end;
end;
{ Recursive schema of traversal of a 2-3 tree with items contained in
internal nodes: }
{ 1) traverse the 1st sub-tree (if exists); 1st sub-tree does not
exist when we are at the level of leaves, because the lowest item in
this sub-tree is contained in some higher internal node }
{ 2) visit the 2nd's sub-tree's smallest item (this is LowItem[2]) }
{ 3) traverse the 2nd sub-tree }
{ 4) visit the smallest item of the 3rd sub-tree (if exists) }
{ 5) traverse the 3rd sub-tree if it exists }
{ NOTE: the fact that StoredItems >= n-1 (i.e. there exists the lowest
item of the n-th sub-tree) does not imply that Child[n] is non-nil
(i.e. there exists the n-th sub-tree); this is because the lowest
item of a sub-tree is not stored in this sub-tree, but in its
parent; so, when there is only one item in a sub-tree the sub-tree
does not physically exist (although it exists logically); in the
above discussion I referred to the physical existence, not logical }
{ advances pair (node,low) to next item }
procedure T23Tree.AdvanceNode(var node : P23TreeNode; var low : Integer);
procedure FinishTraversalOfSubTree;
var
current : P23TreeNode;
begin
{ go up the tree as long as we need to finish the traversal
of the sub-tree we are currently visiting - i.e. as long as
the next item to visit does not exist in the current
sub-tree }
current := node^.Parent;
while (current <> nil) and
((current^.Child[3] = node) or
((current^.Child[2] = node) and
(current^.StoredItems < 2))) do
begin
node := current;
current := current^.Parent;
end;
if current <> nil then
begin
if current^.Child[1] = node then
low := 2 { step 2 }
else { current^.Child[2] = node -> step 4 }
low := 3;
end else
low := 0; { whole tree traversed }
node := current;
end;
begin
Assert((node <> nil) or (low = 1), msgAdvancingFinishIterator);
if (node = nil) and (low = 1) then
begin
node := LeftMostLeafNode(FRoot);
if node <> nil then
low := 2
else
low := 0;
end else if node^.Child[low] <> nil then
{ now step 3 (if low is 2) or 5 (if low is 3) -> start traversal
of the low-th sub-tree - go to the second-smallest item in
that sub-tree (because the first-smallest was already visited)
- this is the LowItem[2] at the left-most leaf of this
sub-tree }
begin
node := LeftMostLeafNode(node^.Child[low]);
low := 2;
end else if low = 2 then
{ skip step 3; go to step 4 - visit lowest item in the 3rd sub-tree }
begin
Inc(low);
if node^.StoredItems < 2 then
{ no lowest item in third sub-tree - finish traversal of
sub-tree of node }
begin
FinishTraversalOfSubTree;
end;
end else { (low = 3) and (node^.Child[3] = nil) }
begin
FinishTraversalOfSubTree;
end;
end;
{ Recursive schema of backwards traversal of a 2-3 tree with items
contained in internal nodes: }
{ 1) traverse the 3rd sub-tree (if exists) }
{ 2) visit the lowest item in the 3rd sub-tree (if exists) }
{ 3) traverse the 2nd sub-tree }
{ 4) visit the lowest item in the 2nd sub-tree }
{ 5) traverse the 1st sub-tree }
{ retreats the pair (node,low) to the previous item; does not accept (nil,1) }
procedure T23Tree.RetreatNode(var node : P23TreeNode; var low : Integer);
var
parent : P23TreeNode;
begin
Assert((node <> nil) or (low = 0), msgInternalError);
if low = 2 then
begin
if node^.Child[1] <> nil then
begin
{ traverse 1st sub-tree }
node := RightMostLeafNode(node^.Child[1]);
if node^.StoredItems = 2 then
low := 3
else
low := 2;
end else
begin
{ we traversed the first sub-tree - finish traversal of
sub-tree of node }
{ go up as long as we have to finish traversal - i.e. as long
as node is the first child of parent }
parent := node^.Parent;
while (parent <> nil) and (parent^.Child[1] = node) do
begin
node := parent;
parent := parent^.Parent;
end;
if parent <> nil then
begin
if parent^.Child[3] = node then
low := 3 { finished traversal of 3rd sub-tree -> visit
3rd sub-tree's lowest item }
else
low := 2;
end else
low := 1; { go to the first item in the tree }
node := parent;
end;
end else if low = 3 then
begin
if node^.Child[2] <> nil then
begin
{ traverse second sub-tree }
node := RightMostLeafNode(node^.Child[2]);
if node^.StoredItems = 2 then
low := 3
else
low := 2;
end else
begin
{ visit 2nd sub-tree's lowest item }
low := 2;
end;
end else { low = 0 }
begin
node := RightMostLeafNode(FRoot);
if node <> nil then
begin
if node^.StoredItems = 2 then
low := 3
else
low := 2;
end else
begin
Assert((FSize <> 0) or not FValidSize, msgRetreatingStartIterator);
low := 1;
end;
end;
end;
function T23Tree.FindNode(aitem : ItemType; startNode : P23TreeNode;
var found : P23TreeNode; var low : Integer) : Boolean;
var
i : Integer;
function FindNodeAux(node : P23TreeNode) : Boolean;
begin
Result := false;
while node <> nil do
begin
found := node;
_mcp_compare_assign(aitem, node^.LowItem[2], i);
if i < 0 then
begin
low := 1;
node := node^.Child[1];
end else if i > 0 then
begin
if node^.StoredItems = 2 then
begin
_mcp_compare_assign(aitem, node^.LowItem[3], i);
if i < 0 then
begin
low := 2;
node := node^.Child[2];
end else if i > 0 then
begin
low := 3;
node := node^.Child[3];
end else { i = 0 -> node^.LowItem[3] = aitem }
begin
{ we have to find the _first_ occurence }
if RepeatedItems then
begin
if not FindNodeAux(node^.Child[2]) then
begin
{ reset found and low to values they had
before invoking this function }
found := node;
low := 3;
end;
end else
low := 3;
Result := true;
Exit;
end;
end else { no third sub-tree }
begin
low := 2;
node := node^.Child[2];
end;
end else { i = 0 -> node^.LowItem[2] = aitem }
begin
{ we have to find the _first_ item equal to aitem }
if RepeatedItems then
begin
if not FindNodeAux(node^.Child[1]) then
begin
found := node;
low := 2;
end;
end else
low := 2;
Result := true;
Exit;
end;
end; { end while node <> nil }
end; { end FindNodeAux }
begin
if (FSize <> 0) or not FValidSize then
begin
_mcp_compare_assign(aitem, LowestItem, i);
if i < 0 then
begin
found := nil;
low := 1;
Result := false;
Exit;
end else if i = 0 then
begin
found := nil;
low := 1;
Result := true;
Exit;
end;
end;
found := nil;
low := 0;
Result := FindNodeAux(startNode);
end;
function T23Tree.LowerBoundNode(aitem : ItemType; node : P23TreeNode;
var lb : P23TreeNode;
var low : Integer) : Boolean;
var
i : Integer;
begin
if (FSize <> 0) or not FValidSize then
begin
_mcp_compare_assign(aitem, LowestItem, i);
if i <= 0 then
begin
lb := nil;
low := 1;
if i = 0 then
Result := true
else
Result := false;
end else if FRoot <> nil then
begin
if not FindNode(aitem, node, lb, low) then
begin
if (low = 3) or (lb^.StoredItems = 1) then
begin
AdvanceNode(lb, low);
end else
Inc(low);
Result := false;
end else
Result := true;
end else
{ only one item in the container which is smaller than aitem }
begin
lb := nil;
low := 0;
Result := false;
end;
end else { empty container }
begin
lb := nil;
low := 0;
Result := false;
end;
end;
procedure T23Tree.InsertNode(parent : P23TreeNode; cnum : Integer;
aitem : ItemType; node : P23TreeNode;
var inserted : P23TreeNode; var low : Integer);
var
pnewnode, tempnode, oldroot, ln, rn, pparent : P23TreeNode;
pnum : Integer; { which child is parent in its parent ? }
itemlow : ItemType; { the lowest item in the sub-tree of node
(logically - i.e. not physically contained
within that sub-tree) }
templow : ItemType;
begin
Assert(parent <> nil, msgInternalError);
Assert((cnum >= 1) and (cnum <= 3));
// Assert(aitem <> nil);
itemlow := DefaultItem; { to shut the compiler up }
try
{ now, insert aitem into the tree as cnum+1 child of parent; if
parent has 4 children after insertion we have to distribute
the excessive child among neighbour nodes or split it (the
node with 4 children) into two nodes with two children each
and proceed up the tree applying the same rule to the newly
created node (to insert it on the higher level); cnum is the
number of the child of parent _after_ which to insert; itemlow
indicates the lowest item in the sub-tree of node; node is the
node from the previous level that must be inserted as a cnum+1
child of parent }
itemlow := aitem;
while parent <> nil do
begin
{ insert node as (cnum + 1)th child and shift children > cnum
right, until one has to be shifted to the 4th child and assign
it to node (this may be as well the original node - when cnum
is 3 before this loop) }
Assert((cnum >= 1) and (cnum <= 3));
Assert(cnum > 0);
Assert(NodeConsistent(parent));
Assert((node = nil) or NodeConsistent(node));
if (cnum < 3) and (aitem = itemlow) then
begin
inserted := parent;
low := cnum + 1;
end;
if cnum = 1 then
begin
tempnode := parent^.Child[2];
templow := parent^.LowItem[2];
Inc(cnum);
parent^.Child[2] := node;
if node <> nil then
node^.Parent := parent;
parent^.LowItem[2] := itemlow;
node := tempnode;
itemlow := templow;
end;
if cnum = 2 then
begin
if parent^.StoredItems = 2 then
begin
tempnode := parent^.Child[3];
templow := parent^.LowItem[3];
Inc(cnum);
end else
parent^.StoredItems := 2;
parent^.Child[3] := node;
if node <> nil then
node^.Parent := parent;
parent^.LowItem[3] := itemlow;
node := tempnode;
itemlow := templow;
end;
if (cnum = 3) then
begin
{ parent would have 4 children after insertion - we can
try three things: }
{ - if parent has a right neighbour and this neighbour
has 2 children then move the 4th child of parent to the
1st child of its right neighbour }
{ - if parent has a left neighbour and this neighbour has
2 children then move the first child of parent to the
3rd child of its left neighbour }
{ - otherwise, if both neighbours of parent have 3
children or don't exist, we have to split it into two
nodes with two children each }
pparent := parent^.Parent;
if pparent <> nil then
begin
if (pparent^.Child[1] = parent) then
pnum := 1
else if pparent^.Child[2] = parent then
pnum := 2
else { if pparent^.Child[3] = parent then }
pnum := 3;
end;
if (pparent <> nil) and (pnum + 1 <= 3) and
(pparent^.Child[pnum + 1] <> nil) and
(pparent^.Child[pnum + 1]^.StoredItems < 2) then
begin
{ move the 4th child of parent to the 1st child of its
right neighbour }
rn := pparent^.Child[pnum + 1];
with rn^ do
begin
StoredItems := 2;
LowItem[3] := LowItem[2];
LowItem[2] := pparent^.LowItem[pnum + 1];
Child[3] := Child[2];
Child[2] := Child[1];
Child[1] := node;
if node <> nil then
node^.Parent := rn;
pparent^.LowItem[pnum + 1] := itemlow;
end;
if pparent^.LowItem[pnum + 1] = aitem then
begin
inserted := pparent;
low := pnum + 1;
end;
break;
end else if (pparent <> nil) and (pnum - 1 >= 1) and
(pparent^.Child[pnum - 1] <> nil) and
(pparent^.Child[pnum - 1]^.StoredItems < 2) then
begin
{ move the 1st child of parent to the 3rd child of
parent's left neighbour }
ln := pparent^.Child[pnum - 1];
with parent^ do
begin
ln^.Child[3] := Child[1];
if ln^.Child[3] <> nil then
ln^.Child[3]^.Parent := ln;
ln^.LowItem[3] := pparent^.LowItem[pnum];
ln^.StoredItems := 2;
pparent^.LowItem[pnum] := LowItem[2];
LowItem[2] := LowItem[3];
LowItem[3] := itemlow;
Child[1] := Child[2];
Child[2] := Child[3];
Child[3] := node;
end;
if node <> nil then
node^.Parent := parent;
if parent^.LowItem[3] = aitem then
begin
inserted := parent;
low := 3;
end else if parent^.LowItem[2] = aitem then
begin
inserted := parent;
low := 2;
end else if pparent^.LowItem[pnum] = aitem then
begin
inserted := pparent;
low := pnum;
end;
break; { propagation finished - the tree is 'in
Ordnung' }
end else { cannot move the execessive child to neighbours }
begin
{ because cnum is 3 the node from the previous level is
required to be inserted after the 3rd child (ie. as
the 4th child in parent - as the 2nd child in
pnewnode, because the last two children of parent go
to pnewnode) }
NewNode(pnewnode);
pnewnode^.Child[1] := parent^.Child[3];
with pnewnode^ do
begin
if Child[1] <> nil then
Child[1]^.Parent := pnewnode;
Child[2] := node;
if node <> nil then
node^.Parent := pnewnode;
LowItem[2] := itemlow;
Child[3] := nil;
LowItem[3] := DefaultItem;
StoredItems := 1;
end;
itemlow := parent^.LowItem[3];
with parent^ do
begin
Child[3] := nil;
LowItem[3] := DefaultItem;
StoredItems := 1;
end;
if pnewnode^.LowItem[2] = aitem then
begin
inserted := pnewnode;
low := 2;
end;
{ now pnewnode must be inserted at higher level }
node := pnewnode;
{ go one level up }
parent := pparent;
cnum := pnum;
end;
end else
{ not cnum = 3 => there is no excessive child - no need to
proceed }
begin
break;
end;
end; { end while (parent <> nil) and (there is at least one item
to be inserted) }
if parent = nil then
begin
{ the root was splitted - we have to create a new root with the
two nodes obtained from splitting the old root as its children }
oldroot := FRoot;
NewNode(FRoot);
with FRoot^ do
begin
Child[1] := oldroot;
Child[2] := node;
Child[3] := nil;
LowItem[2] := itemlow;
LowItem[3] := DefaultItem;
Parent := nil;
StoredItems := 1;
end;
oldroot^.Parent := FRoot;
node^.Parent := FRoot;
Inc(FHeight);
end;
except
{ now, we cannot get the tree back to its previous state and we
are left with node not connected anywhere and itemlow not
placed anywhere in the tree - just dispose node and itemlow not
to leak anything, regardless of how valueable data they may
contain }
if node <> nil then
begin
{ we cannot dispose the item we insert as it is supposed to
be left intact when an exception occurs }
if inserted^.LowItem[low] = aitem then
begin { should be always true, above; but no one ever
knows... }
inserted^.LowItem[low] := DefaultItem;
if low = 3 then
inserted^.StoredItems := 1
else { low = 2 }
begin
DeleteNode(inserted, low);
end;
end;
if itemlow <> aitem then
DisposeItem(itemlow);
DeleteSubTree(node);
FValidSize := false;
end;
raise;
end;
end;
function T23Tree.DoInsert(aitem : ItemType; node : P23TreeNode;
var inserted : P23TreeNode;
var low : Integer) : Boolean;
var
parent : P23TreeNode; { a would-be parent of the new node }
cnum : Integer; { the number of the child _after_ which new node
should be inserted (in parent) }
tmp : ItemType;
dummy1 : P23TreeNode;
dummy2 : Integer;
begin
{$ifdef TEST_PASCAL_ADT }
// LogStatus('DoInsert (aitem = ' + FormatItem(aitem) +
// ', node = %' + IntToStr(PointerValueType(node)) + '%)');
{$endif TEST_PASCAL_ADT }
if (FSize = 0) and FValidSize then
begin
LowestItem := aitem;
FSize := 1;
FValidSize := true;
inserted := nil;
low := 1;
Result := true;
end else if FRoot = nil then
begin
NewNode(FRoot);
with FRoot^ do
begin
if _mcp_lt(aitem, LowestItem) then