@@ -199,30 +199,7 @@ private void insertAndSplit(ref BpTreeNode<T> node, T newValue,
199199 //connect leaves via linked list for faster enumeration
200200 if ( node . IsLeaf )
201201 {
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 ) ;
226203 }
227204
228205 //median of current Node
@@ -396,6 +373,35 @@ private void insertToNotFullNode(ref BpTreeNode<T> node, T newValue,
396373 setChild ( node , node . KeyCount , newValueRight ) ;
397374 }
398375
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+
399405 /// <summary>
400406 /// Time complexity: O(log(n)).
401407 /// </summary>
@@ -552,25 +558,7 @@ private void sandwich(BpTreeNode<T> leftSibling, BpTreeNode<T> rightSibling, T d
552558 //if leaves are merged then update the Next and Prev pointers
553559 if ( leftSibling . IsLeaf )
554560 {
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 ) ;
574562 }
575563
576564 var newIndex = 0 ;
@@ -754,6 +742,28 @@ private void leftRotate(BpTreeNode<T> leftSibling, BpTreeNode<T> rightSibling)
754742 }
755743 }
756744
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+ }
757767 /// <summary>
758768 /// Locate the node in which the item to delete exist
759769 /// </summary>
0 commit comments