-
Notifications
You must be signed in to change notification settings - Fork 1
/
adtbintree_impl.i
2000 lines (1721 loc) · 53.5 KB
/
adtbintree_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
adtbintree_impl.i::prefix=&_mcp_prefix&::item_type=&ItemType&
}
&include adtbintree.defs
&include adtbintree_impl.mcp
{ ---------------------------- Helper routines ------------------------------- }
function LeftMostLeafNode(subtree : PBinaryTreeNode) : PBinaryTreeNode;
begin
Result := subtree;
while subtree <> nil do
begin
Result := subtree;
subtree := Result^.LeftChild;
if subtree = nil then
subtree := Result^.RightChild;
end;
end;
function RightMostLeafNode(subtree : PBinaryTreeNode) : PBinaryTreeNode;
begin
Result := subtree;
while subtree <> nil do
begin
Result := subtree;
subtree := Result^.RightChild;
if subtree = nil then
subtree := Result^.LeftChild;
end;
end;
{ returns the node which is visited first in in-order traversal; it's
the node whose all ancestors are left children of their parents
(except for the root). }
function FirstInOrderNode(subtree : PBinaryTreeNode) : PBinaryTreeNode;
begin
Result := subtree;
if Result <> nil then
while Result^.LeftChild <> nil do
Result := Result^.LeftChild;
end;
{ returns the node which is visited last in in-order traversal; it's
the node whose all ancestors are right children of their parents
(except for the root). }
function LastInOrderNode(subtree : PBinaryTreeNode) : PBinaryTreeNode;
begin
Result := subtree;
if Result <> nil then
while Result^.RightChild <> nil do
Result := Result^.RightChild;
end;
function NextPreOrderNode(node : PBinaryTreeNode) : PBinaryTreeNode;
begin
Assert(node <> nil, msgInvalidIterator);
if node^.LeftChild <> nil then
node := node^.LeftChild
else if node^.RightChild <> nil then
node := node^.RightChild
else
begin
while (node^.Parent <> nil) and
((node^.Parent^.RightChild = node) or (node^.Parent^.RightChild = nil)) do
begin
node := node^.Parent;
end;
node := node^.Parent;
if node <> nil then
node := node^.RightChild;
end;
Result := node;
end;
function NextPostOrderNode(node : PBinaryTreeNode) : PBinaryTreeNode;
begin
Assert(node <> nil, msgInvalidIterator);
if node^.Parent = nil then
node := nil
else if node^.Parent^.LeftChild = node then
begin
if node^.Parent^.RightChild <> nil then
begin
node := LeftMostLeafNode(node^.Parent^.RightChild);
end else
node := node^.Parent;
end else
node := node^.Parent;
Result := node;
end;
function NextInOrderNode(node : PBinaryTreeNode) : PBinaryTreeNode;
begin
Assert(node <> nil, msgInvalidIterator);
if node^.RightChild <> nil then
begin
node := FirstInOrderNode(node^.RightChild);
end else { node is the right child of its parent }
begin
{ go to the ancestor node that has not yet been visited; this is the
node whose right child has not yet been visited }
while (node^.Parent <> nil) and (node^.Parent^.LeftChild <> node) do
begin
node := node^.Parent;
end;
node := node^.Parent;
end;
Result := node;
end;
function PrevPreOrderNode(node, root : PBinaryTreeNode) : PBinaryTreeNode;
begin
Assert(root <> nil, msgRetreatingStartIterator);
Assert(node <> root, msgRetreatingStartIterator);
if node <> nil then
begin
if node^.Parent^.LeftChild <> node then
Result := RightMostLeafNode(node^.Parent^.LeftChild)
else begin
Result := node^.Parent;
end;
end else
Result := RightMostLeafNode(root);
end;
function PrevPostOrderNode(node, root : PBinaryTreeNode) : PBinaryTreeNode;
begin
Assert(root <> nil, msgRetreatingStartIterator);
if node <> nil then
begin
if node^.RightChild <> nil then
Result := node^.RightChild
else if node^.LeftChild <> nil then
Result := node^.LeftChild
else begin
while (node^.Parent <> nil) and
((node^.Parent^.LeftChild = node) or
(node^.Parent^.LeftChild = nil)) do
begin
node := node^.Parent;
end;
Assert(node^.Parent <> nil, msgRetreatingStartIterator);
Result := node^.Parent^.LeftChild;
end;
end else
Result := root;
end;
function PrevInOrderNode(node, root : PBinaryTreeNode) : PBinaryTreeNode;
begin
Assert(root <> nil, msgRetreatingStartIterator);
if node <> nil then
begin
if node^.LeftChild <> nil then
Result := LastInOrderNode(node^.LeftChild)
else begin
while (node^.Parent <> nil) and (node^.Parent^.LeftChild = node) do
begin
node := node^.Parent;
end;
Result := node^.Parent;
Assert(Result <> nil, msgRetreatingStartIterator);
end;
end else
Result := LastInOrderNode(root);
end;
function NodeDepth(node : PBinaryTreeNode) : SizeType;
begin
Result := 0;
while node^.Parent <> nil do
begin
Inc(Result);
node := node^.Parent;
end;
end;
function NodeHeight(node : PBinaryTreeNode) : SizeType;
begin
Result := 0;
if node^.LeftChild <> nil then
Result := NodeHeight(node^.leftchild) + 1;
if node^.RightChild <> nil then
Result := Max(Result, NodeHeight(node^.RightChild) + 1);
end;
{ inserts node at the left of parent and shifts parent left, i.e. if
parent has three children after insertion, takes the right-most one
and calls itself recursively for the next node after parent in
level-order; queue is modified and deallocated; it should contain
nodes as if parent has just been visited }
procedure InsertLeftAndShiftBy1(parent, node : PBinaryTreeNode;
var queue : TPointerDynamicArray);
begin
try
try
repeat
node^.Parent := parent;
if parent^.LeftChild = nil then
begin
parent^.LeftChild := node;
break;
end else if parent^.RightChild = nil then
begin
parent^.RightChild := parent^.LeftChild;
parent^.LeftChild := node;
break;
end else
begin
ArrayCircularPushBack(queue, node); { may raise }
ArrayCircularPushBack(queue, parent^.LeftChild); { may raise }
parent^.LeftChild := node;
node := parent^.RightChild;
parent^.RightChild := ArrayCircularGetItem(queue, queue^.Size - 1);
parent := ArrayCircularPopFront(queue);
end;
until false;
except
{ save what you can - insert node somewhere in order not to
leak it, no matter where }
while parent^.LeftChild <> nil do
parent := parent^.LeftChild;
parent^.LeftChild := node;
node^.Parent := parent;
raise;
end;
finally
ArrayDeallocate(queue);
end;
end;
{ the same as above, but inserts two nodes }
procedure InsertLeftAndShiftBy2(parent, node1, node2 : PBinaryTreeNode;
queue : TPointerDynamicArray);
begin
try
try
repeat
node1^.Parent := parent;
node2^.Parent := parent;
if (parent^.LeftChild = nil) and (parent^.RightChild = nil) then
begin
parent^.LeftChild := node1;
parent^.RightChild := node2;
break;
end else if (parent^.LeftChild = nil) then
begin
parent^.LeftChild := node1;
node1 := parent^.RightChild;
parent^.RightChild := node2;
ArrayCircularPushBack(queue, parent^.LeftChild);
ArrayCircularPushBack(queue, parent^.RightChild);
parent := ArrayCircularPopFront(queue);
InsertLeftAndShiftBy1(parent, node1, queue);
queue := nil;
break;
end else if (parent^.RightChild = nil) then
begin
parent^.RightChild := node2;
node2 := parent^.RightChild;
parent^.LeftChild := node1;
ArrayCircularPushBack(queue, parent^.LeftChild);
ArrayCircularPushBack(queue, parent^.RightChild);
parent := ArrayCircularPopFront(queue);
InsertLeftAndShiftBy1(parent, node2, queue);
queue := nil;
break;
end else
begin
ArrayCircularPushBack(queue, node1);
ArrayCircularPushBack(queue, node2);
node1 := parent^.LeftChild;
node2 := parent^.RightChild;
parent^.LeftChild := ArrayCircularGetItem(queue, queue^.Size - 2);
parent^.RightChild := ArrayCircularGetItem(queue, queue^.Size - 1);
parent := ArrayCircularPopFront(queue);
end;
until false;
except
{ insert node1 and node2 somewhere in order not to leak them }
while parent^.LeftChild <> nil do
parent := parent^.LeftChild;
parent^.LeftChild := node1;
node1^.Parent := parent;
while parent^.LeftChild <> nil do
parent := parent^.LeftChild;
node1^.Parent := nil;
raise;
end;
finally
ArrayDeallocate(queue);
end;
end;
{ **************************************************************************** }
{ Binary tree }
{ **************************************************************************** }
(* Notes on implementation of TBinaryTree:
* TBinaryTree is a binary tree implemented as linked nodes. Each node has
* a pointer to its Parent, left child (LeftChild) and right child (RightChild).
* If some of them is not present the appropriate pointer is nil. The number of
* Items is stored in FSize field, so that the Size operation takes an amortized
* O(1) time.
*
*)
{ ------------------------------ TBinaryTree --------------------------------- }
constructor TBinaryTree.Create;
begin
inherited;
InitFields;
end;
constructor TBinaryTree.CreateCopy(const cont : TBinaryTree;
const itemCopier : IUnaryFunctor);
var
src, destparent : PBinaryTreeNode;
pdest : ^PBinaryTreeNode;
begin
inherited CreateCopy(cont);
InitFields;
if itemCopier <> nil then
begin
{ copy nodes while going pre-order }
src := cont.FRoot;
pdest := @FRoot;
destparent := nil;
try
repeat
NewNode(pdest^);
with pdest^^ do
begin
Item := itemCopier.Perform(src^.Item); { may raise }
Parent := destparent;
LeftChild := nil;
RightChild := nil;
end;
Inc(FSize);
if src^.LeftChild <> nil then
begin
destparent := pdest^;
pdest := @pdest^^.LeftChild;
src := src^.LeftChild;
end else if src^.RightChild <> nil then
begin
destparent := pdest^;
pdest := @pdest^^.RightChild;
src := src^.RightChild;
end else
begin
while (src^.Parent <> nil) and
((src^.Parent^.RightChild = src) or
(src^.Parent^.RightChild = nil)) do
begin
src := src^.Parent;
pdest := @pdest^^.Parent;
destparent := destparent^.Parent;
end;
if src^.Parent = nil then
begin
break;
end else
begin
src := src^.Parent^.RightChild;
pdest := @destparent^.RightChild;
end;
end;
until false;
except
DisposeNode(pdest^);
pdest^ := nil;
raise;
end;
cont.FSize := FSize;
cont.FValidSize := true;
end; { end itemCopier <> nil }
end; { end CreateCopy }
destructor TBinaryTree.Destroy;
begin
Clear;
inherited;
end;
procedure TBinaryTree.InitFields;
begin
FRoot := nil;
FSize := 0;
FValidSize := true;
end;
procedure TBinaryTree.DisposeNodeAndItem(node : PBinaryTreeNode);
begin
DisposeItem(node^.Item);
DisposeNode(node);
end;
procedure TBinaryTree.ReplaceNode(old, pnewnode : PBinaryTreeNode);
begin
Assert(old <> nil, msgInternalError);
if old^.Parent <> nil then
begin
if old^.Parent^.LeftChild = old then
old^.Parent^.LeftChild := pnewnode
else
old^.Parent^.RightChild := pnewnode;
if pnewnode <> nil then
pnewnode^.Parent := old^.Parent;
end else
begin
FRoot := pnewnode;
if pnewnode <> nil then
pnewnode^.Parent := nil;
end;
end;
{ this cannot manipulate FSize or FValidSize as they are changed later }
procedure TBinaryTree.RemoveConnections(node : PBinaryTreeNode);
begin
Assert(node <> nil, msgInternalError);
if node^.Parent = nil then
begin
FRoot := nil;
end else if node^.Parent^.LeftChild = node then
begin
node^.Parent^.LeftChild := nil;
end else { node^.Parent^.RightChild = node }
begin
node^.Parent^.RightChild := nil;
end;
end;
function TBinaryTree.CopySelf(const ItemCopier : IUnaryFunctor) : TContainerAdt;
begin
Result := TBinaryTree.CreateCopy(self, ItemCopier);
end;
procedure TBinaryTree.Swap(cont : TContainerAdt);
var
tree : TBinaryTree;
begin
if cont is TBinaryTree then
begin
BasicSwap(cont);
tree := TBinaryTree(cont);
ExchangePtr(FRoot, tree.FRoot);
ExchangeData(FSize, tree.FSize, SizeOf(SizeType));
ExchangeData(FValidSize, tree.FValidSize, SizeOf(Boolean));
end else
inherited;
end;
function TBinaryTree.Root : TBinaryTreeIterator;
begin
Result := TBinaryTreeIterator.Create(FRoot, self);
end;
function TBinaryTree.BasicRoot : TBasicTreeIterator;
begin
Result := TBinaryTreeIterator.Create(FRoot, self);
end;
function TBinaryTree.Finish : TBasicTreeIterator;
begin
Result := TBinaryTreeIterator.Create(nil, self);
end;
function TBinaryTree.PreOrderIterator : TPreOrderIterator;
begin
Result := TBinaryTreePreOrderIterator.Create(self);
Result.StartTraversal;
end;
function TBinaryTree.PostOrderIterator : TPostOrderIterator;
begin
Result := TBinaryTreePostOrderIterator.Create(self);
Result.StartTraversal;
end;
function TBinaryTree.InOrderIterator : TInOrderIterator;
begin
Result := TBinaryTreeInOrderIterator.Create(self);
Result.StartTraversal;
end;
function TBinaryTree.LevelOrderIterator : TLevelOrderIterator;
begin
Result := TBinaryTreeLevelOrderIterator.Create(self);
Result.StartTraversal;
end;
function TBinaryTree.DeleteSubTree(node : TBasicTreeIterator) : SizeType;
var
xnode : PBinaryTreeNode;
begin
Assert(node is TBinaryTreeIterator, msgDeletingInvalidIterator);
Assert(node.Owner = self, msgWrongOwner);
Assert(TBinaryTreeIterator(node).Node <> nil, msgInvalidIterator);
xnode := TBinaryTreeIterator(node).Node;
Result := NodeSubTreeDelete(xnode);
end;
procedure TBinaryTree.InsertAsRoot(aitem : ItemType);
var
temp : PBinaryTreeNode;
begin
NewNode(temp);
temp^.LeftChild := FRoot;
if FRoot <> nil then
FRoot^.Parent := temp;
FRoot := temp;
temp^.Parent := nil;
temp^.RightChild := nil;
temp^.Item := aitem;
Inc(FSize);
end;
procedure TBinaryTree.InsertAsLeftChild(const node : TBasicTreeIterator;
aitem : ItemType);
var
xnode, pnewnode : PBinaryTreeNode;
begin
Assert(node is TBinaryTreeIterator, msgInvalidIterator);
Assert(node.Owner = self, msgWrongOwner);
xnode := TBinaryTreeIterator(node).Node;
NewNode(pnewnode);
pnewnode^.Item := aitem;
pnewnode^.LeftChild := xnode^.LeftChild;
xnode^.LeftChild := pnewnode;
pnewnode^.RightChild := nil;
pnewnode^.Parent := xnode;
Inc(FSize);
end;
procedure TBinaryTree.InsertAsRightChild(const node : TBasicTreeIterator;
aitem : ItemType);
var
xnode, pnewnode : PBinaryTreeNode;
begin
Assert(node is TBinaryTreeIterator, msgInvalidIterator);
Assert(node.Owner = self, msgWrongOwner);
xnode := TBinaryTreeIterator(node).Node;
NewNode(pnewnode);
pnewnode^.Item := aitem;
pnewnode^.RightChild := xnode^.RightChild;
xnode^.RightChild := pnewnode;
pnewnode^.LeftChild := nil;
pnewnode^.Parent := xnode;
Inc(FSize);
end;
procedure TBinaryTree.MoveToLeftChild(const node, src : TBasicTreeIterator);
var
dest, source : PBinaryTreeNode;
tree2 : TBinaryTree;
begin
Assert(node is TBinaryTreeIterator, msgInvalidIterator);
Assert(src is TBinaryTreeIterator, msgInvalidIterator);
Assert(TBinaryTreeIterator(node).Node <> nil, msgInvalidIterator);
Assert(TBinaryTreeIterator(src).Node <> nil, msgInvalidIterator);
Assert(TBinaryTreeIterator(node).Node^.LeftChild = nil, msgHasLeftChild);
Assert(node.Owner = self, msgWrongOwner);
dest := TBinaryTreeIterator(node).Node;
source := TBinaryTreeIterator(src).Node;
tree2 := TBinaryTreeIterator(src).FTree;
if (source^.RightChild = nil) and (source^.LeftChild = nil) then
begin
Inc(FSize);
Dec(tree2.FSize);
end else if source^.Parent = nil then
begin
FSize := FSize + tree2.FSize;
FValidSize := FValidSize and tree2.FValidSize;
tree2.FSize := 0;
tree2.FValidSize := true;
tree2.FRoot := nil;
end else if tree2 <> self then
begin
FValidSize := false;
tree2.FValidSize := false;
end;
tree2.RemoveConnections(source);
dest^.LeftChild := source;
source^.Parent := dest;
end;
procedure TBinaryTree.MoveToRightChild(const node, src : TBasicTreeIterator);
var
dest, source : PBinaryTreeNode;
tree2 : TBinaryTree;
begin
Assert(node is TBinaryTreeIterator, msgInvalidIterator);
Assert(src is TBinaryTreeIterator, msgInvalidIterator);
Assert(TBinaryTreeIterator(node).Node <> nil, msgInvalidIterator);
Assert(TBinaryTreeIterator(src).Node <> nil, msgInvalidIterator);
Assert(TBinaryTreeIterator(node).Node^.RightChild = nil, msgHasRightChild);
Assert(node.Owner = self, msgWrongOwner);
dest := TBinaryTreeIterator(node).Node;
source := TBinaryTreeIterator(src).Node;
tree2 := TBinaryTreeIterator(src).FTree;
if (source^.RightChild = nil) and (source^.LeftChild = nil) then
begin
Inc(FSize);
Dec(tree2.FSize);
end else if source^.Parent = nil then
begin
FSize := FSize + tree2.FSize;
FValidSize := FValidSize and tree2.FValidSize;
tree2.FSize := 0;
tree2.FValidSize := true;
tree2.FRoot := nil;
end else if tree2 <> self then
begin
FValidSize := false;
tree2.FValidSize := false;
end;
tree2.RemoveConnections(source);
dest^.RightChild := source;
source^.Parent := dest;
end;
procedure TBinaryTree.RotateSingleLeft(const node : TBasicTreeIterator);
begin
Assert(node is TBinaryTreeIterator, msgInvalidIterator);
RotateNodeSingleLeft(TBinaryTreeIterator(node).Node);
end;
procedure TBinaryTree.RotateDoubleLeft(const node : TBasicTreeIterator);
begin
Assert(node is TBinaryTreeIterator, msgInvalidIterator);
RotateNodeDoubleLeft(TBinaryTreeIterator(node).Node);
end;
procedure TBinaryTree.RotateSingleRight(const node : TBasicTreeIterator);
begin
Assert(node is TBinaryTreeIterator, msgInvalidIterator);
RotateNodeSingleRight(TBinaryTreeIterator(node).Node);
end;
procedure TBinaryTree.RotateDoubleRight(const node : TBasicTreeIterator);
begin
Assert(node is TBinaryTreeIterator, msgInvalidIterator);
RotateNodeDoubleRight(TBinaryTreeIterator(node).Node);
end;
procedure TBinaryTree.Clear;
begin
if FRoot <> nil then
begin
NodeSubTreeDelete(FRoot);
FRoot := nil;
end;
FSize := 0;
FValidSize := true;
GrabageCollector.FreeObjects;
end;
function TBinaryTree.Empty : Boolean;
begin
Result := (FRoot = nil);
end;
function TBinaryTree.Size : SizeType;
begin
if not FValidSize then
begin
FSize := NodeSubTreeSize(FRoot);
FValidSize := true;
end;
Result := FSize;
end;
function TBinaryTree.IsDefinedOrder : Boolean;
begin
Result := false;
end;
procedure TBinaryTree.InsertNode(var node : PBinaryTreeNode;
parent : PBinaryTreeNode; aitem : ItemType);
begin
NewNode(node);
node^.Parent := parent;
with node^ do
begin
LeftChild := nil;
RightChild := nil;
Item := aitem;
end;
Inc(FSize);
end;
function TBinaryTree.
ReplaceNodeWithLeftChild(node : PBinaryTreeNode) : PBinaryTreeNode;
begin
Result := node^.RightChild;
ReplaceNode(node, node^.LeftChild);
DisposeNode(node);
Dec(FSize);
end;
function TBinaryTree.
ReplaceNodeWithRightChild(node : PBinaryTreeNode) : PBinaryTreeNode;
begin
Result := node^.LeftChild;
ReplaceNode(node, node^.RightChild);
DisposeNode(node);
Dec(FSize);
end;
function TBinaryTree.ExtractNodePreOrder(var node : PBinaryTreeNode;
fadvance : Boolean) : PBinaryTreeNode;
var
lleaf, next : PBinaryTreeNode;
begin
Assert(node <> nil, msgInvalidIterator);
if (node^.RightChild = nil) then
begin
ReplaceNode(node, node^.LeftChild);
next := node;
node := node^.LeftChild;
if fadvance and (node = nil) then
node := NextPreOrderNode(next);
Result := next^.Parent;
DisposeNode(next);
end else if node^.LeftChild = nil then
begin
ReplaceNode(node, node^.RightChild);
next := node;
node := node^.RightChild;
Result := next^.Parent;
DisposeNode(next);
end else { node has both children }
begin
{ 'shift' items up on the path from node to the left-most leaf
in the sub-tree of node - nodes on this path are visited first
starting from node, so we can shift them up without changing
pre-order of nodes; }
lleaf := node;
next := node;
while next <> nil do
begin
lleaf^.Item := next^.Item;
lleaf := next;
next := next^.LeftChild;
if next = nil then
next := lleaf^.RightChild;
end;
Result := lleaf^.Parent;
RemoveConnections(lleaf);
DisposeNode(lleaf);
end;
Dec(FSize);
end;
function TBinaryTree.ExtractNodePostOrder(var node : PBinaryTreeNode;
fadvance : Boolean) : PBinaryTreeNode;
var
rleaf, next : PBinaryTreeNode;
begin
Assert(node <> nil, msgInvalidIterator);
if (node^.LeftChild = nil) or (node^.RightChild = nil) then
begin
if fadvance then
next := NextPostOrderNode(node)
else
next := nil;
if node^.LeftChild = nil then
ReplaceNode(node, node^.RightChild)
else
ReplaceNode(node, node^.LeftChild);
Result := node^.Parent;
DisposeNode(node);
end else { node has both children }
begin
next := node;
{ shift items up on the path from node to the right-most leaf in
the sub-tree of node; this path is visited last in post-order }
rleaf := node;
while node <> nil do
begin
rleaf^.Item := node^.Item;
rleaf := node;
node := node^.RightChild;
if node = nil then
node := rleaf^.LeftChild;
end;
Result := rleaf^.Parent;
RemoveConnections(rleaf);
DisposeNode(rleaf);
end;
node := next;
Dec(FSize);
end;
function TBinaryTree.ExtractNodeInOrder(var node : PBinaryTreeNode;
fadvance : Boolean) : PBinaryTreeNode;
var
dummy : Boolean;
begin
Result := ExtractNodeInOrderAux(node, fadvance, dummy);
end;
function TBinaryTree.
ExtractNodeInOrderAux(var node : PBinaryTreeNode; fadvance : Boolean;
var isLeftChild : Boolean) : PBinaryTreeNode;
var
nnode : PBinaryTreeNode;
begin
Assert(node <> nil, msgInvalidIterator);
if (node^.RightChild <> nil) and (node^.LeftChild <> nil) then
begin
{ 'toss a coin' and either replace node with the next item after
it or the previous one; this is to keep the binary tree random
when used to implement binary search tree }
if Random(2) = 0 then
begin
{ replace with the next item }
nnode := FirstInOrderNode(node^.RightChild);
if nnode^.Parent^.LeftChild = nnode then
begin
isLeftChild := true;
nnode^.Parent^.LeftChild := nnode^.RightChild
end else { will happen when nnode = node^.RightChild }
begin
isLeftChild := false;
nnode^.Parent^.RightChild := nnode^.RightChild;
end;
if nnode^.RightChild <> nil then
nnode^.RightChild^.Parent := nnode^.Parent;
node^.Item := nnode^.Item;
Result := nnode^.Parent;
DisposeNode(nnode);
end else
begin
{ replace with the previous item }
nnode := LastInOrderNode(node^.LeftChild);
if nnode^.Parent^.RightChild = nnode then
begin
isLeftChild := false;
nnode^.Parent^.RightChild := nnode^.LeftChild;
end else
begin
isLeftChild := true;
nnode^.Parent^.LeftChild := nnode^.LeftChild;
end;
if nnode^.LeftChild <> nil then
nnode^.LeftChild^.Parent := nnode^.Parent;
node^.Item := nnode^.Item;
if fadvance then
node := FirstInOrderNode(node^.RightChild)
else
node := nil;
Result := nnode^.Parent;
DisposeNode(nnode);
end;
end else
begin
if fadvance then
nnode := NextInOrderNode(node)
else
nnode := nil;
Result := node^.Parent;
isLeftChild := (node^.Parent <> nil) and (node^.Parent^.LeftChild = node);
if node^.RightChild <> nil then
ReplaceNode(node, node^.RightChild)
else
ReplaceNode(node, node^.LeftChild);
DisposeNode(node);
node := nnode;
end;
Dec(FSize);
end;
procedure TBinaryTree.RotateNodeSingleLeft(node : PBinaryTreeNode);
var
parent, rchild : PBinaryTreeNode;
begin
Assert((node <> nil) and (node^.RightChild <> nil),
msgInvalidNodeForSingleLeftRotation);
parent := node^.Parent;
rchild := node^.RightChild;
node^.RightChild := rchild^.LeftChild;
if rchild^.LeftChild <> nil then
rchild^.LeftChild^.Parent := node;
rchild^.LeftChild := node;
node^.Parent := rchild;
rchild^.Parent := parent;
if parent <> nil then
begin
if parent^.LeftChild = node then
parent^.LeftChild := rchild
else
parent^.RightChild := rchild;
end else
begin
FRoot := rchild;
end;
end;
procedure TBinaryTree.RotateNodeDoubleLeft(node : PBinaryTreeNode);
var
parent, rchild, t : PBinaryTreeNode;
begin
Assert((node <> nil) and (node^.RightChild <> nil) and