Skip to content

Instantly share code, notes, and snippets.

@jdgo-mars
Created April 21, 2019 15:28
Show Gist options
  • Save jdgo-mars/1cac1b8afb22fd7576f5822713130b2d to your computer and use it in GitHub Desktop.
Save jdgo-mars/1cac1b8afb22fd7576f5822713130b2d to your computer and use it in GitHub Desktop.
function BinarySearchTree(val) {
this.value = val;
this.left = null;
this.right = null;
}
BinarySearchTree.prototype.insert = function(value) {
let subtree = value < this.value ? 'left' : 'right';
if (this[subtree]) {
this[subtree].insert(value);
} else {
this[subtree] = new BinarySearchTree(value);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment