B-Tree is a balanced m-Path search tree.
Root node at least two children, non-failure nodes outside the root node at least ? M/2? Children, all failure nodes are on the H+1 floor.
level H at least 2? M/2? h-1 nodes , so the number of failed nodes n+1≥2? M/2? h-1 .
Each node contains a set of pointers Recptr[m], pointing to the location of the actual record. Recptr[i] and Key[i] form an index entry
Note : Key[0]~key[n] and Ptr[0]~ptr[n](n<m)
Insertion of 1.B trees
Each non-failure node has ? M/2? -1~m-1 a key code , after inserting over the range, you need to split the node.
Before ? M/2? -1 key codes to form a node p, after M-? M/2? Nodes form the node Q, the first ? M/2? A key code and a pointer to Q are inserted into the parent node of P .
In the worst case, the top-down search for leaf nodes requires an H-reading disk, which splits each node on the path from the bottom up. When splitting a non-root node, insert two nodes and three nodes when splitting the root node. Number of read/write disks required =3h+1(requires re-reading from disk, regardless of the read-in node and then upward insertion)
When M is large, the average number of accesses to the disk is close to h+1.
Deletion of 2.B trees
If the deletion of the key code is not in the leaf node , after the deletion of a pointer to the sub-tree to find the smallest key code x substitution , and then delete the leaf node key code x.
Delete the key code in the leaf node:
(1) The key code is deleted the node is the root node , node key number n≥2, directly delete the key code and write the node back to disk.
(2) The key code is deleted node is not the root node, node key number n≥? M/2,delete the key code directly and write the node back to disk.
(3) The key code is deleted in the leaf node, node key number n=? M/2? -1, adjacent right brother / left Brother node key number n≥? M/2?
① The parent node is just better than/less than the key code to be deleted to remove key codes to the location of the key code to be deleted.
② moves the minimum/maximum key code in the right sibling/left sibling node to that position in the parent node.
③ the left/right subtree pointer of the right sibling/left sibling node to the last/nearest subtree pointer position of the node where the key code was deleted.
④ right/left brother node was removed a key code and a pointer, need to be left to fill the adjustment, the number of key code node n also minus 1.
(4) The key code is deleted in the leaf node, node key number n=? M/2? -1, adjacent right brother / left Brother node key number n=? M/2-1, you need to merge the nodes.
① The parent node p is just better than the key code to be deleted to delete key codes to the location of the key code to be deleted.
② will move the KI+1 key code down to merge the P neutron tree pointer Pi and the node pi+1 points, and retain the node the Pi points to .
③ to point to the Pi node all the key codes and pointers are moved to the Pi+1 Point node and Delete it .
④p node is removed a key code and a pointer, need to be left to fill the adjustment, node key number n also minus 1.
⑤ if the P-node is the root node and the number of key codes is reduced to 0, then it is deleted, merging the nodes as the new root, and if the P-node is not the root node and the number of key codes is reduced to ? M/2-1, it will merge and repeat the process with its own brother node .
Template <classT>classBtree: Publicmtree<t>{//B-Tree class inherits from M-Tree Public: Btree (); BOOLInsert (Constt&x); BOOLRemove (t&x); voidLeftadjust (mtreenode<t> *p,mtreenode<t> *q,intDintj); voidRightadjust (mtreenode<t> *p,mtreenode<t> *q,intDintj); voidCompress (mtreenode<t> *p,intj); voidMerge (mtreenode<t> *p,mtreenode<t>* q,mtreenode<t> *PL,intj);}; Template<classT>BOOLBtree<t>::insert (Constt&x) { //Insert the key code X into a M-order B-tree residing on diskTriple<t> loc=Search (x); if(!loc.tag)return false;//already existsMtreenode<t> *p=loc.r,*q;//p is the node address to which the key code is to be insertedMtreenode<t> *ap=null,*t;//The AP is the right-hand pointer to the INSERT code xT k=x;intJ=LOC.I;//(K,AP) formed into a two-tuple while(1){ if(p->n<m-1){//The number of node key codes is not exceeded.Insertkey (P,J,K,AP); Putnode (P); return true; } intS= (m+1)/2;//prepare to split the knot.Insertkey (P,J,K,AP);//P->n reached m after insertionq=NewMtreenode<t>; Move (P,Q,S,M); //key[s+1..m of P] and ptr[s. M] Move to Q Key[1..s-1] and ptr[0..s-1],p->n to S-1,q->n instead of M-sk=p->key[s]; ap=q;//(K,AP) forming an upward insert of two tuples if(p->parent!=NULL) {T=p->parent; GetNode (t); J=0; T->key[(t->n) +1]=Maxkey; while(t->key[j+1]<k) j + +;//Search, find key codes greater than K stopQ->parent=p->parent; Putnode (P); Putnode (q); P=t;//p goes up to the parent node and continues to adjust . } Else{//The original p is the root, need to produce a new rootroot=NewMtreenode<t>; Root->n=1; root->parent=NULL; Root->key[1]=K; Root->ptr[0]=p; Root->ptr[1]=ap; Q->parent=p->parent=Root; Putnode (P); Putnode (q); Putnode (root); return true; }}}template<classT>BOOLBtree<t>::remove (Constt&x) {Triple<T> loc=Search (x); if(Loc.tag)return false;//not foundMtreenode<t> *p=loc.r,*q,*s; intJ=LOC.I;//p->key[j]==x if(P->ptr[j]!=null) {//non-leaf nodal pointss=p->ptr[j]; GetNode (s); q=p; while(s!=NULL) {Q=s; S=s->ptr[0]; } P->key[j]=q->key[1]; Compress (q,1);//move forward the pointer and key code in node Q after 1, remove key[1]p=Q; } ElseCompress (P,J);//leaf nodes, directly deleted . intD= (m+1)/2; while(1){ if(p->n<d-1){//less than minimum limitj=0;q=p->parent; GetNode (q); while(J<=q->n && q->ptr[j]!=p) J++; if(!j) Leftadjust (P,Q,D,J); ElseRightadjust (P,Q,D,J); P=Q; if(P==root) Break; } Else Break; } if(root->n==0) {p=root->ptr[0]; DeleteRoot; Root=p; Root->parent=NULL; } return true;} Template<classT>voidLeftadjust (mtreenode<t> *p,mtreenode<t> *q,intDintj) {Mtreenode<T> *pl=q->ptr[j+1]; if(pl->n>d-1){//Right brother space is enough, just adjustmentp->n++; P->key[p->n]=q->key[j+1]; Q->key[j+1]=pl->key[1]; P->ptr[p->n]=pl->ptr[0]; PL->ptr[0]=pl->ptr[1]; Compress (PL,1); } ElseMerge (p,q,pl,j+1);//P is merged with PL to retain P-node.}template<classT>voidRightadjust (mtreenode<t> *p,mtreenode<t> *q,intDintj) { }voidCompress (mtreenode<t> *p,intj) { for(intI=j; i<p-n; i++) {//Move leftp->key[i]=p->key[i+1]; P->ptr[i]=p->ptr[i+1]; } P->n--;//the number of elements in the node minus 1.}voidMerge (mtreenode<t> *p,mtreenode<t>* q,mtreenode<t> *PL,intj) {P->key[(p->n) +1]=q->Key[j]; P->ptr[(p->n) +1]=pl->ptr[0]; for(intI=1; i<=pl->n; i++) {p->key[(p->n) +i+1]=pl->Key[i]; P->ptr[(p->n) +i+1]=pl->Ptr[i]; } compress (Q,J); P->n=p->n+pl->n+1; DeletePl;}
Algorithm-Sort (6) B-Tree