Skip to content

Instantly share code, notes, and snippets.

@sebastianfdez
Last active April 27, 2020 16:48
Show Gist options
  • Save sebastianfdez/65b7a9fcc44129f670de7c255b3f9e5f to your computer and use it in GitHub Desktop.
Save sebastianfdez/65b7a9fcc44129f670de7c255b3f9e5f to your computer and use it in GitHub Desktop.
BTreeNode with get immediate brother function
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