Last active
June 29, 2018 11:14
-
-
Save boxgames1/0c7596365d748b56d5121c7b5f7d83ab to your computer and use it in GitHub Desktop.
BinaryTreeRemoval
This file contains 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
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