|
Answer:
|
|
Answer:
|
|
Questions:
|
|
/* ========================================================
Delete( x, rx ) from a node N in B+-tree
======================================================== */
Delete( x, rx, N )
{
Delete x, rx from node N;
/* ==================================================
ANSWER:
We add code here to take care of the special case:
N = root
================================================== */
if ( N == root )
{
/* =================================================================
The root node does not need be have ≥ ⌊(n+1)/2⌋ pointers !!!
================================================================= */
if ( N has ≥ 2 pointers )
{
return;
}
else
{ // Root node is empty ===> make left most child of root the new root
root = root.leftMostPointer;
return;
}
}
// ALternative solution (shorter - but less clear)
//
// if ( N == root )
// {
// if ( N is empty (or has 0 keys) )
// {
// root = root.leftMostPointer; // make leftMost child the new root
//
// return;
// }
// }
// Continue here to handle non-root internal node deletion.....
/* ====================================
Check for underflow condition...
==================================== */
if ( N has ≥ ⌊(n+1)/2⌋ pointers /* At least half full*/ )
{
return; // Done
}
else
{
/* ---------------------------------------------------------
N underflowed: fix the size of N with transfer or merge
--------------------------------------------------------- */
/* ========================================================
Always try transfer first !!!
(Merge is only possible if 2 nodes are half filled)
======================================================== */
if ( leftSibling(N) has ≥ ⌊(n+1)/2⌋ + 1 pointers )
{
1. transfer last key from leftSibling(N) through parent into N
as the first key;
2. transfer right subtree link into N as the first link
}
else if ( rightSibling(N) has ≥ ⌊(n+1)/2⌋ + 1 pointers )
{
1. transfer first key from rightSibling(N) through parent into N
as the last key;
2. transfer left subtree link into N as the last link
}
/* ==========================================================
Here: can't solve underflow with a transfer
Because: BOTH sibling nodes have minimum # keys/pointers
(= half filled !!!)
Solution: merge the 2 half filled nodes into 1 node
========================================================== */
else if ( leftSibling(N) exists )
{
/* ===============================================
merge N with left sibling node
=============================================== */
1. Merge (1) leftSibling(N) + (2) key in parent node + (3) N
into the leftSibling(N) node;
2. Delete ( transfered key, right subtree ptr, parent(N) ); // Recurse !!
}
else // Node N must have a right sibling node !!!
{
/* ===============================================
merge N with right sibling node
=============================================== */
1. Merge (1) N + (2) key in parent node + (3) rightSibling(N)
into the node N;
2. Delete ( transfered key, right subtree ptr, parent(N) ); // Recurse !!
}
}
|
|
Questions:
|