UNPKG

red-black-tree-node

Version:

The algorithm of a self-balancing tree is used (Red-Black tree)

137 lines (136 loc) 4.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Color; (function (Color) { Color["red"] = "red"; Color["black"] = "black"; })(Color = exports.Color || (exports.Color = {})); var Node = /** @class */ (function () { function Node(key, value, color) { this._key = key; this._value = value; this._color = color || Color.black; this._left = null; this._right = null; this._length = 1; } Object.defineProperty(Node.prototype, "key", { get: function () { return this._key; }, enumerable: true, configurable: true }); Object.defineProperty(Node.prototype, "value", { get: function () { return this._value; }, set: function (value) { this._value = value; }, enumerable: true, configurable: true }); Object.defineProperty(Node.prototype, "color", { get: function () { return this._color; }, enumerable: true, configurable: true }); Object.defineProperty(Node.prototype, "left", { get: function () { return this._left; }, set: function (node) { if (this.left) { throw Error('Left node already exists'); } this._left = node; this.forwardBalancer(); }, enumerable: true, configurable: true }); Object.defineProperty(Node.prototype, "right", { get: function () { return this._right; }, set: function (node) { if (this.right) { throw Error('Right node already exists'); } this._right = node; this.forwardBalancer(); }, enumerable: true, configurable: true }); Object.defineProperty(Node.prototype, "length", { get: function () { return this._length; }, enumerable: true, configurable: true }); Object.defineProperty(Node.prototype, "isLeaf", { get: function () { return !this._left && !this._right; }, enumerable: true, configurable: true }); Object.defineProperty(Node.prototype, "isRed", { get: function () { return this._color === Color.red; }, enumerable: true, configurable: true }); Object.defineProperty(Node.prototype, "isBlack", { get: function () { return this._color === Color.black; }, enumerable: true, configurable: true }); Node.prototype.forwardBalancer = function () { if ((this.right && this.right.isRed) && (this.left && this.left.isBlack)) { this.rotateLeft(); } if ((this.left && this.left.isRed) && (this.left.left && this.left.left.isRed)) { this.rotateRight(); } if ((this.left && this.left.isRed) && (this.right && this.right.isRed)) { this.swapColors(); } this._length = (this.left && this.left.length || 0) + (this.right && this.right.length || 0) + 1; }; Node.prototype.swapColors = function () { this._color = this.isRed ? Color.black : Color.red; this.left._color = this.left.isRed ? Color.black : Color.red; this.right._color = this.right.isRed ? Color.black : Color.red; }; Node.prototype.rotateLeft = function () { var currentNode = this.right; this.right = currentNode.left; currentNode.left = this; currentNode._color = currentNode.left.color; currentNode.left._color = Color.red; currentNode._length = this.length; this._length = (this.left && this.left.length || 0) + (this.right && this.right.length || 0) + 1; return currentNode; }; Node.prototype.rotateRight = function () { var currentNode = this.left; this.left = currentNode.right; currentNode.right = this; currentNode._color = currentNode.right.color; currentNode.right._color = Color.red; currentNode._length = this.length; this._length = (this.left && this.left.length || 0) + (this.right && this.right.length || 0) + 1; return currentNode; }; return Node; }()); exports.Node = Node;