Example #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); 
     } 
 } 
Example #2
0
 void CBPlusTree::recursive_insert(CNode* parentNode, KeyType key, const DataType& data) 
 { 
     if (parentNode->getType()==LEAF)  // 叶子节点 ,直接插入
     { 
         ((CLeafNode*)parentNode)->insert(key, data); 
     } 
     else 
     { 
         // 找到子结点  
         int keyIndex = parentNode->getKeyIndex(key); 
         int childIndex= parentNode->getChildIndex(key, keyIndex); // 孩子结点指针索引  
         CNode* childNode = ((CInternalNode*)parentNode)->getChild(childIndex); 
         if (childNode->getKeyNum()>=MAXNUM_LEAF)  // 子结点已满,需进行分裂  
         { 
             childNode->split(parentNode, childIndex);       
             if (parentNode->getKeyValue(childIndex)<=key)   // 确定目标子结点  
             { 
                 childNode = ((CInternalNode*)parentNode)->getChild(childIndex+1); 
             } 
         } 
         recursive_insert(childNode, key, data); 
     } 
 }