Skip to content

Instantly share code, notes, and snippets.

@boxgames1
Last active June 29, 2018 11:14
Show Gist options
  • Save boxgames1/0c7596365d748b56d5121c7b5f7d83ab to your computer and use it in GitHub Desktop.
Save boxgames1/0c7596365d748b56d5121c7b5f7d83ab to your computer and use it in GitHub Desktop.
BinaryTreeRemoval
remove(key) {
if (!Number.isInteger(key)) return;
this.root = this.removeNode(this.root, key);
}
removeNode(node, key) {
if (node === null) return null;
else if (key < node.key) {
node.left = this.removeNode(node.left, key);
return node;
} else if (key > node.key) {
node.right = this.removeNode(node.right, key);
return node;
} else {
if (node.left === null && node.right === null) {
node = null;
return node;
}
if (node.left === null) {
node = node.right;
return node;
} else if (node.right === null) {
node = node.left;
return node;
}
const aux = this.findMinimumNode(node.right);
node.key = aux.key;
node.right = this.removeNode(node.right, aux.key);
return node;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment