UNPKG

tstruct

Version:

Data structures & basic algorithms library

139 lines (138 loc) 4.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BinaryTree = exports.BinaryTreeNode = void 0; var Queue_1 = require("../Queue/Queue"); var BinaryTreeNode = (function () { function BinaryTreeNode(value, parent, left, right) { this.val = value; this.left = left; this.right = right; this.parent = parent; } BinaryTreeNode.prototype.copyTo = function (node) { if (!node) return; node.val = this.val; node.left = this.left; node.right = this.right; node.parent = this.parent; }; BinaryTreeNode.prototype.height = function (node) { if (node === void 0) { node = this; } if (!node) { return 0; } var leftHeight = this.height(node.left || null); var rightHeight = this.height(node.right || null); return Math.max(leftHeight, rightHeight) + 1; }; BinaryTreeNode.prototype.parentSide = function () { var _a, _b; return ((_b = (_a = this.parent) === null || _a === void 0 ? void 0 : _a.right) === null || _b === void 0 ? void 0 : _b.val) == this.val ? "right" : "left"; }; BinaryTreeNode.prototype.isBalanced = function () { var _a, _b; return Math.abs(this.height((_a = this.left) !== null && _a !== void 0 ? _a : null) - this.height((_b = this.right) !== null && _b !== void 0 ? _b : null)) <= 1; }; return BinaryTreeNode; }()); exports.BinaryTreeNode = BinaryTreeNode; var BinaryTree = (function () { function BinaryTree() { } Object.defineProperty(BinaryTree.prototype, "head", { get: function () { return this._head; }, enumerable: false, configurable: true }); BinaryTree.prototype.add = function (value) { if (!this._head) { this._head = new BinaryTreeNode(value); } else { var queue = new Queue_1.Queue(); queue.enqueue(this._head); var foundSpot = false; while (!foundSpot) { var currentElement = queue.dequeue(); if (!currentElement.left) { foundSpot = true; currentElement.left = new BinaryTreeNode(value, currentElement); } else if (!currentElement.right) { foundSpot = true; currentElement.right = new BinaryTreeNode(value, currentElement); } else { queue.enqueue(currentElement.left); queue.enqueue(currentElement.right); } } } }; BinaryTree.prototype.remove = function (value) { var node = this.findNode(value); if (node) { var bottomNode = void 0; if (node.left) { bottomNode = this.findBottomNode(node.left); } else { bottomNode = this.findBottomNode(node.right); } if (bottomNode) { var bottomNodeParentSide = bottomNode.parentSide(); bottomNode.parent[bottomNodeParentSide] = undefined; node.val = bottomNode.val; } else { var parentSide = node.parentSide(); node.parent[parentSide] = undefined; } } }; BinaryTree.prototype.findBottomNode = function (node) { if (node.left) return this.findBottomNode(node.left); return node; }; BinaryTree.prototype.toArray = function () { if (!this.head) return []; var result = []; var queue = new Queue_1.Queue(); queue.enqueue(this.head); while (!queue.isEmpty) { var node = queue.dequeue(); result.push(node.val); if (node.left) { queue.enqueue(node.left); } if (node.right) { queue.enqueue(node.right); } } return result; }; BinaryTree.prototype.findNode = function (value, node) { var _a, _b; if (node === void 0) { node = this.head; } if (!node) return; if (node.val == value) { return node; } var left = this.findNode(value, (_a = node.left) !== null && _a !== void 0 ? _a : null); if (left) return left; var right = this.findNode(value, (_b = node.right) !== null && _b !== void 0 ? _b : null); return right; }; BinaryTree.prototype.valueExists = function (value) { return this.findNode(value) != undefined; }; return BinaryTree; }()); exports.BinaryTree = BinaryTree;