Esempio n. 1
0
 void CBPlusTree::recursive_remove(CNode* parentNode, KeyType key, DataType& dataValue) 
 { 
     int keyIndex = parentNode->getKeyIndex(key); 
     int childIndex= parentNode->getChildIndex(key, keyIndex); // 孩子结点指针索引  
     if (parentNode->getType()==LEAF)// 找到目标叶子节点  
     { 
         if (key==m_MaxKey&&keyIndex>0) 
         { 
             m_MaxKey = parentNode->getKeyValue(keyIndex-1); 
         } 
         dataValue = ((CLeafNode*)parentNode)->getData(keyIndex); 
         parentNode->removeKey(keyIndex, childIndex);  // 直接删除  
         // 如果键值在内部结点中存在,也要相应的替换内部结点  
         if (childIndex==0 && m_Root->getType()!=LEAF && parentNode!=m_DataHead) 
         { 
             changeKey(m_Root, key, parentNode->getKeyValue(0)); 
         } 
     } 
     else // 内结点  
     { 
         CNode *pChildNode = ((CInternalNode*)parentNode)->getChild(childIndex); //包含key的子树根节点  
         if (pChildNode->getKeyNum()==MINNUM_KEY)                       // 包含关键字达到下限值,进行相关操作  
         { 
             CNode *pLeft = childIndex>0 ? ((CInternalNode*)parentNode)->getChild(childIndex-1) : NULL;                       //左兄弟节点  
             CNode *pRight = childIndex<parentNode->getKeyNum() ? ((CInternalNode*)parentNode)->getChild(childIndex+1) : NULL;//右兄弟节点  
             // 先考虑从兄弟结点中借  
             if (pLeft && pLeft->getKeyNum()>MINNUM_KEY)// 左兄弟结点可借  
             { 
                 pChildNode->borrowFrom(pLeft, parentNode, childIndex-1, LEFT); 
             } 
             else if (pRight && pRight->getKeyNum()>MINNUM_KEY)//右兄弟结点可借  
             { 
                 pChildNode->borrowFrom(pRight, parentNode, childIndex, RIGHT); 
             } 
             //左右兄弟节点都不可借,考虑合并  
             else if (pLeft)                    //与左兄弟合并  
             { 
                 pLeft->mergeChild(parentNode, pChildNode, childIndex-1); 
                 pChildNode = pLeft; 
             } 
             else if (pRight)                   //与右兄弟合并  
             { 
                 pChildNode->mergeChild(parentNode, pRight, childIndex); 
             } 
         } 
         recursive_remove(pChildNode, key, dataValue); 
     } 
 }