blob: 1084f8e1afbd14b8faa49e303d2b0816f4ab4dc8 (
plain) (
blame)
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
|
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Copyright (c) 1989 Microsoft Corporation
Module Name:
pdict.cxx
Abstract:
This file contains the definition for splay tree self
adjusting binary trees.
History:
1990 Dov Harel Created
----------------------------------------------------------------------------*/
#include "pdict.hxx"
// handly macros used to define common tree operations
#define ROTATELEFT tmp=t->right; t->right=tmp->left; tmp->left =t; t=tmp
#define ROTATERIGHT tmp=t->left; t->left =tmp->right; tmp->right=t; t=tmp
#define LINKLEFT tmp=t; t = t->right; l = l->right = tmp
#define LINKRIGHT tmp=t; t = t->left; r = r->left = tmp
#define ASSEMBLE r->left = t->right; l->right = t->left; \
t->left = Dummy->right; t->right = Dummy->left
static TreeNode Dumbo(Nil);
static TreeNode *Dummy = &Dumbo; // a global dummy node
//*************************************************************************
//***** Core functions (internal) *****
//*************************************************************************
long // return last comparision
Dictionary::SplayUserType( // general top down splay
pUserType keyItem // pointer to a "key item" searched for
) //-----------------------------------------------------------------------//
{
TreeNode* t; // current search point
TreeNode* l; // root of "left subtree" < keyItem
TreeNode* r; // root of "right subtree" > keyItem
long kcmp; // cash comparison results
TreeNode* tmp;
if ((fCompare = Compare(keyItem, root->item)) == 0)
return (fCompare);
Dummy = l = r = &Dumbo;
Dumbo.left = Dumbo.right = Nil;
t = root;
do {
if ( fCompare < 0 ) {
if ( t->left == Nil ) break;
if ( (kcmp = Compare(keyItem, t->left->item)) == 0 ) {
LINKRIGHT;
}
else if ( kcmp < 0 ) {
ROTATERIGHT;
if ( t->left != Nil ) {
LINKRIGHT;
}
}
else { // keyItem > t->left->item
LINKRIGHT;
if ( t->right != Nil ) {
LINKLEFT;
}
}
}
else { // keyItem > t->item
if ( t->right == Nil ) break;
if ( (kcmp = Compare(keyItem, t->right->item)) == 0 ) {
LINKLEFT;
}
else if ( kcmp > 0 ) {
ROTATELEFT;
if ( t->right != Nil ) {
LINKLEFT;
}
}
else { // keyItem < t->right->item
LINKLEFT;
if ( t->left != Nil ) {
LINKRIGHT;
}
}
}
} while ( (fCompare = Compare(keyItem, t->item)) != 0 );
ASSEMBLE;
root = t;
return(fCompare);
}
TreeNode*
SplayLeft(
TreeNode* t // root of tree & current "search" point
) //-----------------------------------------------------------------------//
{
TreeNode* l=Dummy; // root of "left subtree" < keyItem
TreeNode* r=Dummy; // root of "right subtree" > keyItem
TreeNode* tmp;
if (t == Nil || t->left == Nil)
return(t);
if (t->left->left == Nil) {
ROTATERIGHT;
return(t);
}
Dummy->left = Dummy->right = Nil;
while ( t->left != Nil ) {
ROTATERIGHT;
if ( t->left != Nil ) {
LINKRIGHT;
}
}
ASSEMBLE;
return(t);
}
#ifndef DICT_NOPREV
TreeNode*
SplayRight(
TreeNode* t // root of tree & current "search" point
) //-----------------------------------------------------------------------//
{
TreeNode* l=Dummy; // root of "left subtree" < keyItem
TreeNode* r=Dummy; // root of "right subtree" > keyItem
TreeNode* tmp;
if (t == Nil || t->right == Nil)
return(t);
Dummy->left = Dummy->right = Nil;
while ( t->right != Nil ) {
ROTATELEFT;
if ( t->right != Nil ) {
LINKLEFT;
}
}
ASSEMBLE;
return(t);
}
#endif
// Class methods for Splay Tree
Dict_Status
Dictionary::Dict_Find( // return a item that matches
pUserType itemI // this value
// Returns:
// itemCur - Nil if at not in Dict, else found item
) //-----------------------------------------------------------------------//
{
itemCur = Nil;
if (root == Nil)
return (EMPTY_DICTIONARY);
if (itemI == Nil)
return (NULL_ITEM);
if (SplayUserType (itemI) == 0){
itemCur = root->item;
return(SUCCESS);
}
return(ITEM_NOT_FOUND);
}
#ifndef DICT_NONEXT
Dict_Status
Dictionary::Dict_Next( // return the next item
pUserType itemI // of a key greater then this
// Returns:
// itemCur - Nil if at end of Dict, else current item
) //-----------------------------------------------------------------------//
{
TreeNode* t;
itemCur = Nil;
if (root == Nil)
return (EMPTY_DICTIONARY);
if (itemI == Nil) { // no arg, return first record
root = SplayLeft (root);
itemCur = root->item;
return (SUCCESS);
}
if (itemI != root->item)
if (SplayUserType (itemI) > 0) {
itemCur = root->item;
return (SUCCESS);
}
if (root->right == Nil)
return (LAST_ITEM);
t = root;
root = SplayLeft (root->right);
root->left = t;
t->right = Nil;
itemCur = root->item;
return (SUCCESS);
}
#endif // DICT_NONEXT
#ifndef DICT_NOPREV
Dict_Status
Dictionary::Dict_Prev( // return the previous item
pUserType itemI // of a key less then this
// Returns:
// itemCur - Nil if at begining of Dict, else current item
) //-----------------------------------------------------------------------//
{
TreeNode* t;
itemCur = Nil;
if (root == Nil)
return (EMPTY_DICTIONARY);
if (itemI == Nil) { // no arg, return last record
root = SplayRight (root);
itemCur = root->item;
return (SUCCESS);
}
if (itemI != root->item)
if (SplayUserType (itemI) < 0) {
itemCur = root->item;
return (SUCCESS);
}
if (root->left == Nil)
return (LAST_ITEM);
t = root;
root = SplayRight (root->left);
root->right = t;
t->left = Nil;
itemCur = root->item;
return (SUCCESS);
}
#endif // DICT_NOPREV
Dict_Status
Dictionary::Dict_Insert( // insert the given item into the tree
pUserType itemI // the item to be inserted
// Returns:
// itemCur - point to new item
) //-----------------------------------------------------------------------//
{
TreeNode *newNode, *t;
if ((itemCur = itemI) == Nil)
return (NULL_ITEM);
if (root == Nil) {
root = new TreeNode(itemI);
size++;
return (SUCCESS);
}
if (SplayUserType (itemI) == 0)
return (ITEM_ALREADY_PRESENT);
newNode = new TreeNode(itemI);
size++;
t = root;
if (fCompare > 0) {
newNode->right = t->right; // item >= t->item
newNode->left = t;
t->right = Nil;
}
else {
newNode->left = t->left;
newNode->right = t;
t->left = Nil;
}
root = newNode;
return (SUCCESS);
}
Dict_Status
Dictionary::Dict_Delete( // delete the given item from the tree
pUserType *itemI // points to the (key) item to be deleted
// Returns:
// itemCur is Nil - undefined
) //-----------------------------------------------------------------------//
{
TreeNode *t, *r;
itemCur = Nil;
if (root == Nil)
return (EMPTY_DICTIONARY);
if (itemI == Nil)
return (NULL_ITEM);
if (itemI != root->item) {
if (SplayUserType (*itemI) != 0)
return(ITEM_NOT_FOUND);
}
*itemI = root->item;
t = root;
if (t->left == Nil)
root = t->right;
else if ( (r = t->right) == Nil)
root = t->left;
else {
r = SplayLeft (r);
r->left = t->left; // at this point r->left == Nil
root = r;
}
delete t;
size--;
return (SUCCESS);
}
|