Insert Method

https://subscription.packtpub.com/video/programming/9781800206878/p3/video3_4/-insert-method

by Dominic Myers

JavaScript

function BST(value) {
  this.value = value;
  this.left = null;
  this.right = null;
}

BST.prototype.insert = function(value) {
  if (value <= this.value) {
    if (!this.left) {
      this.left = new BST(value);
    } else {
      this.left.insert(value);
    }
  } else { // if (value > this.value){
    if (!this.right) {
      this.right = new BST(value);
    } else {
      this.right.insert(value);
    }
  }
}

var bst = new BST(50)