Skip to content
Snippets Groups Projects
Commit be0feab1 authored by Florian Burgener's avatar Florian Burgener
Browse files

Progress of the implementation

parent 3ef0967e
No related branches found
No related tags found
No related merge requests found
#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
#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
......@@ -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) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment