void split_into_children ( btnode& rootNode, btnode& root ) {
long* key = & ( root->key[0] );
int middle = root->capacity / 2;
int i;
btnode left = new node ( order );
btnode right = new node ( order );
btnode temp = new node ( order );
for ( int i = 0; i < middle; i++ ) {
left->key[i] = root->key[i];
left->value[i] = root->value[i];
left->capacity += 1;
left->nodes[i] = root->nodes[i];
}
left->nodes[middle] = root->nodes[middle];
for ( i = 0; i < root->capacity - middle; i++ ) {
right->key[i] = root->key[middle + 1 + i];
right->value[i] = root->value[middle + 1 + i];
right->capacity += 1;
right->nodes[i] = root->nodes[middle + 1 + i];
}
right->nodes[right->capacity] = root->nodes[root->capacity + 1];
temp->key[0] = root->key[middle];
temp->value[0] = root->value[middle];
temp->nodes[0] = left;
temp->nodes[1] = right;
temp->capacity = 1;
for ( int i = 0; i <= left->capacity; i++ ) {
if ( left->nodes[i] != NULL )
left->nodes[i]->parent = left;
}
for ( int i = 0; i <= right->capacity; i++ ) {
if ( right->nodes[i] != NULL )
right->nodes[i]->parent = right;
}
if ( root->parent == root ) {
right->parent = temp;
left->parent = temp;
temp->parent = temp;
rootNode->parent = temp;
rootNode = temp;
} else {
add_middle_to_parent ( rootNode, root->parent, left, right, root->key[middle], root->value[middle] );
}
}
left と right を使用して b+ ツリー ノードを分割し、temp を使用して左右を指す中間ノードを格納しました。関数から戻る前に、左、右、および一時のメモリの割り当てを解除するにはどうすればよいですか?
ノード クラス:
class node
{
public:
long* key;
int capacity;
node** nodes;
node* parent;
long* value;
node ( int order ) {
key = new long[order + 1];
value = new long[order + 1];
nodes = new node *[order + 2];
capacity = 0;
parent = NULL;
for ( int i = 0; i <= order + 1; i++ ) {
this->nodes[i] = NULL;
}
}
~node() {
delete[] key;
delete[] value;
delete[] nodes;
}
};