0,0 → 1,49 |
|
#include "mavl.h" |
|
|
static struct mavlnode * |
rot(struct mavlnode *n, struct mavlnode **parent, int dir) |
{ |
struct mavlnode *tmp; |
*parent = n->s[dir]; |
tmp = n->s[dir]->s[1-dir]; |
n->s[dir]->s[1-dir]=n; |
n->s[dir]=tmp; |
return NULL; |
} |
|
|
static struct mavlnode * |
balance ( struct mavlnode *n, struct mavlnode **parent) { |
int b,d; |
b = n->s[1]->bal - n->s[0]->bal; |
if (b>-2 && b<2) |
return n; |
d = b<0 ? 1:0; |
if (n->s[d]->bal - n->s[1-d]->bal == -d/2) |
n->s[d]=rot(n->s[d],parent,d); |
return rot(n,parent,1-d); |
} |
|
void * |
mav_insert(struct mavl *t, const void *data, int *exists) |
{ |
const void *d; |
int rc; |
|
d = data; |
/* rc = mav_insert0(t, &t->root, &d);*/ |
|
/* if (rc >= 3) |
return NULL; |
|
if (exists != NULL) { |
if (rc == 2) |
*exists = 1; |
else |
*exists = 0; |
} */ |
return (void *)d; |
} |
|