2015-09-17 07:25:06 +00:00
|
|
|
#include <stdio.h>
|
2015-01-19 21:47:09 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
2015-09-17 07:25:06 +00:00
|
|
|
|
2015-01-27 03:02:10 +00:00
|
|
|
#include <retro_inline.h>
|
2015-01-19 21:47:09 +00:00
|
|
|
|
|
|
|
#include "bintree.h"
|
|
|
|
|
2015-09-17 07:25:06 +00:00
|
|
|
struct bintree_node
|
|
|
|
{
|
|
|
|
void *value;
|
|
|
|
struct bintree_node *parent;
|
|
|
|
struct bintree_node *left;
|
|
|
|
struct bintree_node *right;
|
|
|
|
};
|
2015-01-27 03:02:10 +00:00
|
|
|
|
2015-09-17 18:24:49 +00:00
|
|
|
struct bintree
|
2015-01-27 03:02:10 +00:00
|
|
|
{
|
2015-09-17 07:25:06 +00:00
|
|
|
struct bintree_node *root;
|
|
|
|
bintree_cmp_func cmp;
|
|
|
|
void *ctx;
|
2015-09-17 18:24:49 +00:00
|
|
|
};
|
2015-09-17 07:25:06 +00:00
|
|
|
|
|
|
|
static void *NIL_NODE = &NIL_NODE;
|
2015-01-19 21:47:09 +00:00
|
|
|
|
2015-01-27 03:02:10 +00:00
|
|
|
static struct bintree_node *new_nil_node(struct bintree_node *parent)
|
|
|
|
{
|
2015-09-17 07:25:06 +00:00
|
|
|
struct bintree_node *node = (struct bintree_node *)
|
|
|
|
calloc(1, sizeof(struct bintree_node));
|
2015-01-27 03:02:10 +00:00
|
|
|
|
|
|
|
if (!node)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
node->value = NIL_NODE;
|
|
|
|
node->parent = parent;
|
|
|
|
|
|
|
|
return node;
|
2015-01-19 21:47:09 +00:00
|
|
|
}
|
|
|
|
|
2015-01-27 03:02:10 +00:00
|
|
|
static INLINE int is_nil(const struct bintree_node *node)
|
|
|
|
{
|
2015-06-03 14:57:51 +00:00
|
|
|
return (node == NULL) || (node->value == NIL_NODE);
|
2015-01-19 21:47:09 +00:00
|
|
|
}
|
|
|
|
|
2015-09-17 07:25:06 +00:00
|
|
|
static int insert(bintree_t *t, struct bintree_node *root, void *value)
|
2015-01-27 03:02:10 +00:00
|
|
|
{
|
|
|
|
int cmp_res = 0;
|
|
|
|
|
|
|
|
if (is_nil(root))
|
|
|
|
{
|
2015-06-03 13:59:32 +00:00
|
|
|
root->left = new_nil_node(root);
|
2015-01-27 03:02:10 +00:00
|
|
|
root->right = new_nil_node(root);
|
|
|
|
|
|
|
|
if (!root->left || !root->right)
|
|
|
|
{
|
|
|
|
if (root->left)
|
|
|
|
{
|
|
|
|
free(root->left);
|
|
|
|
root->left = NULL;
|
|
|
|
}
|
|
|
|
if (root->right)
|
|
|
|
{
|
|
|
|
free(root->right);
|
|
|
|
root->right = NULL;
|
|
|
|
}
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
root->value = value;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmp_res = t->cmp(root->value, value, t->ctx);
|
|
|
|
|
|
|
|
if (cmp_res > 0)
|
|
|
|
return insert(t, root->left, value);
|
|
|
|
else if (cmp_res < 0)
|
|
|
|
return insert(t, root->right, value);
|
|
|
|
return -EINVAL;
|
2015-01-19 21:47:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-27 03:02:10 +00:00
|
|
|
static int _bintree_iterate(struct bintree_node *n,
|
|
|
|
bintree_iter_cb cb, void *ctx)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
if (is_nil(n))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ((rv = _bintree_iterate(n->left, cb, ctx)) != 0)
|
|
|
|
return rv;
|
|
|
|
if ((rv = cb(n->value, ctx)) != 0)
|
|
|
|
return rv;
|
|
|
|
if ((rv = _bintree_iterate(n->right, cb, ctx)) != 0)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
return 0;
|
2015-01-19 21:47:09 +00:00
|
|
|
}
|
|
|
|
|
2015-01-27 03:02:10 +00:00
|
|
|
static void bintree_free_node(struct bintree_node *n)
|
|
|
|
{
|
|
|
|
if (!n)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (n->value == NIL_NODE)
|
|
|
|
{
|
|
|
|
free(n);
|
|
|
|
return;
|
|
|
|
}
|
2015-06-03 14:57:51 +00:00
|
|
|
|
2015-01-27 03:02:10 +00:00
|
|
|
n->value = NULL;
|
|
|
|
bintree_free_node(n->left);
|
|
|
|
bintree_free_node(n->right);
|
|
|
|
free(n);
|
2015-01-19 21:47:09 +00:00
|
|
|
}
|
|
|
|
|
2015-09-17 07:25:06 +00:00
|
|
|
int bintree_insert(bintree_t *t, void *value)
|
|
|
|
{
|
|
|
|
return insert(t, t->root, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
int bintree_iterate(const bintree_t *t, bintree_iter_cb cb,
|
|
|
|
void *ctx)
|
|
|
|
{
|
|
|
|
return _bintree_iterate(t->root, cb, ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
bintree_t *bintree_new(bintree_cmp_func cmp, void *ctx)
|
|
|
|
{
|
|
|
|
bintree_t *t = (bintree_t*)calloc(1, sizeof(*t));
|
|
|
|
|
|
|
|
if (!t)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
t->root = new_nil_node(NULL);
|
|
|
|
t->cmp = cmp;
|
|
|
|
t->ctx = ctx;
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bintree_free(bintree_t *t)
|
2015-01-27 03:02:10 +00:00
|
|
|
{
|
|
|
|
bintree_free_node(t->root);
|
2015-01-19 21:47:09 +00:00
|
|
|
}
|