UNPKG

@drozdik.m/avl-tree

Version:

Classic implementation of AVL tree with improvements like efficient search of nth item and iterator.

124 lines (123 loc) 4.54 kB
exports.__esModule = true; var AVLTreeNode = /** @class */ (function () { //-------------------------------------------------- //----------CONSTRUCTOR----------------------------- //-------------------------------------------------- function AVLTreeNode(value) { //-------------------------------------------------- //----------VARIABLE-------------------------------- //-------------------------------------------------- //Nodes this.parent = null; this.left = null; this.right = null; this.count = 1; this.depth = 1; this.sign = 0; this.value = value; //this.Update(); } //-------------------------------------------------- //----------UPDATE---------------------------------- //-------------------------------------------------- AVLTreeNode.prototype.Update = function () { this.UpdateCount(); this.UpdateDepth(); this.UpdateSign(); this.UpdateChildrensParent(); }; //-------------------------------------------------- //----------PARENT---------------------------------- //-------------------------------------------------- AVLTreeNode.prototype.UpdateChildrensParent = function () { if (this.left != null) this.left.parent = this; if (this.right != null) this.right.parent = this; }; //-------------------------------------------------- //----------COUNT----------------------------------- //-------------------------------------------------- /** * Returns number of nodes in this (sub)tree (including this one, thus minimum is 1) * */ AVLTreeNode.prototype.Count = function () { return this.count; }; /** * Returns number of nodes in left subtree. Zero if left subtree is null. * */ AVLTreeNode.prototype.CountLeft = function () { return (this.left == null) ? 0 : this.left.Count(); }; /** * Returns number of nodes in right subtree. Zero if right subtree is null. * */ AVLTreeNode.prototype.CountRight = function () { return (this.right == null) ? 0 : this.right.Count(); }; /** * Updates count number from it's children * */ AVLTreeNode.prototype.UpdateCount = function () { this.count = this.CountLeft() + this.CountRight() + 1; }; //-------------------------------------------------- //----------DEPTH----------------------------------- //-------------------------------------------------- /** * Returns depth of this nodes tree. Zero if no children. * */ AVLTreeNode.prototype.Depth = function () { return this.depth; }; /** * Returns depth of left subtree. Zero if left subtree is null. * */ AVLTreeNode.prototype.DepthLeft = function () { return (this.left == null) ? 0 : this.left.depth; }; /** * Returns depth of right subtree. Zero if right subtree is null. * */ AVLTreeNode.prototype.DepthRight = function () { return (this.right == null) ? 0 : this.right.depth; }; /** * Updates depth number from it's children * */ AVLTreeNode.prototype.UpdateDepth = function () { var depthLeft = this.DepthLeft(); var depthRight = this.DepthRight(); var higherDepth = (depthLeft > depthRight) ? depthLeft : depthRight; this.depth = higherDepth + 1; }; /** * Updates sign number from it's children * */ AVLTreeNode.prototype.UpdateSign = function () { this.sign = this.DepthRight() - this.DepthLeft(); }; //-------------------------------------------------- //----------CLONE----------------------------------- //-------------------------------------------------- AVLTreeNode.prototype.Clone = function () { var res = new AVLTreeNode(null); //IClonable if (this.value != null) { if (typeof this.value.Clone != "undefined") res.value = this.value.Clone(); else res.value = this.value; } res.left = this.left; res.right = this.right; res.parent = this.parent; res.count = this.count; res.depth = this.depth; res.sign = this.sign; return res; }; return AVLTreeNode; }()); exports.AVLTreeNode = AVLTreeNode;