diff --git a/src/bptree.c b/src/bptree.c new file mode 100644 index 0000000000000000000000000000000000000000..b55102442933cb3b9f4bd017bf19e6734fda4d6b --- /dev/null +++ b/src/bptree.c @@ -0,0 +1,45 @@ +#include "bptree.h" + +#include <stdbool.h> +#include <stdlib.h> + +/// + +static bool has_minimum_keys(BPTreeNode *root); +static bool has_maximum_keys(BPTreeNode *root); + +static bool has_minimum_keys(BPTreeNode *root) { + return root->keys_length == root->order; +} + +static bool has_maximum_keys(BPTreeNode *root) { + return root->keys_length == 2 * root->order; +} + +/// + +BPTreeNode *bptree_init(int order) { + BPTreeNode *root = (BPTreeNode *)malloc(sizeof(BPTreeNode)); + root->order = order; + root->is_leaf = true; + root->keys_length = 0; + root->keys = (int *)malloc(sizeof(int) * (2 * order)); + root->children_length = 0; + root->children = (BPTreeNode **)malloc(sizeof(BPTreeNode *) * (2 * order + 1)); + return root; +} + +void bptree_destroy(BPTreeNode **root) { + for (int i = 0; i < (*root)->children_length; i++) { + bptree_destroy((*root)->children[i]); + } + + free((*root)->keys); + free((*root)->children); + free(*root); + *root = NULL; +} + +// Insertion + +// Deletion diff --git a/src/bptree.h b/src/bptree.h index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..559ccfeac5e18d51e9e48f602afaa93e90bdfc5b 100644 --- a/src/bptree.h +++ b/src/bptree.h @@ -0,0 +1,18 @@ +#ifndef BPTREE_H +#define BPTREE_h + +#include "stdbool.h" + +typedef struct BPTreeNode { + int order; + bool is_leaf; + int keys_length; + int *keys; + int children_length; + struct BPTreeNode **children; +} BPTreeNode; + +BPTreeNode *bptree_init(int order); +void bptree_destroy(BPTreeNode **root); + +#endif diff --git a/src/sorted_array.c b/src/sorted_array.c index 0fc9d9190fd037d48a186151f45aabfc03ef59a8..0bbf3a06e28b0a7653ea4fc93c45f5c08f5f6486 100644 --- a/src/sorted_array.c +++ b/src/sorted_array.c @@ -61,15 +61,15 @@ void sorted_array_print(int *array, int array_length) { } int sorted_array_insert(int *array, int *array_length, int value) { - int index = lower_bound(array, *array_length, value); + int insertion_index = lower_bound(array, *array_length, value); - for (int i = *array_length - 1; i >= index; i--) { + for (int i = *array_length - 1; i >= insertion_index; i--) { array[i + 1] = array[i]; } - array[index] = value; + array[insertion_index] = value; *array_length += 1; - return index; + return insertion_index; } void sorted_array_delete(int *array, int *array_length, int value) {