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
|
unit dbf_avl;
interface
{$I dbf_common.inc}
uses
Dbf_Common;
type
TBal = -1..1;
TAvlTree = class;
TKeyType = Cardinal;
TExtraData = Pointer;
PData = ^TData;
TData = record
ID: TKeyType;
ExtraData: TExtraData;
end;
PNode = ^TNode;
TNode = record
Data: TData;
Left: PNode;
Right: PNode;
Bal: TBal; // balance factor: h(Right) - h(Left)
end;
TAvlTreeEvent = procedure(Sender: TAvlTree; Data: PData) of object;
TAvlTree = class(TObject)
private
FRoot: PNode;
FCount: Cardinal;
FOnDelete: TAvlTreeEvent;
FHeightChange: Boolean;
function InternalInsert(X: PNode; var P: PNode): Boolean;
procedure InternalDelete(X: TKeyType; var P: PNode);
procedure DeleteNode(X: PNode);
procedure TreeDispose(X: PNode);
public
constructor Create;
destructor Destroy; override;
procedure Clear;
function Find(Key: TKeyType): TExtraData;
function Insert(Key: TKeyType; Extra: TExtraData): Boolean;
procedure Delete(Key: TKeyType);
function Lowest: PData;
property Count: Cardinal read FCount;
property OnDelete: TAvlTreeEvent read FOnDelete write FOnDelete;
end;
implementation
uses
Math;
procedure RotL(var P: PNode);
var
P1: PNode;
begin
P1 := P^.Right;
P^.Right := P1^.Left;
P1^.Left := P;
P := P1;
end;
procedure RotR(var P: PNode);
var
P1: PNode;
begin
P1 := P^.Left;
P^.Left := P1^.Right;
P1^.Right := P;
P := P1;
end;
function Height(X: PNode): Integer;
begin
if X = nil then
Result := 0
else
Result := 1+Max(Height(X^.Left), Height(X^.Right));
end;
function CheckTree_T(X: PNode; var H: Integer): Boolean;
var
HR: Integer;
begin
if X = nil then
begin
Result := true;
H := 0;
end else begin
Result := CheckTree_T(X^.Left, H) and CheckTree_T(X^.Right, HR) and
((X^.Left = nil) or (X^.Left^.Data.ID < X^.Data.ID)) and
((X^.Right = nil) or (X^.Right^.Data.ID > X^.Data.ID)) and
// ((Height(X^.Right) - Height(X^.Left)) = X^.Bal);
(HR - H = X^.Bal);
H := 1 + Max(H, HR);
end;
end;
function CheckTree(X: PNode): Boolean;
var
H: Integer;
begin
Result := CheckTree_T(X, H);
end;
procedure BalanceLeft(var P: PNode; var HeightChange: Boolean);
var
B1, B2: TBal;
{HeightChange = true, left branch has become less high}
begin
case P^.Bal of
-1: begin P^.Bal := 0 end;
0: begin P^.Bal := 1; HeightChange := false end;
1: begin {Rebalance}
B1 := P^.Right^.Bal;
if B1 >= 0
then {single L rotation}
begin
RotL(P);
//adjust balance factors:
if B1 = 0
then
begin P^.Bal :=-1; P^.Left^.Bal := 1; HeightChange := false end
else
begin P^.Bal := 0; P^.Left^.Bal := 0 end;
end
else {double RL rotation}
begin
B2 := P^.Right^.Left^.Bal;
RotR(P^.Right);
RotL(P);
//adjust balance factors:
if B2=+1 then P^.Left^.Bal := -1 else P^.Left^.Bal := 0;
if B2=-1 then P^.Right^.Bal := 1 else P^.Right^.Bal := 0;
P^.Bal := 0;
end;
end;{1}
end{case}
end;{BalanceLeft}
procedure BalanceRight(var P: PNode; var HeightChange: Boolean);
var
B1, B2: TBal;
{HeightChange = true, right branch has become less high}
begin
case P^.Bal of
1: begin P^.Bal := 0 end;
0: begin P^.Bal := -1; HeightChange := false end;
-1: begin {Rebalance}
B1 := P^.Left^.Bal;
if B1 <= 0
then {single R rotation}
begin
RotR(P);
//adjust balance factors}
if B1 = 0
then
begin P^.Bal :=1; P^.Right^.Bal :=-1; HeightChange:= false end
else
begin P^.Bal := 0; P^.Right^.Bal := 0 end;
end
else {double LR rotation}
begin
B2 := P^.Left^.Right^.Bal;
RotL(P^.Left);
RotR(P);
//adjust balance factors
if B2=-1 then P^.Right^.Bal := 1 else P^.Right^.Bal := 0;
if B2= 1 then P^.Left^.Bal := -1 else P^.Left^.Bal := 0;
P^.Bal := 0;
end;
end;{-1}
end{case}
end;{BalanceRight}
procedure DelRM(var R: PNode; var S: PNode; var HeightChange: Boolean);
// Make S refer to rightmost element of tree with root R;
// Remove that element from the tree
begin
if R^.Right = nil then
begin S := R; R := R^.Left; HeightChange := true end
else
begin
DelRM(R^.Right, S, HeightChange);
if HeightChange then BalanceRight(R, HeightChange)
end
end;
//---------------------------------------
//---****--- Class TAvlTree ---*****-----
//---------------------------------------
constructor TAvlTree.Create;
begin
inherited;
FRoot := nil;
end;
destructor TAvlTree.Destroy;
begin
Clear;
inherited;
end;
procedure TAvlTree.Clear;
begin
TreeDispose(FRoot);
FRoot := nil;
end;
procedure TAvlTree.DeleteNode(X: PNode);
begin
// delete handler installed?
if Assigned(FOnDelete) then
FOnDelete(Self, @X^.Data);
// dispose of memory
Dispose(X);
Dec(FCount);
end;
procedure TAvlTree.TreeDispose(X: PNode);
var
P: PNode;
begin
// nothing to dispose of?
if X = nil then
exit;
// use in-order visiting, maybe someone likes sequential ordering
TreeDispose(X^.Left);
P := X^.Right;
// free mem
DeleteNode(X);
// free right child
TreeDispose(P);
end;
function TAvlTree.Find(Key: TKeyType): TExtraData;
var
H: PNode;
begin
H := FRoot;
while (H <> nil) and (H^.Data.ID <> Key) do // use conditional and
if Key < H^.Data.ID then
H := H^.Left
else
H := H^.Right;
if H <> nil then
Result := H^.Data.ExtraData
else
Result := nil;
end;
function TAvlTree.Insert(Key: TKeyType; Extra: TExtraData): boolean;
var
H: PNode;
begin
// make new node
New(H);
with H^ do
begin
Data.ID := Key;
Data.ExtraData := Extra;
Left := nil;
Right := nil;
Bal := 0;
end;
// insert new node
Result := InternalInsert(H, FRoot);
if not Result then
Dispose(H);
// check tree
// assert(CheckTree(FRoot));
end;
procedure TAvlTree.Delete(Key: TKeyType);
begin
InternalDelete(Key, FRoot);
// assert(CheckTree(FRoot));
end;
function TAvlTree.InternalInsert(X: PNode; var P: PNode): boolean;
begin
if P = nil then
begin
P := X;
Inc(FCount);
FHeightChange := true;
Result := true;
end else begin
if X^.Data.ID < P^.Data.ID then
begin
{ less }
Result := InternalInsert(X, P^.Left);
if FHeightChange then {Left branch has grown higher}
case P^.Bal of
1: begin P^.Bal := 0; FHeightChange := false end;
0: begin P^.Bal := -1 end;
-1: begin {Rebalance}
if P^.Left^.Bal = -1
then {single R rotation}
begin
RotR(P);
//adjust balance factor:
P^.Right^.Bal := 0;
end
else {double LR rotation}
begin
RotL(P^.Left);
RotR(P);
//adjust balance factor:
case P^.Bal of
-1: begin P^.Left^.Bal := 0; P^.Right^.Bal := 1 end;
0: begin P^.Left^.Bal := 0; P^.Right^.Bal := 0 end;
1: begin P^.Left^.Bal := -1; P^.Right^.Bal := 0 end;
end;
end;
P^.Bal := 0;
FHeightChange := false;
// assert(CheckTree(P));
end{-1}
end{case}
end else
if X^.Data.ID > P^.Data.ID then
begin
{ greater }
Result := InternalInsert(X, P^.Right);
if FHeightChange then {Right branch has grown higher}
case P^.Bal of
-1: begin P^.Bal := 0; FHeightChange := false end;
0: begin P^.Bal := 1 end;
1: begin {Rebalance}
if P^.Right^.Bal = 1
then {single L rotation}
begin
RotL(P);
//adjust balance factor:
P^.Left.Bal := 0;
end
else {double RL rotation}
begin
RotR(P^.Right);
RotL(P);
//adjust balance factor
case P^.Bal of
1: begin P^.Right^.Bal := 0; P^.Left^.Bal := -1 end;
0: begin P^.Right^.Bal := 0; P^.Left^.Bal := 0 end;
-1: begin P^.Right^.Bal := 1; P^.Left^.Bal := 0 end;
end;
end;
P^.Bal := 0;
FHeightChange := false;
// assert(CheckTree(P));
end{1}
end{case}
end {greater} else begin
{X already present; do not insert again}
FHeightChange := false;
Result := false;
end;
end;
// assert(CheckTree(P));
end;{InternalInsert}
procedure TAvlTree.InternalDelete(X: TKeyType; var P: PNode);
var
Q: PNode;
H: TData;
begin
if P = nil then
FHeightChange := false
else
if X < P^.Data.ID then
begin
InternalDelete(X, P^.Left);
if FHeightChange then BalanceLeft(P, FHeightChange)
end else
if X > P^.Data.ID then
begin
InternalDelete(X, P^.Right);
if FHeightChange then BalanceRight(P, FHeightChange)
end else begin
if P^.Right = nil then
begin Q := P; P := P^.Left; FHeightChange := true end
else if P^.Left = nil then
begin Q := P; P := P^.Right; FHeightChange := true end
else
begin
DelRM(P^.Left, Q, FHeightChange);
H := P^.Data;
P^.Data := Q^.Data;
Q^.Data := H;
if FHeightChange then BalanceLeft(P, FHeightChange)
end;
DeleteNode(Q)
end;{eq}
end;{InternalDelete}
function TAvlTree.Lowest: PData;
var
H: PNode;
begin
H := FRoot;
if H = nil then
begin
Result := nil;
exit;
end;
while H^.Left <> nil do
H := H^.Left;
Result := @H^.Data;
end;
end.
|