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;
}
};