-
Notifications
You must be signed in to change notification settings - Fork 1
/
adtbstree_impl.i
557 lines (489 loc) · 14.7 KB
/
adtbstree_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
{@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
adtbstree_impl.i::prefix=&_mcp_prefix&::item_type=&ItemType&
}
&include adtbstree.defs
&include adtbstree_impl.mcp
{ -------------------- TBinarysearchTreeBase --------------------------------- }
constructor TBinarySearchTreeBase.Create(btree : TBinaryTree);
begin
inherited Create;
FBinaryTree := btree;
end;
constructor TBinarySearchTreeBase.Create;
begin
inherited Create;
FBinaryTree := TBinaryTree.Create;
end;
constructor TBinarySearchTreeBase.CreateCopy(const cont : TBinarySearchTreeBase;
const itemCopier : IUnaryFunctor);
begin
inherited CreateCopy(TSetAdt(cont));
FBinaryTree := TBinaryTree(cont.FBinaryTree.CopySelf(itemCopier));
end;
destructor TBinarySearchTreeBase.Destroy;
begin
FBinaryTree.Free;
inherited;
end;
procedure TBinarySearchTreeBase.SetOwnsItems(b : Boolean);
begin
inherited;
FBinaryTree.OwnsItems := b;
end;
procedure TBinarySearchTreeBase.SetDisposer(const proc : IUnaryFunctor);
begin
inherited;
FBinaryTree.ItemDisposer := proc;
end;
function TBinarySearchTreeBase.GetDisposer : IUnaryFunctor;
begin
Result := FBinaryTree.ItemDisposer;
end;
function TBinarySearchTreeBase.
FindNode(aitem : ItemType; node : PBinaryTreeNode;
var parent : PBinaryTreeNode) : PBinaryTreeNode;
var
i : Integer;
begin
parent := nil;
Result := node;
while Result <> nil do
begin
_mcp_compare_assign(aitem, Result^.Item, i);
if i < 0 then
begin
parent := Result;
Result := Result^.LeftChild;
end else if i > 0 then
begin
parent := Result;
Result := Result^.RightChild;
end else
begin
{ we have to find the _first_ node equal to aitem and there may
be such node in the left sub-tree }
if RepeatedItems and (Result^.LeftChild <> nil) then
begin
node := FindNode(aitem, Result^.LeftChild, parent);
if node = nil then
begin
parent := Result^.Parent;
end else
Result := node;
end;
break;
end;
end;
end;
function TBinarySearchTreeBase.
LowerBoundNode(aitem : ItemType; node : PBinaryTreeNode) : PBinaryTreeNode;
var
dummy : PBinaryTreeNode;
i : Integer;
begin
Result := node;
i := -1; { to avoid special case (node = nil) in the if below the loop }
while Result <> nil do
begin
_mcp_compare_assign(aitem, Result^.Item, i);
if i < 0 then
begin
node := Result;
Result := Result^.LeftChild;
end else if i > 0 then
begin
node := Result;
Result := Result^.RightChild;
end else
begin
{ we have to find the _first_ node equal to aitem and there may
be such node in the left sub-tree }
if RepeatedItems and (Result^.LeftChild <> nil) then
begin
node := FindNode(aitem, Result^.LeftChild, dummy);
if node <> nil then
begin
Result := node;
end;
end;
break;
end;
end;
if Result = nil then
begin
if i < 0 then
begin
{ there is no left child and aitem < node, so the first node
with item >= aitem is the parent of result (stored in node) }
Result := node;
end else { i > 0 }
begin
{ there is no right child and aitem > node, so the first node
with item >= aitem is the first ancestor node from which we
turned left, or nil if there is no such node }
while (node^.Parent <> nil) and (node^.Parent^.LeftChild <> node) do
begin
node := node^.Parent;
end;
node := node^.Parent;
Result := node;
end;
end;
end;
function TBinarySearchTreeBase.
InsertNode(aitem : ItemType; node : PBinaryTreeNode) : PBinaryTreeNode;
var
i : Integer;
parent : PBinaryTreeNode;
begin
if node <> nil then
begin
while node <> nil do
begin
_mcp_compare_assign(aitem, node^.Item, i);
parent := node;
if i < 0 then
begin
node := node^.LeftChild;
end else if i > 0 then
begin
node := node^.RightChild;
end else { i = 0 }
begin
break;
end;
end;
if i < 0 then
begin
BinaryTree.InsertNode(parent^.LeftChild, parent, aitem);
Result := parent^.LeftChild;
end else if i > 0 then
begin
BinaryTree.InsertNode(parent^.RightChild, parent, aitem);
Result := parent^.RightChild;
end else { i = 0 }
begin
if RepeatedItems then
begin
{ insert as first node in the sub-tree of the right child of node }
if node^.RightChild <> nil then
begin
parent := FirstInOrderNode(node^.RightChild);
FBinaryTree.InsertNode(parent^.LeftChild, parent, aitem);
Result := parent^.LeftChild;
end else
begin
FBinaryTree.InsertNode(node^.RightChild, node, aitem);
Result := node^.RightChild;
end;
end else
Result := nil;
end;
end else { not node <> nil }
begin
if node = FBinaryTree.RootNode then
begin
FBinaryTree.InsertAsRoot(aitem);
Result := FBinaryTree.RootNode;
end else
Result := InsertNode(aitem, BinaryTree.RootNode);
end;
end;
procedure TBinarySearchTreeBase.
ExchangeBinaryTrees(tree : TBinarySearchTreeBase);
begin
ExchangePtr(FBinaryTree, tree.FBinaryTree);
end;
function TBinarySearchTreeBase.Start : TSetIterator;
begin
Result := TBinarySearchTreeBaseIterator.Create(nil, self);
TBinarySearchTreeBaseIterator(Result).GoToStartNode;
end;
function TBinarySearchTreeBase.Finish : TSetIterator;
begin
Result := TBinarySearchTreeBaseIterator.Create(nil, self);
end;
&if (&_mcp_accepts_nil)
function TBinarySearchTreeBase.FindOrInsert(aitem : ItemType) : ItemType;
var
parent, node : PBinaryTreeNode;
begin
if RepeatedItems then
begin
InsertNode(aitem, FBinaryTree.RootNode);
Result := nil;
end else
begin
node := FindNode(aitem, FBinaryTree.RootNode, parent);
if node <> nil then
Result := node^.Item
else begin
InsertNode(aitem, parent);
Result := nil;
end;
end;
end;
function TBinarySearchTreeBase.Find(aitem : ItemType) : ItemType;
var
node, parent : PBinaryTreeNode;
begin
node := FindNode(aitem, FBinaryTree.RootNode, parent);
if node <> nil then
Result := node^.Item
else
Result := nil;
end;
&endif &# end &_mcp_accepts_nil
function TBinarySearchTreeBase.Has(aitem : ItemType) : Boolean;
var
dummy : PBinaryTreeNode;
begin
Result := FindNode(aitem, FBinaryTree.RootNode, dummy) <> nil;
end;
function TBinarySearchTreeBase.Count(aitem : ItemType) : SizeType;
var
node, parent : PBinaryTreeNode;
begin
Result := 0;
node := FindNode(aitem, FBinaryTree.RootNode, parent);
if node <> nil then
begin
repeat
Inc(Result);
node := NextInOrderNode(node);
until (node = nil) or (not _mcp_equal(node^.Item, aitem));
end;
end;
function TBinarySearchTreeBase.Insert(aitem : ItemType) : Boolean;
begin
Result := InsertNode(aitem, BinaryTree.RootNode) <> nil;
end;
function TBinarySearchTreeBase.Insert(pos : TSetIterator;
aitem : ItemType) : Boolean;
var
node, node2 : PBinaryTreeNode;
begin
Assert(pos is TBinarySearchTreeBaseIterator, msgInvalidIterator);
node := TBinarySearchTreeBaseIterator(pos).Node;
if (node <> nil) then
begin
{ find the node to start searching from and assign it to node }
node2 := node;
while node2^.Parent <> nil do
begin
if node2^.Parent^.LeftChild = node then
begin
if _mcp_lt(aitem, node2^.Parent^.Item) then
break
else begin
node := node2^.Parent;
end;
end else
begin
if _mcp_lt(aitem, node2^.Parent^.Item) then
node := node2^.Parent;
end;
node2 := node2^.Parent;
end;
Result := InsertNode(aitem, node) <> nil;
end else
Result := InsertNode(aitem, BinaryTree.RootNode) <> nil;
end;
function TBinarySearchTreeBase.LowerBound(aitem : ItemType) : TSetIterator;
var
node: PBinaryTreeNode;
begin
node := LowerBoundNode(aitem, FBinaryTree.RootNode);
Result := TBinarySearchTreeBaseIterator.Create(node, self);
end;
function TBinarySearchTreeBase.UpperBound(aitem : ItemType) : TSetIterator;
var
node : PBinaryTreeNode;
begin
node := LowerBoundNode(aitem, FBinaryTree.RootNode);
while (node <> nil) and (_mcp_equal(aitem, node^.Item)) do
begin
node := NextInOrderNode(node);
end;
Result := TBinarySearchTreeBaseIterator.Create(node, self);
end;
function TBinarySearchTreeBase.EqualRange(aitem : ItemType) : TSetIteratorRange;
var
node : PBinaryTreeNode;
iter1, iter2 : TBinarySearchTreeBaseIterator;
begin
node := LowerBoundNode(aitem, FBinaryTree.RootNode);
iter1 := TBinarySearchTreeBaseIterator.Create(node, self);
while (node <> nil) and (_mcp_equal(aitem, node^.Item)) do
node := NextInOrderNode(node);
iter2 := TBinarySearchTreeBaseIterator.Create(node, self);
Result := TSetIteratorRange.Create(iter1, iter2);
end;
procedure TBinarySearchTreeBase.Clear;
begin
FBinaryTree.Clear;
GrabageCollector.FreeObjects;
end;
function TBinarySearchTreeBase.Empty : Boolean;
begin
Result := FBinaryTree.RootNode = nil;
end;
function TBinarySearchTreeBase.Size : SizeType;
begin
Result := FBinaryTree.Size;
end;
{ -------------------- TBinarySearchTreeBaseIterator ------------------------- }
constructor TBinarySearchTreeBaseIterator.Create(anode : PBinaryTreeNode;
tree : TBinarySearchTreeBase);
begin
inherited Create(tree);
FTree := tree;
Node := anode;
end;
procedure TBinarySearchTreeBaseIterator.GoToStartNode;
begin
Node := FirstInOrderNode(FTree.BinaryTree.RootNode);
end;
function TBinarySearchTreeBaseIterator.CopySelf : TIterator;
begin
Result := TBinarySearchTreeBaseIterator.Create(Node, FTree);
end;
function TBinarySearchTreeBaseIterator.Equal(const Pos : TIterator) : Boolean;
begin
Assert(pos is TBinarySearchTreeBaseIterator, msgInvalidIterator);
Result := TBinarySearchTreeBaseIterator(pos).Node = Node;
end;
function TBinarySearchTreeBaseIterator.GetItem : ItemType;
begin
Assert(node <> nil, msgInvalidIterator);
Result := Node^.Item;
end;
procedure TBinarySearchTreeBaseIterator.SetItem(aitem : ItemType);
begin
Assert(Node <> nil, msgInvalidIterator);
with FTree do
begin
if _mcp_equal(Node^.Item, aitem) then
begin
DisposeItem(Node^.Item);
Node^.Item := aitem;
end else
begin
DisposeItem(Node^.Item);
Node^.Item := aitem;
ResetItem;
end;
end;
end;
procedure TBinarySearchTreeBaseIterator.ResetItem;
var
aitem : ItemType;
begin
Assert(Node <> nil, msgInvalidIterator);
aitem := Node^.Item;
FTree.BinaryTree.ExtractNodeInOrder(FNode, false);
Node := FTree.InsertNode(aitem, FTree.BinaryTree.RootNode);
Assert(Node <> nil, msgChangedRepeatedItems);
end;
procedure TBinarySearchTreeBaseIterator.Advance;
begin
Assert(Node <> nil, msgInvalidIterator);
Node := NextInOrderNode(Node);
end;
procedure TBinarySearchTreeBaseIterator.Retreat;
begin
Node := PrevInOrderNode(Node, FTree.BinaryTree.RootNode);
end;
procedure TBinarySearchTreeBaseIterator.Insert(aitem : ItemType);
begin
Node := FTree.InsertNode(aitem, FTree.BinaryTree.RootNode);
end;
function TBinarySearchTreeBaseIterator.Extract : ItemType;
begin
Assert(Node <> nil, msgDeletingInvalidIterator);
Result := node^.Item;
FTree.BinaryTree.ExtractNodeInOrder(FNode, true);
end;
function TBinarySearchTreeBaseIterator.IsStart : Boolean;
begin
if Node <> nil then
begin
Result := (Node^.LeftChild = nil) and
(FirstInOrderNode(FTree.BinaryTree.RootNode) = Node);
end else
begin
Result := FTree.BinaryTree.RootNode = nil;
end;
end;
function TBinarySearchTreeBaseIterator.IsFinish : Boolean;
begin
Result := Node = nil;
end;
function TBinarySearchTreeBaseIterator.Owner : TContainerAdt;
begin
Result := FTree;
end;
{ ------------------------- TBinarySearchTree ------------------------------ }
constructor TBinarySearchTree.Create;
begin
inherited Create;
end;
constructor TBinarySearchTree.CreateCopy(const cont : TBinarySearchTree;
const itemCopier : IUnaryFunctor);
begin
inherited CreateCopy(cont, itemCopier);
end;
function TBinarySearchTree.CopySelf(const ItemCopier :
IUnaryFunctor) : TContainerAdt;
begin
Result := TBinarySearchTree.CreateCopy(self, ItemCopier);
end;
procedure TBinarySearchTree.Swap(cont : TContainerAdt);
begin
if cont is TBinarySearchTree then
begin
BasicSwap(cont);
ExchangePtr(FBinaryTree, TBinarySearchTree(cont).FBinaryTree);
end else
inherited;
end;
function TBinarySearchTree.Delete(aitem : ItemType) : SizeType;
var
node, temp : PBinaryTreeNode;
begin
Result := 0;
node := FindNode(aitem, FBinaryTree.RootNode, temp);
while (node <> nil) and (_mcp_equal(aitem, node^.Item)) do
begin
DisposeItem(node^.Item);
BinaryTree.ExtractNodeInOrder(node, true);
Inc(Result);
end;
end;
procedure TBinarySearchTree.Delete(pos : TSetIterator);
var
node : PBinaryTreeNode;
begin
Assert(pos is TBinarySearchTreeBaseIterator, msgInvalidIterator);
node := TBinarySearchTreeBaseIterator(pos).Node;
DisposeItem(node^.Item);
BinaryTree.ExtractNodeInOrder(node, true);
TBinarySearchTreeBaseIterator(pos).Node := node;
end;