-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadtsplaytree_impl.i
289 lines (252 loc) · 7.43 KB
/
adtsplaytree_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
{@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
adtsplaytree_impl.i::prefix=&_mcp_prefix&::item_type=&ItemType&
}
&include adtsplaytree.defs
&include adtsplaytree_impl.mcp
{ --------------------------- TSplayTree ---------------------------------- }
constructor TSplayTree.Create;
begin
inherited;
end;
constructor TSplayTree.CreateCopy(const cont : TSplayTree;
const itemCopier : IUnaryFunctor);
begin
inherited CreateCopy(cont, itemCopier);
end;
function TSplayTree.Splay(aitem : ItemType;
node : PBinaryTreeNode) : PBinaryTreeNode;
var
parent : PBinaryTreeNode;
begin
Result := FindNode(aitem, node, parent);
if Result <> nil then
SplayNode(Result)
else if parent <> nil then
SplayNode(parent);
end;
procedure TSplayTree.SplayNode(node : PBinaryTreeNode);
var
y, z : PBinaryTreeNode;
begin
Assert(node <> nil, msgInternalError);
y := node^.Parent;
if y <> nil then
z := y^.Parent
else
z := nil;
while (z <> nil) and (y <> nil) do
begin
if z^.LeftChild = y then
begin
if y^.LeftChild = node then
begin
BinaryTree.RotateNodeSingleRight(z);
BinaryTree.RotateNodeSingleRight(y);
end else { node is the right child of y }
begin
BinaryTree.RotateNodeDoubleRight(z);
end;
end else { y is the right child }
begin
if y^.LeftChild = node then
begin
BinaryTree.RotateNodeDoubleLeft(z);
end else { node is the right child of y }
begin
BinaryTree.RotateNodeSingleLeft(z);
BinaryTree.RotateNodeSingleLeft(y);
end;
end;
y := node^.Parent;
if y <> nil then
z := y^.Parent
else
z := nil;
end;
if y <> nil then
begin
if y^.LeftChild = node then
BinaryTree.RotateNodeSingleRight(y)
else
BinaryTree.RotateNodeSingleLeft(y);
end;
end;
procedure TSplayTree.DeleteNodeAtRoot;
var
root, rchild : PBinaryTreeNode;
aitem : ItemType;
begin
root := BinaryTree.RootNode;
aitem := root^.Item;
if root^.LeftChild <> nil then
begin
rchild := BinaryTree.ReplaceNodeWithLeftChild(root);
{ move the previous node to the root }
Splay(aitem, BinaryTree.RootNode);
root := BinaryTree.RootNode;
{ because the previous node is the largest in the tree, it
can't have a right child }
Assert(root^.RightChild = nil, msgInternalError);
root^.RightChild := rchild;
if rchild <> nil then
rchild^.Parent := root;
end else
begin
BinaryTree.ReplaceNodeWithRightChild(root);
end;
DisposeItem(aitem);
end;
function TSplayTree.LowerBoundNode(aitem : ItemType;
node : PBinaryTreeNode) : PBinaryTreeNode;
begin
Result := inherited LowerBoundNode(aitem, node);
if Result <> nil then
SplayNode(Result);
end;
function TSplayTree.InsertNode(aitem : ItemType;
node : PBinaryTreeNode) : PBinaryTreeNode;
var
root, oldroot, lchild, rchild : PBinaryTreeNode;
begin
if node <> nil then
begin
Result := Splay(aitem, node);
{ now the found node (or its parent) is at the root of the tree }
if (Result = nil) or RepeatedItems then
begin
oldroot := BinaryTree.RootNode;
lchild := oldroot^.LeftChild;
rchild := oldroot^.RightChild;
BinaryTree.InsertAsRoot(aitem);
root := BinaryTree.RootNode;
if _mcp_lt(oldroot^.Item, aitem) then
begin
oldroot^.RightChild := nil;
root^.RightChild := rchild;
if rchild <> nil then
rchild^.Parent := root;
end else
begin
oldroot^.LeftChild := nil;
root^.LeftChild := lchild;
if lchild <> nil then
lchild^.Parent := root;
root^.RightChild := oldroot;
end;
Result := root;
end else { there already is such node and RepeatedItems is false }
begin
Result := nil;
end;
end else
begin
if BinaryTree.RootNode = node then
begin
BinaryTree.InsertAsRoot(aitem);
Result := BinaryTree.RootNode;
end else
Result := InsertNode(aitem, BinaryTree.RootNode);
end;
end;
function TSplayTree.CopySelf(const ItemCopier : IUnaryFunctor) : TContainerAdt;
begin
Result := TSplayTree.CreateCopy(self, itemCopier);
end;
procedure TSplayTree.Swap(cont : TContainerAdt);
begin
if cont is TSplayTree then
begin
BasicSwap(cont);
ExchangeBinaryTrees(TSplayTree(cont));
end else
inherited;
end;
&if (&_mcp_accepts_nil)
function TSplayTree.FindOrInsert(aitem : ItemType) : ItemType;
var
node : PBinaryTreeNode;
begin
if RepeatedItems then
begin
InsertNode(aitem, BinaryTree.RootNode);
Result := nil;
end else
begin
node := Splay(aitem, BinaryTree.RootNode);
if node <> nil then
Result := node^.Item
else begin
InsertNode(aitem, BinaryTree.RootNode);
Result := nil;
end;
end;
end;
function TSplayTree.Find(aitem : ItemType) : ItemType;
var
node : PBinaryTreeNode;
begin
node := Splay(aitem, BinaryTree.RootNode);
if node <> nil then
Result := node^.Item
else
Result := nil;
end;
&endif
function TSplayTree.Has(aitem : ItemType) : Boolean;
begin
Result := Splay(aitem, BinaryTree.RootNode) <> nil;
end;
function TSplayTree.Count(aitem : ItemType) : SizeType;
var
node : PBinaryTreeNode;
begin
node := Splay(aitem, BinaryTree.RootNode);
Result := 0;
if node <> nil then
begin
repeat
Inc(Result);
node := NextInOrderNode(node);
until (node = nil) or (not _mcp_equal(node^.Item, aitem));
end;
end;
function TSplayTree.Delete(aitem : ItemType) : SizeType;
begin
Result := 0;
while Splay(aitem, BinaryTree.RootNode) <> nil do
begin
{ the found node is at the root }
DeleteNodeAtRoot;
Inc(Result);
end;
end;
procedure TSplayTree.Delete(pos : TSetIterator);
var
node, nnode : PBinaryTreeNode;
begin
Assert((pos <> nil) and (pos is TBinarySearchTreeBaseIterator),
msgInvalidIterator);
Assert(TBinarySearchTreeBaseIterator(pos).Node <> nil, msgInvalidIterator);
node := TBinarySearchTreeBaseIterator(pos).Node;
nnode := NextInOrderNode(node);
SplayNode(node);
DeleteNodeAtRoot;
TBinarySearchTreeBaseIterator(pos).Node := nnode;
end;