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.159 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.158 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.157 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.156 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.155 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.154 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.153 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.152 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.151 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.150 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.149 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.148 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.147 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.146 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.145 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.144 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.143 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.142 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.141 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.140 tracks/javascript/exercises/binary-search-tree/example.js