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