|
onFull(r, key); } void BTree::remove(int key) { if (root->keyNum == 1){//如果根节点只有两个孩子 struct BTNode* pNode1 = root->child[0]; struct BTNode* pNode2 = root->child[1]; if (pNode1->keyNum == M - 1 && pNode2->keyNum == M - 1){//且两个孩子都只有M-1个关键字,合并 merge(root, pNode1, pNode2, 0); delete root; root = pNode1; } else { RemoveNonLess(root, key); } } else { RemoveNonLess(root, key); } } void BTree::PrintRow() { struct BTNode* pNode; queue
q; q.push(root); while (!q.empty()){ pNode = q.front(); q.pop(); cout << "[ "; for (int i = 0; i < pNode->keyNum; i++) cout << pNode->key[i] << " "; cout << "]" << endl; if (pNode->isLeaf) continue; for (int i = 0; i <= pNode->keyNum; i++) q.push(pNode->child[i]); } } int main(void) { BTree tree; tree.insert('c'); tree.insert('n'); tree.insert('g'); tree.insert('a'); tree.insert('h'); tree.insert('e'); tree.insert('k'); tree.insert('q'); tree.insert('m'); tree.insert('f'); tree.insert('w'); tree.insert('l'); tree.insert('t'); tree.insert('z'); tree.insert('d'); tree.insert('p'); tree.insert('r'); tree.insert('x'); tree.insert('y'); tree.insert('s'); tree.remove('n'); tree.remove('b'); tree.PrintRow(); }
|