Skip to content

Instantly share code, notes, and snippets.

@boxgames1
Created June 29, 2018 11:02
Show Gist options
  • Save boxgames1/f88e847b1f8e7b9a529f4d4f0503d79b to your computer and use it in GitHub Desktop.
Save boxgames1/f88e847b1f8e7b9a529f4d4f0503d79b to your computer and use it in GitHub Desktop.
BinaryTreeInsertion
insert(key, value) {
if (!Number.isInteger(key)) return;
const newNode = new BinaryNode(key, value);
if (this.root === null) this.root = newNode;
else this.insertNode(this.root, newNode);
}
insertNode(node, newNode) {
if (newNode.key < node.key) {
if (node.left === null) node.left = newNode;
else this.insertNode(node.left, newNode);
} else if (newNode.key === node.key) {
node.value = newNode.value;
} else {
if (node.right === null) node.right = newNode;
else this.insertNode(node.right, newNode);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment