UNPKG

@drozdik.m/avl-tree

Version:

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

101 lines (100 loc) 3.75 kB
exports.__esModule = true; var AVLTreeIterator = /** @class */ (function () { //-------------------------------------------------- //----------CONSTRUCTOR----------------------------- //-------------------------------------------------- function AVLTreeIterator(node) { //-------------------------------------------------- //----------VARIABLE-------------------------------- //------------------------------------------------- this.current = null; this.current = node; } //-------------------------------------------------- //----------VALUE----------------------------------- //-------------------------------------------------- AVLTreeIterator.prototype.Value = function () { if (!this.HasValue()) return null; return this.current.value; }; AVLTreeIterator.prototype.HasValue = function () { return this.current != null; }; //-------------------------------------------------- //----------MOVE------------------------------------ //-------------------------------------------------- AVLTreeIterator.prototype.Previous = function () { this.current = this.Predecessor(this.current); }; AVLTreeIterator.prototype.Next = function () { this.current = this.Successor(this.current); }; /** * Returns predecessor node to this node * @param node Current node */ AVLTreeIterator.prototype.Predecessor = function (node) { //Node has left child if (node.left != null) return this.FindMaxRec(node.left); //Climb until pop from left var parentNode = node.parent; while (parentNode != null && node == parentNode.left) { node = parentNode; parentNode = parentNode.parent; } return parentNode; }; /** * Returns successor node to this node * @param node Current node */ AVLTreeIterator.prototype.Successor = function (node) { //Node has right child if (node.right != null) return this.FindMinRec(node.right); //Climb until pop from right var parentNode = node.parent; while (parentNode != null && node == parentNode.right) { node = parentNode; parentNode = parentNode.parent; } return parentNode; }; //-------------------------------------------------- //----------MIN-MAX--------------------------------- //-------------------------------------------------- AVLTreeIterator.prototype.FindMinRec = function (node) { //Node is null if (node == null) return null; //We are at the most left if (node.left == null) return node; //Go deeper 0===3 (lil joke heh) return this.FindMinRec(node.left); }; AVLTreeIterator.prototype.FindMaxRec = function (node) { //Node is null if (node == null) return null; //We are at the most right if (node.right == null) return node; //Go deeper return this.FindMaxRec(node.right); }; //-------------------------------------------------- //----------OTHERS---------------------------------- //-------------------------------------------------- /** * Returns currently selected node. Do not use unless you know what you are doing! * @returns Current node */ AVLTreeIterator.prototype.GetCurrentNode = function () { return this.current; }; return AVLTreeIterator; }()); exports.AVLTreeIterator = AVLTreeIterator;