diff --git a/Makefile.am b/Makefile.am index ad67d06..30826a1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -21,6 +21,7 @@ libantd_la_SOURCES = lib/ini.c \ lib/ws.c \ lib/sha1.c \ lib/list.c \ + lib/bst.c \ lib/scheduler.c pkginclude_HEADERS = lib/ini.h \ @@ -31,6 +32,7 @@ pkginclude_HEADERS = lib/ini.h \ lib/ws.h \ lib/sha1.h \ lib/list.h \ + lib/bst.h \ lib/scheduler.h \ lib/plugin.h diff --git a/dist/antd-1.0.4b.tar.gz b/dist/antd-1.0.4b.tar.gz index 48b366f..210cb66 100644 Binary files a/dist/antd-1.0.4b.tar.gz and b/dist/antd-1.0.4b.tar.gz differ diff --git a/lib/bst.c b/lib/bst.c new file mode 100644 index 0000000..c6d0b63 --- /dev/null +++ b/lib/bst.c @@ -0,0 +1,102 @@ +#include +#include + +#include "bst.h" + +void bst_free(bst_node_t* root) +{ + if(root != NULL) + { + bst_free(root->left); + bst_free(root->right); + free(root); + } +} + +bst_node_t* bst_insert(bst_node_t* root, int key, void* data) +{ + if(root == NULL) + { + root = malloc(sizeof(bst_node_t)); + root->key = key; + root->data = data; + root->left = root->right = NULL; + } + else if(key < root->key) + root->left = bst_insert(root->left, key, data); + else if(key > root->key) + root->right = bst_insert(root->right, key, data); + else + root->data = data; + return root; +} + +bst_node_t* bst_find_min(bst_node_t* root) +{ + if(root == NULL) + return NULL; + else if(root->left == NULL) + return root; + else + return bst_find_min(root->left); +} + +bst_node_t* bst_find_max(bst_node_t* root) +{ + if(root == NULL) + return NULL; + else if(root->right == NULL) + return root; + else + return bst_find_max(root->right); +} + +bst_node_t* bst_find(bst_node_t* root, int x) +{ + if(root == NULL) + return NULL; + else if(x < root->key) + return bst_find(root->left, x); + else if(x > root->key) + return bst_find(root->right, x); + else + return root; +} + + +bst_node_t* bst_delete(bst_node_t* root, int x) +{ + bst_node_t* temp; + if(root == NULL) + return NULL; + else if(x < root->key) + root->left = bst_delete(root->left, x); + else if(x > root->key) + root->right = bst_delete(root->right, x); + else if(root->left && root->right) + { + temp = bst_find_min(root->right); + root->key = temp->key; + root->data = temp->data; + root->right = bst_delete(root->right, root->key); + } + else + { + temp = root; + if(root->left == NULL) + root = root->right; + else if(root->right == NULL) + root = root->left; + free(temp); + } + return root; +} + +void bst_for_each(bst_node_t* root, void (*callback)(bst_node_t*, void **, int), void** args, int argc) +{ + if(root == NULL) + return; + bst_for_each(root->left, callback, args, argc); + callback(root, args, argc); + bst_for_each(root->right, callback, args, argc); +} \ No newline at end of file diff --git a/lib/bst.h b/lib/bst.h new file mode 100644 index 0000000..f8c5be5 --- /dev/null +++ b/lib/bst.h @@ -0,0 +1,19 @@ +#ifndef BST_H +#define BST_H 1 + +typedef struct _tree_node +{ + int key; + void* data; + struct _tree_node* left; + struct _tree_node* right; +} bst_node_t; + +void bst_free(bst_node_t* root); +bst_node_t* bst_insert(bst_node_t* root, int key, void* data); +bst_node_t* bst_find_min(bst_node_t* root); +bst_node_t* bst_find_max(bst_node_t* root); +bst_node_t* bst_find(bst_node_t* root, int x); +bst_node_t* bst_delete(bst_node_t* root, int x); +void bst_for_each(bst_node_t* root, void (*callback)(bst_node_t*, void **, int), void** args, int argc); +#endif \ No newline at end of file