feat(lsm): implement bt insert & search
This commit is contained in:
parent
fd42b446a6
commit
13e42181a2
5 changed files with 2056 additions and 1 deletions
70
lsm/src/bt/lsm_bt.c
Normal file
70
lsm/src/bt/lsm_bt.c
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
#include "lsm.h"
|
||||
#include "lsm_bt.h"
|
||||
|
||||
lsm_error lsm_bt_node_init(lsm_bt_node **ptr, const char key, void *data) {
|
||||
lsm_bt_node *node = calloc(1, sizeof(lsm_bt_node));
|
||||
|
||||
if (node == NULL) {
|
||||
return lsm_error_failed_alloc;
|
||||
}
|
||||
|
||||
node->key = key;
|
||||
node->data = data;
|
||||
*ptr = node;
|
||||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
lsm_error lsm_bt_init(lsm_bt **ptr) {
|
||||
lsm_bt *bt = calloc(1, sizeof(lsm_bt));
|
||||
|
||||
if (bt == NULL) {
|
||||
return lsm_error_failed_alloc;
|
||||
}
|
||||
|
||||
*ptr = bt;
|
||||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
lsm_error lsm_bt_insert(lsm_bt *bt, char key, void *data) {
|
||||
lsm_bt_node **dest = &bt->root;
|
||||
|
||||
// Traverse down the tree until we reach the new point to insert our node
|
||||
while (*dest != NULL) {
|
||||
if ((*dest)->key == key) {
|
||||
return lsm_error_already_present;
|
||||
}
|
||||
|
||||
dest = key < (*dest)->key ? &(*dest)->left : &(*dest)->right;
|
||||
}
|
||||
|
||||
lsm_bt_node *node;
|
||||
|
||||
if (lsm_bt_node_init(&node, key, data) != lsm_error_ok) {
|
||||
return lsm_error_failed_alloc;
|
||||
}
|
||||
|
||||
*dest = node;
|
||||
bt->size++;
|
||||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
lsm_error lsm_bt_search(void **out, lsm_bt *bt, char key) {
|
||||
lsm_bt_node *node = bt->root;
|
||||
|
||||
while (node != NULL) {
|
||||
if (node->key == key) {
|
||||
*out = node->data;
|
||||
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
node = key < node->key ? node->left : node->right;
|
||||
}
|
||||
|
||||
return lsm_error_not_found;
|
||||
}
|
||||
78
lsm/src/bt/lsm_bt.h
Normal file
78
lsm/src/bt/lsm_bt.h
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
#ifndef LSM_BT_INTERNAL
|
||||
#define LSM_BT_INTERNAL
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "lsm.h"
|
||||
|
||||
/**
|
||||
* Node inside a binary tree
|
||||
*/
|
||||
typedef struct lsm_bt_node {
|
||||
struct lsm_bt_node *left;
|
||||
struct lsm_bt_node *right;
|
||||
void *data;
|
||||
char key;
|
||||
} lsm_bt_node;
|
||||
|
||||
/**
|
||||
* Initialize a new binary tree node
|
||||
*
|
||||
* @param ptr where to store newly allocated pointer
|
||||
* @param key key for the node
|
||||
* @param data data to store
|
||||
*/
|
||||
lsm_error lsm_bt_node_init(lsm_bt_node **ptr, const char key, void *data);
|
||||
|
||||
/**
|
||||
* Deallocate a single binary tree node
|
||||
*/
|
||||
void lsm_bt_node_free(lsm_bt_node *node);
|
||||
|
||||
/**
|
||||
* A binary tree implementation using char values as keys
|
||||
*/
|
||||
typedef struct lsm_bt {
|
||||
lsm_bt_node *root;
|
||||
uint8_t size;
|
||||
} lsm_bt;
|
||||
|
||||
/**
|
||||
* Initialize a new binary tree
|
||||
*
|
||||
* @param ptr where to store newly allocated pointer
|
||||
*/
|
||||
lsm_error lsm_bt_init(lsm_bt **ptr);
|
||||
|
||||
/**
|
||||
* Deallocate an entire binary tree, including all its nodes
|
||||
*/
|
||||
void lsm_bt_free(lsm_bt *bt);
|
||||
|
||||
/**
|
||||
* Search for the data stored behind the given key.
|
||||
*
|
||||
* @param out pointer to store data pointer in
|
||||
* @param bt binary tree to search
|
||||
* @param key key to search
|
||||
*/
|
||||
lsm_error lsm_bt_search(void **out, lsm_bt *bt, char key);
|
||||
|
||||
/**
|
||||
* Insert a new data value into the tree with the given key.
|
||||
*
|
||||
* @param bt binary tree to insert into
|
||||
* @param key key to insert
|
||||
* @param data data to store
|
||||
*/
|
||||
lsm_error lsm_bt_insert(lsm_bt *bt, char key, void *data);
|
||||
|
||||
/**
|
||||
* Remove the given key from the binary tree.
|
||||
*
|
||||
* @param bt binary tree to remove from
|
||||
* @param key key to remove
|
||||
*/
|
||||
lsm_error lsm_bt_remove(lsm_bt *bt, char key);
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue