UNPKG

tstruct

Version:

Data structures & basic algorithms library

96 lines (95 loc) 4.29 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.AVLTree = void 0; var BinarySearchTree_1 = require("./BinarySearchTree"); var AVLTree = (function (_super) { __extends(AVLTree, _super); function AVLTree() { return _super !== null && _super.apply(this, arguments) || this; } AVLTree.prototype.add = function (value) { _super.prototype.add.call(this, value, this.balanceNode.bind(this)); }; AVLTree.prototype.balanceNode = function (node) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r; var balanceFactor = ((_b = (_a = node.left) === null || _a === void 0 ? void 0 : _a.height()) !== null && _b !== void 0 ? _b : 0) - ((_d = (_c = node.right) === null || _c === void 0 ? void 0 : _c.height()) !== null && _d !== void 0 ? _d : 0); if (balanceFactor > 1) { var leftBalance = ((_g = (_f = (_e = node.left) === null || _e === void 0 ? void 0 : _e.left) === null || _f === void 0 ? void 0 : _f.height()) !== null && _g !== void 0 ? _g : 0) - ((_k = (_j = (_h = node.left) === null || _h === void 0 ? void 0 : _h.right) === null || _j === void 0 ? void 0 : _j.height()) !== null && _k !== void 0 ? _k : 0); if (leftBalance > 0) { this.rightRotate(node); } else if (leftBalance < 0) { this.leftRightRotate(node); } } else if (balanceFactor <= -1) { var rightBalance = ((_o = (_m = (_l = node.right) === null || _l === void 0 ? void 0 : _l.right) === null || _m === void 0 ? void 0 : _m.height()) !== null && _o !== void 0 ? _o : 0) - ((_r = (_q = (_p = node.right) === null || _p === void 0 ? void 0 : _p.left) === null || _q === void 0 ? void 0 : _q.height()) !== null && _r !== void 0 ? _r : 0); if (rightBalance > 0) { this.leftRotate(node); } else if (rightBalance < 0) { this.rightLeftRotate(node); } } if (node.parent) { this.balanceNode(node.parent); } }; AVLTree.prototype.leftRotate = function (node) { var _a; var isHead = node == this.head; var tmpParent = node.parent; var right = node.right; node.right = node.right.left; node.parent = right; right.parent = tmpParent; right.left = node; if (isHead) { this._head = right; } else { var side = ((_a = tmpParent.right) === null || _a === void 0 ? void 0 : _a.val) == node.val ? "right" : "left"; tmpParent[side] = right; } }; AVLTree.prototype.leftRightRotate = function (node) { this.leftRotate(node.left); this.rightRotate(node.right); }; AVLTree.prototype.rightRotate = function (node) { var _a; var isHead = node == this.head || !node.parent; var tmpParent = node.parent; var left = node.left; node.left = node.left.right; node.parent = left; left.parent = tmpParent; left.right = node; if (isHead) { this._head = left; } else { var side = ((_a = tmpParent.right) === null || _a === void 0 ? void 0 : _a.val) == node.val ? "right" : "left"; tmpParent[side] = left; } }; AVLTree.prototype.rightLeftRotate = function (node) { this.rightRotate(node.right); this.leftRotate(node); }; return AVLTree; }(BinarySearchTree_1.BinarySearchTree)); exports.AVLTree = AVLTree;