-
Notifications
You must be signed in to change notification settings - Fork 0
/
bn_avl_tree.pas
1140 lines (1047 loc) · 30.7 KB
/
bn_avl_tree.pas
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
{ **********************************************************************
This file is part of the Free Component Library (FCL)
Copyright (c) 2008 by Mattias Gaertner
Average Level Tree implementation by Mattias Gaertner
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program 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.
**********************************************************************
Author: Mattias Gaertner
Being modified by 0xC45F4253 Tomas Brada for BrodNet project.
Abstract:
TAVLTree is an Average Level binary Tree. This binary tree is always
balanced, so that inserting, deleting and finding a node is performed in
O(log(#Nodes)).
}
unit BN_AVL_Tree;
{$ifdef FPC}{$mode objfpc}{$endif}{$H+}
interface
{off $DEFINE MEM_CHECK}
{off $DEFINE CheckAVLTreeNodeManager}
uses
{$IFDEF MEM_CHECK}MemCheck,{$ENDIF}
Classes, SysUtils;
type
TAVLTree = class;
TAVLTreeKey = type Pointer;
{ TAVLTreeNode }
TAVLTreeNode = class
private
Parent, Left, Right: TAVLTreeNode;
Balance: integer; // = RightDepth-LeftDepth -2..+2, after balancing: -1,0,+1
function TreeDepth: integer; // longest WAY down. e.g. only one node => 0 !
procedure ConsistencyCheck(Tree: TAVLTree); virtual;
function GetCount: SizeInt;
public
function Successor: TAVLTreeNode; // next right
function Precessor: TAVLTreeNode; // next left
procedure Assign(const other: TAVLTreeNode); virtual;
function Compare(const other: TAVLTreeNode): integer; virtual;
function CompareKey(const key: TAVLTreeKey): integer; virtual;
end;
TAVLTreeNodeClass = class of TAVLTreeNode;
TAVLTreeItem= TAVLTreeNode;
{ TAVLTreeNodeEnumerator }
TAVLTreeNodeEnumerator = class
protected
FCurrent: TAVLTreeNode;
FLowToHigh: boolean;
FTree: TAVLTree;
public
constructor Create(Tree: TAVLTree; aLowToHigh: boolean = true);
function GetEnumerator: TAVLTreeNodeEnumerator; inline;
function MoveNext: Boolean;
property Current: TAVLTreeNode read FCurrent;
property LowToHigh: boolean read FLowToHigh;
end;
TAVLTree = class
protected
FCount: SizeInt;
FNodeClass: TAVLTreeNodeClass;
FRoot: TAVLTreeNode;
procedure BalanceAfterInsert(ANode: TAVLTreeNode);
procedure BalanceAfterDelete(ANode: TAVLTreeNode);
procedure DeletingNode({%H-}aNode: TAVLTreeNode); virtual;
function FindInsertPos(ANode: TAVLTreeNode): TAVLTreeNode;
procedure Init; virtual;
procedure NodeAdded({%H-}aNode: TAVLTreeNode); virtual;
procedure RotateLeft(aNode: TAVLTreeNode); virtual;
procedure RotateRight(aNode: TAVLTreeNode); virtual;
procedure SwitchPositionWithSuccessor(aNode, aSuccessor: TAVLTreeNode); virtual;
procedure SetNodeClass(const AValue: TAVLTreeNodeClass);
public
constructor Create;
destructor Destroy; override;
property NodeClass: TAVLTreeNodeClass read FNodeClass write SetNodeClass; // used for new nodes
// add, delete, remove, move
(* Add ANode to the tree. No copy. *)
procedure Add(ANode: TAVLTreeNode);
(* Optimized Add for ascending elements. *)
procedure AddAscendingSequence(Node: TAVLTreeNode; LastAdded: TAVLTreeNode;
var Successor: TAVLTreeNode);
(* Remove exactly this node from the tree. Node is not freed. *)
procedure UnlinkNode(ANode: TAVLTreeNode);
(* Removes and Destroys node which node.Compare(Key) = 0. Key is not changed. *)
function DeleteNode(const Key: TAVLTreeNode): boolean;
(* Removes and Destroys node which node.CompareKey(Key) = 0. *)
function Delete(Key: TAVLTreeKey): boolean;
(* Desroys all nodes in the tree. *)
procedure Clear;
function Equals(Obj: TObject): boolean; override; // same as IsEqual(aTree,false)
function IsEqual(aTree: TAVLTree; CheckDataPointer: boolean): boolean; // checks only keys or Data (references), not the data itself, O(n)
procedure Assign(aTree: TAVLTree); virtual; // clear and copy all Data (references), O(n)
// search
property Root: TAVLTreeNode read fRoot;
property Count: SizeInt read FCount;
(* Get node where node.CompareKey(Key) = 0, or nil when not found. *)
function Find(Key: TAVLTreeKey): TAVLTreeNode; // O(log(n))
(* Get node where node.Compare(ANode) = 0, or nil when not found. *)
function FindNode(ANode: TAVLTreeNode): TAVLTreeNode; // O(log(n))
(* Get node matching key or if not found, nearest to the key. *)
function FindNearest(Key: TAVLTreeKey): TAVLTreeNode; // O(log(n))
(* Get first node in a tree. *)
function FindLowest: TAVLTreeNode; // O(log(n))
(* Get last node in a tree. *)
function FindHighest: TAVLTreeNode; // O(log(n))
function FindLeftMostNode(ANode: TAVLTreeNode): TAVLTreeNode;
function FindRightMostNode(ANode: TAVLTreeNode): TAVLTreeNode;
function FindSuccessor(ANode: TAVLTreeNode): TAVLTreeNode; inline;
function FindPrecessor(ANode: TAVLTreeNode): TAVLTreeNode; inline;
function FindNearestNode(ANode: TAVLTreeNode): TAVLTreeNode;
// enumerators
function GetEnumerator: TAVLTreeNodeEnumerator;
function GetEnumeratorHighToLow: TAVLTreeNodeEnumerator;
// consistency
procedure ConsistencyCheck; virtual; // JuMa: changed to procedure and added "virtual".
//procedure WriteNodeReport(s: TStream; ANode: TAVLTreeNode);
procedure WriteSubtreeReport(s: TStream; ANode: TAVLTreeNode);
procedure WriteReportToStream(s: TStream);
end;
implementation
{ TAVLTreeNodeEnumerator }
constructor TAVLTreeNodeEnumerator.Create(Tree: TAVLTree; aLowToHigh: boolean);
begin
FTree:=Tree;
FLowToHigh:=aLowToHigh;
end;
function TAVLTreeNodeEnumerator.GetEnumerator: TAVLTreeNodeEnumerator;
begin
Result:=Self;
end;
function TAVLTreeNodeEnumerator.MoveNext: Boolean;
begin
if FLowToHigh then begin
if FCurrent<>nil then
FCurrent:=FCurrent.Successor
else
FCurrent:=FTree.FindLowest;
end else begin
if FCurrent<>nil then
FCurrent:=FCurrent.Precessor
else
FCurrent:=FTree.FindHighest;
end;
Result:=FCurrent<>nil;
end;
{ TAVLTree }
procedure TAVLTree.AddAscendingSequence(Node: TAVLTreeNode; LastAdded: TAVLTreeNode;
var Successor: TAVLTreeNode);
{ This is an optimized version of "Add" for adding an ascending sequence of
nodes.
It uses the LastAdded and Successor to skip searching for an insert position.
For nodes with same value the order of the sequence is kept.
Usage:
LastNode:=nil; // TAvlTreeNode
Successor:=nil; // TAvlTreeNode
for i:=1 to 1000 do
LastNode:=Tree.AddAscendingSequence(TItem.Create(i),LastNode,Successor);
}
var
InsertPos: TAVLTreeNode;
begin
if (LastAdded<>nil) and (LastAdded.Compare(Node)<=0)
and ((Successor=nil) or (Node.Compare(Successor)<=0)) then begin
// Data is between LastAdded and Successor
inc(FCount);
if LastAdded.Right=nil then begin
Node.Parent:=LastAdded;
LastAdded.Right:=Node;
end else begin
InsertPos:=LastAdded.Right;
while InsertPos.Left<>nil do
InsertPos:=InsertPos.Left;
Node.Parent:=InsertPos;
InsertPos.Left:=Node;
end;
NodeAdded(Node);
BalanceAfterInsert(Node);
end else begin
// normal Add
Add(Node);
Successor:=Node.Successor;
end;
end;
procedure TAVLTree.Add(ANode: TAVLTreeNode);
// add a node. If there are already nodes with the same value it will be
// inserted rightmost
var InsertPos: TAVLTreeNode;
InsertComp: integer;
begin
ANode.Left:=nil;
ANode.Right:=nil;
inc(FCount);
if Root<>nil then begin
InsertPos:=FindInsertPos(ANode);
InsertComp:=ANode.Compare(InsertPos);
ANode.Parent:=InsertPos;
if InsertComp<0 then begin
// insert to the left
InsertPos.Left:=ANode;
end else begin
// insert to the right
InsertPos.Right:=ANode;
end;
NodeAdded(ANode);
BalanceAfterInsert(ANode);
end else begin
fRoot:=ANode;
ANode.Parent:=nil;
NodeAdded(ANode);
end;
end;
function TAVLTree.FindLowest: TAVLTreeNode;
begin
Result:=Root;
if Result<>nil then
while Result.Left<>nil do Result:=Result.Left;
end;
function TAVLTree.FindHighest: TAVLTreeNode;
begin
Result:=Root;
if Result<>nil then
while Result.Right<>nil do Result:=Result.Right;
end;
procedure TAVLTree.BalanceAfterDelete(ANode: TAVLTreeNode);
var
OldParent, OldRight, OldRightLeft, OldLeft, OldLeftRight: TAVLTreeNode;
begin
while ANode<>nil do begin
if ((ANode.Balance=+1) or (ANode.Balance=-1)) then exit;
OldParent:=ANode.Parent;
if (ANode.Balance=0) then begin
// Treeheight has decreased by one
if (OldParent=nil) then
exit;
if(OldParent.Left=ANode) then
Inc(OldParent.Balance)
else
Dec(OldParent.Balance);
ANode:=OldParent;
end else if (ANode.Balance=+2) then begin
// Node is overweighted to the right
OldRight:=ANode.Right;
if (OldRight.Balance>=0) then begin
// OldRight.Balance is 0 or -1
// rotate ANode,OldRight left
RotateLeft(ANode);
ANode.Balance:=(1-OldRight.Balance); // toggle 0 and 1
Dec(OldRight.Balance);
ANode:=OldRight;
end else begin
// OldRight.Balance=-1
{ double rotate
= rotate OldRightLeft,OldRight right
and then rotate ANode,OldRightLeft left
OldParent OldParent
| |
ANode OldRightLeft
\ / \
OldRight => ANode OldRight
/ \ /
OldRightLeft OldRightLeftLeft OldRightLeftRight
/ \
OldRightLeftLeft OldRightLeftRight
}
OldRightLeft:=OldRight.Left;
RotateRight(OldRight);
RotateLeft(ANode);
if (OldRightLeft.Balance<=0) then
ANode.Balance:=0
else
ANode.Balance:=-1;
if (OldRightLeft.Balance>=0) then
OldRight.Balance:=0
else
OldRight.Balance:=+1;
OldRightLeft.Balance:=0;
ANode:=OldRightLeft;
end;
end else begin
// Node.Balance=-2
// Node is overweighted to the left
OldLeft:=ANode.Left;
if (OldLeft.Balance<=0) then begin
// rotate OldLeft,ANode right
RotateRight(ANode);
ANode.Balance:=(-1-OldLeft.Balance); // toggle 0 and -1
Inc(OldLeft.Balance);
ANode:=OldLeft;
end else begin
// OldLeft.Balance = 1
{ double rotate left right
= rotate OldLeft,OldLeftRight left
and then rotate OldLeft,ANode right
OldParent OldParent
| |
ANode OldLeftRight
/ / \
OldLeft => OldLeft ANode
\ \ /
OldLeftRight OldLeftRightLeft OldLeftRightRight
/ \
OldLeftRightLeft OldLeftRightRight
}
OldLeftRight:=OldLeft.Right;
RotateLeft(OldLeft);
RotateRight(ANode);
if (OldLeftRight.Balance>=0) then
ANode.Balance:=0
else
ANode.Balance:=+1;
if (OldLeftRight.Balance<=0) then
OldLeft.Balance:=0
else
OldLeft.Balance:=-1;
OldLeftRight.Balance:=0;
ANode:=OldLeftRight;
end;
end;
end;
end;
procedure TAVLTree.DeletingNode(aNode: TAVLTreeNode);
// called by Delete
// Node.Left=nil or Node.Right=nil
begin
// for descendants to override
end;
procedure TAVLTree.SetNodeClass(const AValue: TAVLTreeNodeClass);
begin
if FNodeClass=AValue then Exit;
if Count>0 then
raise Exception.Create(ClassName+'.SetNodeClass Count='+IntToStr(Count)
+' Old='+FNodeClass.ClassName+' New='+AValue.ClassName);
FNodeClass:=AValue;
end;
procedure TAVLTree.BalanceAfterInsert(ANode: TAVLTreeNode);
var
OldParent, OldRight, OldLeft: TAVLTreeNode;
begin
OldParent:=ANode.Parent;
while (OldParent<>nil) do begin
if (OldParent.Left=ANode) then begin
// Node is left child
dec(OldParent.Balance);
if (OldParent.Balance=0) then exit;
if (OldParent.Balance=-1) then begin
ANode:=OldParent;
OldParent:=ANode.Parent;
continue;
end;
// OldParent.Balance=-2
if (ANode.Balance=-1) then begin
{ rotate ANode,ANode.Parent right
OldParentParent OldParentParent
| |
OldParent => ANode
/ \
ANode OldParent
\ /
OldRight OldRight }
RotateRight(OldParent);
ANode.Balance:=0;
OldParent.Balance:=0;
end else begin
// Node.Balance = +1
{ double rotate
= rotate ANode,OldRight left and then rotate OldRight,OldParent right
OldParentParent OldParentParent
| |
OldParent OldRight
/ => / \
ANode ANode OldParent
\ \ /
OldRight OldRightLeft OldRightRight
/ \
OldRightLeft OldRightRight
}
OldRight:=ANode.Right;
RotateLeft(ANode);
RotateRight(OldParent);
if (OldRight.Balance<=0) then
ANode.Balance:=0
else
ANode.Balance:=-1;
if (OldRight.Balance=-1) then
OldParent.Balance:=1
else
OldParent.Balance:=0;
OldRight.Balance:=0;
end;
exit;
end else begin
// Node is right child
Inc(OldParent.Balance);
if (OldParent.Balance=0) then exit;
if (OldParent.Balance=+1) then begin
ANode:=OldParent;
OldParent:=ANode.Parent;
continue;
end;
// OldParent.Balance = +2
if(ANode.Balance=+1) then begin
{ rotate OldParent,ANode left
OldParentParent OldParentParent
| |
OldParent => ANode
\ /
ANode OldParent
/ \
OldLeft OldLeft }
RotateLeft(OldParent);
ANode.Balance:=0;
OldParent.Balance:=0;
end else begin
// Node.Balance = -1
{ double rotate
= rotate OldLeft,ANode right and then rotate OldParent,OldLeft right
OldParentParent OldParentParent
| |
OldParent OldLeft
\ => / \
ANode OldParent ANode
/ \ /
OldLeft OldLeftLeft OldLeftRight
/ \
OldLeftLeft OldLeftRight
}
OldLeft:=ANode.Left;
RotateRight(ANode);
RotateLeft(OldParent);
if (OldLeft.Balance>=0) then
ANode.Balance:=0
else
ANode.Balance:=+1;
if (OldLeft.Balance=+1) then
OldParent.Balance:=-1
else
OldParent.Balance:=0;
OldLeft.Balance:=0;
end;
exit;
end;
end;
end;
procedure TAVLTree.Clear;
procedure DeleteNodeR(ANode: TAVLTreeNode);
begin
if ANode<>nil then begin
if ANode.Left<>nil then DeleteNodeR(ANode.Left);
if ANode.Right<>nil then DeleteNodeR(ANode.Right);
ANode.Destroy;
end;
end;
// Clear
begin
DeleteNodeR(Root);
fRoot:=nil;
FCount:=0;
end;
constructor TAVLTree.Create;
begin
Init;
end;
procedure TAVLTree.UnlinkNode(ANode: TAVLTreeNode);
var
OldParent, Child: TAVLTreeNode;
begin
{$IFDEF CheckAVLTreeNodeManager}
OldParent:=ANode;
while OldParent.Parent<>nil do OldParent:=OldParent.Parent;
if OldParent<>Root then
raise Exception.Create('TAVLTree.Delete'); // not my node
{$ENDIF}
if (ANode.Left<>nil) and (ANode.Right<>nil) then begin
// ANode has both: Left and Right
// Switch ANode position with Successor
// Because ANode.Right<>nil the Successor is a child of ANode
SwitchPositionWithSuccessor(ANode,ANode.Successor);
end;
// left or right is nil
DeletingNode(aNode);
OldParent:=ANode.Parent;
ANode.Parent:=nil;
if ANode.Left<>nil then
Child:=ANode.Left
else
Child:=ANode.Right;
if Child<>nil then
Child.Parent:=OldParent;
if (OldParent<>nil) then begin
// Node has parent
if (OldParent.Left=ANode) then begin
// Node is left child of OldParent
OldParent.Left:=Child;
Inc(OldParent.Balance);
end else begin
// Node is right child of OldParent
OldParent.Right:=Child;
Dec(OldParent.Balance);
end;
BalanceAfterDelete(OldParent);
end else begin
// Node was Root
fRoot:=Child;
end;
dec(FCount);
ANode.Destroy;
end;
function TAVLTree.Delete(Key: TAVLTreeKey): boolean;
var
ANode: TAvlTreeNode;
begin
ANode:=Find(Key);
if ANode<>nil then begin
UnlinkNode(ANode);
ANode.Destroy;
Result:=true;
end else
Result:=false;
end;
function TAVLTree.DeleteNode(const Key: TAvlTreeNode): boolean;
var
ANode: TAvlTreeNode;
begin
ANode:=FindNode(Key);
if ANode<>nil then begin
UnlinkNode(ANode);
ANode.Destroy;
Result:=true;
end else
Result:=false;
end;
destructor TAVLTree.Destroy;
begin
Clear;
inherited Destroy;
end;
function TAVLTree.GetEnumerator: TAVLTreeNodeEnumerator;
begin
Result:=TAVLTreeNodeEnumerator.Create(Self,true);
end;
function TAVLTree.GetEnumeratorHighToLow: TAVLTreeNodeEnumerator;
begin
Result:=TAVLTreeNodeEnumerator.Create(Self,false);
end;
function TAVLTree.Find(Key: TAVLTreeKey): TAVLTreeNode;
var Comp: integer;
begin
Result:=Root;
while (Result<>nil) do begin
Comp:=Result.CompareKey(Key);
if Comp=0 then exit;
if Comp>0 then begin
Result:=Result.Left
end else begin
Result:=Result.Right
end;
end;
end;
function TAVLTree.FindNode(ANode: TAVLTreeNode): TAVLTreeNode;
var Comp: integer;
begin
Result:=Root;
while (Result<>nil) do begin
Comp:=Result.Compare(ANode);
if Comp=0 then exit;
if Comp>0 then begin
Result:=Result.Left
end else begin
Result:=Result.Right
end;
end;
end;
function TAVLTree.FindNearest(Key: TAVLTreeKey): TAVLTreeNode;
var Comp: integer;
begin
Result:=fRoot;
while (Result<>nil) do begin
Comp:=Result.CompareKey(Key);
if Comp=0 then exit;
if Comp>0 then begin
if Result.Left<>nil then
Result:=Result.Left
else
exit;
end else begin
if Result.Right<>nil then
Result:=Result.Right
else
exit;
end;
end;
end;
function TAVLTree.FindLeftMostNode(ANode: TAVLTreeNode): TAVLTreeNode;
var
LeftNode: TAVLTreeNode;
begin
if ANode<>nil then begin
Result:=ANode;
repeat
LeftNode:=Result.Precessor;
if (LeftNode=nil) or (LeftNode.Compare(ANode)<>0) then break;
Result:=LeftNode;
until false;
end else begin
Result:=nil;
end;
end;
function TAVLTree.FindRightMostNode(ANode: TAVLTreeNode): TAVLTreeNode;
var
RightNode: TAVLTreeNode;
begin
if ANode<>nil then begin
Result:=ANode;
repeat
RightNode:=Result.Successor;
if (RightNode=nil) or (RightNode.Compare(ANode)<>0) then break;
Result:=RightNode;
until false;
end else begin
Result:=nil;
end;
end;
function TAVLTree.FindNearestNode(ANode: TAVLTreeNode): TAVLTreeNode;
var Comp: integer;
begin
Result:=Root;
while (Result<>nil) do begin
Comp:=Result.Compare(ANode);
if Comp=0 then exit;
if Comp>0 then begin
if Result.Left<>nil then
Result:=Result.Left
else
exit;
end else begin
if Result.Right<>nil then
Result:=Result.Right
else
exit;
end;
end;
end;
function TAVLTree.FindInsertPos(ANode: TAVLTreeNode): TAVLTreeNode;
var Comp: integer;
begin
Result:=Root;
while (Result<>nil) do begin
Comp:=Result.Compare(ANode);
if Comp>0 then begin
if Result.Left<>nil then
Result:=Result.Left
else
exit;
end else begin
if Result.Right<>nil then
Result:=Result.Right
else
exit;
end;
end;
end;
procedure TAVLTree.Init;
begin
FNodeClass:=TAVLTreeNode;
end;
procedure TAVLTree.NodeAdded(aNode: TAVLTreeNode);
begin
// for descendants to override
end;
procedure TAVLTree.RotateLeft(aNode: TAVLTreeNode);
{ Parent Parent
| |
Node => OldRight
/ \ /
Left OldRight Node
/ / \
OldRightLeft Left OldRightLeft }
var
AParent, OldRight, OldRightLeft: TAVLTreeNode;
begin
OldRight:=aNode.Right;
OldRightLeft:=OldRight.Left;
AParent:=aNode.Parent;
if AParent<>nil then begin
if AParent.Left=aNode then
AParent.Left:=OldRight
else
AParent.Right:=OldRight;
end else
fRoot:=OldRight;
OldRight.Parent:=AParent;
aNode.Parent:=OldRight;
aNode.Right:=OldRightLeft;
if OldRightLeft<>nil then
OldRightLeft.Parent:=aNode;
OldRight.Left:=aNode;
end;
procedure TAVLTree.RotateRight(aNode: TAVLTreeNode);
{ Parent Parent
| |
Node => OldLeft
/ \ \
OldLeft Right Node
\ / \
OldLeftRight OldLeftRight Right }
var
AParent, OldLeft, OldLeftRight: TAVLTreeNode;
begin
OldLeft:=aNode.Left;
OldLeftRight:=OldLeft.Right;
AParent:=aNode.Parent;
if AParent<>nil then begin
if AParent.Left=aNode then
AParent.Left:=OldLeft
else
AParent.Right:=OldLeft;
end else
fRoot:=OldLeft;
OldLeft.Parent:=AParent;
aNode.Parent:=OldLeft;
aNode.Left:=OldLeftRight;
if OldLeftRight<>nil then
OldLeftRight.Parent:=aNode;
OldLeft.Right:=aNode;
end;
procedure TAVLTree.SwitchPositionWithSuccessor(aNode, aSuccessor: TAVLTreeNode);
{ called by delete, when aNode.Left<>nil and aNode.Right<>nil
Switch ANode position with Successor
Because ANode.Right<>nil the Successor is a child of ANode }
var
OldBalance: Integer;
OldParent, OldLeft, OldRight,
OldSuccParent, OldSuccLeft, OldSuccRight: TAVLTreeNode;
begin
OldBalance:=aNode.Balance;
aNode.Balance:=aSuccessor.Balance;
aSuccessor.Balance:=OldBalance;
OldParent:=aNode.Parent;
OldLeft:=aNode.Left;
OldRight:=aNode.Right;
OldSuccParent:=aSuccessor.Parent;
OldSuccLeft:=aSuccessor.Left;
OldSuccRight:=aSuccessor.Right;
if OldParent<>nil then begin
if OldParent.Left=aNode then
OldParent.Left:=aSuccessor
else
OldParent.Right:=aSuccessor;
end else
fRoot:=aSuccessor;
aSuccessor.Parent:=OldParent;
if OldSuccParent<>aNode then begin
if OldSuccParent.Left=aSuccessor then
OldSuccParent.Left:=aNode
else
OldSuccParent.Right:=aNode;
aSuccessor.Right:=OldRight;
aNode.Parent:=OldSuccParent;
if OldRight<>nil then
OldRight.Parent:=aSuccessor;
end else begin
{ aNode aSuccessor
\ => \
aSuccessor aNode }
aSuccessor.Right:=aNode;
aNode.Parent:=aSuccessor;
end;
aNode.Left:=OldSuccLeft;
if OldSuccLeft<>nil then
OldSuccLeft.Parent:=aNode;
aNode.Right:=OldSuccRight;
if OldSuccRight<>nil then
OldSuccRight.Parent:=aNode;
aSuccessor.Left:=OldLeft;
if OldLeft<>nil then
OldLeft.Parent:=aSuccessor;
end;
function TAVLTree.FindSuccessor(ANode: TAVLTreeNode): TAVLTreeNode;
begin
if ANode<>nil then
Result:=ANode.Successor
else
Result:=nil;
end;
function TAVLTree.FindPrecessor(ANode: TAVLTreeNode): TAVLTreeNode;
begin
if ANode<>nil then
Result:=ANode.Precessor
else
Result:=nil;
end;
procedure TAVLTree.ConsistencyCheck;
procedure E(Msg: string);
begin
raise Exception.Create('TAVLTree.ConsistencyCheck: '+Msg);
end;
var
RealCount: SizeInt;
begin
RealCount:=0;
if FRoot<>nil then begin
FRoot.ConsistencyCheck(Self);
RealCount:=FRoot.GetCount;
end;
if Count<>RealCount then
E('Count<>RealCount');
end;
function TAVLTree.Equals(Obj: TObject): boolean;
begin
if Obj is TAVLTree then
Result:=IsEqual(TAVLTree(Obj),false)
else
Result:=inherited Equals(Obj);
end;
function TAVLTree.IsEqual(aTree: TAVLTree; CheckDataPointer: boolean): boolean;
var
MyNode, OtherNode: TAVLTreeNode;
begin
if aTree=Self then exit(true);
Result:=false;
if aTree=nil then exit;
if Count<>aTree.Count then exit;
if NodeClass<>aTree.NodeClass then exit;
MyNode:=FindLowest;
OtherNode:=aTree.FindLowest;
while MyNode<>nil do begin
if OtherNode=nil then exit;
if MyNode.Compare(OtherNode)<>0 then exit;
MyNode:=MyNode.Successor;
OtherNode:=OtherNode.Successor;
end;
if OtherNode<>nil then exit;
Result:=true;
end;
procedure TAVLTree.Assign(aTree: TAVLTree);
procedure AssignNode(var MyNode: TAVLTreeNode; OtherNode: TAVLTreeNode);
begin
MyNode:=NodeClass.Create;
MyNode.Assign(OtherNode);
MyNode.Balance:=OtherNode.Balance;
if OtherNode.Left<>nil then begin
AssignNode(MyNode.Left,OtherNode.Left);
MyNode.Left.Parent:=MyNode;
end;
if OtherNode.Right<>nil then begin
AssignNode(MyNode.Right,OtherNode.Right);
MyNode.Right.Parent:=MyNode;
end;
end;
begin
if aTree=nil then
raise Exception.Create('TAVLTree.Assign aTree=nil');
if IsEqual(aTree,true) then exit;
Clear;
NodeClass:=aTree.NodeClass;
if aTree.Root<>nil then
AssignNode(fRoot,aTree.Root);
FCount:=aTree.Count;
end;
procedure TAVLTree.WriteReportToStream(s: TStream);
procedure WriteStr(const Txt: string);
begin
if Txt='' then exit;
s.Write(Txt[1],length(Txt));
end;
procedure WriteTreeNode(ANode: TAVLTreeNode);
var
b: String;
IsLeft: boolean;
AParent: TAVLTreeNode;
WasLeft: Boolean;
begin
if ANode=nil then exit;
WriteTreeNode(ANode.Right);
AParent:=ANode;
WasLeft:=false;
b:='';
while AParent<>nil do begin
if AParent.Parent=nil then begin
if AParent=ANode then
b:='--'+b
else
b:=' '+b;
break;
end;
IsLeft:=AParent.Parent.Left=AParent;
if AParent=ANode then begin
if IsLeft then
b:='\-'
else
b:='/-';
end else begin
if WasLeft=IsLeft then
b:=' '+b
else
b:='| '+b;
end;
WasLeft:=IsLeft;
AParent:=AParent.Parent;
end;
b:=b+Format('%s Self=%p Parent=%p Balance=%d',
[aNode.ToString, Pointer(aNode),Pointer(aNode.Parent), aNode.Balance])
+LineEnding;
WriteStr(b);
WriteTreeNode(ANode.Left);
end;
// TAVLTree.WriteReportToStream
begin
WriteStr('-Start-of-AVL-Tree-------------------'+LineEnding);
WriteTreeNode(fRoot);
WriteStr('-End-Of-AVL-Tree---------------------'+LineEnding);
end;
{function TAVLTree.ReportAsString: string;
var ms: TMemoryStream;
begin
Result:='';
ms:=TMemoryStream.Create;
try
WriteReportToStream(ms);
ms.Position:=0;
SetLength(Result,ms.Size);