4 > Created (Julienne Walker): September 10, 2005
5 > Corrections (James Bucanek): April 10, 2006
7 up != 0 should be top != 0
9 skew ( path[top] ) should be skew ( up )
10 split ( path[top] ) should be split ( up )
11 3) Bug in skew and split macros:
12 Condition should test for nil
14 Search for successor should save the path
16 #include "jsw_atree.h"
21 #define HEIGHT_LIMIT 64 /* Tallest allowable tree */
24 typedef struct jsw_anode {
25 int level; /* Horizontal level for balance */
26 void *data; /* User-defined content */
27 struct jsw_anode *link[2]; /* Left (0) and right (1) links */
31 jsw_anode_t *root; /* Top of the tree */
32 jsw_anode_t *nil; /* End of tree sentinel */
33 cmp_f cmp; /* Compare two items */
34 dup_f dup; /* Clone an item (user-defined) */
35 rel_f rel; /* Destroy an item (user-defined) */
36 size_t size; /* Number of items (user-defined) */
40 jsw_atree_t *tree; /* Paired tree */
41 jsw_anode_t *it; /* Current node */
42 jsw_anode_t *path[HEIGHT_LIMIT]; /* Traversal path */
43 size_t top; /* Top of stack */
46 /* Remove left horizontal links */
47 #define skew(t) do { \
48 if ( t->link[0]->level == t->level && t->level != 0 ) { \
49 jsw_anode_t *save = t->link[0]; \
50 t->link[0] = save->link[1]; \
56 /* Remove consecutive horizontal links */
57 #define split(t) do { \
58 if ( t->link[1]->link[1]->level == t->level && t->level != 0 ) { \
59 jsw_anode_t *save = t->link[1]; \
60 t->link[1] = save->link[0]; \
67 static jsw_anode_t *new_node ( jsw_atree_t *tree, void *data )
69 jsw_anode_t *rn = malloc ( sizeof *rn );
75 rn->data = tree->dup ( data );
76 rn->link[0] = rn->link[1] = tree->nil;
81 jsw_atree_t *jsw_anew ( cmp_f cmp, dup_f dup, rel_f rel )
83 jsw_atree_t *rt = malloc ( sizeof *rt );
88 /* Initialize sentinel */
89 rt->nil = malloc ( sizeof *rt->nil );
90 if ( rt->nil == NULL ) {
95 rt->nil->data = NULL; /* Simplifies some ops */
97 rt->nil->link[0] = rt->nil->link[1] = rt->nil;
109 void jsw_adelete ( jsw_atree_t *tree )
111 jsw_anode_t *it = tree->root;
114 /* Destruction by rotation */
115 while ( it != tree->nil ) {
116 if ( it->link[0] == tree->nil ) {
119 tree->rel ( it->data );
125 it->link[0] = save->link[1];
132 /* Finalize destruction */
137 void *jsw_afind ( jsw_atree_t *tree, void *data )
139 jsw_anode_t *it = tree->root;
141 while ( it != tree->nil ) {
142 int cmp = tree->cmp ( it->data, data );
147 it = it->link[cmp < 0];
150 /* nil->data == NULL */
154 int jsw_ainsert ( jsw_atree_t *tree, void *data )
156 if ( tree->root == tree->nil ) {
157 /* Empty tree case */
158 tree->root = new_node ( tree, data );
159 if ( tree->root == tree->nil )
163 jsw_anode_t *it = tree->root;
164 jsw_anode_t *path[HEIGHT_LIMIT];
167 /* Find a spot and save the path */
170 dir = tree->cmp ( it->data, data ) < 0;
172 if ( it->link[dir] == tree->nil )
178 /* Create a new item */
179 it->link[dir] = new_node ( tree, data );
180 if ( it->link[dir] == tree->nil )
183 /* Walk back and rebalance */
184 while ( --top >= 0 ) {
187 dir = path[top - 1]->link[1] == path[top];
194 path[top - 1]->link[dir] = path[top];
196 tree->root = path[top];
205 int jsw_aerase ( jsw_atree_t *tree, void *data )
207 if ( tree->root == tree->nil )
210 jsw_anode_t *it = tree->root;
211 jsw_anode_t *path[HEIGHT_LIMIT];
212 int top = 0, dir, cmp;
214 /* Find node to remove and save path */
218 if ( it == tree->nil )
221 cmp = tree->cmp ( it->data, data );
229 /* Remove the found node */
230 if ( it->link[0] == tree->nil
231 || it->link[1] == tree->nil )
233 /* Single child case */
234 int dir2 = it->link[0] == tree->nil;
236 /* Unlink the item */
238 path[top - 1]->link[dir] = it->link[dir2];
240 tree->root = it->link[1];
242 tree->rel ( it->data );
247 jsw_anode_t *heir = it->link[1];
248 jsw_anode_t *prev = it;
250 while ( heir->link[0] != tree->nil ) {
251 path[top++] = prev = heir;
252 heir = heir->link[0];
257 (free item, replace item, free heir)
259 tree->rel ( it->data );
260 it->data = heir->data;
261 prev->link[prev == it] = heir->link[1];
265 /* Walk back up and rebalance */
266 while ( --top >= 0 ) {
267 jsw_anode_t *up = path[top];
270 dir = path[top - 1]->link[1] == up;
272 /* Rebalance (aka. black magic) */
273 if ( up->link[0]->level < up->level - 1
274 || up->link[1]->level < up->level - 1 )
276 if ( up->link[1]->level > --up->level )
277 up->link[1]->level = up->level;
279 /* Order is important! */
281 skew ( up->link[1] );
282 skew ( up->link[1]->link[1] );
284 split ( up->link[1] );
289 path[top - 1]->link[dir] = up;
300 size_t jsw_asize ( jsw_atree_t *tree )
305 jsw_atrav_t *jsw_atnew ( void )
307 return malloc ( sizeof ( jsw_atrav_t ) );
310 void jsw_atdelete ( jsw_atrav_t *trav )
316 First step in traversal,
319 static void *start ( jsw_atrav_t *trav,
320 jsw_atree_t *tree, int dir )
323 trav->it = tree->root;
326 /* Build a path to work with */
327 if ( trav->it != tree->nil ) {
328 while ( trav->it->link[dir] != tree->nil ) {
329 trav->path[trav->top++] = trav->it;
330 trav->it = trav->it->link[dir];
334 /* Could be nil, but nil->data == NULL */
335 return trav->it->data;
339 Subsequent traversal steps,
340 handles ascending and descending
342 static void *move ( jsw_atrav_t *trav, int dir )
344 jsw_anode_t *nil = trav->tree->nil;
346 if ( trav->it->link[dir] != nil ) {
347 /* Continue down this branch */
348 trav->path[trav->top++] = trav->it;
349 trav->it = trav->it->link[dir];
351 while ( trav->it->link[!dir] != nil ) {
352 trav->path[trav->top++] = trav->it;
353 trav->it = trav->it->link[!dir];
357 /* Move to the next branch */
361 if ( trav->top == 0 ) {
367 trav->it = trav->path[--trav->top];
368 } while ( last == trav->it->link[dir] );
371 /* Could be nil, but nil->data == NULL */
372 return trav->it->data;
375 void *jsw_atfirst ( jsw_atrav_t *trav, jsw_atree_t *tree )
377 return start ( trav, tree, 0 ); /* Min value */
380 void *jsw_atlast ( jsw_atrav_t *trav, jsw_atree_t *tree )
382 return start ( trav, tree, 1 ); /* Max value */
385 void *jsw_atnext ( jsw_atrav_t *trav )
387 return move ( trav, 1 ); /* Toward larger items */
390 void *jsw_atprev ( jsw_atrav_t *trav )
392 return move ( trav, 0 ); /* Toward smaller items */