Sha256: 07640464ee725a01354d5fde16ed3caab6f47d49818d3f215efa6e0eb55da17b

Contents?: true

Size: 873 Bytes

Versions: 73

Compression:

Stored size: 873 Bytes

Contents

'use strict';

function BinarySearchTree(data) {
  this.data  = data;
  this.left  = null;
  this.right = null;
}

BinarySearchTree.prototype.insert = function (value) {
  if (value <= this.data) {
    this.insertLeft(value);
  } else {
    this.insertRight(value);
  }

  return this;
};

BinarySearchTree.prototype.insertLeft = function (value) {
  if (!this.left) {
    this.left = new BinarySearchTree(value);
  } else {
    this.left.insert(value);
  }

  return this;
};

BinarySearchTree.prototype.insertRight = function (value) {
  if (!this.right) {
    this.right = new BinarySearchTree(value);
  } else {
    this.right.insert(value);
  }

  return this;
};

BinarySearchTree.prototype.each = function (fn) {
  if (this.left)  { this.left.each(fn);  }
  fn.call(this, this.data);
  if (this.right) { this.right.each(fn); }
};

module.exports = BinarySearchTree;

Version data entries

73 entries across 73 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.179 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.178 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.177 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.176 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.175 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.174 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.173 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.172 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.171 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.170 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.169 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.167 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.166 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.165 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.164 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.163 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.162 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.161 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.160 tracks/javascript/exercises/binary-search-tree/example.js