-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBPlusTree.h
More file actions
1162 lines (1048 loc) · 31.1 KB
/
BPlusTree.h
File metadata and controls
1162 lines (1048 loc) · 31.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// BPlusTree.h
// Minisql
// Description: The basic b+ tree implement of template.
//
// Created by 陈泓宇 on 2017/6/20.
// Copyright (c) 2014年 陈泓宇. All rights reserved.
//
#ifndef __Minisql__BPlusTree__
#define __Minisql__BPlusTree__
#include <vector>
#include <stdio.h>
#include <string.h>
#include "BufferManager.h"
#include "Minisql.h"
#include <string>
using namespace std;
static BufferManager bm;
//**********************TreeNode***************************//
typedef int offsetNumber; // the value of the tree node
template <typename KeyType>
class TreeNode{
public:
size_t count; // the count of keys
TreeNode* parent;
vector <KeyType> keys;
vector <TreeNode*> childs;
vector <offsetNumber> vals;
TreeNode* nextLeafNode; // point to the next leaf node
bool isLeaf; // the flag whether this node is a leaf node
private:
int degree;
public:
//create a new node. if the newLeaf = false, create a branch node.Otherwise, create a leaf node
TreeNode(int degree,bool newLeaf=false);
~TreeNode();
public:
bool isRoot();
bool search(KeyType key,size_t &index);//search a key and return by the reference of a parameter
TreeNode* splite(KeyType &key);
size_t add(KeyType &key); //add the key in the branch and return the position
size_t add(KeyType &key,offsetNumber val); // add a key-value in the leaf node and return the position
bool removeAt(size_t index);
#ifdef _DEBUG
public:
void debug_print();
#endif
};
//**********************BplusTree***************************//
template <typename KeyType>
class BPlusTree
{
private:
typedef TreeNode<KeyType>* Node;
// a struct helping to find the node containing a specific key
struct searchNodeParse
{
Node pNode; // a pointer pointering to the node containing the key
size_t index; // the position of the key
bool ifFound; // the flag that whether the key is found.
};
private:
string fileName;
Node root;
Node leafHead; // the head of the leaf node
size_t keyCount;
size_t level;
size_t nodeCount;
fileNode* file; // the filenode of this tree
int keySize; // the size of key
int degree;
public:
BPlusTree(string m_name,int keySize,int degree);
~BPlusTree();
offsetNumber search(KeyType& key); // search the value of specific key
bool insertKey(KeyType &key,offsetNumber val);
bool deleteKey(KeyType &key);
void dropTree(Node node);
void readFromDiskAll();
void writtenbackToDiskAll();
void readFromDisk(blockNode* btmp);
private:
void init_tree();// init the tree
bool adjustAfterinsert(Node pNode);
bool adjustAfterDelete(Node pNode);
void findToLeaf(Node pNode,KeyType key,searchNodeParse &snp);
//DEBUG
#ifdef _DEBUG
public:
void debug_print();
void debug_print_node(Node pNode);
#endif
};
/* the implement of BPlusTree function */
//******** The definition of the functions of the class TreeNode **********
/**
* Constructor: create the tree node.
*
* @param int the degreee
* @param bool the flag that whether the node is a tree node or not
*
*/
template <class KeyType>
TreeNode<KeyType>::TreeNode(int m_degree,bool newLeaf):count(0),parent(NULL),nextLeafNode(NULL),isLeaf(newLeaf),degree(m_degree)
{
for(size_t i = 0;i < degree+1;i ++)
{
childs.push_back(NULL);
keys.push_back(KeyType());
vals.push_back(offsetNumber());
}
childs.push_back(NULL);
}
/**
* @Deconstructor
*
*/
template <class KeyType>
TreeNode<KeyType>::~TreeNode()
{
}
/**
* Test if this node is the root or not.
*
* @return bool the flag that whether this node is the root or not
*
*/
template <class KeyType>
bool TreeNode<KeyType>::isRoot()
{
if(parent != NULL) return false;
else return true;
}
/**
* Search the key in the node
*
* @param KeyType
* @param size_t return the position of the node by reference
*
* @return bool the flag that whether the key exists in the node
*
*/
template <class KeyType>
bool TreeNode<KeyType>::search(KeyType key,size_t &index)
{
if(count == 0 ) // no values in the node
{
index = 0;
return false;
}
else
{
// test if key are beyond the area of the keys array
if(keys[count-1] < key)
{
index = count;
return false;
}
else if(keys[0] > key)
{
index = 0;
return false;
} // end of test
else if(count <= 20) // sequential search
{
for(size_t i = 0;i < count;i ++)
{
if(keys[i] == key)
{
index = i;
return true;
}
else if(keys[i] < key)
{
continue;
}
else if(keys[i] > key)
{
index = i;
return false;
}
}
} // end sequential search
else if(count > 20) // too many keys, binary search. 2* log(n,2) < (1+n)/2
{
size_t left = 0, right = count - 1, pos = 0;
while(right>left+1)
{
pos = (right + left) / 2;
if(keys[pos] == key)
{
index = pos;
return true;
}
else if(keys[pos] < key)
{
left = pos;
}
else if(keys[pos] > key)
{
right = pos;
}
} // end while
// right == left + 1
if(keys[left] >= key)
{
index = left;
return (keys[left] == key);
}
else if(keys[right] >= key)
{
index = right;
return (keys[right] == key);
}
else if(keys[right] < key)
{
index = right ++;
return false;
}
} // end binary search
}
return false;
}
/**
* Splite this node to two and the new node will be the next node.
*
* @param KeyType & the key reference returns the key that will go to the upper level
*
* @return TreeNode *
*
*/
template <class KeyType>
TreeNode<KeyType>* TreeNode<KeyType>::splite(KeyType &key)
{
size_t minmumNode = (degree - 1) / 2;
TreeNode* newNode = new TreeNode(degree,this->isLeaf);
if(newNode == NULL)
{
cout << "Problems in allocate momeory of TreeNode in splite node of " << key << endl;
exit(2);
}
if(isLeaf) // this is a leaf node
{
key = keys[minmumNode + 1];
for(size_t i = minmumNode + 1;i < degree;i ++) // copy the right hand of the keys to the new node
{
newNode->keys[i-minmumNode-1] = keys[i];
keys[i] = KeyType();
newNode->vals[i-minmumNode-1] = vals[i];
vals[i] = offsetNumber();
}
newNode->nextLeafNode = this->nextLeafNode;
this->nextLeafNode = newNode;
newNode->parent = this->parent;
newNode->count = minmumNode;
this->count = minmumNode + 1;
} // end leaf
else if(!isLeaf)
{
key = keys[minmumNode];
for(size_t i = minmumNode + 1;i < degree+1;i ++)
{
newNode->childs[i-minmumNode-1] = this->childs[i];
newNode->childs[i-minmumNode-1]->parent = newNode;
this->childs[i] = NULL;
}
for(size_t i = minmumNode + 1;i < degree;i ++)
{
newNode->keys[i-minmumNode-1] = this->keys[i];
this->keys[i] = KeyType();
}
this->keys[minmumNode] = KeyType();
newNode->parent = this->parent;
newNode->count = minmumNode;
this->count = minmumNode;
}
return newNode;
}
/**
* Add the key in the branch node and return the position added.
*
* @param KeyType &
*
* @return size_t the position to insert
*
*/
template <class KeyType>
size_t TreeNode<KeyType>::add(KeyType &key)
{
if(count == 0)
{
keys[0] = key;
count ++;
return 0;
}
else //count > 0
{
size_t index = 0; // record the index of the tree
bool exist = search(key, index);
if(exist)
{
cout << "Error:In add(Keytype &key),key has already in the tree!" << endl;
exit(3);
}
else // add the key into the node
{
for(size_t i = count;i > index;i --)
keys[i] = keys[i-1];
keys[index] = key;
for(size_t i = count + 1;i > index+1;i --)
childs[i] = childs[i-1];
childs[index+1] = NULL; // this child will link to another node
count ++;
return index;
}
}
}
/**
* Add the key in the leaf node and return the position added.
*
* @param Keytype &
* @param offsetNumber the value
*
* @return size_t the position to insert
*
*/
template <class KeyType>
size_t TreeNode<KeyType>::add(KeyType &key,offsetNumber val)
{
if(!isLeaf)
{
cout << "Error:add(KeyType &key,offsetNumber val) is a function for leaf nodes" << endl;
return -1;
}
if(count == 0)
{
keys[0] = key;
vals[0] = val;
count ++;
return 0;
}
else //count > 0
{
size_t index = 0; // record the index of the tree
bool exist = search(key, index);
if(exist)
{
cout << "Error:In add(Keytype &key, offsetNumber val),key has already in the tree!" << endl;
exit(3);
}
else // add the key into the node
{
for(size_t i = count;i > index;i --)
{
keys[i] = keys[i-1];
vals[i] = vals[i-1];
}
keys[index] = key;
vals[index] = val;
count ++;
return index;
}
}
}
/**
* Delete the key-value or key-child by the position.
*
* @param size_t the position to delete
*
* @return bool the falg that delation successes or not
*
*/
template <class KeyType>
bool TreeNode<KeyType>::removeAt(size_t index)
{
if(index > count)
{
cout << "Error:In removeAt(size_t index), index is more than count!" << endl;
return false;
}
else
{
if(isLeaf)
{
for(size_t i = index;i < count-1;i ++)
{
keys[i] = keys[i+1];
vals[i] = vals[i+1];
}
keys[count-1] = KeyType();
vals[count-1] = offsetNumber();
}
else // is nonleaf
{
for(size_t i = index;i < count-1;i ++)
keys[i] = keys[i+1];
for(size_t i = index+1;i < count;i ++)
childs[i] = childs[i+1];
keys[count-1] = KeyType();
childs[count] = NULL;
}
count --;
return true;
}
}
#ifdef _DEBUG
/**
* For debug, print the whole tree
*
* @param
*
* @return void
*
*/
template <class KeyType>
void TreeNode<KeyType>::debug_print()
{
cout << "############DEBUG for node###############" << endl;
cout << "Address: " << (void*)this << ",count: " << count << ",Parent: " << (void*)parent << ",isleaf: " << isLeaf << ",nextNode: " << (void*)nextLeafNode << endl;
cout << "KEYS:{";
for(size_t i = 0;i < count;i ++)
{
cout << keys[i] << " ";
}
cout << "}" << endl;
if(isLeaf)
{
cout << "VALS:{";
for(size_t i = 0;i < count;i ++)
{
cout << vals[i] << " ";
}
cout << "}" << endl;
}
else // nonleaf node
{
cout << "CHILDREN:{";
for(size_t i = 0;i < count + 1;i ++)
{
cout << (void*)childs[i] << " ";
}
cout << "}" << endl;
}
cout << "#############END OF DEBUG IN NODE########"<< endl;
}
#endif
//******** The definition of the functions of the class BPlusTree **********
/**
* Constructor: init the tree, allocate the memory of the root and then,if users have created the tree before, read from disk and rebuild it.
*
* @param string m_name
* @param int keysize
* @param int m_degree
*
*/
template <class KeyType>
BPlusTree<KeyType>::BPlusTree(string m_name,int keysize,int m_degree):fileName(m_name),keyCount(0),level(0),nodeCount(0),root(NULL),leafHead(NULL),keySize(keysize),file(NULL),degree(m_degree)
{
init_tree();
readFromDiskAll();
}
/**
* Deconstrucor: free the allocated memory and write back to the disk if required.
*
*/
template <class KeyType>
BPlusTree<KeyType>:: ~BPlusTree()
{
dropTree(root);
keyCount = 0;
root = NULL;
level = 0;
}
/**
* Init the tree,allocate memory for the root node.
*
* @return void
*
*/
template <class KeyType>
void BPlusTree<KeyType>::init_tree()
{
root = new TreeNode<KeyType>(degree,true);
keyCount = 0;
level = 1;
nodeCount = 1;
leafHead = root;
}
/**
* Search the node to its leaf level to find the node contains the key
*
* @param Node
* @param KeyType&
* @param searchNodeParse& return the searching information by reference
*
* @return
*
*/
template <class KeyType>
void BPlusTree<KeyType>::findToLeaf(Node pNode,KeyType key,searchNodeParse & snp)
{
size_t index = 0;
if(pNode->search(key,index)) // find the key in the node
{
if(pNode->isLeaf)
{
snp.pNode = pNode;
snp.index = index;
snp.ifFound = true;
}
else // the node is not a leaf, continue search until the leaf level
{
pNode = pNode -> childs[index + 1];
while(!pNode->isLeaf)
{
pNode = pNode->childs[0];
}
snp.pNode = pNode;
snp.index = 0;
snp.ifFound = true;
}
}
else // can not find the key in the node
{
if(pNode->isLeaf)
{
snp.pNode = pNode;
snp.index = index;
snp.ifFound = false;
}
else
{
findToLeaf(pNode->childs[index],key,snp);
}
}
}
/**
* Insert the key in right position.Then, adjust the whole tree for the rules of b+ tree.
*
* @param KeyType&
* @param offsetNumber the value
*
* @return bool the flag that the insertion successes or not
*
*/
template <class KeyType>
bool BPlusTree<KeyType>::insertKey(KeyType &key,offsetNumber val)
{
searchNodeParse snp;
if(!root) init_tree();
findToLeaf(root,key,snp);
if(snp.ifFound)
{
cout << "Error:in insert key to index: the duplicated key!" << endl;
return false;
}
else
{
snp.pNode->add(key,val);
if(snp.pNode->count == degree)
{
adjustAfterinsert(snp.pNode);
}
keyCount ++;
return true;
}
}
/**
* Adjust the node after insertion. Rrecursively call this function itself if the father node contradicts the rules.
*
* @param Node the pointer pointing to the node
*
* @return bool the flag
*
*/
template <class KeyType>
bool BPlusTree<KeyType>::adjustAfterinsert(Node pNode)
{
KeyType key;
Node newNode = pNode->splite(key);
nodeCount ++;
if(pNode->isRoot()) // the node is the root
{
Node root = new TreeNode<KeyType>(degree,false);
if(root == NULL)
{
cout << "Error: can not allocate memory for the new root in adjustAfterinsert" << endl;
exit(1);
}
else
{
level ++;
nodeCount ++;
this->root = root;
pNode->parent = root;
newNode->parent = root;
root->add(key);
root->childs[0] = pNode;
root->childs[1] = newNode;
return true;
}
}// end root
else // if it is not the root
{
Node parent = pNode->parent;
size_t index = parent->add(key);
parent->childs[index+1] = newNode;
newNode->parent = parent;
if(parent->count == degree)
return adjustAfterinsert(parent);
return true;
}
}
/**
* Search the tree to find the value of specific key
*
* @param KeyType&
*
* @return offsetnumber. The value of the key, -1 means not found in the tree
*
*/
template <class KeyType>
offsetNumber BPlusTree<KeyType>::search(KeyType& key)
{
if(!root) return -1;
searchNodeParse snp;
findToLeaf(root, key, snp);
if(!snp.ifFound)
{
return -1; // Don't find the key in the tree;
}
else
{
return snp.pNode->vals[snp.index];
}
}
/**
* Delete the key-value or key-child by the inputed key.Then adjust the whole tree if required.
*
* @param KeyType&
*
* @return bool the flag
*
*/
template <class KeyType>
bool BPlusTree<KeyType>::deleteKey(KeyType &key)
{
searchNodeParse snp;
if(!root)
{
cout << "ERROR: In deleteKey, no nodes in the tree " << fileName << "!" << endl;
return false;
}
else
{
findToLeaf(root, key, snp);
if(!snp.ifFound)
{
cout << "ERROR: In deleteKey, no keys in the tree " << fileName << "!" << endl;
return false;
}
else // find the key in the leaf node
{
if(snp.pNode->isRoot())
{
snp.pNode->removeAt(snp.index);
keyCount --;
return adjustAfterDelete(snp.pNode);
}
else
{
if(snp.index == 0 && leafHead != snp.pNode) // the key exist in the branch.
{
// go to upper level to update the branch level
size_t index = 0;
Node now_parent = snp.pNode->parent;
bool if_found_inBranch = now_parent->search(key,index);
while(!if_found_inBranch)
{
if(now_parent->parent)
now_parent = now_parent->parent;
else
{
break;
}
if_found_inBranch = now_parent->search(key,index);
}// end of search in the branch
now_parent -> keys[index] = snp.pNode->keys[1];
snp.pNode->removeAt(snp.index);
keyCount--;
return adjustAfterDelete(snp.pNode);
}
else //this key must just exist in the leaf too.
{
snp.pNode->removeAt(snp.index);
keyCount--;
return adjustAfterDelete(snp.pNode);
}
}
}
}
}
/**
* Adjust the node after deletion. Rrecursively call this function itself if the father node contradicts the rules.
*
* @param Node the pointer pointing to the node
*
* @return bool the flag
*
*/
template <class KeyType>
bool BPlusTree<KeyType>::adjustAfterDelete(Node pNode)
{
size_t minmumKey = (degree - 1) / 2;
if(((pNode->isLeaf)&&(pNode->count >= minmumKey)) || ((degree != 3)&&(!pNode->isLeaf)&&(pNode->count >= minmumKey - 1)) || ((degree ==3)&&(!pNode->isLeaf)&&(pNode->count < 0))) // do not need to adjust
{
return true;
}
if(pNode->isRoot())
{
if(pNode->count > 0) //do not need to adjust
{
return true;
}
else
{
if(root->isLeaf) //the true will be an empty tree
{
delete pNode;
root = NULL;
leafHead = NULL;
level --;
nodeCount --;
}
else // root will be the leafhead
{
root = pNode -> childs[0];
root -> parent = NULL;
delete pNode;
level --;
nodeCount --;
}
}
}// end root
else
{
Node parent = pNode->parent,brother = NULL;
if(pNode->isLeaf)
{
size_t index = 0;
parent->search(pNode->keys[0],index);
if((parent->childs[0] != pNode) && (index + 1 == parent->count)) //choose the left brother to merge or replace
{
brother = parent->childs[index];
if(brother->count > minmumKey) // choose the most right key of brother to add to the left hand of the pnode
{
for(size_t i = pNode->count;i > 0;i --)
{
pNode->keys[i] = pNode->keys[i-1];
pNode->vals[i] = pNode->vals[i-1];
}
pNode->keys[0] = brother->keys[brother->count-1];
pNode->vals[0] = brother->vals[brother->count-1];
brother->removeAt(brother->count-1);
pNode->count ++;
parent->keys[index] = pNode->keys[0];
return true;
} // end add
else // merge the node with its brother
{
parent->removeAt(index);
for(int i = 0;i < pNode->count;i ++)
{
brother->keys[i+brother->count] = pNode->keys[i];
brother->vals[i+brother->count] = pNode->vals[i];
}
brother->count += pNode->count;
brother->nextLeafNode = pNode->nextLeafNode;
delete pNode;
nodeCount --;
return adjustAfterDelete(parent);
}// end merge
}// end of the left brother
else // choose the right brother
{
if(parent->childs[0] == pNode)
brother = parent->childs[1];
else
brother = parent->childs[index+2];
if(brother->count > minmumKey)//// choose the most left key of brother to add to the right hand of the node
{
pNode->keys[pNode->count] = brother->keys[0];
pNode->vals[pNode->count] = brother->vals[0];
pNode->count ++;
brother->removeAt(0);
if(parent->childs[0] == pNode)
parent->keys[0] = brother->keys[0];
else
parent->keys[index+1] = brother->keys[0];
return true;
}// end add
else // merge the node with its brother
{
for(int i = 0;i < brother->count;i ++)
{
pNode->keys[pNode->count+i] = brother->keys[i];
pNode->vals[pNode->count+i] = brother->vals[i];
}
if(pNode == parent->childs[0])
parent->removeAt(0);
else
parent->removeAt(index+1);
pNode->count += brother->count;
pNode->nextLeafNode = brother->nextLeafNode;
delete brother;
nodeCount --;
return adjustAfterDelete(parent);
}//end merge
}// end of the right brother
}// end leaf
else // branch node
{
size_t index = 0;
parent->search(pNode->childs[0]->keys[0],index);
if((parent->childs[0] != pNode) && (index + 1 == parent->count)) // choose the left brother to merge or replace
{
brother = parent->childs[index];
if(brother->count > minmumKey - 1) // choose the most right key and child to add to the left hand of the pnode
{
//modify the pnode
pNode->childs[pNode->count+1] = pNode->childs[pNode->count];
for(size_t i = pNode->count;i > 0;i --)
{
pNode->childs[i] = pNode->childs[i-1];
pNode->keys[i] = pNode->keys[i-1];
}
pNode->childs[0] = brother->childs[brother->count];
pNode->keys[0] = parent->keys[index];
pNode->count ++;
//modify the father
parent->keys[index]= brother->keys[brother->count-1];
//modify the brother and child
if(brother->childs[brother->count])
{
brother->childs[brother->count]->parent = pNode;
}
brother->removeAt(brother->count-1);
return true;
}// end add
else // merge the node with its brother
{
//modify the brother and child
brother->keys[brother->count] = parent->keys[index];
parent->removeAt(index);
brother->count ++;
for(int i = 0;i < pNode->count;i ++)
{
brother->childs[brother->count+i] = pNode->childs[i];
brother->keys[brother->count+i] = pNode->keys[i];
brother->childs[brother->count+i]-> parent= brother;
}
brother->childs[brother->count+pNode->count] = pNode->childs[pNode->count];
brother->childs[brother->count+pNode->count]->parent = brother;
brother->count += pNode->count;
delete pNode;
nodeCount --;
return adjustAfterDelete(parent);
}
}// end of the left brother
else // choose the right brother
{
if(parent->childs[0] == pNode)
brother = parent->childs[1];
else
brother = parent->childs[index+2];
if(brother->count > minmumKey - 1)// choose the most left key and child to add to the right hand of the pnode
{
//modifty the pnode and child
pNode->childs[pNode->count+1] = brother->childs[0];
pNode->keys[pNode->count] = brother->keys[0];
pNode->childs[pNode->count+1]->parent = pNode;
pNode->count ++;
//modify the fater
if(pNode == parent->childs[0])
parent->keys[0] = brother->keys[0];
else
parent->keys[index+1] = brother->keys[0];
//modify the brother
brother->childs[0] = brother->childs[1];
brother->removeAt(0);
return true;
}
else // merge the node with its brother
{
//modify the pnode and child
pNode->keys[pNode->count] = parent->keys[index];
if(pNode == parent->childs[0])
parent->removeAt(0);
else
parent->removeAt(index+1);
pNode->count ++;
for(int i = 0;i < brother->count;i++)
{
pNode->childs[pNode->count+i] = brother->childs[i];
pNode->keys[pNode->count+i] = brother->keys[i];
pNode->childs[pNode->count+i]->parent = pNode;
}
pNode->childs[pNode->count+brother->count] = brother->childs[brother->count];
pNode->childs[pNode->count+brother->count]->parent = pNode;
pNode->count += brother->count;