-
Notifications
You must be signed in to change notification settings - Fork 1
/
adttree_impl.i
1939 lines (1708 loc) · 50.3 KB
/
adttree_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
adttree_impl.i::prefix=&_mcp_prefix&::item_type=&ItemType&
}
&include adttree.defs
&include adttree_impl.mcp
{ ---------------------- helper functions ---------------------------------- }
{ returns the right-most child of node; takes exactly O(n) time, where
n is the number of children of node }
function RightMostChildNode(node : PTreeNode) : PTreeNode;
begin
Assert(node <> nil, msgInvalidIterator);
Result := node^.LeftmostChild;
if Result <> nil then
while (Result^.RightSibling <> nil) do
Result := Result^.RightSibling;
end;
{ returns the left sibling (neighbour) of node or nil if node is the
left-most child; @complexity O(ls), where ls is the number of left
siblings of node }
function LeftSiblingNode(node : PTreeNode) : PTreeNode;
begin
Result := nil;
if (node^.Parent <> nil) and (node^.Parent^.LeftmostChild <> node) then
begin
Result := node^.Parent^.LeftmostChild;
while Result^.RightSibling <> node do
Result := Result^.RightSibling;
end;
end;
{ Right-most leaf in a sub-tree is the node which is visited in
pre-order traversal after all other nodes the sub-tree. This
algorithm is worst-case O(n) time when all nodes are children of the
root of the subtree or if every node has exactly one child; n is the
number of nodes in the subtree. }
function RightMostLeafNode(subtree : PTreeNode) : PTreeNode;
begin
if subtree <> nil then
begin
Result := RightMostChildNode(subtree);
while Result <> nil do
begin
subtree := Result;
Result := RightMostChildNode(subtree);
end;
Result := subtree;
end else
Result := nil;
end;
{ Left-most leaf in a sub-tree is the node that is visited in
post-order and in-order traversals before any other node in the
sub-tree. This is worst-case O(n) time when every node in the
sub-tree has exactly one child; n is the number of nodes in the
sub-tree. }
function LeftMostLeafNode(subtree : PTreeNode) : PTreeNode;
begin
if subtree <> nil then
begin
Result := subtree;
while Result^.LeftmostChild <> nil do
Result := Result^.LeftmostChild;
end else
Result := nil;
end;
function LastInOrderNode(node : PTreeNode) : PTreeNode;
begin
Assert(node <> nil, msgInvalidIterator);
while (node^.LeftmostChild <> nil) and
(node^.LeftmostChild^.RightSibling <> nil) do
begin
node := RightMostChildNode(node);
end;
Result := node;
end;
function NodeDepth(node : PTreeNode) : SizeType;
begin
Assert(node <> nil, msgInvalidIterator);
Result := 0;
while node^.Parent <> nil do
begin
Inc(Result);
node := node^.Parent;
end;
end;
function NodeHeight(node : PTreeNode) : SizeType;
var
h : SizeType;
begin
Assert(node <> nil, msgInvalidIterator);
Result := 0;
node := node^.LeftmostChild;
while node <> nil do
begin
h := NodeHeight(node) + 1;
if h > Result then
Result := h;
node := node^.RightSibling;
end;
end;
function NodeChildren(node : PTreeNode) : SizeType;
begin
Result := 0;
if node <> nil then
begin
node := node^.LeftmostChild;
while node <> nil do
begin
Inc(Result);
node := node^.RightSibling;
end;
end;
end;
function NextPreOrderNode(node : PTreeNode) : PTreeNode;
begin
Assert(Node <> nil, msgInvalidIterator);
if node^.LeftmostChild <> nil then
begin
node := node^.LeftmostChild;
end else
begin
while (node <> nil) and (node^.RightSibling = nil) do
node := node^.Parent;
if node <> nil then
node := node^.RightSibling;
end;
Result := node;
end;
function NextPostOrderNode(node : PTreeNode) : PTreeNode;
begin
Assert(Node <> nil, msgInvalidIterator);
if Node^.RightSibling <> nil then
begin
Node := LeftMostLeafNode(Node^.RightSibling);
end else
begin
Node := Node^.Parent;
end;
Result := node;
end;
function NextInOrderNode(node : PTreeNode) : PTreeNode;
begin
Assert(Node <> nil, msgInvalidIterator);
if (Node^.LeftmostChild <> nil) and
(Node^.LeftmostChild^.RightSibling <> nil) then
{ after visiting the parent start traversing the remaining
children from left to right }
begin
Node := LeftMostLeafNode(Node^.LeftmostChild^.RightSibling);
end else if Node^.Parent = nil then { it's the root }
{ we now know that Node has at most one child; see if statement above }
begin
Node := nil; { end of traversal }
end else if Node^.Parent^.LeftmostChild = Node then
{ go to the parent after traversing the left-most subtree }
begin
Node := Node^.Parent;
end else if Node^.RightSibling = nil then
{ no more subtrees to traverse - go to the ancestor node which has
not yet been traversed }
begin
while (Node <> nil) and (Node^.RightSibling = nil) do
begin
if (Node^.Parent <> nil) and (Node^.Parent^.LeftmostChild = Node) then
begin
{ if Node is the left-most child of its parent then parent has
not yet been visited }
Node := Node^.Parent;
Break;
end;
Node := Node^.Parent;
end;
if Node <> nil then
begin
if (Node^.Parent <> nil) and (Node^.Parent^.LeftmostChild = Node) then
begin
{ Node is the left-most child of its parent, so parent has
not yet been visited -> do nothing }
Node := Node^.Parent;
end else
begin
{ we have already visited parent and now traverse
sub-trees from left to right }
Node := LeftMostLeafNode(Node^.RightSibling);
end;
end;
end else
begin
{ after visiting the parent (some steps before, not necessarily
one) traverse all subtrees from left to right, except for the
left-most one }
Node := LeftMostLeafNode(Node^.RightSibling);
end;
Result := node;
end;
function PrevPreOrderNode(node, root : PTreeNode) : PTreeNode;
begin
if node <> nil then
begin
Result := LeftSiblingNode(node);
if Result <> nil then
begin
Result := RightMostLeafNode(Result);
end else
begin
Assert(node^.Parent <> nil, msgRetreatingStartIterator);
Result := node^.Parent;
end;
end else
Result := RightMostLeafNode(root);
end;
function PrevPostOrderNode(node, root : PTreeNode) : PTreeNode;
begin
if node <> nil then
begin
Result := RightMostChildNode(node);
if Result = nil then
begin
Result := node;
while (Result^.Parent <> nil) and
(Result^.Parent^.LeftmostChild = Result) do
begin
Result := Result^.Parent;
end;
Assert(Result^.Parent <> nil, msgRetreatingStartIterator);
Result := LeftSiblingNode(Result);
end;
end else
Result := root;
end;
function PrevInOrderNode(node, root : PTreeNode) : PTreeNode;
begin
if node <> nil then
begin
Result := node^.LeftmostChild;
if result <> nil then
Result := LastInOrderNode(Result)
else begin
Result := node;
while (Result^.Parent <> nil) and
(Result^.Parent^.LeftmostChild = result) do
begin
Result := Result^.Parent;
end;
Assert(Result^.Parent <> nil, msgRetreatingStartIterator);
if Result^.Parent^.LeftmostChild^.RightSibling = Result then
{ result is the second child - go to the parent }
begin
Result := Result^.Parent;
end else
Result := LastInOrderNode(LeftSiblingNode(Result));
end;
end else
Result := LastInOrderNode(root);
end;
{ replaces node with its children; if fadvance if true returns the
next node after node accoring to PRE-order }
function ReplaceNodeWithChildren(node : PTreeNode;
fadvancePreOrder : Boolean) : PTreeNode;
var
lsib, child : PTreeNode;
begin
Assert(node^.Parent <> nil, msgInternalError);
lsib := LeftSiblingNode(node);
child := node^.LeftmostChild;
if child <> nil then
begin
Result := child;
if lsib <> nil then
begin
lsib^.RightSibling := child;
end else
node^.Parent^.LeftmostChild := child;
while child^.RightSibling <> nil do
begin
child^.Parent := node^.Parent;
child := child^.RightSibling;
end;
child^.Parent := node^.Parent;
child^.RightSibling := node^.RightSibling;
end else
begin
if lsib <> nil then
lsib^.RightSibling := node^.RightSibling
else
node^.Parent^.LeftmostChild := node^.RightSibling;
if node^.RightSibling <> nil then
begin
Result := node^.RightSibling;
end else
begin
if fadvancePreOrder then
Result := NextPreOrderNode(node)
else
Result := nil;
end;
end;
end;
{ replaces Node with its right-most child and moves all other children
of Node, from right to left, one by one, to the left-most leaves of
the new sub-tree of right-most child of Node; Node is disconnected
from the tree; returns the subtree which contains the left-most leaf
of the sub-tree of right-most child of Node, or nil if there is no
right-most child. }
function ReorganiseTreeRight(Node : PTreeNode; FTree : TTree) : PTreeNode;
const
InitialStackSize = 128;
var
rchild, temp, lleaf : PTreeNode;
stack : TPointerDynamicBuffer;
top : IndexType;
begin
Assert(Node <> nil, msgInvalidIterator);
{ replace the removed node with its right-most child, then move all
other children (from right to left) to left-most leaves of
the right-most child }
if Node^.LeftmostChild <> nil then
BufferAllocate(stack, InitialStackSize)
else
stack := nil;
top := -1;
try
{ Find the right-most child and push the other children at the
stack, in order to retrieve them easily from right to left,
later. }
rchild := Node^.LeftMostChild;
if rchild <> nil then
begin
while rchild^.RightSibling <> nil do
begin
Inc(top);
if top >= stack^.Capacity then
BufferReallocate(stack, stack^.Capacity * 2);
stack^.Items[top] := rchild;
rchild := rchild^.RightSibling;
end;
end;
{ from now on no exceptions can be raised }
{ disconnect Node from the tree and put its right-most child in
its place }
if Node^.Parent <> nil then
begin
temp := Node^.Parent^.LeftMostChild;
if temp <> Node then
begin
while temp^.RightSibling <> Node do
temp := temp^.RightSibling;
if rchild <> nil then
temp^.RightSibling := rchild
else
temp^.RightSibling := Node^.RightSibling;
end else
begin
if rchild <> nil then
Node^.Parent^.LeftMostChild := rchild
else
Node^.Parent^.LeftMostChild := Node^.RightSibling;
end;
end else
begin
FTree.FRoot := rchild;
end;
{ In each step make one child the only child of the left-most
leaf. Start from the one-before right-most child and proceed
from right to left (that's why a stack is needed). }
if rchild <> nil then
begin
rchild^.Parent := Node^.Parent;
rchild^.RightSibling := Node^.RightSibling;
Result := rchild;
while top <> -1 do
begin
lleaf := LeftMostLeafNode(Result);
Result := stack^.Items[top];
Dec(top);
lleaf^.LeftMostChild := Result;
Result^.Parent := lleaf;
Result^.RightSibling := nil;
end;
end else
Result := nil;
finally
if stack <> nil then
BufferDeallocate(stack);
end;
end;
{ returns the newly created node }
function InsertAsRightMostLeaf(tree : TTree; aitem : ItemType) : PTreeNode;
begin
{$warnings off }
tree.NewNode(Result);
{$warnings on }
with Result^ do
begin
Item := aitem;
Leftmostchild := nil;
RightSibling := nil;
end;
tree.InsertNodeAsRightMostLeaf(tree.FRoot, Result);
Inc(tree.FSize);
end;
{ **************************************************************************** }
{ LCRS representation of a tree }
{ **************************************************************************** }
(* Notes on implementation of TTree:
* TTree is a left-most child, right sibling (LCRS) representation of a general
* purpose tree. Every tree is composed of nodes, each of which contains a pointer
* to its left-most child, its right sibling and its parent. This assures that
* all the three basic tree operations take O(1) time. The tree object contains
* a pointer to the root node and the number of Items in the tree. It also
* contains FValidSize field, which indicates whether the FSize field is valid
* (it may be invalidated after move operation). Size operation takes amortized
* O(1) time, with the worst case of O(n), when FSize is invalid. The absence of
* some node (child, sibling, root, etc.) is indicated by a nil pointer in its
* place.
*)
{ ------------------------------ TTree members ------------------------------- }
constructor TTree.Create;
begin
inherited;
InitFields;
end;
constructor TTree.CreateCopy(const cont : TTree;
const itemCopier : IUnaryFunctor);
var
src, destparent : PTreeNode;
dest : ^PTreeNode;
begin
inherited CreateCopy(cont);
InitFields;
if itemCopier <> nil then
begin
try
{ copy the tree structure while moving pre-order }
destparent := nil;
dest := @FRoot;
src := cont.FRoot;
while src <> nil do
begin
NewNode(dest^); { may raise }
with dest^^ do
begin
Item := itemCopier.Perform(src^.Item); { may raise }
Parent := destparent;
Leftmostchild := nil;
RightSibling := nil;
end;
Inc(FSize);
if src^.Leftmostchild <> nil then
begin
src := src^.Leftmostchild;
destparent := dest^;
dest := @dest^^.Leftmostchild;
end else if src^.RightSibling <> nil then
begin
src := src^.RightSibling;
// destparent doesn't change
dest := @dest^^.RightSibling;
end else
{ we have to go back to some ancestor node }
begin
while (src <> nil) and (src^.RightSibling = nil) do
begin
src := src^.Parent;
dest := @dest^^.Parent;
end;
if src <> nil then
begin
src := src^.RightSibling;
{ necessary since destparent is not adjusted in the
loop }
destparent := dest^^.Parent;
dest := @dest^^.RightSibling;
end; { else end of traversal }
end;
end;
except
DisposeNode(dest^);
dest^ := nil;
raise;
end;
cont.FValidSize := true;
cont.FSize := FSize;
end;
end;
destructor TTree.Destroy;
begin
Clear;
inherited;
end;
procedure TTree.InitFields;
begin
FRoot := nil;
FSize := 0;
FValidSize := true;
end;
procedure TTree.DisposeNodeAndItem(node : PTreeNode);
begin
DisposeItem(node^.Item);
DisposeNode(node);
end;
procedure TTree.RemoveConnections(node : PTreeNode);
var
xnode : PTreeNode;
begin
Assert(Node <> nil);
if Node^.Parent = nil then
FRoot := nil
else
begin
xnode := Node^.Parent^.LeftmostChild;
if xnode = Node then
begin
Node^.Parent^.LeftmostChild := Node^.RightSibling;
end else
begin
while xnode^.RightSibling <> node do
xnode := xnode^.RightSibling;
xnode^.RightSibling := Node^.RightSibling;
end;
end;
end;
procedure TTree.InsertNodeAsRightMostLeaf(var proot : PTreeNode;
node : PTreeNode);
var
rleaf : PTreeNode;
begin
if (proot <> nil) and (node <> nil) then
begin
rleaf := RightMostLeafNode(proot);
rleaf^.Leftmostchild := node;
while node <> nil do
begin
node^.Parent := rleaf;
node := node^.RightSibling;
end;
end else if (proot = nil) then
begin
Assert((node = nil) or (node^.RightSibling = nil), msgInternalError);
proot := node;
if node <> nil then
node^.Parent := nil;
end;
end;
function TTree.CopySelf(const ItemCopier : IUnaryFunctor) : TContainerAdt;
begin
Result := TTree.CreateCopy(self, itemCopier);
end;
procedure TTree.Swap(cont : TContainerAdt);
var
tree : TTree;
begin
if cont is TTree then
begin
BasicSwap(cont);
tree := TTree(cont);
ExchangePtr(FRoot, tree.FRoot);
ExchangeData(FSize, tree.FSize, SizeOf(SizeType));
ExchangeData(FValidSize, tree.FValidSize, SizeOf(Boolean));
end else
inherited;
end;
function TTree.Root : TTreeIterator;
begin
Result := TTreeIterator.Create(FRoot, self);
end;
function TTree.BasicRoot : TBasicTreeIterator;
begin
Result := TTreeIterator.Create(FRoot, self);
end;
function TTree.Finish : TBasicTreeIterator;
begin
Result := TTreeIterator.Create(nil, self);
end;
function TTree.PreOrderIterator : TPreOrderIterator;
begin
Result := TTreePreOrderIterator.Create(self);
Result.StartTraversal;
end;
function TTree.PostOrderIterator : TPostOrderIterator;
begin
Result := TTreePostOrderIterator.Create(self);
Result.StartTraversal;
end;
function TTree.InOrderIterator : TInOrderIterator;
begin
Result := TTreeInOrderIterator.Create(self);
Result.StartTraversal;
end;
function TTree.LevelOrderIterator : TLevelOrderIterator;
begin
Result := TTreeLevelOrderIterator.Create(self);
Result.StartTraversal;
end;
function TTree.DeleteSubTree(node : TBasicTreeIterator) : SizeType;
var
xnode : PTreeNode;
begin
Assert(node is TTreeIterator, msgInvalidIterator);
Assert(TTreeIterator(node).Node <> nil, msgDeletingInvalidIterator);
Assert(node.Owner = self, msgWrongOwner);
xnode := TTreeIterator(node).Node;
Result := NodeSubTreeDelete(xnode);
end;
procedure TTree.InsertAsRoot(aitem : ItemType);
var
temp : PTreeNode;
begin
NewNode(temp);
temp^.LeftMostChild := FRoot;
if FRoot <> nil then
FRoot^.Parent := temp;
FRoot := temp;
temp^.Parent := nil;
temp^.RightSibling := nil;
temp^.Item := aitem;
Inc(FSize);
end;
procedure TTree.InsertAsRightSibling(node : TBasicTreeIterator;
aitem : ItemType);
var
xnode, pnewnode : PTreeNode;
begin
Assert(node is TTreeIterator, msgInvalidIterator);
Assert(node.Owner = self, msgWrongOwner);
Assert(TTreeIterator(node).Node <> FRoot, msgInsertingRootSibling);
xnode := TTreeIterator(node).Node;
NewNode(pnewnode);
pnewnode^.Item := aitem;
pnewnode^.LeftmostChild := nil;
pnewnode^.Parent := xnode^.Parent;
pnewnode^.RightSibling := xnode^.RightSibling;
xnode^.RightSibling := pnewnode;
Inc(FSize);
end;
procedure TTree.InsertAsLeftMostChild(node : TBasicTreeIterator;
aitem : ItemType);
var
xnode, pnewnode : PTreeNode;
begin
Assert(node is TTreeIterator, msgInvalidIterator);
Assert(node.Owner = self, msgWrongOwner);
Assert(TTreeIterator(node).Node <> nil, msgInvalidIterator);
xnode := TTreeIterator(node).Node;
NewNode(pnewnode);
pnewnode^.Item := aitem;
pnewnode^.LeftmostChild := nil;
pnewnode^.Parent := xnode;
pnewnode^.RightSibling := xnode^.LeftmostChild;
xnode^.LeftMostChild := pnewnode;
Inc(FSize);
end;
procedure TTree.MoveToRightSibling(destnode, sourcenode : TBasicTreeIterator);
var
dest, source : PTreeNode;
tree2 : TTree;
begin
Assert(destnode is TTreeIterator, msgInvalidIterator);
Assert(sourcenode is TTreeIterator, msgInvalidIterator);
Assert(TTreeIterator(sourcenode).Node <> nil, msgInvalidIterator);
Assert(destnode.Owner = self, msgWrongOwner);
Assert(TTreeIterator(destnode).Node <> FRoot);
source := TTreeIterator(sourcenode).Node;
dest := TTreeIterator(destnode).Node;
tree2 := TTreeIterator(sourcenode).FTree;
if source^.LeftmostChild = nil then
begin
Inc(FSize);
Dec(tree2.FSize);
end else if source^.Parent = nil then { source is the root }
begin
FSize := FSize + tree2.FSize;
FValidSize := FValidSize and tree2.FValidSize;
tree2.FSize := 0;
tree2.FValidSize := true;
end else if tree2 <> self then
begin
FValidSize := false;
tree2.FValidSize := false;
end;
tree2.RemoveConnections(source);
source^.RightSibling := dest^.RightSibling;
source^.Parent := dest^.Parent;
dest^.RightSibling := source;
{ the iterators are invalidated anyway, so there's no need to set }
end;
procedure TTree.MoveToLeftMostChild(destnode, sourcenode : TBasicTreeIterator);
var
dest, source : PTreeNode;
tree2 : TTree;
begin
Assert(destnode is TTreeIterator, msgInvalidIterator);
Assert(sourcenode is TTreeIterator, msgInvalidIterator);
Assert(destnode.Owner = self, msgWrongOwner);
source := TTreeIterator(sourcenode).Node;
dest := TTreeIterator(destnode).Node;
tree2 := TTreeIterator(sourcenode).FTree;
if source^.LeftmostChild = nil then
begin
Inc(FSize);
Dec(tree2.FSize);
end else if source^.Parent = nil then { source is the root }
begin
FSize := FSize + tree2.FSize;
FValidSize := FValidSize and tree2.FValidSize;
tree2.FSize := 0;
tree2.FValidSize := true;
end else if tree2 <> self then
begin
FValidSize := false;
tree2.FValidSize := false;
end;
tree2.RemoveConnections(source);
source^.RightSibling := dest^.LeftmostChild;
source^.Parent := dest;
dest^.LeftmostChild := source;
end;
procedure TTree.Clear;
begin
if FRoot <> nil then
begin
NodeSubTreeDelete(FRoot);
FRoot := nil;
FSize := 0;
FValidSize := true;
end;
GrabageCollector.FreeObjects;
end;
function TTree.Empty : Boolean;
begin
Result := FRoot = nil;
end;
function TTree.Size : SizeType;
begin
if not FValidSize then
begin
FSize := NodeSubTreeSize(FRoot);
FValidSize := true;
end;
Result := FSize;
end;
function TTree.IsDefinedOrder : Boolean;
begin
Result := false;
end;
procedure TTree.InsertNode(var node : PTreeNode; parent, rsibling : PTreeNode;
aitem : ItemType);
begin
NewNode(node);
node^.Parent := parent;
with node^ do
begin
RightSibling := rsibling;
LeftmostChild := nil;
Item := aitem;
end;
Inc(FSize);
end;
function TTree.ExtractNodePreOrder(var node : PTreeNode;
fadvance : Boolean) : PTreeNode;
var
node1, child, nnode : PTreeNode;
begin
Assert(node <> nil, msgInvalidIterator);
if node^.Parent <> nil then
begin
{ insert the children of node at the place of node }
nnode := ReplaceNodeWithChildren(node, fadvance);
Result := node^.Parent;
DisposeNode(node);
node := nnode;
end else
begin
child := node^.LeftmostChild;
Result := node^.Parent;
DisposeNode(node);
node := child;
FRoot := child;
if child <> nil then
begin
child^.Parent := nil;
node1 := child^.RightSibling;
child^.RightSibling := nil;
InsertNodeAsRightMostLeaf(child, node1);
end;
end;
Dec(FSize);
end;
function TTree.ExtractNodePostOrder(var node : PTreeNode;
fadvance : Boolean) : PTreeNode;
var
nnode : PTreeNode;
begin
Assert(node <> nil, msgInvalidIterator);
if node^.Parent <> nil then
begin
{ move children of node to the place of node }
if node^.RightSibling <> nil then
begin
if fadvance then
nnode := LeftMostLeafNode(node^.RightSibling)
else
nnode := nil;
end else
nnode := node^.Parent;
ReplaceNodeWithChildren(node, false);
Result := node^.Parent;
DisposeNode(node);
node := nnode;
end else
begin
ReorganiseTreeRight(node, self);
Result := node^.Parent;
DisposeNode(node);
node := nil;
end;
Dec(FSize);
end;
function TTree.ExtractNodeInOrder(var node : PTreeNode;
fadvance : Boolean) : PTreeNode;
var
child, lsib, nnode, parent : PTreeNode;
{ returns the parent of the node actually disposed }
function ShiftItemsUp(aparent, rsib : PTreeNode) : PTreeNode;
begin
nnode := LeftMostLeafNode(rsib);
while (nnode^.Parent^.LeftmostChild = nnode) and
(nnode^.RightSibling <> nil) do
begin
aparent^.Item := nnode^.Item;
nnode^.Item := nnode^.Parent^.Item;
aparent := nnode^.Parent;
nnode := LeftMostLeafNode(nnode^.RightSibling);
end;
aparent^.Item := nnode^.Item;
Result := nnode^.Parent;
RemoveConnections(nnode);
DisposeNode(nnode);
end;
begin
Assert(node <> nil, msgInvalidIterator);
child := node^.LeftmostChild;
if child <> nil then
begin
if child^.RightSibling <> nil then
begin
Result := ShiftItemsUp(node, child^.RightSibling);
end else
begin
if fadvance then
nnode := NextInOrderNode(node)
else
nnode := nil;
{ replace node with child }
parent := node^.Parent;
lsib := LeftSiblingNode(node);
if lsib <> nil then
lsib^.RightSibling := child
else if parent <> nil then
parent^.LeftmostChild := child
else
FRoot := child;
child^.Parent := parent;
child^.RightSibling := node^.RightSibling;
Result := node^.Parent;
DisposeNode(node);
node := nnode;
end;
end else
begin
if (node^.Parent <> nil) and
(node^.Parent^.LeftmostChild = node) and
(node^.RightSibling <> nil) then
begin
node^.Item := node^.Parent^.Item;
Result := ShiftItemsUp(node^.Parent, node^.RightSibling);