-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbinarytree.pas
294 lines (261 loc) · 7.02 KB
/
binarytree.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
unit binarytree;
{$mode objfpc}{$H+}
interface
type
PTreeNode = ^TTreeNode;
TTreeNode = record
Key : Pointer;
Data : Pointer;
Left, Right: PTreeNode;
end;
TIteratorProc = procedure (Key, Data: Pointer) of object;
TCompareFunc = function (Key1, Key2: Pointer): Integer;
{ TBinaryTree }
TBinaryTree = class(TObject)
private
FRoot: PTreeNode;
FRecordCount: Integer;
_Compare: TCompareFunc;
function _Count(Root: PTreeNode): Integer;
function _Remove(var Root: PTreeNode; Key: Pointer): Boolean;
function _Insert(var Root: PTreeNode; Node: PTreeNode): Boolean;
procedure _Iterate(var Root: PTreeNode; Proc: TIteratorProc);
function _Search(var Root: PTreeNode; Key: Pointer): PTreeNode;
procedure _Clear(var Root: PTreeNode);
protected
function CreateNode(Key: Pointer; Data: Pointer): PTreeNode;
procedure DestroyNode(var Node: PTreeNode); virtual;
public
constructor Create(Compare: TCompareFunc);
destructor Destroy; override;
function Insert(Key: Pointer; Data: Pointer): Boolean; overload;
function Insert(Key: NativeInt; Data: Pointer): Boolean; overload;
function Search(Key: Pointer; var Data: Pointer): Boolean; overload;
function Search(Key: NativeInt; var Data: Pointer): Boolean; overload;
function Remove(Key: Pointer): Boolean; overload;
function Remove(Key: NativeInt): Boolean; overload;
procedure Iterate(Proc: TIteratorProc);
procedure Clear;
function CalcRecordCount: Integer;
property RecordCount: Integer read FRecordCount;
end;
function IntegerCompareFunc(Key1, Key2: Pointer): Integer;
function PointerCompareFunc(Key1, Key2: Pointer): Integer;
implementation
// Overflow save integer compare
function IntegerCompareFunc(Key1, Key2: Pointer): Integer;
begin
if PInteger(Key1) > PInteger(Key2) then
Result := 1
else
if PInteger(Key1) < PInteger(Key2) then
Result := -1
else
Result := 0;
end;
// Overflow save pointer compare that works with 32 or 64 bit systems
function PointerCompareFunc(Key1, Key2: Pointer): Integer;
begin
if PtrUInt(Key1) > PtrUInt(Key2) then
Result := 1
else
if PtrUInt(Key1) < PtrUInt(Key2) then
Result := -1
else
Result := 0;
end;
constructor TBinaryTree.Create(Compare: TCompareFunc);
begin
inherited Create;
_Compare := Compare;
end;
destructor TBinaryTree.Destroy;
begin
Clear;
inherited Destroy;
end;
function TBinaryTree.CreateNode(Key: Pointer; Data: Pointer): PTreeNode;
begin
Result := New(PTreeNode);
Result^.Key := Key;
Result^.Data := Data;
Result^.Left := nil;
Result^.Right := nil;
end;
procedure TBinaryTree.DestroyNode(var Node: PTreeNode);
begin
Dispose(Node);
Node := nil;
end;
function TBinaryTree.Insert(Key: Pointer; Data: Pointer): Boolean;
begin
Result := _Insert(FRoot,CreateNode(Key,Data));
if Result then inc(FRecordCount);
end;
function TBinaryTree.Insert(Key: NativeInt; Data: Pointer): Boolean;
begin
Result := Insert(PPtrInt(Key),Data);
end;
function TBinaryTree._Insert(var Root: PTreeNode; Node: PTreeNode): Boolean;
var
CompareResult: Integer;
begin
Result := True;
if Root = nil then
Root := Node
else
begin
CompareResult := _Compare(Node^.Key,Root^.Key);
if CompareResult < 0 then
Result := _Insert(Root^.Left,Node)
else
if CompareResult > 0 then
Result := _Insert(Root^.Right,Node)
else
begin
Result := False;
DestroyNode(Node);
end;
end;
end;
function TBinaryTree.Search(Key: Pointer; var Data: Pointer): Boolean;
var
Node: PTreeNode;
begin
Node := _Search(FRoot,Key);
Result := Node <> nil;
if Result then
Data := Node^.Data;
end;
function TBinaryTree.Search(Key: NativeInt; var Data: Pointer): Boolean;
begin
Result := Search(PPtrInt(Key),Data);
end;
function TBinaryTree._Search(var Root: PTreeNode; Key: Pointer): PTreeNode;
var
CompareResult: Integer;
begin
Result := Root;
if Root <> nil then
begin
CompareResult := _Compare(Key,Root^.Key);
if CompareResult < 0 then
Result := _Search(Root^.Left,Key)
else
if CompareResult > 0 then
Result := _Search(Root^.Right,Key);
end;
end;
procedure TBinaryTree.Iterate(Proc: TIteratorProc);
begin
_Iterate(FRoot,Proc);
end;
procedure TBinaryTree._Iterate(var Root: PTreeNode; Proc: TIteratorProc);
begin
if Root <> nil then
begin
_Iterate(Root^.Left,Proc);
Proc(Root^.Key,Root^.Data);
_Iterate(Root^.Right,Proc);
end;
end;
procedure TBinaryTree.Clear;
begin
_Clear(FRoot);
FRecordCount := 0;
end;
function TBinaryTree._Count(Root: PTreeNode): Integer;
begin
if Root = nil then
Result := 0
else
Result := 1 + _Count(Root^.Left) + _Count(Root^.Right);
end;
function TBinaryTree.CalcRecordCount: Integer;
begin
Result := _Count(FRoot);
end;
procedure TBinaryTree._Clear(var Root: PTreeNode);
begin
if Root <> nil then
begin
_Clear(Root^.Left);
_Clear(Root^.Right);
DestroyNode(Root);
end;
end;
function TBinaryTree.Remove(Key: Pointer): Boolean;
begin
Result := _Remove(FRoot,Key);
if Result then
dec(FRecordCount);
end;
function TBinaryTree.Remove(Key: NativeInt): Boolean;
begin
Result := Remove(PPtrInt(Key));
end;
function TBinaryTree._Remove(var Root: PTreeNode; Key: Pointer): Boolean;
var
CompareResult: Integer;
Q: PTreeNode;
begin
if Root = nil then
Result := False
else
begin
CompareResult := _Compare(Key,Root^.Key);
if CompareResult < 0 then
Result := _Remove(Root^.Left,Key)
else
if CompareResult > 0 then
Result := _Remove(Root^.Right,Key)
else
begin
Result := True;
if Root^.Left = nil then // No left tree, move right tree up to replace node.
begin
Q := Root;
Root := Root^.Right;
DestroyNode(Q);
end
else
if Root^.Right = nil then // No right tree, move left tree up to replace node
begin
Q := Root;
Root := Root^.Left;
DestroyNode(Q);
end
else
begin
// Find immediate successor of root and store in Q
Q := Root^.Right;
while Q^.Left <> nil do
Q := Q^.Left;
// Copy successor data into root
Root^.Key := Q^.Key;
Root^.Data := Q^.Data;
// Remove the successor instead
Result := _Remove(Root^.Right,Q^.Key);
end;
end;
end;
end;
{procedure TRBTree.RotateLeft(var Node: PTreeNode);
var
Q: PTreeNode;
begin
Q := Node^.Right;
Node^.Right := Q^.Left;
Q^.Left := Node;
Node := Q;
end;
procedure TRBTree.RotateRight(var Node: PTreeNode);
var
Q: PTreeNode;
begin
Q := Node^.Left;
Node^.Left := Q^.Right;
Q^.Right := Node;
Node := Q;
end; }
end.