@@ -199,30 +199,7 @@ private void insertAndSplit(ref BpTreeNode<T> node, T newValue,
199
199
//connect leaves via linked list for faster enumeration
200
200
if ( node . IsLeaf )
201
201
{
202
- left . Next = right ;
203
- right . Prev = left ;
204
-
205
- if ( node . Next != null )
206
- {
207
- right . Next = node . Next ;
208
- node . Next . Prev = right ;
209
- }
210
- else
211
- {
212
- //bottom right most node
213
- BottomRightNode = right ;
214
- }
215
-
216
- if ( node . Prev != null )
217
- {
218
- left . Prev = node . Prev ;
219
- node . Prev . Next = left ;
220
- }
221
- else
222
- {
223
- //bottom left most node
224
- BottomLeftNode = left ;
225
- }
202
+ connectLeaves ( node , left , right ) ;
226
203
}
227
204
228
205
//median of current Node
@@ -396,6 +373,35 @@ private void insertToNotFullNode(ref BpTreeNode<T> node, T newValue,
396
373
setChild ( node , node . KeyCount , newValueRight ) ;
397
374
}
398
375
376
+
377
+ private void connectLeaves ( BpTreeNode < T > node , BpTreeNode < T > left , BpTreeNode < T > right )
378
+ {
379
+ left . Next = right ;
380
+ right . Prev = left ;
381
+
382
+ if ( node . Next != null )
383
+ {
384
+ right . Next = node . Next ;
385
+ node . Next . Prev = right ;
386
+ }
387
+ else
388
+ {
389
+ //bottom right most node
390
+ BottomRightNode = right ;
391
+ }
392
+
393
+ if ( node . Prev != null )
394
+ {
395
+ left . Prev = node . Prev ;
396
+ node . Prev . Next = left ;
397
+ }
398
+ else
399
+ {
400
+ //bottom left most node
401
+ BottomLeftNode = left ;
402
+ }
403
+ }
404
+
399
405
/// <summary>
400
406
/// Time complexity: O(log(n)).
401
407
/// </summary>
@@ -552,25 +558,7 @@ private void sandwich(BpTreeNode<T> leftSibling, BpTreeNode<T> rightSibling, T d
552
558
//if leaves are merged then update the Next and Prev pointers
553
559
if ( leftSibling . IsLeaf )
554
560
{
555
- if ( leftSibling . Prev != null )
556
- {
557
- newNode . Prev = leftSibling . Prev ;
558
- leftSibling . Prev . Next = newNode ;
559
- }
560
- else
561
- {
562
- BottomLeftNode = newNode ;
563
- }
564
-
565
- if ( rightSibling . Next != null )
566
- {
567
- newNode . Next = rightSibling . Next ;
568
- rightSibling . Next . Prev = newNode ;
569
- }
570
- else
571
- {
572
- BottomRightNode = newNode ;
573
- }
561
+ mergeLeaves ( newNode , leftSibling , rightSibling ) ;
574
562
}
575
563
576
564
var newIndex = 0 ;
@@ -754,6 +742,28 @@ private void leftRotate(BpTreeNode<T> leftSibling, BpTreeNode<T> rightSibling)
754
742
}
755
743
}
756
744
745
+ private void mergeLeaves ( BpTreeNode < T > newNode , BpTreeNode < T > leftSibling , BpTreeNode < T > rightSibling )
746
+ {
747
+ if ( leftSibling . Prev != null )
748
+ {
749
+ newNode . Prev = leftSibling . Prev ;
750
+ leftSibling . Prev . Next = newNode ;
751
+ }
752
+ else
753
+ {
754
+ BottomLeftNode = newNode ;
755
+ }
756
+
757
+ if ( rightSibling . Next != null )
758
+ {
759
+ newNode . Next = rightSibling . Next ;
760
+ rightSibling . Next . Prev = newNode ;
761
+ }
762
+ else
763
+ {
764
+ BottomRightNode = newNode ;
765
+ }
766
+ }
757
767
/// <summary>
758
768
/// Locate the node in which the item to delete exist
759
769
/// </summary>
0 commit comments