From d98f46ce647846b0aa30b2e16a30fd4e152a1bf5 Mon Sep 17 00:00:00 2001 From: Carlos Maiolino Date: Thu, 10 Jul 2025 22:55:07 +0200 Subject: Add new code Signed-off-by: Carlos Maiolino --- Algorithms/BPlusTree/btree | Bin 0 -> 8384 bytes Algorithms/BPlusTree/btree.c | 60 +++++++++++++++++++++++++++++++++++++++++++ Algorithms/BPlusTree/btree.h | 23 +++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 Algorithms/BPlusTree/btree create mode 100644 Algorithms/BPlusTree/btree.c create mode 100644 Algorithms/BPlusTree/btree.h (limited to 'Algorithms/BPlusTree') diff --git a/Algorithms/BPlusTree/btree b/Algorithms/BPlusTree/btree new file mode 100644 index 0000000..7b9ebf2 Binary files /dev/null and b/Algorithms/BPlusTree/btree differ diff --git a/Algorithms/BPlusTree/btree.c b/Algorithms/BPlusTree/btree.c new file mode 100644 index 0000000..41d96b0 --- /dev/null +++ b/Algorithms/BPlusTree/btree.c @@ -0,0 +1,60 @@ +#include +#include +#include + +#include "btree.h" + +struct b_node *ROOT; + +struct b_node* init_node(void) +{ + struct b_node* node; + node = malloc(sizeof(struct b_node)); + + if (!node) + return NULL; + + node->key_count = 0; + node->is_leaf = false; + node->keys = NULL; + node->ptrs = NULL; + node->leaf = NULL; + + return node; +} + +void print_leaf(struct b_node *node) +{ + if (!node->is_leaf) { + printf("Error: trying to print a node instead of a leaf\n"); + return; + } + + /* printf(" Parent node: */ + printf("Data: %d\n", node->leaf->data); + printf("Next sib: %p\n", node->leaf->) + +void print_root(void) +{ + printf("This is the ROOT of the tree \n"); + + if (ROOT->is_leaf) + print_leaf(ROOT); + else + print_node(ROOT); +} + +int main(void) { + + ROOT = init_node(); + if (!ROOT) { + printf("Error to initialize root node\n"); + goto exit0; + } + + printf("ROOT: %p\n", ROOT); + free(ROOT); + +exit0: + return 0; +} diff --git a/Algorithms/BPlusTree/btree.h b/Algorithms/BPlusTree/btree.h new file mode 100644 index 0000000..a045dd4 --- /dev/null +++ b/Algorithms/BPlusTree/btree.h @@ -0,0 +1,23 @@ + +#define ORDER 4 +#define PTRS 5 + +/* + * ptrs: will point to data when the node is a leaf + * will contain the next nodes when just a node + * accessed as an array MAX = PTRS + * sib: will only be valid when the node is a leaf + */ +struct b_node { + int key_count; + bool is_leaf; + int *keys; + void **ptrs; + struct b_leaf *leaf; + struct b_node *sib; +}; + +struct b_leaf { + int data; + struct b_leaf *sib; +}; -- cgit v1.2.3