Created
June 29, 2018 11:02
-
-
Save boxgames1/f88e847b1f8e7b9a529f4d4f0503d79b to your computer and use it in GitHub Desktop.
BinaryTreeInsertion
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
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