UNPKG

arayts

Version:

让 TypeScript 开发如丝般顺滑。ArayTS 提供了一套高效、优雅的算法工具集,包含常用的数据结构与算法实现,帮助开发者轻松构建可靠的应用程序。

129 lines (128 loc) 4.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RedBlackTree = void 0; /** * 红黑树 */ var RedBlackTree = /** @class */ (function () { function RedBlackTree() { this.root = null; } RedBlackTree.prototype.rotateLeft = function (node) { var right = node.right; node.right = right.left; if (right.left) right.left.parent = node; right.parent = node.parent; if (!node.parent) this.root = right; else if (node === node.parent.left) node.parent.left = right; else node.parent.right = right; right.left = node; node.parent = right; }; RedBlackTree.prototype.rotateRight = function (node) { var left = node.left; node.left = left.right; if (left.right) left.right.parent = node; left.parent = node.parent; if (!node.parent) this.root = left; else if (node === node.parent.right) node.parent.right = left; else node.parent.left = left; left.right = node; node.parent = left; }; RedBlackTree.prototype.insert = function (value) { var node = new RBNode(value); if (!this.root) { this.root = node; node.color = false; return; } var parent = null; var current = this.root; while (current) { parent = current; if (value < current.value) current = current.left; else current = current.right; } node.parent = parent; if (value < parent.value) parent.left = node; else parent.right = node; this.fixInsertion(node); }; RedBlackTree.prototype.fixInsertion = function (node) { var _a, _b, _c; while ((_a = node.parent) === null || _a === void 0 ? void 0 : _a.color) { if (node.parent === ((_b = node.parent.parent) === null || _b === void 0 ? void 0 : _b.left)) { var uncle = node.parent.parent.right; if (uncle === null || uncle === void 0 ? void 0 : uncle.color) { node.parent.color = false; uncle.color = false; node.parent.parent.color = true; node = node.parent.parent; } else { if (node === node.parent.right) { node = node.parent; this.rotateLeft(node); } node.parent.color = false; node.parent.parent.color = true; this.rotateRight(node.parent.parent); } } else { var uncle = (_c = node.parent.parent) === null || _c === void 0 ? void 0 : _c.left; if (uncle === null || uncle === void 0 ? void 0 : uncle.color) { node.parent.color = false; uncle.color = false; node.parent.parent.color = true; node = node.parent.parent; } else { if (node === node.parent.left) { node = node.parent; this.rotateRight(node); } node.parent.color = false; node.parent.parent.color = true; this.rotateLeft(node.parent.parent); } } if (node === this.root) break; } this.root.color = false; }; return RedBlackTree; }()); exports.RedBlackTree = RedBlackTree; /** * 红黑树节点 */ var RBNode = /** @class */ (function () { function RBNode(value, color, // true为红色,false为黑色 left, right, parent) { if (color === void 0) { color = true; } if (left === void 0) { left = null; } if (right === void 0) { right = null; } if (parent === void 0) { parent = null; } this.value = value; this.color = color; this.left = left; this.right = right; this.parent = parent; } return RBNode; }());