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
|
/*
* $Id: splay.c,v 1.16 2006/08/06 18:40:59 hno Exp $
*/
#include "config.h"
#if HAVE_STDIO_H
#include <stdio.h>
#endif
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "splay.h"
#include "util.h"
int splayLastResult = 0;
splayNode *
splay_insert(void *data, splayNode * top, SPLAYCMP * compare)
{
splayNode *new = xcalloc(sizeof(splayNode), 1);
new->data = data;
top = splay_splay(data, top, compare);
if (top == NULL) {
new->left = new->right = NULL;
return new;
}
if (splayLastResult < 0) {
new->left = top->left;
new->right = top;
top->left = NULL;
return new;
} else if (splayLastResult > 0) {
new->right = top->right;
new->left = top;
top->right = NULL;
return new;
} else {
/* duplicate entry */
xfree(new);
return top;
}
}
splayNode *
splay_splay(const void *data, splayNode * top, SPLAYCMP * compare)
{
splayNode N;
splayNode *l;
splayNode *r;
splayNode *y;
if (top == NULL) {
splayLastResult = -1;
return top;
}
N.left = N.right = NULL;
l = r = &N;
for (;;) {
splayLastResult = compare(data, top->data);
if (splayLastResult < 0) {
if (top->left == NULL)
break;
if ((splayLastResult = compare(data, top->left->data)) < 0) {
y = top->left; /* rotate right */
top->left = y->right;
y->right = top;
top = y;
if (top->left == NULL)
break;
}
r->left = top; /* link right */
r = top;
top = top->left;
} else if (splayLastResult > 0) {
if (top->right == NULL)
break;
if ((splayLastResult = compare(data, top->right->data)) > 0) {
y = top->right; /* rotate left */
top->right = y->left;
y->left = top;
top = y;
if (top->right == NULL)
break;
}
l->right = top; /* link left */
l = top;
top = top->right;
} else {
break;
}
}
l->right = top->left; /* assemble */
r->left = top->right;
top->left = N.right;
top->right = N.left;
return top;
}
void
splay_destroy(splayNode * top, SPLAYFREE * free_func)
{
if (!top)
return;
if (top->left)
splay_destroy(top->left, free_func);
if (top->right)
splay_destroy(top->right, free_func);
free_func(top->data);
xfree(top);
}
void
splay_walk(splayNode * top, SPLAYWALKEE * walkee, void *state)
{
if (top->left)
splay_walk(top->left, walkee, state);
walkee(top->data, state);
if (top->right)
splay_walk(top->right, walkee, state);
}
#ifdef DEBUG
void
splay_dump_entry(void *data, int depth)
{
printf("%*s%s\n", depth, "", (char *) data);
}
static void
splay_do_dump(splayNode * top, void printfunc(void *data, int depth), int depth)
{
if (!top)
return;
splay_do_dump(top->left, printfunc, depth + 1);
printfunc(top->data, depth);
splay_do_dump(top->right, printfunc, depth + 1);
}
void
splay_dump(splayNode * top, void printfunc(void *data, int depth))
{
splay_do_dump(top, printfunc, 0);
}
#endif
#ifdef DRIVER
typedef struct {
int i;
} intnode;
int
compareint(void *a, splayNode * n)
{
intnode *A = a;
intnode *B = n->data;
return A->i - B->i;
}
void
printint(void *a, void *state)
{
intnode *A = a;
printf("%d\n", "", A->i);
}
main(int argc, char *argv[])
{
int i;
intnode *I;
splayNode *top = NULL;
srandom(time(NULL));
for (i = 0; i < 100; i++) {
I = xcalloc(sizeof(intnode), 1);
I->i = random();
top = splay_insert(I, top, compareint);
}
splay_walk(top, printint, NULL);
return 0;
}
#endif /* DRIVER */
|