This repository has been archived by the owner on Nov 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
EvilWorks.Generics.AVLTree.pas
632 lines (561 loc) · 14 KB
/
EvilWorks.Generics.AVLTree.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
//
// EvilLibrary by Vedran Vuk 2010-2012
//
// Name: EvilWorks.DataStructures.AVLTree
// Description: An Generic AVL tree implementation.
// Largely a translation from C by Julienne Walker but implemented as a generic class
// with independant Key (Node key) and Val (Node data).
// http://eternallyconfuzzled.com/tuts/datastructures/jsw_tut_avl.aspx
// File last change date: November 16th. 2012
// File version: Dev 0.0.0
// Licence: Free.
//
unit EvilWorks.Generics.AVLTree;
interface
uses
System.SysUtils;
type
{ Exceptions }
EAVLTree = class(Exception); // Base exception
EAVLTreeItemNotFound = class(EAVLTree); // Used in GetItem().
{ TAVLTree<TKey, TVal> }
{ A Generic balanced binary tree implementation. }
TAVLTree<TKey, TVal> = class
private const
HEIGHT_LIMIT = 65536;
public type
TCompareFunc = reference to function(const aKeyA, aKeyB: TKey): integer;
TReleaseKeyProc = reference to procedure(var aKey: TKey);
TReleaseValProc = reference to procedure(var aVal: TVal);
private type
{ TAVLNode }
PAVLNode = ^TAVLNode;
TAVLNode = record
Key: TKey;
Val: TVal;
Bal: integer;
Lnk: array [boolean] of PAVLNode;
end;
{ TTokensEnumerator }
TAVLTreeEnumerator = class
private
FTree: TAVLTree<TKey, TVal>;
FCurr: PAVLNode;
FPath: array [0 .. HEIGHT_LIMIT] of PAVLNode;
FTop : cardinal;
public
constructor Create(aTree: TAVLTree<TKey, TVal>);
function GetCurrent: TVal; inline;
function MoveNext: Boolean; inline;
property Current: TVal read GetCurrent;
end;
private
FRoot : PAVLNode;
FCount : cardinal;
FCompare : TCompareFunc;
FReleaseKey: TReleaseKeyProc;
FReleaseVal: TReleaseValProc;
function GetCount: integer;
function GetItem(const aKey: TKey): TVal;
procedure SetItem(const aKey: TKey; const aVal: TVal);
protected
procedure RotateSingle(var aRoot: PAVLNode; aDir: boolean);
procedure RotateDouble(var aRoot: PAVLNode; aDir: boolean);
procedure AdjustBalance(var aRoot: PAVLNode; aDir: boolean; aBalance: integer);
procedure BalanceAfterInsert(var aRoot: PAVLNode; aDir: boolean);
procedure BalanceAfterRemove(var aRoot: PAVLNode; aDir: boolean; var aDone: boolean);
procedure RemoveNode(const aKey: TKey; const aFreeData: boolean; var aSaveKey: TKey; var aSaveVal: TVal);
function Find(const aKey: TKey): PAVLNode;
public
constructor Create(const aCompare: TCompareFunc; const aReleaseKey: TReleaseKeyProc; const aReleaseVal: TReleaseValProc);
destructor Destroy; override;
procedure Assign(const aSource: TAVLTree<TKey, TVal>);
function GetEnumerator: TAVLTreeEnumerator;
procedure Insert(const aKey: TKey; const aVal: TVal);
procedure Delete(const aKey: TKey);
procedure ReKey(const aOldKey, aNewKey: TKey);
procedure Clear;
property Items[const aKey: TKey]: TVal read GetItem write SetItem; default;
property Count: integer read GetCount;
function Exists(const aKey: TKey): boolean;
end;
implementation
{ ======================================= }
{ TAVLTree<TKey, TVal>.TAVLTreeEnumerator }
{ ======================================= }
{ Constructor. }
constructor TAVLTree<TKey, TVal>.TAVLTreeEnumerator.Create(aTree: TAVLTree<TKey, TVal>);
begin
FTree := aTree;
FCurr := nil;
FTop := 0;
end;
{ Gets curent item for the iterator. }
function TAVLTree<TKey, TVal>.TAVLTreeEnumerator.GetCurrent: TVal;
begin
Result := FCurr^.Val;
end;
{ Advances to next item for the iterator. }
function TAVLTree<TKey, TVal>.TAVLTreeEnumerator.MoveNext: Boolean;
var
last: PAVLNode;
begin
if (FCurr = nil) then
begin
FCurr := FTree.FRoot;
FTop := 0;
// build a path to work with
if (FCurr <> nil) then
begin
while (FCurr^.Lnk[False] <> nil) do
begin
FPath[FTop] := FCurr;
Inc(FTop);
FCurr := FCurr^.Lnk[False];
end;
end;
end
else
begin
if (FCurr^.Lnk[True] <> nil) then
begin
// continue down this branch
FPath[FTop] := FCurr;
Inc(FTop);
FCurr := FCurr^.Lnk[True];
while (FCurr^.Lnk[not True] <> nil) do
begin
FPath[FTop] := FCurr;
Inc(FTop);
FCurr := FCurr^.Lnk[not True];
end;
end
else
begin
// move to the next branch
repeat
if (FTop = 0) then
begin
FCurr := nil;
Break;
end;
last := FCurr;
Dec(FTop);
FCurr := FPath[FTop];
until (last <> FCurr^.Lnk[True]);
end;
end;
Result := (FCurr <> nil);
end;
{ ==================== }
{ TAVLTree<TKey, TVal> }
{ ==================== }
{ Constructor. aCompare compares two TVal items, aReleaseKey disposes of TKey, aReleaseVal of TVal. }
constructor TAVLTree<TKey, TVal>.Create(const aCompare: TCompareFunc; const aReleaseKey: TReleaseKeyProc; const aReleaseVal: TReleaseValProc);
begin
FRoot := nil;
FCompare := aCompare;
FReleaseKey := aReleaseKey;
FReleaseVal := aReleaseVal;
FCount := 0
end;
{ Destructor. }
destructor TAVLTree<TKey, TVal>.Destroy;
begin
Clear;
inherited;
end;
{ Assign from an instance of the same type. }
procedure TAVLTree<TKey, TVal>.Assign(const aSource: TAVLTree<TKey, TVal>);
begin
end;
{ Implements GetEnumerator for for in iterator. }
function TAVLTree<TKey, TVal>.GetEnumerator: TAVLTreeEnumerator;
begin
Result := TAVLTreeEnumerator.Create(Self);
end;
{ Performs a single rotation. }
procedure TAVLTree<TKey, TVal>.RotateSingle(var aRoot: PAVLNode; aDir: boolean);
var
save: PAVLNode;
begin
save := aRoot^.Lnk[not aDir];
aRoot^.Lnk[not aDir] := save^.Lnk[aDir];
save^.Lnk[aDir] := aRoot;
aRoot := save;
end;
{ Performs a double rotation. }
procedure TAVLTree<TKey, TVal>.RotateDouble(var aRoot: PAVLNode; aDir: boolean);
var
save: PAVLNode;
begin
save := aRoot^.Lnk[not aDir]^.Lnk[aDir];
aRoot^.Lnk[not aDir]^.Lnk[aDir] := save^.Lnk[not aDir];
save^.Lnk[not aDir] := aRoot^.Lnk[not aDir];
aRoot^.Lnk[not aDir] := save;
save := aRoot^.Lnk[not aDir];
aRoot^.Lnk[not aDir] := save^.Lnk[aDir];
save^.Lnk[aDir] := aRoot;
aRoot := save;
end;
{ Balances the tree height. }
procedure TAVLTree<TKey, TVal>.AdjustBalance(var aRoot: PAVLNode; aDir: boolean; aBalance: integer);
var
n, nn: PAVLNode;
begin
n := aRoot^.Lnk[aDir];
nn := n^.Lnk[not aDir];
if (nn^.Bal = 0) then
begin
aRoot^.Bal := 0;
n^.Bal := 0;
end
else if (nn^.Bal = aBalance) then
begin
aRoot^.Bal := - aBalance;
n^.Bal := 0;
end
else
begin
aRoot^.Bal := 0;
n^.Bal := aBalance;
end;
nn^.Bal := 0;
end;
{ Balances the tree height after insertion. }
procedure TAVLTree<TKey, TVal>.BalanceAfterInsert(var aRoot: PAVLNode; aDir: boolean);
var
n : PAVLNode;
bal: integer;
begin
n := aRoot^.Lnk[aDir];
if (not aDir) then
bal := - 1
else
bal := + 1;
if (n^.Bal = bal) then
begin
aRoot^.Bal := 0;
n^.Bal := 0;
RotateSingle(aRoot, not aDir);
end
else
begin
AdjustBalance(aRoot, aDir, bal);
RotateDouble(aRoot, not aDir);
end;
end;
{ Balances the tree height after deletion. }
procedure TAVLTree<TKey, TVal>.BalanceAfterRemove(var aRoot: PAVLNode; aDir: boolean; var aDone: boolean);
var
n : PAVLNode;
bal: integer;
begin
n := aRoot^.Lnk[not aDir];
if (not aDir) then
bal := - 1
else
bal := + 1;
if (n^.Bal = - bal) then
begin
aRoot^.Bal := 0;
n^.Bal := 0;
RotateSingle(aRoot, aDir);
end
else if (n^.Bal = bal) then
begin
AdjustBalance(aRoot, not aDir, - bal);
RotateDouble(aRoot, aDir);
end
else
begin
aRoot^.Bal := - bal;
n^.Bal := bal;
RotateSingle(aRoot, aDir);
aDone := True;
end;
end;
{ Internal function for removing a node. if aFreeData frees node, Key and Val, }
{ otherwise returns Key in aSaveKey and Val in aSaveVal then removes the node. }
procedure TAVLTree<TKey, TVal>.RemoveNode(const aKey: TKey; const aFreeData: boolean; var aSaveKey: TKey; var aSaveVal: TVal);
var
it : PAVLNode;
heir: PAVLNode;
save: TVal;
up : array [0 .. HEIGHT_LIMIT] of PAVLNode;
upd : array [0 .. HEIGHT_LIMIT - 1] of boolean;
top : integer;
done: boolean;
dir : boolean;
begin
top := 0;
done := boolean(0);
if (FRoot <> nil) then
begin
it := FRoot;
// Search down the tree and save path
while (True) do
begin
if (it = nil) then
Exit
else if (FCompare(it^.Key, aKey) = 0) then
Break;
// Push direction and node onto stack
upd[top] := (FCompare(it^.Key, aKey) < 0);
up[top] := it;
it := it^.Lnk[upd[top]];
Inc(top);
end;
// Remove the node
if (it^.Lnk[False] = nil) or (it^.Lnk[True] = nil) then
begin
// Which child is not nil?
dir := (it^.Lnk[False] = nil);
// Fix parent
if (top <> 0) then
up[top - 1]^.Lnk[upd[top - 1]] := it^.Lnk[dir]
else
FRoot := it^.Lnk[dir];
if (aFreeData) then
begin
FReleaseKey(it^.Key);
FReleaseVal(it^.Val);
end
else
begin
aSaveKey := it^.Key;
aSaveVal := it^.Val;
end;
FreeMem(it);
end
else
begin
// Find the inorder successor
heir := it^.Lnk[True];
// Save this path too
upd[top] := True;
up[top] := it;
Inc(top);
while (heir^.Lnk[False] <> nil) do
begin
upd[top] := False;
up[top] := heir;
Inc(top);
heir := heir^.Lnk[False];
end;
// Swap data
save := it^.Val;
it^.Val := heir^.Val;
heir^.Val := save;
// Unlink successor and fix parent
up[top - 1]^.Lnk[(up[top - 1] = it)] := heir^.Lnk[True];
if (aFreeData) then
begin
FReleaseKey(it^.Key);
FReleaseVal(it^.Val);
end
else
begin
aSaveKey := it^.Key;
aSaveVal := it^.Val;
end;
FreeMem(heir);
end;
// Walk back up the search path
Dec(top);
while (top >= 0) and (not done) do
begin
// Update balance factors
if (upd[top]) then
up[top]^.Bal := up[top]^.Bal - 1
else
up[top]^.Bal := up[top]^.Bal + 1;
// Terminate or rebalance as neccesary
if (Abs(up[top]^.Bal) = 1) then
Break
else if (Abs(up[top]^.Bal) > 1) then
begin
BalanceAfterRemove(up[top], upd[top], done);
// Fix parent
if (top <> 0) then
up[top - 1]^.Lnk[upd[top - 1]] := up[top]
else
FRoot := up[0];
end;
Dec(top);
end;
Dec(FCount);
end;
end;
{ Searches for a node keyed with aKey. }
function TAVLTree<TKey, TVal>.Find(const aKey: TKey): PAVLNode;
var
it : PAVLNode;
cmp: integer;
begin
it := FRoot;
while (it <> nil) do
begin
cmp := FCompare(it^.Key, aKey);
if (cmp = 0) then
Break;
it := it^.Lnk[cmp < 0];
end;
Result := it;
end;
{ Inserts a new node keyed with aey with value aVal. }
procedure TAVLTree<TKey, TVal>.Insert(const aKey: TKey; const aVal: TVal);
var
head : TAVLNode;
s, t, p, q: PAVLNode;
dir : boolean;
begin
if (FRoot = nil) then
begin
FRoot := AllocMem(SizeOf(TAVLNode));
if (FRoot = nil) then
Exit;
FRoot^.Key := aKey;
FRoot^.Val := aVal;
end
else
begin
// If node of aKey exists, update its Val and exit.
s := Find(aKey);
if (s <> nil) then
begin
s^.Val := aVal;
Exit;
end;
// Set up false root to ease maintenance
FillChar(head, SizeOf(head), 0);
t := @head;
t^.Lnk[True] := FRoot;
// Search down the tree, saving rebalance points
s := t.Lnk[True];
p := t.Lnk[True];
while (True) do
begin
dir := (FCompare(p^.Key, aKey) < 0);
q := p^.Lnk[dir];
if (q = nil) then
Break;
if (q^.Bal <> 0) then
begin
t := p;
s := q;
end;
p := q;
end;
q := AllocMem(SizeOf(TAVLNode));
q^.Key := aKey;
q^.Val := aVal;
p^.Lnk[dir] := q;
if (q = nil) then
Exit;
// Update balance factors
p := s;
while (p <> q) do
begin
dir := (FCompare(p^.Key, aKey) < 0);
if (not dir) then
p^.Bal := p^.Bal - 1
else
p^.Bal := p^.Bal + 1;
p := p^.Lnk[dir];
end;
q := s; // Save rebalance point for parent fix
// Rebalance if necessary
if (Abs(s^.Bal) > 1) then
begin
dir := (FCompare(s^.Key, aKey) < 0);
BalanceAfterInsert(s, dir);
end;
// Fix parent
if (q = head.Lnk[True]) then
FRoot := s
else
t^.Lnk[(q = t^.Lnk[True])] := s;
end;
Inc(FCount);
end;
{ Deletes a node keyed with aKey. }
procedure TAVLTree<TKey, TVal>.Delete(const aKey: TKey);
var
tempKey: TKey;
tempVal: TVal;
begin
RemoveNode(aKey, True, tempKey, tempVal);
end;
{ Changes the Key of a node. }
procedure TAVLTree<TKey, TVal>.ReKey(const aOldKey, aNewKey: TKey);
var
tempKey: TKey;
tempVal: TVal;
begin
RemoveNode(aOldKey, False, tempKey, tempVal);
FReleaseKey(tempKey);
Insert(aNewKey, tempVal);
end;
{ Clears the tree. Disposition methods called for every node. }
procedure TAVLTree<TKey, TVal>.Clear;
var
it : PAVLNode;
save: PAVLNode;
begin
it := FRoot;
// Destruction by rotation
while (it <> nil) do
begin
if (it^.Lnk[False] = nil) then
begin
// Remove node
save := it^.Lnk[True];
FReleaseKey(it^.Key);
FReleaseVal(it^.Val);
FreeMem(it);
end
else
begin
// Rotate right
save := it^.Lnk[False];
it^.Lnk[False] := save^.Lnk[True];
save^.Lnk[True] := it;
end;
it := save;
end;
FRoot := nil;
FCount := 0;
end;
{ Checks if a node keyed with aKey exists. }
function TAVLTree<TKey, TVal>.Exists(const aKey: TKey): boolean;
begin
Result := (Find(aKey) <> nil);
end;
{ Returns the count of tree nodes. }
function TAVLTree<TKey, TVal>.GetCount: integer;
begin
Result := integer(FCount);
end;
{ Item getter. If not found raises EAVLTreeItemNotFound. }
function TAVLTree<TKey, TVal>.GetItem(const aKey: TKey): TVal;
var
node: PAVLNode;
begin
node := Find(aKey);
if (node = nil) then
raise EAVLTreeItemNotFound.Create('Item not found.');
Result := node^.Val;
end;
{ Item setter. If not found inserts new item. }
procedure TAVLTree<TKey, TVal>.SetItem(const aKey: TKey; const aVal: TVal);
var
node: PAVLNode;
begin
node := Find(aKey);
if (node <> nil) then
node^.Val := aVal
else
Insert(aKey, aVal);
end;
end.