B+tree 實現

轉自:http://blog.csdn.net/hpghy123456/article/details/7719052

  1. /** 
  2.  BPlusTree.h 
  3.      
  4.  2012/6/24--2012/7/5 
  5.  hpghy, [email protected] 
  6.   
  7.  **/  
  8.   
  9. #ifndef BPLUSTREE_H_  
  10. #define BPLUSTREE_H_  
  11.   
  12. #include <assert.h>  
  13. #include <queue>  
  14. #include <iostream>  
  15. using std::queue;  
  16. using std::cout;  
  17. using std::endl;  
  18. using std::ends;  
  19.   
  20. //======================================================================================  
  21. class BPlusNode  
  22. {  
  23. protected:  
  24.     BPlusNode* parent;  
  25.     bool isLeafNode;  
  26.   
  27. public:  
  28.     BPlusNode( bool leaf=true ) {  
  29.         parent = NULL;  
  30.         isLeafNode = leaf;  
  31.     }  
  32.     ~BPlusNode() {}  
  33.     bool& Leaf() {  
  34.         return isLeafNode;  
  35.     }  
  36.     BPlusNode*& Parent() {  
  37.         return this->parent;  
  38.     }  
  39. };  
  40.   
  41. //======================================================================================  
  42. template < typename KeyClass, int M >  
  43. class BInternalNode: public BPlusNode  
  44. {  
  45. public:  
  46.     static const int MaxKey = M-1;  
  47.     static const int MinKey = (M+1)/2-1;  
  48.   
  49. private:  
  50.     KeyClass key[M+1];              //key count: = child count - 1;  
  51.     BPlusNode* child[M+1];          //child count: = ceil[M/2] --> M;  
  52.     int keyCnt;  
  53.   
  54. public:  
  55.     BInternalNode(): BPlusNode(false) {  
  56.         keyCnt = 0;  
  57.     }  
  58.     ~BInternalNode() {}  
  59.     KeyClass& Key(int idx) { return key[idx]; }  
  60.     BPlusNode*& Child(int idx) { return child[idx]; }  
  61.     int& KeyCnt() { return keyCnt; }  
  62.     bool IsOver() { return keyCnt > MaxKey; }  //key count: ceil(m/2)-1 --> M-1  
  63.     bool IsTooLittle() { return keyCnt < MinKey; }  
  64.   
  65.     int Search( KeyClass const& key );  
  66.     void Insert( KeyClass const& key, BPlusNode * pNode );  
  67.     void Delete( int );  
  68. };  
  69.   
  70. /** 
  71.  *  find the child index where the key should insert  
  72.  *  Maybe the name "SearchChild" is better! 
  73.  */  
  74. template < typename KeyClass, int M >  
  75. int BInternalNode<KeyClass, M>::Search( KeyClass const& key ) {  
  76.     int i, j, mid;  
  77.     i = 0;   
  78.     j = keyCnt-1;  
  79.     while ( i < j ) {  
  80.         mid = (i+j)>>1;  
  81.         if ( key < this->key[mid] )  
  82.             j = mid-1;  
  83.         else if ( this->key[mid] < key )  
  84.             i = mid+1;  
  85.         else  
  86.             break;  
  87.     }  
  88.     mid = i;  
  89.     //judge:  
  90.     if ( !( key < this->key[mid] ) ) {  
  91.         return (mid+1);  
  92.     }  
  93.     return mid;  
  94. }  
  95.   
  96. /* 
  97.  just insert, no split 
  98.  */  
  99. template < typename KeyClass, int M >  
  100. void BInternalNode<KeyClass, M>::Insert( KeyClass const& key, BPlusNode * pNode ) {  
  101.     int i;  
  102.     for ( i = keyCnt; i > 0; -- i ) {  
  103.         if ( this->key[i-1] < key )  
  104.             break;  
  105.         this->key[i] = this->key[i-1];  
  106.         this->child[i+1] = this->child[i];  
  107.     }  
  108.     this->key[i] = key;  
  109.     this->child[i+1] = pNode;  
  110.     ++ keyCnt;  
  111.     //setting parent  
  112.     pNode->Parent() = this;  
  113. }  
  114.   
  115. /* 
  116. Just delete key[index] and child[index+1] 
  117. */  
  118. template < typename KeyClass, int M >  
  119. void BInternalNode<KeyClass, M>::Delete( int idx )  
  120. {  
  121.     for ( int i = idx+1; i < keyCnt; ++ i ) {  
  122.         key[i-1] = key[i];  
  123.         child[i] = child[i+1];  
  124.     }  
  125.     -- keyCnt;  
  126. }  
  127.   
  128. //======================================================================================  
  129. template < typename KeyClass, typename ValueClass, int L >  
  130. class BLeafNode: public BPlusNode  
  131. {  
  132. public:  
  133.     static const int MaxKey = L;  
  134.     static const int MinKey = (L+1)/2;  
  135.   
  136. private:  
  137.     KeyClass key[L+1];                  //key count == value count  
  138.     ValueClass value[L+1];  
  139.     BLeafNode* next;  
  140.     int keyCnt;  
  141.   
  142. public:  
  143.     BLeafNode(): BPlusNode(true) {  
  144.         keyCnt = 0;  
  145.         next = NULL;  
  146.     }  
  147.     ~BLeafNode() {}  
  148.     KeyClass& Key(int idx) { return key[idx]; }  
  149.     ValueClass& Value(int idx) { return value[idx]; }  
  150.   
  151.     BLeafNode*& Next() { return next; }  
  152.     int& KeyCnt() { return keyCnt; }  
  153.     bool IsOver() { return keyCnt > MaxKey; }  
  154.     bool IsTooLittle() { return keyCnt < MinKey; }  
  155.   
  156.     int Search( KeyClass const& key );  
  157.     void Insert( KeyClass const& key, ValueClass const& value );  
  158.     void Delete( int );  
  159. };  
  160.   
  161. /** 
  162.  *  -1: not exist! 
  163.  */  
  164. template < typename KeyClass, typename ValueClass, int L >  
  165. int BLeafNode<KeyClass,ValueClass,L>::Search( KeyClass const& key ) {  
  166.     int i, j, mid;  
  167.     i = 0;   
  168.     j = keyCnt-1;  
  169.     while ( i <= j ) {  
  170.         mid = (i+j)>>1;  
  171.         if ( key < this->key[mid] )  
  172.             j = mid-1;  
  173.         else if ( this->key[mid] < key )  
  174.             i = mid+1;  
  175.         else  
  176.             return mid;  
  177.     }  
  178.     return -1;  
  179. }  
  180.   
  181. /** 
  182.  *  just insert key-value at specified position; 
  183.  */  
  184. template < typename KeyClass, typename ValueClass, int L >  
  185. void BLeafNode<KeyClass,ValueClass,L>::Insert( KeyClass const& key, ValueClass const& value ) {  
  186.     int i;  
  187.     for ( i = keyCnt; i > 0; -- i ) {  
  188.         if ( this->key[i-1] < key )  
  189.             break;  
  190.         this->key[i] = this->key[i-1];  
  191.         this->value[i] = this->value[i-1];  
  192.     }  
  193.     this->key[i] = key;  
  194.     this->value[i] = value;  
  195.     ++ keyCnt;  
  196. }  
  197.   
  198. /** 
  199.  * 
  200.  * 
  201.  **/  
  202. template < typename KeyClass, typename ValueClass, int L >  
  203. void BLeafNode<KeyClass,ValueClass,L>::Delete( int idx )   
  204. {  
  205.     for ( int i = idx; i < keyCnt; ++ i )   
  206.     {  
  207.         key[i] = key[i+1];  
  208.         value[i] = value[i+1];  
  209.     }  
  210.     -- keyCnt;  
  211. }  
  212.   
  213. //======================================================================================  
  214. template < typename KeyClass, typename ValueClass, int M, int L=M >  
  215. class BPlusTree  
  216. {  
  217. public:  
  218.     typedef BPlusNode           Node;  
  219.     typedef BPlusNode*          PNode;  
  220.     typedef BInternalNode<KeyClass,M>     InternalNode;  
  221.     typedef BInternalNode<KeyClass,M>*        PInternalNode;  
  222.     typedef BLeafNode<KeyClass, ValueClass, L>        LeafNode;  
  223.     typedef BLeafNode<KeyClass, ValueClass, L>*       PLeafNode;  
  224.   
  225. private:  
  226.     PNode root;  
  227.     PLeafNode head;  
  228.   
  229. public:  
  230.     BPlusTree();  
  231.     ~BPlusTree();  
  232.   
  233.     bool Search( KeyClass const& key, ValueClass *pValue );  
  234.     bool Insert( KeyClass const& key, ValueClass const& value );  
  235.     bool Delete( KeyClass const& key );  
  236.   
  237.     //just for test  
  238.     void Print();  
  239.     void PrintLevel();  
  240.     void Print( PNode );  
  241. private:  
  242.     void Clear( PNode );  
  243.     PLeafNode Search( KeyClass const& key, int& idx );  
  244.     PInternalNode SearchInternal( KeyClass const& key, int& idx );  
  245.     void SplitRootNode();  
  246.     void SplitLeafNode( PLeafNode );  
  247.     void SplitInternalNode( PInternalNode );  
  248.   
  249.     void Rebalance( PLeafNode );  
  250.     void Rebalance( PInternalNode );  
  251.     void MergeLeafNode( PLeafNode, int );  
  252.     void MergeInternalNode( PInternalNode, int );  
  253. };  
  254.   
  255. //=======================================================================================  
  256. template < typename KeyClass, typename ValueClass, int M, int L >  
  257. BPlusTree<KeyClass, ValueClass, M, L>::BPlusTree() {  
  258.     root = NULL;  
  259. }  
  260.   
  261. template < typename KeyClass, typename ValueClass, int M, int L >  
  262. BPlusTree<KeyClass, ValueClass, M, L>::~BPlusTree() {  
  263.     Clear( root );  
  264.     root = head = NULL;  
  265. }  
  266.   
  267. /** 
  268.  *  Search whether it contains the specified key, if true, return the corresponding value. 
  269.  *  otherwise, return false. 
  270.  **/  
  271. template < typename KeyClass, typename ValueClass, int M, int L >  
  272. bool BPlusTree<KeyClass, ValueClass, M, L>::Search( KeyClass const& key, ValueClass *pValue ) {  
  273.     int idx;  
  274.     if ( NULL == root ) {  
  275.         return false;  
  276.     }  
  277.     PNode p = (PNode)Search( key, idx );  
  278.     if ( NULL == p || -1 == idx )  
  279.         return false;  
  280.     if ( NULL != pValue )  
  281.         //*pValue = ((PLeafNode)p)->Value(idx);  
  282.         *pValue = (static_cast<PLeafNode>(p))->Value(idx);  
  283.     return true;  
  284. }  
  285.   
  286. template < typename KeyClass, typename ValueClass, int M, int L >  
  287. bool BPlusTree<KeyClass, ValueClass, M, L>::Insert( KeyClass const& key,  
  288.         ValueClass const& value ) {  
  289.   
  290.     if ( NULL == root ) {  
  291.         root = new LeafNode();  
  292.         head = (PLeafNode)root;  
  293.         ((PLeafNode)root)->Insert( key, value );  
  294.     }  
  295.     else {  
  296.         int idx;  
  297.         //idx: the position which key inserted in  
  298.         PLeafNode p = Search( key, idx );  
  299.   
  300.         //the key exist!  
  301.         if ( -1 != idx )   
  302.             return false;  
  303.         p->Insert( key, value );  
  304.   
  305.         if ( p->IsOver() )  
  306.         {  
  307.             if ( root == p )        //root must be a leafnode  
  308.                 SplitRootNode();  
  309.             else  
  310.                 SplitLeafNode(p);  
  311.         }  
  312.     }  
  313.     return true;  
  314. }  
  315. /**  
  316.  *  1 Reach the leaf that contains the key.  
  317.  *  2 Delete key from leaf:  
  318.  *    remove the key from leaf node.  
  319.  *    2.1 if internal node contains this key, replace it by the small key in the new leaf.  
  320.  *    2.2 else   
  321.  *        2.2.1 Lend a key from left sibling leaf.  
  322.  *        2.2.2 Lend a key form right sibling leaf.  
  323.  *        2.2.3 Merge leaf and its sibling.  
  324.  *  for attention:  
  325.  *      Keep the parent of leaf contains the smallest key in the leaf.  
  326.  */  
  327. template < typename KeyClass, typename ValueClass, int M, int L >  
  328. bool BPlusTree<KeyClass, ValueClass, M, L>::Delete( KeyClass const& key ){  
  329.     int idx;  
  330.     PLeafNode p = Search( key, idx );  
  331.     if ( NULL == p || idx < 0 )  
  332.         return false;  
  333.   
  334.     p->Delete( idx );  
  335.     if ( 0 == idx && root != p ) {  
  336.         PInternalNode internal = SearchInternal( key, idx );  
  337.         if ( NULL != internal && idx > -1 )  
  338.             internal->Key(idx) = p->Key(0);  
  339.     }  
  340.   
  341.     if ( p->IsTooLittle() && root != p )  
  342.     {  
  343.         Rebalance(p);  
  344.     }  
  345.     return true;  
  346. }  
  347.   
  348. //just for test  
  349. template < typename KeyClass, typename ValueClass, int M, int L >  
  350. void BPlusTree<KeyClass, ValueClass, M, L>::Print() {  
  351.     cout << "Print head list:" << endl;  
  352.     PLeafNode p = head;  
  353.     while ( NULL != p ) {  
  354.         Print(p);  
  355.         p = p->Next();  
  356.     }  
  357.     cout << endl;  
  358. }  
  359. template < typename KeyClass, typename ValueClass, int M, int L >  
  360. void BPlusTree<KeyClass, ValueClass, M, L>::Print( PNode p ) {  
  361.     cout << "( ";  
  362.     if ( true == p->Leaf() ) {  
  363.         PLeafNode leaf = static_cast<PLeafNode>(p);  
  364.         for ( int i = 0; i < leaf->KeyCnt(); ++ i )  
  365.             cout << leaf->Key(i) << "--" << leaf->Value(i) << " ";  
  366.     }  
  367.     else {  
  368.         PInternalNode inter = static_cast<PInternalNode>(p);  
  369.         for ( int i = 0; i < inter->KeyCnt(); ++ i )  
  370.             cout << inter->Key(i) << " ";  
  371.     }  
  372.     cout << ") ";  
  373. }  
  374. template < typename KeyClass, typename ValueClass, int M, int L >  
  375. void BPlusTree<KeyClass, ValueClass, M, L>::PrintLevel() {  
  376.     cout << "PrintLevel:" << endl;  
  377.     if ( true == root->Leaf() ) {  
  378.         cout << "[ ";  
  379.         Print( root );  
  380.         cout << " ]" << endl;  
  381.         return;  
  382.     }  
  383.   
  384.     int i, level;  
  385.     queue<PNode> q;  
  386.     PNode p;  
  387.     q.push( root );  
  388.     q.push( NULL );  
  389.     level = 0;  
  390.     cout << "Level :" << level << endl;  
  391.     while ( !q.empty() ) {  
  392.         p = q.front();  
  393.         q.pop();  
  394.         if ( NULL == p ) {  
  395.             ++ level;  
  396.             if ( !q.empty() )  
  397.                 q.push(NULL);  
  398.             cout << endl << "Level :" << level << ends;  
  399.             continue;  
  400.         }  
  401.         Print(p);  
  402.         if ( true != p->Leaf() ) {  
  403.             PInternalNode tmp = static_cast<PInternalNode>(p);  
  404.             for ( i = 0; i <= tmp->KeyCnt(); ++ i )  
  405.                 q.push( tmp->Child(i) );  
  406.         }  
  407.     }  
  408.     cout << endl;  
  409. }  
  410.   
  411. //==============================================================================================================  
  412. //private:  
  413. template < typename KeyClass, typename ValueClass, int M, int L >  
  414. void BPlusTree<KeyClass, ValueClass, M, L>::Clear( PNode p ) {  
  415.     if ( NULL == p )  
  416.         return;  
  417.     if ( false == p->Leaf() ) {  
  418.         PInternalNode inter = static_cast<PInternalNode>(p);  
  419.         for ( int i = 0; i <= inter->KeyCnt(); ++ i ) {  
  420.             Clear( inter->Child(i) );  
  421.         }  
  422.     }  
  423.     delete p;  
  424. }  
  425.   
  426. /** 
  427.  *  If tree has the parameter key, the return the leafnode and key index; 
  428.  *  otherwise, set idx = -1. 
  429.  **/  
  430. template < typename KeyClass, typename ValueClass, int M, int L >  
  431. typename BPlusTree<KeyClass, ValueClass, M, L>::PLeafNode  
  432.     BPlusTree<KeyClass, ValueClass, M, L>::Search( KeyClass const& key, int& result ){  
  433.   
  434.     if ( NULL == root )  
  435.         return NULL;  
  436.   
  437.     PNode p = root;  
  438.     if ( true == root->Leaf() )   
  439.     {  
  440.         result = ((PLeafNode)p)->Search(key);  
  441.     }  
  442.     else {  
  443.         int idx;  
  444.         PInternalNode internal;  
  445.         //internal node  
  446.         while ( NULL != p && p->Leaf() == false )   
  447.         {  
  448.             internal = static_cast<PInternalNode>(p);  
  449.             idx = internal->Search(key);  
  450.       
  451.             p = internal->Child(idx);  
  452.         }  
  453.         //leaf node  
  454.         //idx = ((PLeafNode)p)->Search(key);  
  455.         result = static_cast<PLeafNode>(p)->Search(key);  
  456.     }  
  457.     return (PLeafNode)p;  
  458. }  
  459.   
  460. /** 
  461.  *  Search a internal node that contains the key! 
  462.  * */  
  463. template < typename KeyClass, typename ValueClass, int M, int L >  
  464. typename BPlusTree<KeyClass, ValueClass, M, L>::PInternalNode  
  465.     BPlusTree<KeyClass, ValueClass, M, L>::SearchInternal( KeyClass const& key, int& result ){  
  466.       
  467.     if ( NULL == root || true == root->Leaf() )  
  468.         return NULL;  
  469.     PNode p = root;  
  470.     int idx;  
  471.     PInternalNode internal;  
  472.   
  473.     while ( NULL != p && p->Leaf() == false )   
  474.     {  
  475.         internal = static_cast<PInternalNode>(p);  
  476.         idx = internal->Search(key);  
  477.         if ( idx > 0 && internal->Key(idx-1) == key ) {  
  478.             result = idx-1;  
  479.             return internal;  
  480.         }  
  481.         p = internal->Child(idx);  
  482.     }  
  483.     result = -1;  
  484.     return NULL;  
  485. }  
  486.   
  487. //root must be a leafnode  
  488. template < typename KeyClass, typename ValueClass, int M, int L >  
  489. void BPlusTree<KeyClass, ValueClass, M, L>::SplitRootNode()   
  490. {  
  491.     PLeafNode pRNode;  
  492.     PLeafNode p = (PLeafNode)root;  
  493.     int i, left, right;  
  494.       
  495.     pRNode = new LeafNode();  
  496.     left = (L+1)>>1;  
  497.     right = p->KeyCnt() - left;  
  498.     for ( i = 0; i < right; ++ i ) {  
  499.         pRNode->Key(i) = p->Key(i+left);  
  500.         pRNode->Value(i) = p->Value(i+left);  
  501.     }  
  502.     pRNode->Next() = NULL;  
  503.     p->Next() = pRNode;  
  504.   
  505.     pRNode->KeyCnt() = right;  
  506.     p->KeyCnt() = left;  
  507.   
  508.     PInternalNode newRoot = new InternalNode();  
  509.     p->Parent() = newRoot;  
  510.     pRNode->Parent() = newRoot;  
  511.   
  512.     newRoot->Key(0) = pRNode->Key(0);  
  513.     newRoot->Child(0) = root;  
  514.     newRoot->Child(1) = pRNode;  
  515.     newRoot->KeyCnt() = 1;  
  516.   
  517.     //head = (BPlusNode)root;  
  518.     root = newRoot;  
  519. }  
  520.   
  521. template < typename KeyClass, typename ValueClass, int M, int L >  
  522. void BPlusTree<KeyClass, ValueClass, M, L>::SplitLeafNode( PLeafNode p )   
  523. {  
  524.     PLeafNode pRNode = new LeafNode();  
  525.     int i, left, right;  
  526.   
  527.     left = (L+1)>>1;  
  528.     right = p->KeyCnt() - left;  
  529.     for ( i = 0; i < right; ++ i ) {  
  530.         pRNode->Key(i) = p->Key(i+left);  
  531.         pRNode->Value(i) = p->Value(i+left);  
  532.     }  
  533.     pRNode->Next() = p->Next();  
  534.     p->Next() = pRNode;  
  535.   
  536.     pRNode->KeyCnt() = right;  
  537.     p->KeyCnt() = left;  
  538.   
  539.     if ( NULL !=p->Parent() ) {  
  540.         PInternalNode tmp = static_cast<PInternalNode>( p->Parent() );  
  541.         tmp->Insert( pRNode->Key(0), pRNode );  
  542.         if ( tmp->IsOver() ) {  
  543.             SplitInternalNode( tmp );  
  544.         }  
  545.     }  
  546.     else {  
  547.         PInternalNode newRoot = new InternalNode();  
  548.         newRoot->KeyCnt() = 1;  
  549.         newRoot->Child(0) = p;  
  550.         newRoot->Child(1) = pRNode;  
  551.         newRoot->Key(0) = pRNode->Key(0);  
  552.         p->Parent() = newRoot;  
  553.         pRNode->Parent() = newRoot;  
  554.   
  555.         root = newRoot;  
  556.     }  
  557. }  
  558.   
  559. /* 
  560. SplitInternalNode() 
  561.     1 Node ==> Left( ceil(M/2)-1 ) + Key(中間) + Right( M/2 ) 
  562.     2 InsertInternalNode( Key, Right ) 
  563. */  
  564. template < typename KeyClass, typename ValueClass, int M, int L >  
  565. void BPlusTree<KeyClass, ValueClass, M, L>::SplitInternalNode( PInternalNode p )   
  566. {  
  567.     int leftK, rightK, i;  
  568.     KeyClass midkey;  
  569.   
  570.     leftK = (M+1)/2-1;  
  571.     rightK = M/2;  
  572.     midkey = p->Key(leftK);  
  573.   
  574.     PInternalNode pRNode = new InternalNode();  
  575.     for ( i = 0; i < rightK; ++ i ) {  
  576.         pRNode->Key(i) = p->Key( i+leftK+1 );  
  577.         pRNode->Child(i) = p->Child( i+leftK+1 );  
  578.     }  
  579.     pRNode->Child(rightK) = p->Child( p->KeyCnt() );  
  580.     pRNode->KeyCnt() = rightK;  
  581.     p->KeyCnt() = leftK;  
  582.   
  583.     for ( i = 0; i < pRNode->KeyCnt()+1; ++ i ) {  
  584.         pRNode->Child(i)->Parent() = pRNode;  
  585.     }  
  586.   
  587.     if ( NULL != p->Parent() ) {  
  588.         PInternalNode tmp = static_cast<PInternalNode>( p->Parent() );  
  589.         tmp->Insert( midkey, pRNode );  
  590.         if ( tmp->IsOver() ) {  
  591.             SplitInternalNode( tmp );  
  592.         }  
  593.     }  
  594.     else {  
  595.         PInternalNode newRoot = new InternalNode();  
  596.         newRoot->KeyCnt() = 1;  
  597.         newRoot->Child(0) = p;  
  598.         newRoot->Child(1) = pRNode;  
  599.         newRoot->Key(0) = midkey;  
  600.         p->Parent() = newRoot;  
  601.         pRNode->Parent() = newRoot;  
  602.   
  603.         root = newRoot;  
  604.     }  
  605. }  
  606.   
  607. /* 
  608. */  
  609. template < typename KeyClass, typename ValueClass, int M, int L >  
  610. void BPlusTree<KeyClass, ValueClass, M, L>::Rebalance( PLeafNode p )  
  611. {  
  612.     int index, i;  
  613.     PInternalNode pa;  
  614.     PLeafNode prev, next;  
  615.   
  616.     pa = static_cast<PInternalNode>( p->Parent() );  
  617.     index = pa->Search( p->Key(0) );      //index: child pointer index!  
  618.   
  619.     //attention:   
  620.     // prev and next node must have the same parent node!  
  621.     prev = next = NULL;  
  622.     if ( index != 0 )  
  623.         prev = static_cast<PLeafNode>( pa->Child(index-1) );  
  624.     if ( index < pa->KeyCnt() )  
  625.         next = static_cast<PLeafNode>( pa->Child(index+1) );  
  626.   
  627.     if ( NULL != next && next->KeyCnt() > LeafNode::MinKey ) {  
  628.   
  629.         p->Key( p->KeyCnt() ) = next->Key(0);      // pa->Key(index) == next->Key(0)  
  630.         p->Value( p->KeyCnt() ) = next->Value(0);  
  631.         ++ p->KeyCnt();  
  632.   
  633.         for ( i = 1; i < next->KeyCnt(); ++ i ) {  
  634.             next->Key(i-1) = next->Key(i);  
  635.             next->Value(i-1) = next->Value(i);  
  636.         }  
  637.         -- next->KeyCnt();  
  638.   
  639.         pa->Key( index ) = next->Key(0);  
  640.     }  
  641.     else if ( NULL != prev && prev->KeyCnt() > LeafNode::MinKey ) {  
  642.           
  643.         for ( i = p->KeyCnt()-1; i >= 0; -- i ) {  
  644.             p->Key(i+1) = p->Key(i);  
  645.             p->Value(i+1) = p->Value(i);  
  646.         }  
  647.   
  648.         p->Key(0) = prev->Key( prev->KeyCnt()-1 );       
  649.         p->Value(0) = prev->Value( prev->KeyCnt()-1 );  
  650.         ++ p->KeyCnt();  
  651.         -- prev->KeyCnt();  
  652.   
  653.         pa->Key(index-1) = p->Key(0);  
  654.     }  
  655.     else {  
  656.         MergeLeafNode( p, index );  
  657.     }  
  658. }  
  659.   
  660. template < typename KeyClass, typename ValueClass, int M, int L >  
  661. void BPlusTree<KeyClass, ValueClass, M, L>::Rebalance( PInternalNode p )  
  662. {  
  663.     int index, i;  
  664.     PInternalNode pa, prev, next;  
  665.   
  666.     pa = static_cast<PInternalNode>( p->Parent() );  
  667.     index = pa->Search( p->Key(0) );      //index: child pointer index!  
  668.     prev = next = NULL;  
  669.   
  670.     if ( index < pa->KeyCnt() )  
  671.         next = static_cast<PInternalNode>( pa->Child(index+1) );  
  672.     if ( index > 0 )  
  673.         prev = static_cast<PInternalNode>( pa->Child(index-1) );  
  674.   
  675.     if ( NULL != next && next->KeyCnt() > InternalNode::MinKey ) {  
  676.   
  677.         p->Key( p->KeyCnt() ) = pa->Key( index );        
  678.   
  679.         pa->Key( index ) = next->Key(0);  
  680.   
  681.         p->Child( p->KeyCnt()+1 ) = next->Child(0);  
  682.         p->Child( p->KeyCnt()+1 )->Parent() = p;  
  683.         ++ p->KeyCnt();  
  684.   
  685.         for ( i = 1; i < next->KeyCnt(); ++ i ) {  
  686.             next->Key(i-1) = next->Key(i);  
  687.             next->Child(i-1) = next->Child(i);  
  688.         }  
  689.         next->Child( next->KeyCnt()-1 ) = next->Child( next->KeyCnt() );  
  690.         -- next->KeyCnt();  
  691.     }  
  692.     else if ( NULL != prev && prev->KeyCnt() > InternalNode::MinKey ) {  
  693.           
  694.         for ( i = p->KeyCnt()-1; i >= 0; -- i ) {  
  695.             p->Key(i+1) = p->Key(i);  
  696.             p->Child(i+2) = p->Child(i+1);  
  697.         }  
  698.         p->Child(1) = p->Child(0);  
  699.   
  700.         p->Key(0) = pa->Key( index-1 );  
  701.         pa->Key( index-1 ) = prev->Key( prev->KeyCnt()-1 );  
  702.   
  703.         p->Child(0) = prev->Child( prev->KeyCnt() );  
  704.         p->Child(0)->Parent() = p;  
  705.   
  706.         ++ p->KeyCnt();  
  707.         -- prev->KeyCnt();  
  708.     }  
  709.     else {  
  710.         MergeInternalNode( p, index );  
  711.     }  
  712. }  
  713.   
  714. /////////////////////////////////////////////////////////////////  
  715. /* 
  716.  p->Parent()->Child(idx) == p  
  717.  */  
  718. template < typename KeyClass, typename ValueClass, int M, int L >  
  719. void BPlusTree<KeyClass, ValueClass, M, L>::MergeLeafNode( PLeafNode p, int idx )   
  720. {  
  721.     PLeafNode next, prev, first, second;  
  722.     PInternalNode pa;  
  723.     int i, pos;  
  724.   
  725.     pos = -1;  
  726.     pa = static_cast<PInternalNode>( p->Parent() );  
  727.     prev = next = NULL;  
  728.     //attention: prev and next must have the same parent node.  
  729.     if ( idx != 0 )  
  730.         prev = static_cast<PLeafNode>( pa->Child(idx-1) );  
  731.     if ( idx < pa->KeyCnt() )  
  732.         next = static_cast<PLeafNode>( pa->Child(idx+1) );  
  733.   
  734.     if ( NULL != next ) {  
  735.         pos = idx;  
  736.         first = p;  
  737.         second = next;  
  738.     }  
  739.     else if ( idx > 0 ) {          
  740.         pos = idx-1;  
  741.         first = prev;  
  742.         second = p;  
  743.     }  
  744.   
  745.     for ( i = 0; i < second->KeyCnt(); ++ i ) {  
  746.         first->Key( first->KeyCnt()+i ) = second->Key(i);  
  747.         first->Value( first->KeyCnt()+i ) = second->Value(i);  
  748.     }  
  749.     first->KeyCnt() += second->KeyCnt();  
  750.     first->Next() = second->Next();  
  751.   
  752.     pa->Delete( pos );           //remove key[pos] and child[pos+1]  
  753.     delete second;  
  754.   
  755.     if ( root == pa && 0 == pa->KeyCnt() ) {  
  756.         root = first;  
  757.         delete pa;  
  758.     }  
  759.     else if ( pa->IsTooLittle() && root != pa ) {  
  760.         Rebalance( pa );  
  761.     }  
  762. }  
  763.   
  764. /* 
  765.  p->Parent()->Child(idx) == p  
  766.  */  
  767. template < typename KeyClass, typename ValueClass, int M, int L >  
  768. void BPlusTree<KeyClass, ValueClass, M, L>::MergeInternalNode( PInternalNode p, int idx )   
  769. {  
  770.     PInternalNode next, prev, first, second, pa;  
  771.     int i, pos;  
  772.   
  773.     pa = static_cast<PInternalNode>( p->Parent() );  
  774.     prev = next = NULL;  
  775.     pos = -1;  
  776.     if ( idx < pa->KeyCnt() )  
  777.         next = static_cast<PInternalNode>( pa->Child(idx+1) );  
  778.     if ( idx > 0 )  
  779.         prev = static_cast<PInternalNode>( pa->Child(idx-1) );  
  780.   
  781.     if ( NULL != next ) {  
  782.         pos = idx;  
  783.         first = p;  
  784.         second = next;  
  785.     }  
  786.     else {  
  787.         pos = idx-1;  
  788.         first = prev;  
  789.         second = p;  
  790.     }  
  791.       
  792.   
  793.     first->Key( first->KeyCnt() ) = pa->Key( pos );  
  794.     for ( i = 0; i < second->KeyCnt(); ++ i ) {  
  795.         first->Key( first->KeyCnt()+1+i ) = second->Key(i);  
  796.         first->Child( first->KeyCnt()+1+i ) = second->Child(i);  
  797.         second->Child(i)->Parent() = first;  
  798.     }  
  799.     first->KeyCnt() += second->KeyCnt() + 1;  
  800.     first->Child( first->KeyCnt() ) = second->Child( second->KeyCnt() );  
  801.     second->Child( second->KeyCnt() )->Parent() = first;  
  802.   
  803.     pa->Delete( pos );           //remove key[pos] and child[pos+1]  
  804.     delete second;  
  805.       
  806.     if ( root == pa && 0 == pa->KeyCnt() ) {  
  807.         root = first;  
  808.         delete pa;  
  809.     }  
  810.     else if ( pa->IsTooLittle() && root != pa ) {  
  811.         Rebalance( pa );  
  812.     }  
  813. }  
  814.   
  815. #endif

發佈了2 篇原創文章 · 獲贊 6 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章