#include <iostream> #include <vector> #include <queue>using namespace std;class btree{static const int M = 2;struct btnode{int keynum;int key[2 * M-1]; Key word group struct btnode* child[2 * m];//child knot point group bool isleaf; btnode* root;//when inserting, ensure that the key of the Pnode node is less than 2*m-1 void Insertnonfull (btnode* pnode, int key), and////////When the child node has 2m-1 keyword, split this node void Splitchild (btnode* parent, int i, btnode* child),//two M-1 element nodes merge void merge (btnode* parent, btnode* pNode1, btnode* pNode2 , int index);//Find the largest keyword smaller than the first keyword of the Pnode node, that is, the forward node int predecessor (btnode* pnode);//Find the successor node int successor (btnode* pnode); /pnode1 to the parent to a node key[index],parent to PNode0 to a node, pNode1 keyword number is m-1void exchangeleftnode (Btnode *parent, btnode* PNODE0, btnode* pNode1, int index), void Exchangerightnode (btnode* parent, btnode* pNode1, btnode *pnode2, int index);//Delete, The number of node keywords is not less than mvoid removenonless (btnode* pnode, int key); void Diskwrite (btnode* pnode); void Diskread (Btnode *pnode); btnode* Search (btnode* pnode, int key, int &index);p ublic:btree (); ~btreE (); btnode* Search (int key, int &index), void Insert (int key), void remove (int key),//print at level. void Printrow ();}; Btree::btree () {root = new Btnode (); root->isleaf = True;root->keynum = 0;diskwrite (root);} Btree::~btree () {struct btnode* pnode;queue<struct btnode*> q;q.push (root); while (!q.empty ()) {Pnode = Q.front (); Q.pop (); if (pnode->isleaf) continue;for (int i = 0; I <= pnode->keynum; i++) Q.push (Pnode->child[i]);d elete Pnode;}} void BTree::D iskwrite (btnode* pnode) {} void BTree::D iskread (Btnode *pnode) {} btree::btnode* Btree::search (btnode* Pnode, int key, int &index) {int i = 0;while (I<pnode->keynum&&key>pnode->key[i])// Find the first subscript greater than or equal to key i++;if (i < Pnode->keynum&&key = = Pnode->key[i]) {//If a keyword is found, return index = I;return pnode;} if (pnode->isleaf)//has been searched for leaf nodes, there is no return Null;else{diskread (Pnode->child[i]); return Search (Pnode->child[i] , key, index);//Recursive search in the first child node greater than the key value}} void Btree::insertnonfull (btnode* pnode, int key) {int i =Pnode->keynum-1;if (pnode->isleaf) {//If it is a leaf node, insert directly while (I >= 0 && key < Pnode->key[i]) {pnode-& Gt;key[i + 1] = pnode->key[i];i--;} Pnode->key[i + 1] = key;pnode->keynum++;D iskwrite (pnode);} else {while (i >= 0 && key < Pnode->key[i]) i--;//find the first subscript i++;D iskread (Pnode->child[i]) less than or equal to key; Pnode->child[i]->keynum = = 2 * M-1) {//To determine if a child node has a 2*m-1 keyword, there is a need to split Splitchild (Pnode, I, pnode->child[i]); if (key >pnode->key[i])//If key is larger than the element moved up to the parent node i++;} Insertnonfull (Pnode->child[i], key);//guaranteed child node keyword number less than 2*m-1}} void Btree::splitchild (btnode* parent, int i, btnode* Child) {int j;struct btnode* pnode = new Btnode ();p node->isleaf = Child->isleaf;pnode->keynum = m-1;for (j = 0; J < M-1; J + +)//Assign the post M-1 key to the new node pnode->key[j] = child->key[j + m];if (!child->isleaf) {//If child is not a leaf node, Assigns the next m child nodes to the new node. for (j = 0; J < M; J + +) Pnode->child[j] = child->child[j + M];} Child->keynum = m-1;for (j = parent->keynum; J > i; j--) Parent->child[j + 1] = parent->child[j];//The node pointer after the parent of the child node is shifted backwards by one bit, parent->child[j + 1] = pnode;//the newly generated node as a child of the parent for (j = parent->keynum-1; J >= i; j--)//To move the keywords behind i all backwards one parent->key[j + 1] = parent ->KEY[J];p arent->key[j + 1] = child->key[m-1];//moves the middle node of the child node to the specified position of the parent parent->keynum++;D Iskwrite (parent) ;D iskwrite (Pnode);D iskwrite (child); void Btree::merge (btnode* parent, btnode* pNode1, btnode* pNode2, int index) {pnode1->key[m-1] = Parent->key[index] ; for (int i = 0; i < M-1; i++)//move pNode2 keyword to pNode1 pnode1->key[i + M] = pnode2->key[i];p node1->keynum = 2 * M-1;if (!pnode1->isleaf) {//If it is not a leaf, move PNode2 's child pointer to pNode1 for (int i = 0; i < m; i++) Pnode1->child[i + m] = Pnod E2->child[i];} for (int i = index; i < parent->keynum; i++) {//Move the parent node to the keyword after index and the child's pointer forward one parent->key[i] = parent->key[i + 1] ;p arent->child[i + 1] = parent->child[i + 2];} parent->keynum--;d elete PNode2;} int BTREE::p redecessor (btnode* pnode) {while (!pnode->isleaf) Pnode = Pnode->child[pnode->keynum];return pNode- >KEY[PNODE->KEYNUM-1];} int Btree::successor (btnode* pnode) {while (!pnode->isleaf) Pnode = Pnode->child[0];return pNode->key[0];} void Btree::exchangeleftnode (Btnode *parent, btnode* pNode0, btnode* pNode1, int index) {for (int i = pnode1->keynum; I > 0; i--) Pnode1->key[i] = pnode1->key[i-1];//pnode1 node all the keywords move backwards one pnode1->key[0] = parent->key[index];// No. 0 keyword from parent node pnode1->keynum++;p Arent->key[index] = pnode0->key[pnode0->keynum-1];// The keyword at the index of the parent node comes from the PNODE0 maximum keyword if (!pnode0->isleaf) {//If it is not a leaf node, for (int i = pnode1->keynum; i > 0; i--)// Move the PNODE1 child pointer back one bit and assign the last child pointer of PNode0 to its first pnode1->child[i] = Pnode1->child[i-1];p node1->child[0] = Pnode0->child[pnode0->keynum];} pnode0->keynum--;} void Btree::exchangerightnode (btnode* parent, btnode* pNode1, btnode *pnode2, int index) {pnode1->key[pnode1-> Keynum] = Parent->key[index];p node1->keynum++;p Arent->key[index] = pnode2->key[0];for (int i = 0; i < pNode2-> KeyNum-1; i++) Pnode2->key[i] = pnode2->key[i + 1];if (!pnode2->isleaf) {Pnode1->child[pnode1->keynum] = pNode2- >child[0];for (int i = 0; i < pnode2->keynum; i++) Pnode2->child[i] = pnode2->child[i + 1];} pnode2->keynum--;} void Btree::removenonless (btnode* pnode, int key) {if (pnode->isleaf) {///to leaf node, directly delete int i = 0;while (i<pnode-> Keynum&&key>pnode->key[i]) i++;if (i < Pnode->keynum&&key = Pnode->key[i]) {while (I < pnode->keynum-1) {Pnode->key[i] = pnode->key[i + 1];i++;} pnode->keynum--;} else {cout << "not found!" << Endl;}} else {int i = 0;while (i < Pnode->keynum&&key > Pnode->key[i])//Find the first keyword greater than or equal to key i++;if (I < pnode-& Gt;keynum&&key = = Pnode->key[i]) {//Find the keyword to delete in the node struct btnode* pNode1 = pnode->child[i];struct btnode* PNode2 = Pnode->child[i + 1];if (pnode1->keynum >= M) {//If the key word of the child node to the left of the keyword is greater than or equal to the mint target = predecessor (PNODE1);// Move its forward node to pnode pnode->key[i] = target; Removenonless (pNode1, target);//Recursive delete target}else if (pnode2->keynum >= M) {///right, same as int target = successor (PNODE2); Pnode->key[i] = target; Removenonless (PNode2, target);} else {merge (Pnode, PNode1, PNode2, i);//both are less than M, merge removenonless (PNode1, key);}} else {//not in this node struct btnode *pnode1 = pnode->child[i];struct Btnode *pnode0 = null;struct Btnode *pnode2 = NULL;if (I&G t;0) pNode0 = pnode->child[i-1];//Left node if (i < pnode->keynum) PNode2 = pnode->child[i + 1];//right node if (pnode1->k Eynum = = M-1) {//If the number of child node keywords to be deleted is m-1if (i > 0 && pnode0->keynum >= m) {//If the their neighbourhood node has at least M keywords, lend it a Exchangelef Tnode (Pnode, PNode0, PNode1, i-1);} else if (i < pnode->keynum&&pnode2->keynum >= M) {//the same, Exchangerightnode (Pnode, PNode1, PNode2, i);} else if (i>0) {///two adjacent nodes have only M-1 keywords, merging merge (Pnode, PNode0, PNode1, i-1);p Node1= PNode0;} Else{merge (Pnode, PNode1, PNode2, i);} Removenonless (PNode1, key);} Else{removenonless (PNode1, key);}}} btree::btnode* btree::search (int key, int &index) {return search (root, key, index), void Btree::insert (int key) { struct btnode* r = root;if (Root->keynum = = 2 * M-1) {///root node special handling if the number of root node keywords is 2*m-1,struct btnode* pnode = new Btnode (); /new node as the new root, and the current root node as root = pnode;//the new root node of the child node Pnode->isleaf = False;pnode->keynum = 0;pnode->child[0] = r; Splitchild (pnode, 0, R);//child node R has a 2*m-1 keyword insertnonfull (pnode, key);} Elseinsertnonfull (R, key);} void Btree::remove (int key) {if (Root->keynum = = 1) {//If the root node has only two child struct btnode* pNode1 = root->child[0];struct btnode* PNode2 = root->child[1];if (Pnode1->keynum = = M-1 && pnode2->keynum = M-1) {//And two children have only M-1 keywords , merging merge (Root, PNode1, PNode2, 0);d elete root;root = pNode1;} else {removenonless (root, key);}} else {removenonless (root, key);}} void BTree::P rintrow () {struct btnode* pnode;queue<struct BTNODE*> 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 '); Printrow ();}
Well, actually I just encapsulated the front C code, still refer to the introduction of the algorithm and http://blog.chinaunix.net/uid-20196318-id-3030529.html
C + + implementation of B-tree