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.119 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.118 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.117 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.116 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.115 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.114 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.113 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.111 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.110 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.109 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.108 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.107 tracks/javascript/exercises/binary-search-tree/example.js
trackler-2.2.1.106 tracks/javascript/exercises/binary-search-tree/example.js