UNPKG

tstruct

Version:

Data structures & basic algorithms library

88 lines (87 loc) 3.38 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.BinarySearchTree = void 0; var BinaryTree_1 = require("./BinaryTree"); var CompareFunction_1 = require("../CompareFunction"); var BinarySearchTree = (function (_super) { __extends(BinarySearchTree, _super); function BinarySearchTree(compareFunction) { var _this = _super.call(this) || this; _this.compareFunction = CompareFunction_1.descendingCompareFunction; if (compareFunction) { _this.compareFunction = compareFunction; } return _this; } BinarySearchTree.prototype.add = function (value, callback) { if (callback === void 0) { callback = undefined; } if (!this._head) { this._head = new BinaryTree_1.BinaryTreeNode(value); } else { var newNode = this.insertNode(value, this._head); callback === null || callback === void 0 ? void 0 : callback(newNode); } }; BinarySearchTree.prototype.delete = function (value) { throw "Not Implemented Yet"; }; Object.defineProperty(BinarySearchTree.prototype, "min", { get: function () { if (!this.head) return undefined; var currentNode = this.head; while (currentNode.left) { currentNode = currentNode.left; } return currentNode.val; }, enumerable: false, configurable: true }); Object.defineProperty(BinarySearchTree.prototype, "max", { get: function () { if (!this.head) return undefined; var currentNode = this.head; while (currentNode.right) { currentNode = currentNode.right; } return currentNode.val; }, enumerable: false, configurable: true }); BinarySearchTree.prototype.insertNode = function (value, node) { var compareResult = this.compareFunction(node.val, value); if (compareResult > 0 && !node.left) { node.left = new BinaryTree_1.BinaryTreeNode(value, node); return node.left; } else if (compareResult > 0 && node.left) { return this.insertNode(value, node.left); } else if (compareResult < 0 && !node.right) { node.right = new BinaryTree_1.BinaryTreeNode(value, node); return node.right; } else if (compareResult < 0 && node.right) { return this.insertNode(value, node.right); } }; return BinarySearchTree; }(BinaryTree_1.BinaryTree)); exports.BinarySearchTree = BinarySearchTree;