UNPKG

ts-ds-tool

Version:

Data structure and algorithm of TypeScript

1,695 lines (1,607 loc) 147 kB
var CollectionEnumerator = function CollectionEnumerator(array) { this.array = array; this.index = 0; }; var prototypeAccessors = { Current: { configurable: true } }; CollectionEnumerator.prototype.next = function next () { this.index++; return this; }; prototypeAccessors.Current.get = function () { return { value: this.array[this.index], done: this.array.length > 0 ? this.index === this.array.length - 1 : true, }; }; Object.defineProperties( CollectionEnumerator.prototype, prototypeAccessors ); var Collection = function Collection () {}; Collection.prototype.getEnumerator = function getEnumerator () { return new CollectionEnumerator(this.toArray()); }; Collection.prototype.toArray = function toArray () { var arr = []; this.__iterate(function (item, index) { arr[index] = item; }); return arr; }; var DoubleLinkNode = function DoubleLinkNode(value, next, prev) { if ( next === void 0 ) next = null; if ( prev === void 0 ) prev = null; this.value = value; this.next = next; this.prev = prev; }; var prototypeAccessors$1 = { Value: { configurable: true },Next: { configurable: true },Prev: { configurable: true } }; prototypeAccessors$1.Value.get = function () { return this.value; }; prototypeAccessors$1.Next.get = function () { return this.next; }; prototypeAccessors$1.Prev.get = function () { return this.prev; }; DoubleLinkNode.prototype.setValue = function setValue (value) { this.value = value; }; DoubleLinkNode.prototype.setNext = function setNext (node) { this.next = node; if (node) { node.prev = this; } }; DoubleLinkNode.prototype.toString = function toString () { return ("" + (this.value)); }; Object.defineProperties( DoubleLinkNode.prototype, prototypeAccessors$1 ); var DoubleLinkList = (function (Collection$$1) { function DoubleLinkList() { Collection$$1.call(this); this.headNode = null; this.tailNode = null; this.size = 0; } if ( Collection$$1 ) DoubleLinkList.__proto__ = Collection$$1; DoubleLinkList.prototype = Object.create( Collection$$1 && Collection$$1.prototype ); DoubleLinkList.prototype.constructor = DoubleLinkList; var prototypeAccessors = { Size: { configurable: true } }; prototypeAccessors.Size.get = function () { return this.size; }; DoubleLinkList.prototype.append = function append (value) { this.size++; if (!this.headNode) { this.headNode = this.tailNode = new DoubleLinkNode(value, null, null); return this.headNode; } if (this.headNode === this.tailNode) { this.tailNode = new DoubleLinkNode(value); this.headNode.setNext(this.tailNode); return this.headNode; } var tailNode = new DoubleLinkNode(value); this.tailNode.setNext(tailNode); this.tailNode = tailNode; return this.headNode; }; DoubleLinkList.prototype.prepend = function prepend (value) { if (!this.headNode) { this.headNode = this.tailNode = new DoubleLinkNode(value); } else { var headNode = this.headNode; this.headNode = new DoubleLinkNode(value); this.headNode.setNext(headNode); } this.size++; return this.headNode; }; DoubleLinkList.prototype.emptyList = function emptyList () { this.headNode = this.tailNode = null; this.size = 0; }; DoubleLinkList.prototype.clear = function clear () { this.emptyList(); }; DoubleLinkList.prototype.deleteNode = function deleteNode (arg) { var this$1 = this; var temp = this.headNode; var result = false; var prevNode; while (temp) { var match = typeof arg === "function" ? arg(temp.Value) : (temp.Value === arg); if (match) { this$1.size--; result = true; if (temp === this$1.headNode) { this$1.headNode = temp.Next; } else if (temp === this$1.tailNode) { prevNode.setNext(null); this$1.tailNode = prevNode; } else { prevNode.setNext(temp.Next); } } if (temp.Next && temp.Next === this$1.headNode) { break; } if (this$1.size === 0) { this$1.emptyList(); break; } prevNode = temp; temp = temp.Next; } return result; }; DoubleLinkList.prototype.findNode = function findNode (arg) { var temp = this.headNode; var result; while (temp) { var match = typeof arg === "function" ? arg(temp.Value) : (temp.Value === arg); if (match) { result = temp; break; } temp = temp.Next; } return result; }; DoubleLinkList.prototype.insertAfter = function insertAfter (value, oriNode) { var newNode = new DoubleLinkNode(value); if (oriNode) { var nextNode = oriNode.Next; if (!nextNode) { this.tailNode = newNode; } newNode.setNext(nextNode); oriNode.setNext(newNode); return true; } return false; }; DoubleLinkList.prototype.getHeadNode = function getHeadNode () { return this.headNode; }; DoubleLinkList.prototype.getTailNode = function getTailNode () { return this.tailNode; }; DoubleLinkList.prototype.shift = function shift () { if (this.size === 0) { return null; } else if (this.size === 1) { this.tailNode = null; } var temp = this.headNode; this.headNode = temp.Next; this.size--; return temp; }; DoubleLinkList.prototype.pop = function pop () { var this$1 = this; var temp = this.headNode; var result; var prevNode; if (this.size === 0) { return null; } else if (this.size === 1) { result = this.headNode; this.emptyList(); } else { while (temp) { if (!temp.Next) { result = temp; this$1.tailNode = prevNode; prevNode.setNext(null); break; } prevNode = temp; temp = temp.Next; } this.size--; } return result; }; DoubleLinkList.prototype.__iterate = function __iterate (fn) { var this$1 = this; var temp = this.headNode, index = 0; while (temp) { fn(temp, index); index++; var nextNode = temp.Next; if (!nextNode || nextNode === this$1.headNode) { break; } temp = nextNode; } }; DoubleLinkList.prototype.toString = function toString () { return this.toArray().map(function (node) { return node.toString(); }).toString(); }; DoubleLinkList.fromArray = function fromArray (arr) { if (!arr) { return new DoubleLinkList(); } var linkList = new DoubleLinkList(); arr.forEach(function (item) { linkList.append(item); }); return linkList; }; Object.defineProperties( DoubleLinkList.prototype, prototypeAccessors ); return DoubleLinkList; }(Collection)); var LinkNode = function LinkNode(value, next) { if ( next === void 0 ) next = null; this.value = value; this.next = next; }; var prototypeAccessors$2 = { Value: { configurable: true },Next: { configurable: true } }; prototypeAccessors$2.Value.get = function () { return this.value; }; prototypeAccessors$2.Next.get = function () { return this.next; }; LinkNode.prototype.setValue = function setValue (value) { this.value = value; }; LinkNode.prototype.setNext = function setNext (node) { this.next = node; }; LinkNode.prototype.toString = function toString () { return ("" + (this.value)); }; Object.defineProperties( LinkNode.prototype, prototypeAccessors$2 ); var LinkList = (function (Collection$$1) { function LinkList() { Collection$$1.call(this); this.headNode = null; this.tailNode = null; this.size = 0; } if ( Collection$$1 ) LinkList.__proto__ = Collection$$1; LinkList.prototype = Object.create( Collection$$1 && Collection$$1.prototype ); LinkList.prototype.constructor = LinkList; var prototypeAccessors = { Size: { configurable: true } }; prototypeAccessors.Size.get = function () { return this.size; }; LinkList.prototype.append = function append (value) { this.size++; if (!this.headNode) { this.headNode = this.tailNode = new LinkNode(value); return this.headNode; } if (this.headNode === this.tailNode) { this.tailNode = new LinkNode(value); this.headNode.setNext(this.tailNode); return this.headNode; } var tailNode = new LinkNode(value); this.tailNode.setNext(tailNode); this.tailNode = tailNode; return this.headNode; }; LinkList.prototype.prepend = function prepend (value) { if (!this.headNode) { this.headNode = this.tailNode = new LinkNode(value); } else { this.headNode = new LinkNode(value, this.headNode); } this.size++; return this.headNode; }; LinkList.prototype.emptyList = function emptyList () { this.headNode = this.tailNode = null; this.size = 0; }; LinkList.prototype.clear = function clear () { this.emptyList(); }; LinkList.prototype.deleteNode = function deleteNode (arg) { var this$1 = this; var temp = this.headNode; var result = false; var prevNode; while (temp) { var match = typeof arg === "function" ? arg(temp.Value) : (temp.Value === arg); if (match) { this$1.size--; result = true; if (temp === this$1.headNode) { this$1.headNode = temp.Next; } else if (temp === this$1.tailNode) { prevNode.setNext(null); this$1.tailNode = prevNode; } else { prevNode.setNext(temp.Next); } } if (temp.Next && temp.Next === this$1.headNode) { break; } if (this$1.size === 0) { this$1.emptyList(); break; } prevNode = temp; temp = temp.Next; } return result; }; LinkList.prototype.findNode = function findNode (arg) { var temp = this.headNode; var result; while (temp) { var match = typeof arg === "function" ? arg(temp.Value) : (temp.Value === arg); if (match) { result = temp; break; } temp = temp.Next; } return result; }; LinkList.prototype.insertAfter = function insertAfter (value, oriNode) { var newNode = new LinkNode(value); if (oriNode) { var nextNode = oriNode.Next; if (!nextNode || nextNode === this.headNode) { this.tailNode = newNode; } newNode.setNext(nextNode); oriNode.setNext(newNode); this.size++; return true; } return false; }; LinkList.prototype.getHeadNode = function getHeadNode () { return this.headNode; }; LinkList.prototype.getTailNode = function getTailNode () { return this.tailNode; }; LinkList.prototype.shift = function shift () { if (this.size === 0) { return null; } else if (this.size === 1) { this.tailNode = null; } var temp = this.headNode; this.headNode = temp.Next; this.size--; return temp; }; LinkList.prototype.pop = function pop () { var this$1 = this; var temp = this.headNode; var result; var prevNode; if (this.size === 0) { return null; } if (this.size === 1) { result = this.headNode; this.emptyList(); return result; } while (temp) { var nextNode = temp.Next; if (!nextNode || nextNode === this$1.headNode) { result = temp; this$1.tailNode = prevNode; prevNode.setNext(nextNode); break; } prevNode = temp; temp = nextNode; } this.size--; return result; }; LinkList.prototype.__iterate = function __iterate (fn) { var this$1 = this; var temp = this.headNode, index = 0; while (temp) { fn(temp, index); index++; var nextNode = temp.Next; if (!nextNode || nextNode === this$1.headNode) { break; } temp = nextNode; } }; LinkList.prototype.toString = function toString () { return this.toArray().map(function (node) { return node.toString(); }).toString(); }; LinkList.fromArray = function fromArray (arr) { if (!arr) { return new LinkList(); } var linkList = new LinkList(); arr.forEach(function (item) { linkList.append(item); }); return linkList; }; LinkList.prototype.toDoubleLinkList = function toDoubleLinkList () { if (!this.headNode) { return new DoubleLinkList(); } var arr = this.toArray(); var doubleListList = new DoubleLinkList(); arr.forEach(function (item) { doubleListList.append(item.Value); }); return doubleListList; }; LinkList.prototype.toCycleLinkList = function toCycleLinkList () { var cyclelinklist = new CycleLinkList(); this.toArray().forEach(function (node) { cyclelinklist.append(node.Value); }); return cyclelinklist; }; Object.defineProperties( LinkList.prototype, prototypeAccessors ); return LinkList; }(Collection)); var CycleLinkList = function CycleLinkList() { this.linklist = new LinkList(); }; var prototypeAccessors$3 = { Size: { configurable: true } }; CycleLinkList.prototype.setCircle = function setCircle () { this.getTailNode().setNext(this.getHeadNode()); }; prototypeAccessors$3.Size.get = function () { return this.linklist.Size; }; CycleLinkList.prototype.append = function append (value) { var result = this.linklist.append(value); this.setCircle(); return result; }; CycleLinkList.prototype.prepend = function prepend (value) { var result = this.linklist.prepend(value); this.setCircle(); return result; }; CycleLinkList.prototype.deleteNode = function deleteNode (arg) { var isFirstOrLast = this.linklist.findNode(arg) === this.getHeadNode() || this.linklist.findNode(arg) === this.getTailNode(), result = this.linklist.deleteNode(arg); if (isFirstOrLast) { this.setCircle(); } return result; }; CycleLinkList.prototype.findNode = function findNode (arg) { return this.linklist.findNode(arg); }; CycleLinkList.prototype.getHeadNode = function getHeadNode () { return this.linklist.getHeadNode(); }; CycleLinkList.prototype.getTailNode = function getTailNode () { return this.linklist.getTailNode(); }; CycleLinkList.prototype.shift = function shift () { var result = this.linklist.shift(); if (this.Size) { this.setCircle(); } return result; }; CycleLinkList.prototype.pop = function pop () { var result = this.linklist.pop(); if (this.Size) { this.setCircle(); } return result; }; CycleLinkList.prototype.insertAfter = function insertAfter (value, oriNode) { return this.linklist.insertAfter(value, oriNode); }; CycleLinkList.prototype.clear = function clear () { this.linklist.clear(); }; CycleLinkList.prototype.toString = function toString () { return this.linklist.toString(); }; CycleLinkList.fromArray = function fromArray (arr) { if (!arr) { return new CycleLinkList(); } var linkList = new CycleLinkList(); arr.forEach(function (item) { linkList.append(item); }); return linkList; }; CycleLinkList.prototype.toArray = function toArray () { return this.linklist.toArray(); }; CycleLinkList.prototype.getEnumerator = function getEnumerator () { var temp = this.getHeadNode(); var enumerator = { next: function () { temp = temp.Next; enumerator.Current = { value: temp.Value, done: false, }; return enumerator; }, Current: { value: temp.Value, done: false, }, }; return enumerator; }; Object.defineProperties( CycleLinkList.prototype, prototypeAccessors$3 ); var DoubleLinkListNode = function DoubleLinkListNode(value) { this.value = value; }; var prototypeAccessors$4 = { Next: { configurable: true },Prev: { configurable: true } }; DoubleLinkListNode.prototype.setNext = function setNext (node) { this.next = node; }; DoubleLinkListNode.prototype.setPre = function setPre (node) { this.pre = node; }; prototypeAccessors$4.Next.get = function () { return this.next; }; prototypeAccessors$4.Prev.get = function () { return this.pre; }; DoubleLinkListNode.prototype.toString = function toString () { return ("" + (this.value)); }; Object.defineProperties( DoubleLinkListNode.prototype, prototypeAccessors$4 ); var DoubleLinkListCycle = function DoubleLinkListCycle() { this.headNode = null; this.tailNode = null; this.size = 0; }; var prototypeAccessors$5 = { Size: { configurable: true } }; prototypeAccessors$5.Size.get = function () { return this.size; }; DoubleLinkListCycle.prototype.append = function append (node) { var currentNode = new DoubleLinkListNode(node); if (!this.tailNode) { this.headNode = this.tailNode = currentNode; this.headNode.setNext(this.tailNode); this.tailNode.setPre(this.headNode); } else { currentNode.setPre(this.tailNode); currentNode.setNext(this.headNode); this.tailNode.setNext(currentNode); this.headNode.setPre(currentNode); this.tailNode = currentNode; } this.size++; return this; }; DoubleLinkListCycle.prototype.prepend = function prepend (node) { var currentNode = new DoubleLinkListNode(node); if (!this.headNode) { this.headNode = this.tailNode = currentNode; this.headNode.setNext(this.tailNode); this.tailNode.setPre(this.headNode); } else { this.headNode.setPre(currentNode); currentNode.setNext(this.headNode); currentNode.setPre(this.tailNode); this.tailNode.setNext(currentNode); this.headNode = currentNode; } this.size++; return this; }; DoubleLinkListCycle.prototype.emptyList = function emptyList () { this.headNode = this.tailNode = null; this.size = 0; }; DoubleLinkListCycle.prototype.shift = function shift () { var result = this.headNode; if (this.headNode === this.tailNode) { this.emptyList(); } else { this.headNode = this.headNode.next; this.headNode.setPre(this.tailNode); this.size--; } return result; }; DoubleLinkListCycle.prototype.pop = function pop () { var result = this.tailNode; if (this.headNode === this.tailNode) { this.emptyList(); } else { this.tailNode = this.tailNode.Prev; this.tailNode.setNext(this.headNode); this.size--; } return result; }; DoubleLinkListCycle.prototype.deleteNode = function deleteNode (arg) { var this$1 = this; var deleteArr = []; if (this.isEmpty()) { return deleteArr; } var cycleNode = this.headNode; var index = 0; while (cycleNode) { var match = typeof arg === "function" ? arg(cycleNode.value) : (cycleNode.value === arg); var deleteNode = null; if (match) { if (this$1.headNode === this$1.tailNode) { this$1.emptyList(); break; } else { cycleNode.Prev.setNext(cycleNode.Next); cycleNode.Next.setPre(cycleNode.Prev); } deleteNode = cycleNode; deleteArr.push(index); } cycleNode = cycleNode.Next; var shouldBreak = cycleNode === this$1.headNode; if (deleteNode) { if (deleteNode === this$1.headNode) { this$1.headNode = deleteNode.Next; } if (deleteNode === this$1.tailNode) { this$1.tailNode = deleteNode.Prev; } deleteNode.setNext(null); deleteNode.setPre(null); } if (shouldBreak) { break; } index++; } }; DoubleLinkListCycle.prototype.findNode = function findNode (arg) { var this$1 = this; var cycleNode = this.headNode; var result = null; while (cycleNode) { var match = typeof arg === "function" ? arg(cycleNode.value) : (cycleNode.value === arg); if (match) { result = cycleNode; break; } else if (cycleNode === this$1.tailNode) { break; } cycleNode = cycleNode.Next; } return result; }; DoubleLinkListCycle.prototype.getHeadNode = function getHeadNode () { return this.headNode; }; DoubleLinkListCycle.prototype.getTailNode = function getTailNode () { return this.tailNode; }; DoubleLinkListCycle.prototype.isEmpty = function isEmpty () { return !this.Size; }; DoubleLinkListCycle.prototype.toString = function toString () { var this$1 = this; var temp = this.headNode; var arr = []; while (temp) { arr.push(temp); temp = temp.Next; if (temp === this$1.headNode) { break; } } return arr.toString(); }; DoubleLinkListCycle.prototype.getEnumerator = function getEnumerator () { var temp = this.getHeadNode(); var enumerator = { next: function () { temp = temp.Next; enumerator.Current = { value: temp.value, done: false, }; return enumerator; }, Current: { value: temp.value, done: false, }, }; return enumerator; }; Object.defineProperties( DoubleLinkListCycle.prototype, prototypeAccessors$5 ); var Queue = (function (Collection$$1) { function Queue() { Collection$$1.call(this); this.linkList = new LinkList(); } if ( Collection$$1 ) Queue.__proto__ = Collection$$1; Queue.prototype = Object.create( Collection$$1 && Collection$$1.prototype ); Queue.prototype.constructor = Queue; Queue.prototype.isEmpty = function isEmpty () { return !this.linkList.getTailNode(); }; Queue.prototype.peek = function peek () { if (!this.linkList.getHeadNode()) { return null; } return this.linkList.getHeadNode().Value; }; Queue.prototype.enqueue = function enqueue (value) { this.linkList.append(value); }; Queue.prototype.dequeue = function dequeue () { var head = this.linkList.shift(); return head ? head.Value : null; }; Queue.prototype.toString = function toString () { return this.linkList.toString(); }; Queue.prototype.__iterate = function __iterate (fn) { var temp = this.linkList.getHeadNode(), index = 0; while (temp) { fn(temp, index); index++; temp = temp.Next; } }; return Queue; }(Collection)); var SkipListNode = function SkipListNode(item) { if ( item === void 0 ) item = null; this.item = item; this.next = []; this.prev = []; }; SkipListNode.prototype.getItem = function getItem () { return this.item; }; SkipListNode.prototype.getNext = function getNext (level) { return this.next[level]; }; SkipListNode.prototype.setNext = function setNext (level, node) { this.next[level] = node; }; SkipListNode.prototype.getPrev = function getPrev (level) { return this.prev[level]; }; SkipListNode.prototype.setPrev = function setPrev (level, node) { this.prev[level] = node; }; SkipListNode.prototype.deleteLastLevel = function deleteLastLevel () { this.next.length--; }; SkipListNode.prototype.getNextLevel = function getNextLevel () { return this.next.length; }; SkipListNode.prototype.getPrevLevel = function getPrevLevel () { return this.prev.length; }; SkipListNode.prototype.getHeight = function getHeight () { return Math.max(this.getPrevLevel(), this.getNextLevel()); }; var SkipList = function SkipList(compareKey) { this.compareKey = compareKey; this.level = 0; this.count = 0; this.head = new SkipListNode(); }; var prototypeAccessors$6 = { Level: { configurable: true },Count: { configurable: true },Head: { configurable: true } }; prototypeAccessors$6.Level.get = function () { return this.level; }; prototypeAccessors$6.Count.get = function () { return this.count; }; prototypeAccessors$6.Head.get = function () { return this.head; }; SkipList.prototype.isEmpty = function isEmpty () { return this.count === 0; }; SkipList.prototype.randomLevel = function randomLevel () { var k = 0; var random = parseInt((Math.random() * 10).toString(), 10); while (random % 2 === 0) { k++; random = parseInt((Math.random() * 10).toString(), 10); } return k > this.level ? this.level : k; }; SkipList.prototype.findNode = function findNode (item) { var obj; var result = null; var temp = this.head; for (var i = this.level - 1; i >= 0; i--) { while (temp.getNext(i) && this.compare(temp.getNext(i).getItem(), this.compareKey ? ( obj = {}, obj[this.compareKey] = item, obj ) : item)) { temp = temp.getNext(i); } } if (!temp.getNext(0)) { return result; } var isEqual = false; if (this.compareKey) { isEqual = temp.getNext(0).getItem()[this.compareKey] === item; } else { isEqual = temp.getNext(0).getItem() === item; } if (isEqual) { result = temp.getNext(0); } return result; }; SkipList.prototype.insert = function insert (item) { var updateNodes = this.findUpdateNodes(item); if (updateNodes[0] && updateNodes[0].getNext(0) && updateNodes[0].getNext(0).getItem() === item) { return this; } var level = this.randomLevel(); if (level === this.level) { updateNodes[level] = this.head; this.level++; } this.insertNode(new SkipListNode(item), updateNodes, level); this.count++; return this; }; SkipList.prototype.remove = function remove (item) { var this$1 = this; var node = this.findNode(item); if (node) { var height = node.getHeight(); for (var i = 0; i < height; i++) { var prev = node.getPrev(i); var next = node.getNext(i); prev.setNext(i, next); if (next) { next.setPrev(i, prev); } } while (this.level && !this.head.getNext(this.level - 1)) { this$1.head.deleteLastLevel(); this$1.level--; } this.count--; } return this; }; SkipList.prototype.getSkipTables = function getSkipTables () { var this$1 = this; var table = []; for (var index = 0; index < this.level; index++) { var levelTables = []; var temp = this$1.head; while (temp = temp.getNext(index)) { levelTables.push(temp); } table[index] = levelTables; } return table; }; SkipList.prototype.toString = function toString () { var tables = this.getSkipTables(); return tables.reverse().reduce(function (ori, item) { ori.push(item.map(function (node) { return node.getItem().toString(); }).toString()); return ori; }, []).join("\n"); }; SkipList.prototype.compare = function compare (a, b) { if (this.compareKey) { return a[this.compareKey] < b[this.compareKey]; } return a < b; }; SkipList.prototype.findUpdateNodes = function findUpdateNodes (item) { var this$1 = this; var updateNodes = []; for (var i = this.level - 1; i >= 0; i--) { var tempNode = this$1.head.getNext(i); var prevNode = null; while (tempNode && this.compare(tempNode.getItem(), item)) { prevNode = tempNode; tempNode = tempNode.getNext(i); } if (tempNode) { updateNodes[i] = tempNode.getPrev(i); } else { updateNodes[i] = prevNode; } } return updateNodes; }; SkipList.prototype.insertNode = function insertNode (node, updateNodes, level) { for (var i = level; i >= 0; i--) { var nextTemp = updateNodes[i].getNext(i); if (nextTemp) { nextTemp.setPrev(i, node); node.setNext(i, nextTemp); } updateNodes[i].setNext(i, node); node.setPrev(i, updateNodes[i]); } }; Object.defineProperties( SkipList.prototype, prototypeAccessors$6 ); var Stack = (function (Collection$$1) { function Stack() { Collection$$1.call(this); this.linkList = new LinkList(); } if ( Collection$$1 ) Stack.__proto__ = Collection$$1; Stack.prototype = Object.create( Collection$$1 && Collection$$1.prototype ); Stack.prototype.constructor = Stack; Stack.prototype.push = function push (node) { return this.linkList.append(node); }; Stack.prototype.pop = function pop () { var node = this.linkList.pop(); if (node) { return node.Value; } return null; }; Stack.prototype.peek = function peek () { if (!this.linkList.getTailNode()) { return null; } return this.linkList.getTailNode().Value; }; Stack.prototype.isEmpty = function isEmpty () { return !this.linkList.getTailNode(); }; Stack.prototype.toString = function toString () { return this.linkList.toString(); }; Stack.prototype.__iterate = function __iterate (fn) { var temp = this.linkList.getHeadNode(), index = 0; while (temp) { fn(temp.Value, index); index++; temp = temp.Next; } }; Stack.prototype.toArray = function toArray () { return Collection$$1.prototype.toArray.call(this).reverse(); }; return Stack; }(Collection)); function compareFn(a, b) { return a <= b; } var BinomialHeap = function BinomialHeap(compare) { if ( compare === void 0 ) compare = compareFn; this.compare = compare; this.count = 0; }; var prototypeAccessors$7 = { Count: { configurable: true },Head: { configurable: true } }; prototypeAccessors$7.Count.get = function () { return this.count; }; prototypeAccessors$7.Head.get = function () { return this.head; }; BinomialHeap.prototype.setHead = function setHead (value) { this.head = new LinkNode({ value: value, degree: 0, }); this.count = 1; }; BinomialHeap.prototype.clear = function clear () { this.head = null; this.count = 0; }; BinomialHeap.prototype.isEmpty = function isEmpty () { return !this.head; }; BinomialHeap.prototype.insert = function insert (value) { var heap = new BinomialHeap(); heap.setHead(value); var newNode = heap.Head; this.union(heap); return newNode; }; BinomialHeap.prototype.deleteExtremum = function deleteExtremum () { if (!this.head) { return null; } var deleteNode = this._findExtremum(); if (!deleteNode.minPrev) { this.head = deleteNode.minNode.Next; } else { deleteNode.minPrev.setNext(deleteNode.minNode.Next); } var child = deleteNode.minNode.Value.child; var newHead = child; while (child) { child.Value.parent = null; child = child.Next; } var heap = new BinomialHeap(this.compare); heap.head = newHead; this.union(heap); this.count--; return deleteNode.minNode.Value.value; }; BinomialHeap.prototype._findExtremum = function _findExtremum () { var this$1 = this; var next = this.head.Next; var minNode = this.head; var prev = this.head; var minPrev = null; var min = minNode.Value.value; while (next) { if (!this$1.compare(min, next.Value.value)) { minPrev = prev; minNode = next; min = minNode.Value.value; } prev = next; next = next.Next; } return { minNode: minNode, minPrev: minPrev }; }; BinomialHeap.prototype.findExtremum = function findExtremum () { if (!this.head) { return null; } return this._findExtremum().minNode.Value.value; }; BinomialHeap.prototype.union = function union (heap) { var this$1 = this; if (!heap) { return this; } this.count += heap.Count; var newHead = this.mergeHeaps(heap); if (!newHead) { return this; } this.head = null; heap.head = null; var prev; var curr = newHead; var next = newHead.Next; while (next) { if (next.Value.degree !== curr.Value.degree || (next.Next && next.Next.Value.degree === curr.Value.degree)) { prev = curr; curr = next; } else { if (this$1.compare(curr.Value.value, next.Value.value)) { curr.setNext(next.Next); this$1.link(curr, next); } else { if (!prev) { newHead = next; } else { prev.setNext(next); } this$1.link(next, curr); curr = next; } } next = curr.Next; } this.head = newHead; return this; }; BinomialHeap.prototype.link = function link (tomerge, frommerge) { frommerge.setNext(tomerge.Value.child); frommerge.Value.parent = tomerge; tomerge.Value.child = frommerge; tomerge.Value.degree++; }; BinomialHeap.prototype.mergeHeaps = function mergeHeaps (heap) { var thisHead = this.head; var thatHead = heap.Head; if (!thisHead) { return heap.head; } if (!thatHead) { return this.head; } var newHead; if (thisHead.Value.degree <= thatHead.Value.degree) { newHead = this.head; thisHead = thisHead.Next; } else { newHead = heap.head; thatHead = thatHead.Next; } var temp = newHead; while (thisHead && thatHead) { if (thisHead.Value.degree <= thatHead.Value.degree) { temp.setNext(thisHead); thisHead = thisHead.Next; } else { temp.setNext(thatHead); thatHead = thatHead.Next; } temp = temp.Next; } temp.setNext(thisHead ? thisHead : thatHead); return newHead; }; Object.defineProperties( BinomialHeap.prototype, prototypeAccessors$7 ); var Heap = function Heap() { this.container = []; }; var prototypeAccessors$8 = { Size: { configurable: true } }; prototypeAccessors$8.Size.get = function () { return this.container.length; }; Heap.prototype.getLeftChildIndex = function getLeftChildIndex (parent) { return (2 * parent) + 1; }; Heap.prototype.getRigthChildIndex = function getRigthChildIndex (parent) { return (2 * parent) + 2; }; Heap.prototype.getParentIndex = function getParentIndex (index) { return Math.floor((index - 1) / 2); }; Heap.prototype.getLeftChild = function getLeftChild (parent) { return this.container[this.getLeftChildIndex(parent)]; }; Heap.prototype.getRightChild = function getRightChild (parent) { return this.container[this.getRigthChildIndex(parent)]; }; Heap.prototype.getParent = function getParent (index) { return this.container[this.getParentIndex(index)]; }; Heap.prototype.hasLeftChild = function hasLeftChild (parent) { return this.getLeftChildIndex(parent) < this.container.length; }; Heap.prototype.hasRightChild = function hasRightChild (parent) { return this.getRigthChildIndex(parent) < this.container.length; }; Heap.prototype.hasParent = function hasParent (index) { return this.getParentIndex(index) >= 0; }; Heap.prototype.swap = function swap (indexOne, indexTwo) { var temp = this.container[indexTwo]; this.container[indexTwo] = this.container[indexOne]; this.container[indexOne] = temp; }; Heap.prototype.heapifyUp = function heapifyUp (customStartIndex) { var this$1 = this; var currentIndex = customStartIndex || this.container.length - 1; while (this.hasParent(currentIndex) && !this.compare(this.getParent(currentIndex), this.container[currentIndex])) { this$1.swap(currentIndex, this$1.getParentIndex(currentIndex)); currentIndex = this$1.getParentIndex(currentIndex); } }; Heap.prototype.heapifyDown = function heapifyDown (customStartIndex) { var this$1 = this; var currentIndex = customStartIndex || 0; var nextIndex = null; while (this.hasLeftChild(currentIndex)) { if (this$1.hasRightChild(currentIndex) && this$1.compare(this$1.getRightChild(currentIndex), this$1.getLeftChild(currentIndex))) { nextIndex = this$1.getRigthChildIndex(currentIndex); } else { nextIndex = this$1.getLeftChildIndex(currentIndex); } if (this$1.compare(this$1.container[currentIndex], this$1.container[nextIndex])) { break; } this$1.swap(currentIndex, nextIndex); currentIndex = nextIndex; } }; Heap.prototype.poll = function poll () { if (this.container.length === 0) { return null; } if (this.container.length === 1) { return this.container.pop(); } var item = this.container[0]; this.container[0] = this.container.pop(); this.heapifyDown(); return item; }; Heap.prototype.peek = function peek () { if (this.container.length === 0) { return null; } return this.container[0]; }; Heap.prototype.add = function add (item) { this.container.push(item); this.heapifyUp(); return this; }; Heap.prototype.remove = function remove (item) { var this$1 = this; var numberOfItemsToRemove = this.findAll(item).length; for (var iteration = 0; iteration < numberOfItemsToRemove; iteration += 1) { var indexToRemove = this$1.findAllIndex(item).pop(); if (indexToRemove === (this$1.container.length - 1)) { this$1.container.pop(); } else { this$1.container[indexToRemove] = this$1.container.pop(); var parentItem = this$1.getParent(indexToRemove); if (this$1.hasLeftChild(indexToRemove) && (!parentItem || this$1.compare(parentItem, this$1.container[indexToRemove]))) { this$1.heapifyDown(indexToRemove); } else { this$1.heapifyUp(indexToRemove); } } } return numberOfItemsToRemove > 0; }; Heap.prototype.toString = function toString () { return this.container.toString(); }; Heap.prototype.isEmpty = function isEmpty () { return !this.container.length; }; Heap.prototype.find = function find (arg) { var this$1 = this; var temp = null; for (var index = 0; index < this.container.length; index++) { var element = this$1.container[index]; var match = typeof arg === "function" ? arg(element) : arg === element; if (match) { temp = element; break; } } return temp; }; Heap.prototype.findAll = function findAll (arg) { var temp = []; this.container.forEach(function (item) { var match = typeof arg === "function" ? arg(item) : arg === item; if (match) { temp.push(item); } }); return temp; }; Heap.prototype.clear = function clear () { this.container.length = 0; }; Heap.prototype.entries = function entries () { return [].concat( this.container ); }; Heap.prototype.findAllIndex = function findAllIndex (arg) { var temp = []; this.container.forEach(function (item, index) { var match = typeof arg === "function" ? arg(item) : arg === item; if (match) { temp.push(index); } }); return temp; }; Object.defineProperties( Heap.prototype, prototypeAccessors$8 ); var MaxHeap = (function (Heap$$1) { function MaxHeap(key) { Heap$$1.call(this); this.key = key; } if ( Heap$$1 ) MaxHeap.__proto__ = Heap$$1; MaxHeap.prototype = Object.create( Heap$$1 && Heap$$1.prototype ); MaxHeap.prototype.constructor = MaxHeap; MaxHeap.prototype.compare = function compare (a, b) { if (this.key) { return a[this.key] >= b[this.key]; } return a >= b; }; return MaxHeap; }(Heap)); var MinHeap = (function (Heap$$1) { function MinHeap(key) { Heap$$1.call(this); this.key = key; } if ( Heap$$1 ) MinHeap.__proto__ = Heap$$1; MinHeap.prototype = Object.create( Heap$$1 && Heap$$1.prototype ); MinHeap.prototype.constructor = MinHeap; MinHeap.prototype.compare = function compare (a, b) { if (this.key) { return a[this.key] <= b[this.key]; } return a <= b; }; return MinHeap; }(Heap)); var BasicBinaryTree = function BasicBinaryTree () {}; BasicBinaryTree.preTraversal = function preTraversal (tree) { var arr = []; this._preTraversal(tree, function (item) { arr.push(item); }); return arr; }; BasicBinaryTree._preTraversal = function _preTraversal (tree, fn) { if (!tree) { return; } fn(tree.Value); this._preTraversal(tree.Left, fn); this._preTraversal(tree.Right, fn); }; BasicBinaryTree.inTraversal = function inTraversal (tree) { var arr = []; this._inTraversal(tree, function (item) { arr.push(item); }); return arr; }; BasicBinaryTree._inTraversal = function _inTraversal (tree, fn) { if (!tree) { return; } this._inTraversal(tree.Left, fn); fn(tree.Value); this._inTraversal(tree.Right, fn); }; BasicBinaryTree.postOrderTraversal = function postOrderTraversal (tree) { var arr = []; this._postOrderTraversal(tree, function (item) { arr.push(item); }); return arr; }; BasicBinaryTree._postOrderTraversal = function _postOrderTraversal (tree, fn) { if (!tree) { return; } this._postOrderTraversal(tree.Left, fn); this._postOrderTraversal(tree.Right, fn); fn(tree.Value); }; BasicBinaryTree.getHeight = function getHeight (tree) { if (!tree) { return 0; } var length = 1; length += Math.max(this.getHeight(tree.Left), this.getHeight(tree.Right)); return length; }; var BasicBinaryTreeNode = function BasicBinaryTreeNode(value) { this.value = value; }; var prototypeAccessors$9 = { Value: { configurable: true },Left: { configurable: true },Right: { configurable: true } }; prototypeAccessors$9.Value.get = function () { return this.value; }; prototypeAccessors$9.Left.get = function () { return this.left; }; prototypeAccessors$9.Right.get = function () { return this.right; }; BasicBinaryTreeNode.prototype.setValue = function setValue (value) { this.value = value; }; BasicBinaryTreeNode.prototype.setLeft = function setLeft (node) { this.left = node; if (node) { this.left.parent = this; } return this; }; BasicBinaryTreeNode.prototype.setRight = function setRight (node) { this.right = node; if (node) { this.right.parent = this; } return this; }; BasicBinaryTreeNode.prototype.removeChild = function removeChild (node) { if (node === this.left) { this.left.parent = null; this.left = null; return true; } if (node === this.right) { this.right.parent = null; this.right = null; return true; } return false; }; BasicBinaryTreeNode.prototype.getHeight = function getHeight () { return Math.max(this.getLeftHeight(), this.getRightHeight()); }; BasicBinaryTreeNode.prototype.getRightHeight = function getRightHeight () { if (!this.right) { return 0; } return this.right.getHeight() + 1; }; BasicBinaryTreeNode.prototype.getLeftHeight = function getLeftHeight () { if (!this.left) { return 0; } return this.left.getHeight() + 1; }; BasicBinaryTreeNode.prototype.balanceFactor = function balanceFactor () { return this.getLeftHeight() - this.getRightHeight(); }; BasicBinaryTreeNode.prototype.getSibling = function getSibling () { if (!this.parent) { return; } if (this.parent.Left === this) { return this.parent.Right; } return this.parent.Left; }; BasicBinaryTreeNode.prototype.getUncle = function getUncle () { if (!this.parent) { return; } if (!this.parent.parent) { return; } var parent = this.parent; if (parent.parent.Left === parent) { return parent.parent.Right; } return parent.parent.Left; }; BasicBinaryTreeNode.prototype.toString = function toString () { return BasicBinaryTree.inTraversal(this).toString(); }; Object.defineProperties( BasicBinaryTreeNode.prototype, prototypeAccessors$9 ); function compareFn$1(a, b) { return a <= b; } var LeftistTreeNode = (function (BasicBinaryTreeNode$$1) { function LeftistTreeNode(value, rank) { BasicBinaryTreeNode$$1.call(this, value); this.rank = rank; } if ( BasicBinaryTreeNode$$1 ) LeftistTreeNode.__proto__ = BasicBinaryTreeNode$$1; LeftistTreeNode.prototype = Object.create( BasicBinaryTreeNode$$1 && BasicBinaryTreeNode$$1.prototype ); LeftistTreeNode.prototype.constructor = LeftistTreeNode; var prototypeAccessors = { Rank: { configurable: true } }; prototypeAccessors.Rank.set = function (rank) { this.rank = rank; }; prototypeAccessors.Rank.get = function () { return this.rank; }; Object.defineProperties( LeftistTreeNode.prototype, prototypeAccessors ); return LeftistTreeNode; }(BasicBinaryTreeNode)); var LeftistTree = function LeftistTree(compare, value) { if ( compare === void 0 ) compare = compareFn$1; this.compare = compare; this.count = 0; if (typeof value !== "undefined") { this.root = new LeftistTreeNode(value, 0); this.count = 1; } }; var prototypeAccessors$1$1 = { Root: { configurable: true },Count: { configurable: true } }; prototypeAccessors$1$1.Root.get = function () { return this.root; }; prototypeAccessors$1$1.Count.get = function () { return this.count; }; LeftistTree.prototype.isEmpty = function isEmpty () { return !this.root; }; LeftistTree.prototype.fixNode = function fixNode (node) { var left = node.Left; var right = node.Right; if (left && right && left.Rank < right.Rank) { var temp = node.Right; node.setRight(node.Left); node.setLeft(temp); } else if (node.Right && !node.Left) { node.setLeft(node.Right); node.setRight(null); } if (node.Right) { node.Rank = node.Right.Rank + 1; } else { node.Rank = 0; } }; LeftistTree.prototype._merge = function _merge (root1, root2) { if (!root1) { return root2; } if (!root2) { return root1; } if (!this.compare(root1.Value, root2.Value)) { var temp = root2; root2 = root1; root1 = temp; } root1.setRight(this._merge(root1.Right, root2)); this.fixNode(root1); return root1; }; LeftistTree.prototype.merge = function merge (tree2) { if (!tree2 || tree2.isEmpty()) { return this; } if (!this.root) { this.root = tree2.Root; this.count = tree2.Count; return this; } var root1 = this.Root; var root2 = tree2.Root; this.root = this._merge(root1, root2); this.count += tree2.Count; return this; }; LeftistTree.prototype.findExtremum = function findExtremum () { if (!this.root) { return null; } return this.root.Value; }; LeftistTree.prototype.insert = function insert (value) { var node = new LeftistTree(this.compare, value); this.merge(node); return node; }; LeftistTree.prototype.deleteExtremum = function deleteExtremum () { if (!this.root) { return null; } var value = this.root.Value; this.root = this._merge(this.root.Left, this.root.Right); this.count--; return value; }; Object.defineProperties( LeftistTree.prototype, prototypeAccessors$1$1 ); var PriorityQueueNode = function PriorityQueueNode(value, priority) { this.value = value; this.priority = priority; }; var prototypeAccessors$