Hi,
I'm in the process of implementing a btree. I'm able to insert the keys into the correct positions (or so I think) while without breaking any invariants. The only problem I seem to be having is keeping track of the number of keys.
This is the main insert function.
it calls build_node()
The problem I am having is that the output is saying that the number of keys is 4 in each of the various insertion scenarios. I thought I was implementing a counter i to keep track of the number of keys, walking from 0 up to the order of the btree (which is 5 in this case).
This is the struct of the btree:
I'm also having a similar problem with remove. I'm able to remove a given key, but I can't get it to keep the number of keys being used.
remove:
Remove prints out 3 for the num_key, then 2 after the second loop, but does not continue to decrement.
I'm also not sure why "i" in the insert function defaults to 4, instead of zero.
I'm heading to office hours tomorrow after classes, but I would like to go in with a strategy, or solve it before that time.
Thanks for any help.
I'm in the process of implementing a btree. I'm able to insert the keys into the correct positions (or so I think) while without breaking any invariants. The only problem I seem to be having is keeping track of the number of keys.
This is the main insert function.
void insert(btree* &root, int key) { //insert into an empty root; for (int i=0; i<BTREE_ORDER; i++) { root = build_node(i, &key); } }
it calls build_node()
btree* build_node(int size, int* keys) { btree* node = init_node(); node->num_keys = size; for (int i=0; i < node->num_keys; i++) { node->keys[i] = keys[i]; i return node;
The problem I am having is that the output is saying that the number of keys is 4 in each of the various insertion scenarios. I thought I was implementing a counter i to keep track of the number of keys, walking from 0 up to the order of the btree (which is 5 in this case).
This is the struct of the btree:
struct btree { int num_keys; int keys[BTREE_ORDER]; bool is_leaf; btree* children[BTREE_ORDER + 1]; };
I'm also having a similar problem with remove. I'm able to remove a given key, but I can't get it to keep the number of keys being used.
remove:
for (int i=0; i < root->num_keys; i++) { if (root->keys[i] == key) { //if key is in root root->keys[i] = root->keys[i-1]; root->num_keys = root->num_keys-i; } else if (!root->is_leaf && root->keys[i] > key) { //if root is not a leaf, and answer in children (leaf) remove(root->children[i], key); } }
Remove prints out 3 for the num_key, then 2 after the second loop, but does not continue to decrement.
I'm also not sure why "i" in the insert function defaults to 4, instead of zero.
I'm heading to office hours tomorrow after classes, but I would like to go in with a strategy, or solve it before that time.
Thanks for any help.