@@ -436,7 +436,7 @@ export class BTree<TKey, TEntry> {
436436 const pathBranch = path . branches . at ( - 1 ) ! ;
437437 this . updatePartition ( pathBranch . index , path , path . branches . length - 1 , this . keyFromEntry ( path . leafNode . entries [ path . leafIndex ] ) ) ;
438438 }
439- const newRoot = this . rebalanceLeaf ( path , path . branches . length ) ;
439+ const newRoot = this . rebalanceLeaf ( path ) ;
440440 if ( newRoot ) {
441441 this . _root = newRoot ;
442442 }
@@ -584,29 +584,30 @@ export class BTree<TKey, TEntry> {
584584 return new Split < TKey > ( newPartition , newBranch , delta ) ;
585585 }
586586
587- private rebalanceLeaf ( path : Path < TKey , TEntry > , depth : number ) : ITreeNode | undefined {
588- if ( depth === 0 || path . leafNode . entries . length >= ( NodeCapacity >>> 1 ) ) {
587+ private rebalanceLeaf ( path : Path < TKey , TEntry > ) : ITreeNode | undefined {
588+ if ( path . leafNode . entries . length >= ( NodeCapacity >>> 1 ) ) {
589589 return undefined ;
590590 }
591591
592592 const leaf = path . leafNode ;
593- const parent = path . branches . at ( depth - 1 ) ! ;
593+ const parent = path . branches . at ( - 1 ) ! ;
594+ const depth = path . branches . length - 1 ;
594595 const pIndex = parent . index ;
595596 const pNode = parent . node ;
596597
597598 const rightSib = pNode . nodes [ pIndex + 1 ] as LeafNode < TEntry > | undefined ;
598599 if ( rightSib && rightSib . entries . length > ( NodeCapacity >>> 1 ) ) { // Attempt to borrow from right sibling
599600 const entry = rightSib . entries . shift ( ) ! ;
600601 leaf . entries . push ( entry ) ;
601- this . updatePartition ( pIndex + 1 , path , depth - 1 , this . keyFromEntry ( rightSib . entries [ 0 ] ! ) ) ;
602+ this . updatePartition ( pIndex + 1 , path , depth , this . keyFromEntry ( rightSib . entries [ 0 ] ! ) ) ;
602603 return undefined ;
603604 }
604605
605606 const leftSib = pNode . nodes [ pIndex - 1 ] as LeafNode < TEntry > | undefined ;
606607 if ( leftSib && leftSib . entries . length > ( NodeCapacity >>> 1 ) ) { // Attempt to borrow from left sibling
607608 const entry = leftSib . entries . pop ( ) ! ;
608609 leaf . entries . unshift ( entry ) ;
609- this . updatePartition ( pIndex , path , depth - 1 , this . keyFromEntry ( entry ) ) ;
610+ this . updatePartition ( pIndex , path , depth , this . keyFromEntry ( entry ) ) ;
610611 path . leafIndex += 1 ;
611612 return undefined ;
612613 }
@@ -616,9 +617,9 @@ export class BTree<TKey, TEntry> {
616617 pNode . partitions . splice ( pIndex , 1 ) ;
617618 pNode . nodes . splice ( pIndex + 1 , 1 ) ;
618619 if ( pIndex === 0 ) { // 0th node of parent, update parent key
619- this . updatePartition ( pIndex , path , depth - 1 , this . keyFromEntry ( leaf . entries [ 0 ] ! ) ) ;
620+ this . updatePartition ( pIndex , path , depth , this . keyFromEntry ( leaf . entries [ 0 ] ! ) ) ;
620621 }
621- return this . rebalanceBranch ( path , depth - 1 ) ;
622+ return this . rebalanceBranch ( path , depth ) ;
622623 }
623624
624625 if ( leftSib && leftSib . entries . length + leaf . entries . length <= NodeCapacity ) { // Attempt to merge into left sibling (leaf deleted)
@@ -627,7 +628,7 @@ export class BTree<TKey, TEntry> {
627628 leftSib . entries . push ( ...leaf . entries ) ;
628629 pNode . partitions . splice ( pIndex - 1 , 1 ) ;
629630 pNode . nodes . splice ( pIndex , 1 ) ;
630- return this . rebalanceBranch ( path , depth - 1 ) ;
631+ return this . rebalanceBranch ( path , depth ) ;
631632 }
632633 }
633634
0 commit comments