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.139 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.138 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.137 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.136 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.135 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.134 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.133 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.132 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.131 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.130 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.129 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.128 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.127 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.126 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.125 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.124 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.123 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.122 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.121 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.120 tracks/javascript/exercises/binary-search-tree/example.js