void LeafNode::removeKey(int keyIndex, int childIndex) { for (int i = keyIndex; i < getKeyNum() - 1; ++i) { setKeyValue(i, getKeyValue(i + 1)); setData(i, getData(i + 1)); } setKeyNum(getKeyNum() - 1); }
void CInternalNode::removeKey(int keyIndex, int childIndex) { for (int i=0; i<getKeyNum()-keyIndex-1; ++i) { setKeyValue(keyIndex+i, getKeyValue(keyIndex+i+1)); setChild(childIndex+i, getChild(childIndex+i+1)); } setKeyNum(getKeyNum()-1); }
/*删除内部结点的键值*/ void InternalNode::removeKey(int keyIndex, int childIndex) { for (int i = 0; i < getKeyNum() - childIndex; i++) { setChild(childIndex + i, getChild(childIndex + i + 1)); } for (int i = 0; i < getKeyNum() - keyIndex - 1; ++i) { setKeyValue(keyIndex + i, getKeyValue(keyIndex + i + 1)); }//键值对依次向前移动 setKeyNum(getKeyNum() - 1); }
/*插入叶子节点*/ void LeafNode::insert(KeyType key, const DataType &data) { int i; for (i = m_KeyNum; i >= 1 && m_KeyValues[i - 1] > key; --i) { setKeyValue(i, m_KeyValues[i - 1]); setData(i, m_Datas[i - 1]); } setKeyValue(i, key); setData(i, data); setKeyNum(m_KeyNum + 1); }
/*分裂内部结点,此种情况发生在原先结点的keyNum=MAX_KEY下*/ void InternalNode::splitNode(FatherNode* parentNode, int childIndex) { InternalNode* newNode = new InternalNode(); //分裂后的新结点 newNode->setKeyNum(MIN_KEY); int i; for (i = 0; i < MIN_KEY; ++i) { newNode->setKeyValue(i, m_KeyValues[i + MIN_CHILD]); } //向新结点中拷贝键值 for (i = 0; i < MIN_CHILD; ++i) { newNode->setChild(i, m_Childs[i + MIN_CHILD]); } //向新结点中拷贝指针 setKeyNum(MIN_KEY); //更新原先结点中的键值个数 ((InternalNode*)parentNode)->insert(childIndex, childIndex + 1, m_KeyValues[MIN_KEY], newNode); //将新结点插入树中 }
/*插入内部结点*/ void InternalNode::insert(int keyIndex, int childIndex, KeyType key, FatherNode* childNode) { int i; for (i = getKeyNum(); i > keyIndex; --i) { setChild(i + 1, m_Childs[i]); setKeyValue(i, m_KeyValues[i - 1]); } //将父节点的childIndex后所有键值对后移一个单位 if (i == childIndex) { setChild(i + 1, m_Childs[i]); } setChild(childIndex, childNode); //插入新结点 setKeyValue(keyIndex, key); setKeyNum(m_KeyNum + 1); }
void CInternalNode::insert(int keyIndex, int childIndex, KeyType key, CNode* childNode) { int i; for (i=getKeyNum(); i>keyIndex; --i)//将父节点中的childIndex后的所有关键字的值和子树指针向后移一位 { setChild(i+1,m_Childs[i]); setKeyValue(i,m_KeyValues[i-1]); } if (i==childIndex) { setChild(i+1, m_Childs[i]); } setChild(childIndex, childNode); setKeyValue(keyIndex, key); setKeyNum(m_KeyNum+1); }
void LeafNode::splitNode(FatherNode* parentNode, int childIndex) { LeafNode* newNode = new LeafNode(); setKeyNum(MIN_LEAF); newNode->setKeyNum(MIN_LEAF + 1); newNode->setRightSibling(getRightSibling()); setRightSibling(newNode); newNode->setLeftSibling(this); int i; for (i = 0; i < MIN_LEAF + 1; ++i) { newNode->setKeyValue(i, m_KeyValues[i + MIN_LEAF]); } for (i = 0; i < MIN_LEAF + 1; ++i) { newNode->setData(i, m_Datas[i + MIN_LEAF]); } ((InternalNode*)parentNode)->insert(childIndex, childIndex + 1, m_KeyValues[MIN_LEAF], newNode); }
void CInternalNode::split(CNode* parentNode, int childIndex) { CInternalNode* newNode = new CInternalNode();//分裂后的右节点 newNode->setKeyNum(MINNUM_KEY); int i; for (i=0; i<MINNUM_KEY; ++i)// 拷贝关键字的值 { newNode->setKeyValue(i, m_KeyValues[i+MINNUM_CHILD]); } for (i=0; i<MINNUM_CHILD; ++i) // 拷贝孩子节点指针 { newNode->setChild(i, m_Childs[i+MINNUM_CHILD]); } setKeyNum(MINNUM_KEY); //更新左子树的关键字个数 ((CInternalNode*)parentNode)->insert(childIndex, childIndex+1, m_KeyValues[MINNUM_KEY], newNode); }
void CLeafNode::split(CNode* parentNode, int childIndex) { CLeafNode* newNode = new CLeafNode();//分裂后的右节点 setKeyNum(MINNUM_LEAF); newNode->setKeyNum(MINNUM_LEAF+1); newNode->setRightSibling(getRightSibling()); setRightSibling(newNode); newNode->setLeftSibling(this); int i; for (i=0; i<MINNUM_LEAF+1; ++i)// 拷贝关键字的值 { newNode->setKeyValue(i, m_KeyValues[i+MINNUM_LEAF]); } for (i=0; i<MINNUM_LEAF+1; ++i)// 拷贝数据 { newNode->setData(i, m_Datas[i+MINNUM_LEAF]); } ((CInternalNode*)parentNode)->insert(childIndex, childIndex+1, m_KeyValues[MINNUM_LEAF], newNode); }
FatherNode::~FatherNode() { setKeyNum(0); }
/*基本节点*/ FatherNode::FatherNode() { setType(LEAF); setKeyNum(0); }
CNode::~CNode() { setKeyNum(0); }
CNode::CNode() { setType(LEAF); setKeyNum(0); }