Last active
April 27, 2020 16:48
-
-
Save sebastianfdez/65b7a9fcc44129f670de7c255b3f9e5f to your computer and use it in GitHub Desktop.
BTreeNode with get immediate brother function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class BTreeNode { | |
... | |
/** | |
* Get the immediate with more values. If there no one with extra | |
* values, return one of the immediate brothers | |
* @returns {BTreeNode} | |
*/ | |
getImmediateBrother() { | |
// Get position of node in parent children list | |
const index = this.parent.children.indexOf(this); | |
if (index > 0 && this.parent.children[index-1].n > this.tree.order - 1) { | |
// Previous child exists and has more than t-1 values | |
return this.parent.children[index-1]; | |
} | |
if (index < this.parent.n && this.parent.children[index+1].n > this.tree.order - 1) { | |
// Next child exists and has more than t-1 values | |
return this.parent.children[index+1]; | |
} | |
// There is no brother with extra values, return any of them | |
return index > 0 ? this.parent.children[index-1] : this.parent.children[index+1]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment