forked from yaosj2k/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 9
/
bst.c
executable file
·339 lines (284 loc) · 8.07 KB
/
bst.c
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
#include <string.h>
#include <assert.h>
#include "bst.h"
#include "utils.h"
#include "logs.h"
PRIFUNC Bst_NodeHead *GetUnusedNode(Bst *t)
{
if( t->FreeList == NULL )
{
return t->Nodes.Add(&(t->Nodes),
NULL,
sizeof(Bst_NodeHead) + t->ElementLength,
TRUE
);
} else {
Bst_NodeHead *ret = t->FreeList;
t->FreeList = ret->Right;
return ret;
}
}
PRIFUNC const void *InsertNode(Bst *t,
Bst_NodeHead *ParentNode,
int CompareResult,
const void *Data
)
{
Bst_NodeHead *NewNode = GetUnusedNode(t);
if( NewNode == NULL )
{
return NULL;
}
/* Set parent node */
if( ParentNode == NULL )
{
/* Insert as the root */
t->Root = NewNode;
} else {
/* Non-root */
if( CompareResult <= 0 )
{
assert(ParentNode->Left == NULL);
ParentNode->Left = NewNode;
} else {
assert(ParentNode->Right == NULL);
ParentNode->Right = NewNode;
}
}
/* Set the new child node */
NewNode->Parent = ParentNode;
NewNode->Left = NULL;
NewNode->Right = NULL;
/* Copy the data */
memcpy(NewNode + 1, Data, t->ElementLength);
/* Return the data position */
return (const void *)(NewNode + 1);
}
PUBFUNC const void *Bst_Add(Bst *t, const void *Data)
{
if( t->Root == NULL )
{
/* Insert as root */
return InsertNode(t, NULL, 0, Data);
} else {
/* Non-root, finding the currect place to insert */
Bst_NodeHead *Current = t->Root;
while( TRUE )
{
int CompareResult = (t->Compare)(Data, (const void *)(Current + 1));
if( CompareResult <= 0 )
{
/* Left branch */
Bst_NodeHead *Left = Current->Left;
if( Left == NULL )
{
/* Insert as a left child */
return InsertNode(t, Current, CompareResult, Data);
}
Current = Left;
} else {
/* Right branch */
Bst_NodeHead *Right = Current->Right;
if( Right == NULL )
{
/* Insert as a right child */
return InsertNode(t, Current, CompareResult, Data);
}
Current = Right;
}
}
}
}
PUBFUNC const void *Bst_Search(Bst *t, const void *Key, const void *Last)
{
Bst_NodeHead *Current;
/* Set the starting point */
if( Last == NULL )
{
/* root as the starting point */
Current = t->Root;
} else {
Current = (((Bst_NodeHead *)Last) - 1)->Left;
}
while( Current != NULL )
{
int CompareResult = (t->Compare)(Key, (const void *)(Current + 1));
if( CompareResult == 0 )
{
return (const void *)(Current + 1);
} else if( CompareResult < 0 ){
Current = Current->Left;
} else /** CompareResult > 0 */{
Current = Current->Right;
}
}
return NULL;
}
PRIFUNC void Bst_Enum_Inner(Bst *t,
Bst_NodeHead *n,
Bst_Enum_Callback cb,
void *Arg,
BOOL *StopFlag
)
{
if( n == NULL || *StopFlag )
{
return;
}
*StopFlag = cb(t, n + 1, Arg) != 0;
Bst_Enum_Inner(t, n->Left, cb, Arg, StopFlag);
Bst_Enum_Inner(t, n->Right, cb, Arg, StopFlag);
}
PUBFUNC void Bst_Enum(Bst *t, Bst_Enum_Callback cb, void *Arg)
{
BOOL StopFlag = FALSE;
Bst_Enum_Inner(t, t->Root, cb, Arg, &StopFlag);
}
PUBFUNC const void *Bst_Minimum(Bst *t, const void *Subtree)
{
Bst_NodeHead *Current;
if( Subtree == NULL )
{
/* Starting with the root */
if( t->Root == NULL )
{
/* Empty tree */
return NULL;
}
Current = t->Root;
} else {
Current = ((Bst_NodeHead *)Subtree) - 1;
}
while( Current->Left != NULL )
{
Current = Current->Left;
}
return (const void *)(Current + 1);
}
PUBFUNC const void *Bst_Successor(Bst *t, const void *Last)
{
Bst_NodeHead *Current = ((Bst_NodeHead *)Last) - 1;
if( Current->Right != NULL )
{
return Bst_Minimum(t, (Current->Right) + 1);
} else {
Bst_NodeHead *Parent = Current->Parent;
while( Parent != NULL && Parent->Left != Current )
{
Current = Parent;
Parent = Parent->Parent;
}
return Parent == NULL ? NULL : (const void *)(Parent + 1);
}
}
PUBFUNC void Bst_Delete(Bst *t, const void *Node)
{
Bst_NodeHead *Current = ((Bst_NodeHead *)Node) - 1;
Bst_NodeHead *ActuallyRemoved, *Child;
/* Finding the node that will be actually removed. */
if( Current->Left == NULL || Current->Right == NULL )
{
/* If Current has one or no child */
ActuallyRemoved = Current;
} else {
/* If Current has two child */
ActuallyRemoved = ((Bst_NodeHead *)Bst_Successor(t, Current + 1)) - 1;
}
/* If ActuallyRemoved:
has two child, impossible case,
has only one child, get the child,
or no child, set it to NULL
*/
if( ActuallyRemoved->Left != NULL )
{
Child = ActuallyRemoved->Left;
} else {
Child = ActuallyRemoved->Right;
}
/* If ActuallyRemoved has one child ( Child != NULL ) */
if( Child != NULL )
{
/* Set the child's parent to its parent's parent */
Child->Parent = ActuallyRemoved->Parent;
}
if( ActuallyRemoved->Parent == NULL )
{
/* If ActuallyRemoved is the root */
t->Root = Child;
} else {
/* Or not the root */
if( ActuallyRemoved->Parent->Left == ActuallyRemoved )
{
/* If ActuallyRemoved is a left child */
ActuallyRemoved->Parent->Left = Child;
} else {
/* Or a right child */
ActuallyRemoved->Parent->Right = Child;
}
}
if( ActuallyRemoved != Current )
{
/* Replace Current with ActuallyRemoved */
/*memcpy(Current + 1, ActuallyRemoved + 1, t->ElementLength);*/
Bst_NodeHead *CurrentParent = Current->Parent;
Bst_NodeHead *CurrentLeft = Current->Left;
Bst_NodeHead *CurrentRight = Current->Right;
/* Parent */
if( CurrentParent != NULL )
{
if( CurrentParent->Left == Current )
{
CurrentParent->Left = ActuallyRemoved;
} else {
CurrentParent->Right = ActuallyRemoved;
}
} else {
t->Root = ActuallyRemoved;
}
/* Left Child */
CurrentLeft->Parent = ActuallyRemoved;
/* Right Child */
if( CurrentRight != NULL )
{
CurrentRight->Parent = ActuallyRemoved;
}
/* ActuallyRemoved */
ActuallyRemoved->Parent = CurrentParent;
ActuallyRemoved->Left = CurrentLeft;
ActuallyRemoved->Right = CurrentRight;
ActuallyRemoved = Current;
}
ActuallyRemoved->Right = t->FreeList;
t->FreeList = ActuallyRemoved;
return;
}
PUBFUNC void Bst_Reset(Bst *t)
{
t->Nodes.Clear(&(t->Nodes));
t->Root = NULL;
t->FreeList = NULL;
}
PUBFUNC void Bst_Free(Bst *t)
{
t->Nodes.Free(&(t->Nodes));
}
int Bst_Init(Bst *t, int ElementLength, CompareFunc Compare)
{
t->Compare = Compare;
t->Root = NULL;
t->FreeList = NULL;
t->ElementLength = ElementLength;
if( StableBuffer_Init(&(t->Nodes)) != 0 )
{
return -497;
}
t->Add = Bst_Add;
t->Delete = Bst_Delete;
t->Enum = Bst_Enum;
t->Free = Bst_Free;
t->Minimum = Bst_Minimum;
t->Reset = Bst_Reset;
t->Search = Bst_Search;
t->Successor = Bst_Successor;
return 0;
}