forked from JuYanYan/AA-Tree
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaa_chain.cpp
More file actions
328 lines (226 loc) · 6.46 KB
/
Copy pathaa_chain.cpp
File metadata and controls
328 lines (226 loc) · 6.46 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aa_chain.h"
/* orignal in Github JuYanYan/AA-Tree */
/* AA tree with minimum code, modified for use with multiple structs, each in its own tree.
This code requires that structs are declared beginning with NODE at the bginnning and uses casts to
treat NODE as equivalent to start of each data structure. As in -
typedef struct test
{
NODE node;
int data1;
int data2;
..etc...
} EXAMPLE;
Delete - swopping nodes is complex as this code does not keep track of 'parent node' which would
be required to relink both of the two nodes. The original code simply swaps the data in the two nodes
and then Skew() and Split() as required. So this code is slightly less efficient, using memcpy to copy the data
of the struct after the NODE.
Note that there is now a COMPARE and a PRINT subroutine for each tree, so TREE and NODE are no longer the same
structure, and must be supplied for each type of STRUCT.
*/
static NODE* Skew(NODE* t)
{
NODE *l;
if (t == NULL) return NULL;
if (t->left == NULL) return t;
if (t->left->level == t->level)
{
l = t->left;
t->left = l->right;
l->right = t;
return l;
}
return t;
}
static NODE * Split(NODE *t)
{
NODE *r;
if (t == NULL) return NULL;
if (t->right == NULL || t->right->right == NULL)
return t;
if (t->right->right->level == t->level)
{
r = t->right;
t->right = r->left;
r->left = t;
r->level++;
return r;
}
return t;
}
static NODE *Find_node (TREE *tree, NODE *node, NODE *data)
{
int ans ;
node = tree->root;
while (node != NULL)
{
ans = tree->cmp(data, node);
if (!ans)
{
// tree->lastnode = node;
return node;
}
if (ans > 0)
node = node->right;
else
node = node->left;
}
return NULL; // Not found.
}
static NODE* Predecessor (NODE* Node)
{
Node = Node->left;
while (Node->right != NULL)
{
Node = Node->right;
}
return Node;
}
static NODE* Successor(NODE* Node)
{
Node = Node->right;
while (Node->left != NULL)
{
Node = Node->left;
}
return Node; //need parent of this node also....
}
static NODE * Decrease_level(NODE *t)
{
int wdo;
if (t->left != NULL && t->right != NULL)
{
wdo = t->left->level > t->right->level ? t->right->level : t->left->level;
wdo++;
if (wdo < t->level)
{
t->level = wdo;
if (t->right != NULL && wdo < t->right->level)
{
t->right->level = wdo;
}
}
}
return t;
}
static NODE * InsertData(TREE *x, NODE *t, NODE *data)
{
int ans;
if (t == NULL) // reached a leaf
{
// t = mallocNode(); // new node to insert
// t->data = data; // void * for anything attached
t = (NODE*) data; //if data is node THIS WORKS !!
t->left = NULL;
t->right = NULL;
t->level = 1;
// x->lastnode = t; // node inserted
return t;
}
ans = x->cmp(data, t);
if (ans < 0 ) // Find position to insert node.
t->left = InsertData(x,t->left, data); // if t->left = null add here
else if (ans > 0)
t->right = InsertData(x,t->right, data); // if t->right = null add here
t = Skew(t); // Follow the path from the new node to node.
t = Split(t); // Always do: Skew and Split.
return t;
}
static void copy_data(TREE *tree, NODE *a, NODE *b)
{
char *x, *y;
int size;
x = (char*) a + sizeof(NODE);
y = (char*) b + sizeof(NODE);
size = tree->esize - sizeof(NODE);
memcpy (x,y, size);
}
static NODE * DeleteNode(TREE *x, NODE *t, NODE *data)
{
NODE *m;
int ans;
if (t == NULL) return NULL;
ans = x->cmp(data, t);
// Find the node to delete.
if (ans < 0)
{
t->left = DeleteNode(x, t->left, data);
}
else if (ans > 0)
{
t->right = DeleteNode(x, t->right, data);
}
else
{ // ans == 0 .. found node // key == t->key
if (t->left == NULL && t->right == NULL) // The node is a leaf. Simply remove it.
{
free(t);
return NULL;
}
if (t->left == NULL) // Only has right children.
{
NODE *l;
l = Successor(t); // get next item by data (not by tree link)
copy_data(x,t,l);
t->right = DeleteNode(x, t->right, l); // Delete successor node.
}
else { // has a left child
NODE *l;
l = Predecessor(t); //get previous item by data
copy_data(x,t,l);
t->left = DeleteNode(x, t->left, l); // delete left node ??
}
}
t = Decrease_level(t);
t = Skew(t);
m = t->right;
t->right = Skew(m);
if (m != NULL && m->right != NULL)
{
t->right->right = Skew(m->right);
}
t = Split(t);
t->right = Split(m);
return t;
}
static void free_node (NODE *t)
{
if (t == NULL) return;
free_node(t->left);
free_node(t->right);
free(t);
t = NULL; //probably redundant
}
static void PrintNode(TREE *t, NODE *node)
{
if (!node) return;
PrintNode (t, node->right);
t->prt(t,node,1);
PrintNode (t, node->left);
}
//external calls
extern void aat_insertData(TREE *t, void *data)
{
t->root = InsertData (t, t->root, (NODE*) data); // To make code simple, using a tiny function to call _InsertData.
}
extern void aat_deleteData(TREE *t, void * data)
{
DeleteNode (t, t->root, (NODE*) data);
}
extern void* aat_searchData(TREE *tree, void *data)
{
return Find_node (tree, tree->root, (NODE*) data);
}
extern void aat_deleteTree(TREE *t)
{
if (t == NULL) return;
free_node(t->root);
}
extern void aat_printTree(TREE *tree)
{
PrintNode(tree, tree->root);
printf("\n\n ROOT ");
tree->prt(tree, tree->root,1);
}