add BST to lib

This commit is contained in:
lxsang 2020-08-06 18:45:12 +02:00
parent d2a5f3220a
commit adb0811a08
4 changed files with 123 additions and 0 deletions

View File

@ -21,6 +21,7 @@ libantd_la_SOURCES = lib/ini.c \
lib/ws.c \ lib/ws.c \
lib/sha1.c \ lib/sha1.c \
lib/list.c \ lib/list.c \
lib/bst.c \
lib/scheduler.c lib/scheduler.c
pkginclude_HEADERS = lib/ini.h \ pkginclude_HEADERS = lib/ini.h \
@ -31,6 +32,7 @@ pkginclude_HEADERS = lib/ini.h \
lib/ws.h \ lib/ws.h \
lib/sha1.h \ lib/sha1.h \
lib/list.h \ lib/list.h \
lib/bst.h \
lib/scheduler.h \ lib/scheduler.h \
lib/plugin.h lib/plugin.h

Binary file not shown.

102
lib/bst.c Normal file
View File

@ -0,0 +1,102 @@
#include<stdio.h>
#include<stdlib.h>
#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);
}

19
lib/bst.h Normal file
View File

@ -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