diff --git a/examples/PacktDataStructuresAlgorithms.min.js b/examples/PacktDataStructuresAlgorithms.min.js index b06c53f5..c4644f16 100644 --- a/examples/PacktDataStructuresAlgorithms.min.js +++ b/examples/PacktDataStructuresAlgorithms.min.js @@ -1 +1,2 @@ -!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("PacktDataStructuresAlgorithms",[],t):"object"==typeof exports?exports.PacktDataStructuresAlgorithms=t():e.PacktDataStructuresAlgorithms=t()}("undefined"!=typeof self?self:this,function(){return function(e){function t(r){if(n[r])return n[r].exports;var i=n[r]={i:r,l:!1,exports:{}};return e[r].call(i.exports,i,i.exports,t),i.l=!0,i.exports}var n={};return t.m=e,t.c=n,t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:r})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=14)}([function(e,t,n){var r,i,o;!function(n,u){i=[t],r=u,void 0!==(o="function"==typeof r?r.apply(t,i):r)&&(e.exports=o)}(0,function(e){"use strict";function t(e,t,n){var r=n(e,t);return r===l.LESS_THAN||r===l.EQUALS}function n(e,t,n){var r=n(e,t);return r===l.BIGGER_THAN||r===l.EQUALS}function r(e,t){return e===t?0:e0&&void 0!==arguments[0]?arguments[0]:n.defaultEquals;i(this,e),this.equalsFn=t,this.count=0,this.head=void 0}return o(e,[{key:"push",value:function(e){var t=new r.Node(e),n=void 0;if(null==this.head)this.head=t;else{for(n=this.head;null!=n.next;)n=n.next;n.next=t}this.count++}},{key:"getElementAt",value:function(e){if(e>=0&&e<=this.count){for(var t=this.head,n=0;n=0&&t<=this.count){var n=new r.Node(e);if(0===t){var i=this.head;n.next=i,this.head=n}else{var o=this.getElementAt(t-1);n.next=o.next,o.next=n}return this.count++,!0}return!1}},{key:"removeAt",value:function(e){if(e>=0&&e1&&void 0!==arguments[1]?arguments[1]:t.defaultCompare;if(e&&e.length>0){for(var r=e[0],i=1;i1&&void 0!==arguments[1]?arguments[1]:t.defaultCompare;if(e&&e.length>0){for(var r=e[0],i=1;i0)this.lowestCount--,this.items[this.lowestCount]=e;else{for(var t=this.count;t>0;t--)this.items[t]=this.items[t-1];this.count++,this.items[0]=e}}},{key:"addBack",value:function(e){this.items[this.count]=e,this.count++}},{key:"removeFront",value:function(){if(!this.isEmpty()){var e=this.items[this.lowestCount];return delete this.items[this.lowestCount],this.lowestCount++,e}}},{key:"removeBack",value:function(){if(!this.isEmpty()){this.count--;var e=this.items[this.count];return delete this.items[this.count],e}}},{key:"peekFront",value:function(){if(!this.isEmpty())return this.items[this.lowestCount]}},{key:"peekBack",value:function(){if(!this.isEmpty())return this.items[this.count-1]}},{key:"isEmpty",value:function(){return 0===this.size()}},{key:"clear",value:function(){this.items={},this.count=0,this.lowestCount=0}},{key:"size",value:function(){return this.count-this.lowestCount}},{key:"toString",value:function(){if(this.isEmpty())return"";for(var e=""+this.items[this.lowestCount],t=this.lowestCount+1;t0&&void 0!==arguments[0]?arguments[0]:n.defaultEquals;o(this,t);var r=u(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e));return r.tail=void 0,r}return a(t,e),l(t,[{key:"push",value:function(e){var t=new i.DoublyNode(e);null==this.head?(this.head=t,this.tail=t):(this.tail.next=t,t.prev=this.tail,this.tail=t),this.count++}},{key:"insert",value:function(e,t){if(t>=0&&t<=this.count){var n=new i.DoublyNode(e),r=this.head;if(0===t)null==this.head?(this.head=n,this.tail=n):(n.next=this.head,this.head.prev=n,this.head=n);else if(t===this.count)r=this.tail,r.next=n,n.prev=r,this.tail=n;else{var o=this.getElementAt(t-1);r=o.next,n.next=r,o.next=n,r.prev=n,n.prev=o}return this.count++,!0}return!1}},{key:"removeAt",value:function(e){if(e>=0&&e0&&void 0!==arguments[0]?arguments[0]:n.defaultToString;i(this,e),this.toStrFn=t,this.table={}}return o(e,[{key:"set",value:function(e,t){if(null!=e&&null!=t){var n=this.toStrFn(e);return this.table[n]=new r.ValuePair(e,t),!0}return!1}},{key:"get",value:function(e){var t=this.table[this.toStrFn(e)];return null==t?void 0:t.value}},{key:"hasKey",value:function(e){return null!=this.table[this.toStrFn(e)]}},{key:"remove",value:function(e){return!!this.hasKey(e)&&(delete this.table[this.toStrFn(e)],!0)}},{key:"values",value:function(){return this.keyValues().map(function(e){return e.value})}},{key:"keys",value:function(){return this.keyValues().map(function(e){return e.key})}},{key:"keyValues",value:function(){return Object.values(this.table)}},{key:"forEach",value:function(e){for(var t=this.keyValues(),n=0;n0&&void 0!==arguments[0]?arguments[0]:n.defaultCompare;i(this,e),this.compareFn=t,this.root=null}return o(e,[{key:"insert",value:function(e){null==this.root?this.root=new r.Node(e):this.insertNode(this.root,e)}},{key:"insertNode",value:function(e,t){this.compareFn(t,e.key)===n.Compare.LESS_THAN?null==e.left?e.left=new r.Node(t):this.insertNode(e.left,t):null==e.right?e.right=new r.Node(t):this.insertNode(e.right,t)}},{key:"getRoot",value:function(){return this.root}},{key:"search",value:function(e){return this.searchNode(this.root,e)}},{key:"searchNode",value:function(e,t){return null!=e&&(this.compareFn(t,e.key)===n.Compare.LESS_THAN?this.searchNode(e.left,t):this.compareFn(t,e.key)!==n.Compare.BIGGER_THAN||this.searchNode(e.right,t))}},{key:"inOrderTraverse",value:function(e){this.inOrderTraverseNode(this.root,e)}},{key:"inOrderTraverseNode",value:function(e,t){null!=e&&(this.inOrderTraverseNode(e.left,t),t(e.key),this.inOrderTraverseNode(e.right,t))}},{key:"preOrderTraverse",value:function(e){this.preOrderTraverseNode(this.root,e)}},{key:"preOrderTraverseNode",value:function(e,t){null!=e&&(t(e.key),this.preOrderTraverseNode(e.left,t),this.preOrderTraverseNode(e.right,t))}},{key:"postOrderTraverse",value:function(e){this.postOrderTraverseNode(this.root,e)}},{key:"postOrderTraverseNode",value:function(e,t){null!=e&&(this.postOrderTraverseNode(e.left,t),this.postOrderTraverseNode(e.right,t),t(e.key))}},{key:"min",value:function(){return this.minNode(this.root)}},{key:"minNode",value:function(e){for(var t=e;null!=t&&null!=t.left;)t=t.left;return t}},{key:"max",value:function(){return this.maxNode(this.root)}},{key:"maxNode",value:function(e){for(var t=e;null!=t&&null!=t.right;)t=t.right;return t}},{key:"remove",value:function(e){this.root=this.removeNode(this.root,e)}},{key:"removeNode",value:function(e,t){if(null==e)return null;if(this.compareFn(t,e.key)===n.Compare.LESS_THAN)return e.left=this.removeNode(e.left,t),e;if(this.compareFn(t,e.key)===n.Compare.BIGGER_THAN)return e.right=this.removeNode(e.right,t),e;if(null==e.left&&null==e.right)return e=null;if(null==e.left)return e=e.right;if(null==e.right)return e=e.left;var r=this.minNode(e.right);return e.key=r.key,e.right=this.removeNode(e.right,r.key),e}}]),e}();t.default=u,e.exports=t.default})},function(e,t,n){var r,i,o;!function(n,u){i=[t],r=u,void 0!==(o="function"==typeof r?r.apply(t,i):r)&&(e.exports=o)}(0,function(e){"use strict";function t(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0});var n=function(){function e(e,t){for(var n=0;n1&&void 0!==arguments[1]?arguments[1]:t.defaultCompare,r=e.length,i=void 0,o=1;o0&&n(e[u-1],i)===t.Compare.BIGGER_THAN;)e[u]=e[u-1],u--;e[u]=i}return e}})},function(e,t,n){var r,i,o;!function(u,a){i=[t,n(0)],r=a,void 0!==(o="function"==typeof r?r.apply(t,i):r)&&(e.exports=o)}(0,function(e,t){"use strict";function n(e,n,r,i){for(var o=e[Math.floor((r+n)/2)],u=n,a=r;u<=a;){for(;i(e[u],o)===t.Compare.LESS_THAN;)u++;for(;i(e[a],o)===t.Compare.BIGGER_THAN;)a--;u<=a&&((0,t.swap)(e,u,a),u++,a--)}return u}function r(e,t,i,o){var u=void 0;return e.length>1&&(u=n(e,t,i,o),t1&&void 0!==arguments[1]?arguments[1]:t.defaultCompare;return r(e,0,e.length-1,n)}Object.defineProperty(e,"__esModule",{value:!0}),e.quickSort=i})},function(e,t,n){var r,i,o;!function(u,a){i=[t,n(15),n(3),n(16),n(17),n(18),n(4),n(7),n(19),n(20),n(1),n(8),n(21),n(22),n(23),n(24),n(9),n(25),n(26),n(27),n(28),n(30),n(31),n(10),n(32),n(53),n(33),n(34),n(35),n(36),n(37),n(38),n(39),n(40),n(41),n(42),n(43),n(44),n(45),n(12),n(46),n(13),n(47),n(48),n(49),n(50),n(51),n(52),n(6),n(0)],r=a,void 0!==(o="function"==typeof r?r.apply(t,i):r)&&(e.exports=o)}(0,function(e,t,n,r,i,o,u,a,f,l,s,c,h,p,v,d,y,b,g,m,k,_,O,E,S,j,P,w,x,C,T,N,L,A,M,H,F,B,I,D,R,G,z,q,V,U,Y,W,K,X){"use strict";function Q(e){return e&&e.__esModule?e:{default:e}}Object.defineProperty(e,"__esModule",{value:!0}),e.findMinValue=e.findMaxValue=e.sequentialSearch=e.interpolationSearch=e.binarySearch=e.shellSort=e.selectionSort=e.radixSort=e.quickSort=e.mergeSort=e.insertionSort=e.countingSort=e.bucketSort=e.modifiedBubbleSort=e.bubbleSort=e.shuffle=e.kruskal=e.prim=e.floydWarshall=e.dijkstra=e.DFS=e.depthFirstSearch=e.BFS=e.breadthFirstSearch=e.Graph=e.heapSort=e.MaxHeap=e.MinHeap=e.AVLTree=e.BinarySearchTree=e.fibonacciMemoization=e.fibonacciIterative=e.fibonacci=e.factorial=e.factorialIterative=e.HashTableLinearProbingLazy=e.HashTableLinearProbing=e.HashTableSeparateChaining=e.HashTable=e.Dictionary=e.Set=e.StackLinkedList=e.SortedLinkedList=e.CircularLinkedList=e.DoublyLinkedList=e.LinkedList=e.palindromeChecker=e.hotPotato=e.Deque=e.Queue=e.parenthesesChecker=e.decimalToBinary=e.baseConverter=e.hanoiStack=e.hanoi=e.Stack=e.StackArray=e.util=void 0,Object.defineProperty(e,"StackArray",{enumerable:!0,get:function(){return Q(t).default}}),Object.defineProperty(e,"Stack",{enumerable:!0,get:function(){return Q(n).default}}),Object.defineProperty(e,"hanoi",{enumerable:!0,get:function(){return Q(r).default}}),Object.defineProperty(e,"hanoiStack",{enumerable:!0,get:function(){return Q(r).default}}),Object.defineProperty(e,"baseConverter",{enumerable:!0,get:function(){return Q(i).default}}),Object.defineProperty(e,"decimalToBinary",{enumerable:!0,get:function(){return Q(i).default}}),Object.defineProperty(e,"parenthesesChecker",{enumerable:!0,get:function(){return Q(o).default}}),Object.defineProperty(e,"Queue",{enumerable:!0,get:function(){return Q(u).default}}),Object.defineProperty(e,"Deque",{enumerable:!0,get:function(){return Q(a).default}}),Object.defineProperty(e,"hotPotato",{enumerable:!0,get:function(){return Q(f).default}}),Object.defineProperty(e,"palindromeChecker",{enumerable:!0,get:function(){return Q(l).default}}),Object.defineProperty(e,"LinkedList",{enumerable:!0,get:function(){return Q(s).default}}),Object.defineProperty(e,"DoublyLinkedList",{enumerable:!0,get:function(){return Q(c).default}}),Object.defineProperty(e,"CircularLinkedList",{enumerable:!0,get:function(){return Q(h).default}}),Object.defineProperty(e,"SortedLinkedList",{enumerable:!0,get:function(){return Q(p).default}}),Object.defineProperty(e,"StackLinkedList",{enumerable:!0,get:function(){return Q(v).default}}),Object.defineProperty(e,"Set",{enumerable:!0,get:function(){return Q(d).default}}),Object.defineProperty(e,"Dictionary",{enumerable:!0,get:function(){return Q(y).default}}),Object.defineProperty(e,"HashTable",{enumerable:!0,get:function(){return Q(b).default}}),Object.defineProperty(e,"HashTableSeparateChaining",{enumerable:!0,get:function(){return Q(g).default}}),Object.defineProperty(e,"HashTableLinearProbing",{enumerable:!0,get:function(){return Q(m).default}}),Object.defineProperty(e,"HashTableLinearProbingLazy",{enumerable:!0,get:function(){return Q(k).default}}),Object.defineProperty(e,"factorialIterative",{enumerable:!0,get:function(){return Q(_).default}}),Object.defineProperty(e,"factorial",{enumerable:!0,get:function(){return Q(_).default}}),Object.defineProperty(e,"fibonacci",{enumerable:!0,get:function(){return Q(O).default}}),Object.defineProperty(e,"fibonacciIterative",{enumerable:!0,get:function(){return Q(O).default}}),Object.defineProperty(e,"fibonacciMemoization",{enumerable:!0,get:function(){return Q(O).default}}),Object.defineProperty(e,"BinarySearchTree",{enumerable:!0,get:function(){return Q(E).default}}),Object.defineProperty(e,"AVLTree",{enumerable:!0,get:function(){return Q(S).default}}),Object.defineProperty(e,"MinHeap",{enumerable:!0,get:function(){return j.MinHeap}}),Object.defineProperty(e,"MaxHeap",{enumerable:!0,get:function(){return j.MaxHeap}}),Object.defineProperty(e,"heapSort",{enumerable:!0,get:function(){return Q(P).default}}),Object.defineProperty(e,"Graph",{enumerable:!0,get:function(){return Q(w).default}}),Object.defineProperty(e,"breadthFirstSearch",{enumerable:!0,get:function(){return x.breadthFirstSearch}}),Object.defineProperty(e,"BFS",{enumerable:!0,get:function(){return x.BFS}}),Object.defineProperty(e,"depthFirstSearch",{enumerable:!0,get:function(){return C.depthFirstSearch}}),Object.defineProperty(e,"DFS",{enumerable:!0,get:function(){return C.DFS}}),Object.defineProperty(e,"dijkstra",{enumerable:!0,get:function(){return T.dijkstra}}),Object.defineProperty(e,"floydWarshall",{enumerable:!0,get:function(){return N.floydWarshall}}),Object.defineProperty(e,"prim",{enumerable:!0,get:function(){return L.prim}}),Object.defineProperty(e,"kruskal",{enumerable:!0,get:function(){return A.kruskal}}),Object.defineProperty(e,"shuffle",{enumerable:!0,get:function(){return M.shuffle}}),Object.defineProperty(e,"bubbleSort",{enumerable:!0,get:function(){return H.bubbleSort}}),Object.defineProperty(e,"modifiedBubbleSort",{enumerable:!0,get:function(){return F.modifiedBubbleSort}}),Object.defineProperty(e,"bucketSort",{enumerable:!0,get:function(){return B.bucketSort}}),Object.defineProperty(e,"countingSort",{enumerable:!0,get:function(){return I.countingSort}}),Object.defineProperty(e,"insertionSort",{enumerable:!0,get:function(){return D.insertionSort}}),Object.defineProperty(e,"mergeSort",{enumerable:!0,get:function(){return R.mergeSort}}),Object.defineProperty(e,"quickSort",{enumerable:!0,get:function(){return G.quickSort}}),Object.defineProperty(e,"radixSort",{enumerable:!0,get:function(){return z.radixSort}}),Object.defineProperty(e,"selectionSort",{enumerable:!0,get:function(){return q.selectionSort}}),Object.defineProperty(e,"shellSort",{enumerable:!0,get:function(){return V.shellSort}}),Object.defineProperty(e,"binarySearch",{enumerable:!0,get:function(){return U.binarySearch}}),Object.defineProperty(e,"interpolationSearch",{enumerable:!0,get:function(){return Y.interpolationSearch}}),Object.defineProperty(e,"sequentialSearch",{enumerable:!0,get:function(){return W.sequentialSearch}}),Object.defineProperty(e,"findMaxValue",{enumerable:!0,get:function(){return K.findMaxValue}}),Object.defineProperty(e,"findMinValue",{enumerable:!0,get:function(){return K.findMinValue}});var J=function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t.default=e,t}(X);e.util=J})},function(e,t,n){var r,i,o;!function(n,u){i=[e,t],r=u,void 0!==(o="function"==typeof r?r.apply(t,i):r)&&(e.exports=o)}(0,function(e,t){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var r=function(){function e(e,t){for(var n=0;n7&&void 0!==arguments[7]?arguments[7]:[];if(e<=0)return f;if(1===e){i.push(t.pop());var l={};l[o]=t.toString(),l[u]=r.toString(),l[a]=i.toString(),f.push(l)}else{n(e-1,t,i,r,o,a,u,f),i.push(t.pop());var s={};s[o]=t.toString(),s[u]=r.toString(),s[a]=i.toString(),f.push(s),n(e-1,r,t,i,u,o,a,f)}return f}function r(e){for(var t=new o.default,r=new o.default,i=new o.default,u=e;u>0;u--)t.push(u);return n(e,t,i,r,"source","helper","dest")}function i(e,t,n,r){var o=arguments.length>4&&void 0!==arguments[4]?arguments[4]:[];return e<=0?o:(1===e?o.push([t,r]):(i(e-1,t,r,n,o),o.push([t,r]),i(e-1,n,t,r,o)),o)}Object.defineProperty(e,"__esModule",{value:!0}),e.hanoiStack=r,e.hanoi=i;var o=function(e){return e&&e.__esModule?e:{default:e}}(t)})},function(e,t,n){var r,i,o;!function(u,a){i=[t,n(3)],r=a,void 0!==(o="function"==typeof r?r.apply(t,i):r)&&(e.exports=o)}(0,function(e,t){"use strict";function n(e){for(var t=new i.default,n=e,r=void 0,o="";n>0;)r=Math.floor(n%2),t.push(r),n=Math.floor(n/2);for(;!t.isEmpty();)o+=t.pop().toString();return o}function r(e,t){var n=new i.default,r=e,o=void 0,u="";if(!(t>=2&&t<=36))return"";for(;r>0;)o=Math.floor(r%t),n.push(o),r=Math.floor(r/t);for(;!n.isEmpty();)u+="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[n.pop()];return u}Object.defineProperty(e,"__esModule",{value:!0}),e.decimalToBinary=n,e.baseConverter=r;var i=function(e){return e&&e.__esModule?e:{default:e}}(t)})},function(e,t,n){var r,i,o;!function(u,a){i=[t,n(3)],r=a,void 0!==(o="function"==typeof r?r.apply(t,i):r)&&(e.exports=o)}(0,function(e,t){"use strict";function n(e){for(var t=new r.default,n="([{",i=")]}",o=!0,u=0,a=void 0,f=void 0;u=0?t.push(a):t.isEmpty()?o=!1:(f=t.pop(),n.indexOf(f)!==i.indexOf(a)&&(o=!1)),u++;return!(!o||!t.isEmpty())}Object.defineProperty(e,"__esModule",{value:!0}),e.parenthesesChecker=n;var r=function(e){return e&&e.__esModule?e:{default:e}}(t)})},function(e,t,n){var r,i,o;!function(u,a){i=[t,n(4)],r=a,void 0!==(o="function"==typeof r?r.apply(t,i):r)&&(e.exports=o)}(0,function(e,t){"use strict";function n(e,t){for(var n=new r.default,i=[],o=0;o1;){for(var u=0;u1&&i;)o=t.removeFront(),u=t.removeBack(),o!==u&&(i=!1);return i}Object.defineProperty(e,"__esModule",{value:!0}),e.palindromeChecker=n;var r=function(e){return e&&e.__esModule?e:{default:e}}(t)})},function(e,t,n){var r,i,o;!function(u,a){i=[e,t,n(0),n(1),n(5)],r=a,void 0!==(o="function"==typeof r?r.apply(t,i):r)&&(e.exports=o)}(0,function(e,t,n,r,i){"use strict";function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function u(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function a(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}Object.defineProperty(t,"__esModule",{value:!0});var f=function(e){return e&&e.__esModule?e:{default:e}}(r),l=function(){function e(e,t){for(var n=0;n0&&void 0!==arguments[0]?arguments[0]:n.defaultEquals;return o(this,t),u(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e))}return a(t,e),l(t,[{key:"push",value:function(e){var t=new i.Node(e),n=void 0;null==this.head?this.head=t:(n=this.getElementAt(this.size()-1),n.next=t),t.next=this.head,this.count++}},{key:"insert",value:function(e,t){if(t>=0&&t<=this.count){var n=new i.Node(e),r=this.head;if(0===t)null==this.head?(this.head=n,n.next=this.head):(n.next=r,r=this.getElementAt(this.size()),this.head=n,r.next=this.head);else{var o=this.getElementAt(t-1);n.next=o.next,o.next=n}return this.count++,!0}return!1}},{key:"removeAt",value:function(e){if(e>=0&&e0&&void 0!==arguments[0]?arguments[0]:n.defaultEquals,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:n.defaultCompare;i(this,t);var u=o(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e));return u.equalsFn=e,u.compareFn=r,u}return u(t,e),f(t,[{key:"push",value:function(e){if(this.isEmpty())l(t.prototype.__proto__||Object.getPrototypeOf(t.prototype),"push",this).call(this,e);else{var n=this.getIndexNextSortedElement(e);l(t.prototype.__proto__||Object.getPrototypeOf(t.prototype),"insert",this).call(this,e,n)}}},{key:"insert",value:function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;if(this.isEmpty())return l(t.prototype.__proto__||Object.getPrototypeOf(t.prototype),"insert",this).call(this,e,0===n?n:0);var r=this.getIndexNextSortedElement(e);return l(t.prototype.__proto__||Object.getPrototypeOf(t.prototype),"insert",this).call(this,e,r)}},{key:"getIndexNextSortedElement",value:function(e){for(var t=this.head,r=0;r0&&(o=i,u=r),u.forEach(function(e){o.includes(e)&&n.add(e)}),n}},{key:"difference",value:function(t){var n=new e;return this.values().forEach(function(e){t.has(e)||n.add(e)}),n}},{key:"isSubsetOf",value:function(e){if(this.size()>e.size())return!1;var t=!0;return this.values().every(function(n){return!!e.has(n)||(t=!1,!1)}),t}},{key:"isEmpty",value:function(){return 0===this.size()}},{key:"size",value:function(){return Object.keys(this.items).length}},{key:"clear",value:function(){this.items={}}},{key:"toString",value:function(){if(this.isEmpty())return"";for(var e=this.values(),t=""+e[0],n=1;n0&&void 0!==arguments[0]?arguments[0]:n.defaultToString;i(this,e),this.toStrFn=t,this.table={}}return o(e,[{key:"loseloseHashCode",value:function(e){if("number"==typeof e)return e;for(var t=this.toStrFn(e),n=0,r=0;r "+this.table[e[0]].toString()+"}",n=1;n "+this.table[e[n]].toString()+"}";return t}}]),e}();t.default=u,e.exports=t.default})},function(e,t,n){var r,i,o;!function(u,a){i=[e,t,n(0),n(1),n(2)],r=a,void 0!==(o="function"==typeof r?r.apply(t,i):r)&&(e.exports=o)}(0,function(e,t,n,r,i){"use strict";function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var u=function(e){return e&&e.__esModule?e:{default:e}}(r),a=function(){function e(e,t){for(var n=0;n0&&void 0!==arguments[0]?arguments[0]:n.defaultToString;o(this,e),this.toStrFn=t,this.table={}}return a(e,[{key:"loseloseHashCode",value:function(e){if("number"==typeof e)return e;for(var t=this.toStrFn(e),n=0,r=0;r "+this.table[e[0]].toString()+"}",n=1;n "+this.table[e[n]].toString()+"}";return t}}]),e}();t.default=f,e.exports=t.default})},function(e,t,n){var r,i,o;!function(u,a){i=[e,t,n(0),n(2)],r=a,void 0!==(o="function"==typeof r?r.apply(t,i):r)&&(e.exports=o)}(0,function(e,t,n,r){"use strict";function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var o=function(){function e(e,t){for(var n=0;n0&&void 0!==arguments[0]?arguments[0]:n.defaultToString;i(this,e),this.toStrFn=t,this.table={}}return o(e,[{key:"loseloseHashCode",value:function(e){if("number"==typeof e)return e;for(var t=this.toStrFn(e),n=0,r=0;r "+this.table[e[0]].toString()+"}",n=1;n "+this.table[e[n]].toString()+"}";return t}}]),e}();t.default=u,e.exports=t.default})},function(e,t,n){var r,i,o;!function(u,a){i=[e,t,n(0),n(29)],r=a,void 0!==(o="function"==typeof r?r.apply(t,i):r)&&(e.exports=o)}(0,function(e,t,n,r){"use strict";function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var o=function(){function e(e,t){for(var n=0;n0&&void 0!==arguments[0]?arguments[0]:n.defaultToString;i(this,e),this.toStrFn=t,this.table={}}return o(e,[{key:"loseloseHashCode",value:function(e){if("number"==typeof e)return e;for(var t=this.toStrFn(e),n=0,r=0;r "+this.table[e[0]].toString()+"}",n=1;n "+this.table[e[n]].toString()+"}";return t}}]),e}();t.default=u,e.exports=t.default})},function(e,t,n){var r,i,o;!function(u,a){i=[t,n(2)],r=a,void 0!==(o="function"==typeof r?r.apply(t,i):r)&&(e.exports=o)}(0,function(e,t){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function r(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function i(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}Object.defineProperty(e,"__esModule",{value:!0}),e.ValuePairLazy=void 0;e.ValuePairLazy=function(e){function t(e,i){var o=arguments.length>2&&void 0!==arguments[2]&&arguments[2];n(this,t);var u=r(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e,i));return u.key=e,u.value=i,u.isDeleted=o,u}return i(t,e),t}(t.ValuePair)})},function(e,t,n){var r,i,o;!function(n,u){i=[t],r=u,void 0!==(o="function"==typeof r?r.apply(t,i):r)&&(e.exports=o)}(0,function(e){"use strict";function t(e){if(!(e<0)){for(var t=1,n=e;n>1;n--)t*=n;return t}}function n(e){if(!(e<0))return 1===e||0===e?1:e*n(e-1)}Object.defineProperty(e,"__esModule",{value:!0}),e.factorialIterative=t,e.factorial=n})},function(e,t,n){var r,i,o;!function(n,u){i=[t],r=u,void 0!==(o="function"==typeof r?r.apply(t,i):r)&&(e.exports=o)}(0,function(e){"use strict";function t(e){return e<1?0:e<=2?1:t(e-1)+t(e-2)}function n(e){if(e<1)return 0;for(var t=0,n=1,r=e,i=2;i<=e;i++)r=n+t,t=n,n=r;return r}function r(e){if(e<1)return 0;var t=[0,1];return function e(n){return null!=t[n]?t[n]:(t[n]=e(n-1)+e(n-2),t[n]=e(n-1)+e(n-2))}(e)}Object.defineProperty(e,"__esModule",{value:!0}),e.fibonacci=t,e.fibonacciIterative=n,e.fibonacciMemoization=r})},function(e,t,n){var r,i,o;!function(u,a){i=[e,t,n(0),n(10),n(11)],r=a,void 0!==(o="function"==typeof r?r.apply(t,i):r)&&(e.exports=o)}(0,function(e,t,n,r,i){"use strict";function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function u(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function a(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}Object.defineProperty(t,"__esModule",{value:!0});var f=function(e){return e&&e.__esModule?e:{default:e}}(r),l=function(){function e(e,t){for(var n=0;n0&&void 0!==arguments[0]?arguments[0]:n.defaultCompare;o(this,t);var r=u(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e));return r.compareFn=e,r.root=null,r}return a(t,e),l(t,[{key:"getNodeHeight",value:function(e){return null==e?-1:Math.max(this.getNodeHeight(e.left),this.getNodeHeight(e.right))+1}},{key:"rotationLL",value:function(e){var t=e.left;return e.left=t.right,t.right=e,t}},{key:"rotationRR",value:function(e){var t=e.right;return e.right=t.left,t.left=e,t}},{key:"rotationLR",value:function(e){return e.left=this.rotationRR(e.left),this.rotationLL(e)}},{key:"rotationRL",value:function(e){return e.right=this.rotationLL(e.right),this.rotationRR(e)}},{key:"getBalanceFactor",value:function(e){switch(this.getNodeHeight(e.left)-this.getNodeHeight(e.right)){case-2:return c.UNBALANCED_RIGHT;case-1:return c.SLIGHTLY_UNBALANCED_RIGHT;case 1:return c.SLIGHTLY_UNBALANCED_LEFT;case 2:return c.UNBALANCED_LEFT;default:return c.BALANCED}}},{key:"insert",value:function(e){this.root=this.insertNode(this.root,e)}},{key:"insertNode",value:function(e,t){if(null==e)return new i.Node(t);if(this.compareFn(t,e.key)===n.Compare.LESS_THAN)e.left=this.insertNode(e.left,t);else{if(this.compareFn(t,e.key)!==n.Compare.BIGGER_THAN)return e;e.right=this.insertNode(e.right,t)}var r=this.getBalanceFactor(e);if(r===c.UNBALANCED_LEFT){if(this.compareFn(t,e.left.key)!==n.Compare.LESS_THAN)return this.rotationLR(e);e=this.rotationLL(e)}if(r===c.UNBALANCED_RIGHT){if(this.compareFn(t,e.right.key)!==n.Compare.BIGGER_THAN)return this.rotationRL(e);e=this.rotationRR(e)}return e}},{key:"removeNode",value:function(e,n){if(null==(e=s(t.prototype.__proto__||Object.getPrototypeOf(t.prototype),"removeNode",this).call(this,e,n)))return e;var r=this.getBalanceFactor(e);if(r===c.UNBALANCED_LEFT){if(this.getBalanceFactor(e.left)===c.BALANCED||this.getBalanceFactor(e.left)===c.SLIGHTLY_UNBALANCED_LEFT)return this.rotationLL(e);if(this.getBalanceFactor(e.left)===c.SLIGHTLY_UNBALANCED_RIGHT)return this.rotationLR(e.left)}if(r===c.UNBALANCED_RIGHT){if(this.getBalanceFactor(e.right)===c.BALANCED||this.getBalanceFactor(e.right)===c.SLIGHTLY_UNBALANCED_RIGHT)return this.rotationRR(e);if(this.getBalanceFactor(e.right)===c.SLIGHTLY_UNBALANCED_LEFT)return this.rotationRL(e.right)}return e}}]),t}(f.default);t.default=h,e.exports=t.default})},function(e,t,n){var r,i,o;!function(u,a){i=[e,t,n(0)],r=a,void 0!==(o="function"==typeof r?r.apply(t,i):r)&&(e.exports=o)}(0,function(e,t,n){"use strict";function r(e,t,i,o){var u=t,a=2*t+1,f=2*t+2;a0&&(u=a),f0&&(u=f),u!==t&&((0,n.swap)(e,t,u),r(e,u,i,o))}function i(e,t){for(var n=Math.floor(e.length/2);n>=0;n-=1)r(e,n,e.length,t);return e}function o(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:n.defaultCompare,o=e.length;for(i(e,t);o>1;)(0,n.swap)(e,0,--o),r(e,0,o,t);return e}Object.defineProperty(t,"__esModule",{value:!0}),t.default=o,e.exports=t.default})},function(e,t,n){var r,i,o;!function(u,a){i=[e,t,n(9)],r=a,void 0!==(o="function"==typeof r?r.apply(t,i):r)&&(e.exports=o)}(0,function(e,t,n){"use strict";function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var i=function(e){return e&&e.__esModule?e:{default:e}}(n),o=function(){function e(e,t){for(var n=0;n0&&void 0!==arguments[0]&&arguments[0];r(this,e),this.isDirected=t,this.vertices=[],this.adjList=new i.default}return o(e,[{key:"addVertex",value:function(e){this.vertices.includes(e)||(this.vertices.push(e),this.adjList.set(e,[]))}},{key:"addEdge",value:function(e,t){this.adjList.get(e)||this.addVertex(e),this.adjList.get(t)||this.addVertex(t),this.adjList.get(e).push(t),this.isDirected||this.adjList.get(t).push(e)}},{key:"getVertices",value:function(){return this.vertices}},{key:"getAdjList",value:function(){return this.adjList}},{key:"toString",value:function(){for(var e="",t=0;t ";for(var n=this.adjList.get(this.vertices[t]),r=0;r1&&void 0!==arguments[1]?arguments[1]:t.defaultCompare,r=e.length,i=0;i1&&void 0!==arguments[1]?arguments[1]:t.defaultCompare,r=e.length,i=0;ir&&(r=e[i]);for(var o=Math.floor((r-n)/t)+1,u=[],a=0;a1&&void 0!==arguments[1]?arguments[1]:5;return e.length<2?e:i(r(e,t))}Object.defineProperty(e,"__esModule",{value:!0}),e.bucketSort=o})},function(e,t,n){var r,i,o;!function(u,a){i=[t,n(6)],r=a,void 0!==(o="function"==typeof r?r.apply(t,i):r)&&(e.exports=o)}(0,function(e,t){"use strict";function n(e){if(e.length<2)return e;var n=(0,t.findMaxValue)(e),r=0,i=new Array(n+1);return e.forEach(function(e){i[e]||(i[e]=0),i[e]++}),i.forEach(function(t,n){for(;t>0;)e[r++]=n,t--}),e}Object.defineProperty(e,"__esModule",{value:!0}),e.countingSort=n})},function(e,t,n){var r,i,o;!function(u,a){i=[t,n(0)],r=a,void 0!==(o="function"==typeof r?r.apply(t,i):r)&&(e.exports=o)}(0,function(e,t){"use strict";function n(e,n,r){for(var i=0,o=0,u=[];i1&&void 0!==arguments[1]?arguments[1]:t.defaultCompare;if(e.length>1){var o=e,u=o.length,a=Math.floor(u/2);e=n(r(e.slice(0,a),i),r(e.slice(a,u),i),i)}return e}Object.defineProperty(e,"__esModule",{value:!0}),e.mergeSort=r})},function(e,t,n){var r,i,o;!function(u,a){i=[t,n(6)],r=a,void 0!==(o="function"==typeof r?r.apply(t,i):r)&&(e.exports=o)}(0,function(e,t){"use strict";function n(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:10;if(e.length<2)return e;for(var r=(0,t.findMinValue)(e),o=(0,t.findMaxValue)(e),u=1;(o-r)/u>=1;)e=i(e,n,u,r),u*=n;return e}Object.defineProperty(e,"__esModule",{value:!0}),e.radixSort=n;var r=function(e,t,n,r){return Math.floor((e-t)/n%r)},i=function(e,t,n,i){for(var o=void 0,u=[],a=[],f=0;f=0;c--)o=r(e[c],i,n,t),a[--u[o]]=e[c];for(var h=0;h1&&void 0!==arguments[1]?arguments[1]:t.defaultCompare,r=e.length,i=void 0,o=0;o1&&void 0!==arguments[1]?arguments[1]:t.defaultCompare,r=e.length/2;r>0;){for(var i=r;i=r&&n(e[o-r],u)===t.Compare.BIGGER_THAN;)e[o]=e[o-r],o-=r;e[o]=u}r=2===r?1:Math.floor(5*r/11)}return e}Object.defineProperty(e,"__esModule",{value:!0}),e.shellSort=n})},function(e,t,n){var r,i,o;!function(u,a){i=[t,n(0),n(13)],r=a,void 0!==(o="function"==typeof r?r.apply(t,i):r)&&(e.exports=o)}(0,function(e,t,n){"use strict";function r(e,r){for(var i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:t.defaultCompare,o=(0,n.quickSort)(e),u=0,a=o.length-1;u<=a;){var f=Math.floor((u+a)/2),l=o[f];if(i(l,r)===t.Compare.LESS_THAN)u=f+1;else{if(i(l,r)!==t.Compare.BIGGER_THAN)return f;a=f-1}}return t.DOES_NOT_EXIST}Object.defineProperty(e,"__esModule",{value:!0}),e.binarySearch=r})},function(e,t,n){var r,i,o;!function(u,a){i=[t,n(0)],r=a,void 0!==(o="function"==typeof r?r.apply(t,i):r)&&(e.exports=o)}(0,function(e,t){"use strict";function n(e,n){for(var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:t.defaultCompare,i=arguments.length>3&&void 0!==arguments[3]?arguments[3]:t.defaultEquals,o=arguments.length>4&&void 0!==arguments[4]?arguments[4]:t.defaultDiff,u=e.length,a=0,f=u-1,l=-1,s=-1;a<=f&&(0,t.biggerEquals)(n,e[a],r)&&(0,t.lesserEquals)(n,e[f],r);){if(s=o(n,e[a])/o(e[f],e[a]),l=a+Math.floor((f-a)*s),i(e[l],n))return l;r(e[l],n)===t.Compare.LESS_THAN?a=l+1:f=l-1}return t.DOES_NOT_EXIST}Object.defineProperty(e,"__esModule",{value:!0}),e.interpolationSearch=n})},function(e,t,n){var r,i,o;!function(u,a){i=[t,n(0)],r=a,void 0!==(o="function"==typeof r?r.apply(t,i):r)&&(e.exports=o)}(0,function(e,t){"use strict";function n(e,n){for(var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:t.defaultEquals,i=0;i0&&void 0!==arguments[0]?arguments[0]:t.defaultCompare;i(this,e),this.compareFn=n,this.heap=[]}return o(e,[{key:"getLeftIndex",value:function(e){return 2*e+1}},{key:"getRightIndex",value:function(e){return 2*e+2}},{key:"getParentIndex",value:function(e){if(0!==e)return Math.floor((e-1)/2)}},{key:"size",value:function(){return this.heap.length}},{key:"isEmpty",value:function(){return this.size()<=0}},{key:"clear",value:function(){this.heap=[]}},{key:"find",value:function(){return this.isEmpty()?void 0:this.heap[0]}},{key:"insert",value:function(e){if(null!=e){var t=this.heap.length;return this.heap.push(e),this.siftUp(t),!0}return!1}},{key:"siftDown",value:function(e){var n=e,r=this.getLeftIndex(e),i=this.getRightIndex(e),o=this.size();rt.Compare.BIGGER_THAN&&(n=r),it.Compare.BIGGER_THAN&&(n=i),e!==n&&((0,t.swap)(this.heap,e,n),this.siftDown(n))}},{key:"siftUp",value:function(e){for(var n=this.getParentIndex(e);e>0&&this.compareFn(this.heap[n],this.heap[e])>t.Compare.BIGGER_THAN;)(0,t.swap)(this.heap,n,e),e=n,n=this.getParentIndex(e)}},{key:"extract",value:function(){if(!this.isEmpty()){if(1===this.size())return this.heap.shift();var e=this.heap.shift();return this.siftDown(0),e}}},{key:"heapify",value:function(e){e&&(this.heap=e,this.heap.unshift(null));for(var t=this.size()-1;t>0;t--)this.siftDown(t)}}]),e}();e.MaxHeap=function(e){function o(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:t.defaultCompare;i(this,o);var r=n(this,(o.__proto__||Object.getPrototypeOf(o)).call(this,e));return r.compareFn=e,r.compareFn=(0,t.reverseCompare)(e),r}return r(o,e),o}(u)})}])}); \ No newline at end of file +!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("PacktDataStructuresAlgorithms",[],t):"object"==typeof exports?exports.PacktDataStructuresAlgorithms=t():e.PacktDataStructuresAlgorithms=t()}(window,function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var i=t[r]={i:r,l:!1,exports:{}};return e[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:r})},n.r=function(e){Object.defineProperty(e,"__esModule",{value:!0})},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=53)}([function(e,t,n){var r,i,o;i=[t],void 0===(o="function"==typeof(r=function(e){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.lesserEquals=function(e,n,r){var i=r(e,n);return i===t.LESS_THAN||i===t.EQUALS},e.biggerEquals=function(e,n,r){var i=r(e,n);return i===t.BIGGER_THAN||i===t.EQUALS},e.defaultCompare=function(e,n){return e===n?0:e0&&void 0!==arguments[0]?arguments[0]:n.defaultEquals;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.equalsFn=t,this.count=0,this.head=void 0}return i(e,[{key:"push",value:function(e){var t=new r.Node(e),n=void 0;if(null==this.head)this.head=t;else{for(n=this.head;null!=n.next;)n=n.next;n.next=t}this.count++}},{key:"getElementAt",value:function(e){if(e>=0&&e<=this.count){for(var t=this.head,n=0;n=0&&t<=this.count){var n=new r.Node(e);if(0===t){var i=this.head;n.next=i,this.head=n}else{var o=this.getElementAt(t-1);n.next=o.next,o.next=n}return this.count++,!0}return!1}},{key:"removeAt",value:function(e){if(e>=0&&e1&&void 0!==arguments[1]?arguments[1]:t.defaultCompare;if(e&&e.length>0){for(var r=e[0],i=1;i1&&void 0!==arguments[1]?arguments[1]:t.defaultCompare;if(e&&e.length>0){for(var r=e[0],i=1;i1&&(u=function(e,n,r,i){for(var o=e[Math.floor((r+n)/2)],u=n,a=r;u<=a;){for(;i(e[u],o)===t.Compare.LESS_THAN;)u++;for(;i(e[a],o)===t.Compare.BIGGER_THAN;)a--;u<=a&&((0,t.swap)(e,u,a),u++,a--)}return u}(e,r,i,o),r1&&void 0!==arguments[1]?arguments[1]:t.defaultCompare;return n(e,0,e.length-1,r)}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t,n(0)],void 0===(o="function"==typeof(r=function(e,t){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.insertionSort=void 0,e.insertionSort=function(e){for(var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:t.defaultCompare,r=e.length,i=void 0,o=1;o0&&n(e[u-1],i)===t.Compare.BIGGER_THAN;)e[u]=e[u-1],u--;e[u]=i}return e}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t],void 0===(o="function"==typeof(r=function(e){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var t=function(){function e(e,t){for(var n=0;n0&&void 0!==arguments[0]?arguments[0]:n.defaultCompare;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.compareFn=t,this.root=null}return i(e,[{key:"insert",value:function(e){null==this.root?this.root=new r.Node(e):this.insertNode(this.root,e)}},{key:"insertNode",value:function(e,t){this.compareFn(t,e.key)===n.Compare.LESS_THAN?null==e.left?e.left=new r.Node(t):this.insertNode(e.left,t):null==e.right?e.right=new r.Node(t):this.insertNode(e.right,t)}},{key:"getRoot",value:function(){return this.root}},{key:"search",value:function(e){return this.searchNode(this.root,e)}},{key:"searchNode",value:function(e,t){return null!=e&&(this.compareFn(t,e.key)===n.Compare.LESS_THAN?this.searchNode(e.left,t):this.compareFn(t,e.key)!==n.Compare.BIGGER_THAN||this.searchNode(e.right,t))}},{key:"inOrderTraverse",value:function(e){this.inOrderTraverseNode(this.root,e)}},{key:"inOrderTraverseNode",value:function(e,t){null!=e&&(this.inOrderTraverseNode(e.left,t),t(e.key),this.inOrderTraverseNode(e.right,t))}},{key:"preOrderTraverse",value:function(e){this.preOrderTraverseNode(this.root,e)}},{key:"preOrderTraverseNode",value:function(e,t){null!=e&&(t(e.key),this.preOrderTraverseNode(e.left,t),this.preOrderTraverseNode(e.right,t))}},{key:"postOrderTraverse",value:function(e){this.postOrderTraverseNode(this.root,e)}},{key:"postOrderTraverseNode",value:function(e,t){null!=e&&(this.postOrderTraverseNode(e.left,t),this.postOrderTraverseNode(e.right,t),t(e.key))}},{key:"min",value:function(){return this.minNode(this.root)}},{key:"minNode",value:function(e){for(var t=e;null!=t&&null!=t.left;)t=t.left;return t}},{key:"max",value:function(){return this.maxNode(this.root)}},{key:"maxNode",value:function(e){for(var t=e;null!=t&&null!=t.right;)t=t.right;return t}},{key:"remove",value:function(e){this.root=this.removeNode(this.root,e)}},{key:"removeNode",value:function(e,t){if(null==e)return null;if(this.compareFn(t,e.key)===n.Compare.LESS_THAN)return e.left=this.removeNode(e.left,t),e;if(this.compareFn(t,e.key)===n.Compare.BIGGER_THAN)return e.right=this.removeNode(e.right,t),e;if(null==e.left&&null==e.right)return e=null;if(null==e.left)return e=e.right;if(null==e.right)return e=e.left;var r=this.minNode(e.right);return e.key=r.key,e.right=this.removeNode(e.right,r.key),e}}]),e}();t.default=o,e.exports=t.default})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[e,t,n(0),n(1)],void 0===(o="function"==typeof(r=function(e,t,n,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var i=function(){function e(e,t){for(var n=0;n0&&void 0!==arguments[0]?arguments[0]:n.defaultToString;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.toStrFn=t,this.table={}}return i(e,[{key:"set",value:function(e,t){if(null!=e&&null!=t){var n=this.toStrFn(e);return this.table[n]=new r.ValuePair(e,t),!0}return!1}},{key:"get",value:function(e){var t=this.table[this.toStrFn(e)];return null==t?void 0:t.value}},{key:"hasKey",value:function(e){return null!=this.table[this.toStrFn(e)]}},{key:"remove",value:function(e){return!!this.hasKey(e)&&(delete this.table[this.toStrFn(e)],!0)}},{key:"values",value:function(){return this.keyValues().map(function(e){return e.value})}},{key:"keys",value:function(){return this.keyValues().map(function(e){return e.key})}},{key:"keyValues",value:function(){return Object.values(this.table)}},{key:"forEach",value:function(e){for(var t=this.keyValues(),n=0;n0&&void 0!==arguments[0]?arguments[0]:n.defaultEquals;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,t);var r=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e));return r.tail=void 0,r}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}(t,e),a(t,[{key:"push",value:function(e){var t=new i.DoublyNode(e);null==this.head?(this.head=t,this.tail=t):(this.tail.next=t,t.prev=this.tail,this.tail=t),this.count++}},{key:"insert",value:function(e,t){if(t>=0&&t<=this.count){var n=new i.DoublyNode(e),r=this.head;if(0===t)null==this.head?(this.head=n,this.tail=n):(n.next=this.head,this.head.prev=n,this.head=n);else if(t===this.count)(r=this.tail).next=n,n.prev=r,this.tail=n;else{var o=this.getElementAt(t-1);r=o.next,n.next=r,o.next=n,r.prev=n,n.prev=o}return this.count++,!0}return!1}},{key:"removeAt",value:function(e){if(e>=0&&e0)this.lowestCount--,this.items[this.lowestCount]=e;else{for(var t=this.count;t>0;t--)this.items[t]=this.items[t-1];this.count++,this.items[0]=e}}},{key:"addBack",value:function(e){this.items[this.count]=e,this.count++}},{key:"removeFront",value:function(){if(!this.isEmpty()){var e=this.items[this.lowestCount];return delete this.items[this.lowestCount],this.lowestCount++,e}}},{key:"removeBack",value:function(){if(!this.isEmpty()){this.count--;var e=this.items[this.count];return delete this.items[this.count],e}}},{key:"peekFront",value:function(){if(!this.isEmpty())return this.items[this.lowestCount]}},{key:"peekBack",value:function(){if(!this.isEmpty())return this.items[this.count-1]}},{key:"isEmpty",value:function(){return 0===this.size()}},{key:"clear",value:function(){this.items={},this.count=0,this.lowestCount=0}},{key:"size",value:function(){return this.count-this.lowestCount}},{key:"toString",value:function(){if(this.isEmpty())return"";for(var e=""+this.items[this.lowestCount],t=this.lowestCount+1;t0&&void 0!==arguments[0]?arguments[0]:n.defaultCompare;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,t);var r=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e));return r.compareFn=e,r.root=null,r}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}(t,e),a(t,[{key:"getNodeHeight",value:function(e){return null==e?-1:Math.max(this.getNodeHeight(e.left),this.getNodeHeight(e.right))+1}},{key:"rotationLL",value:function(e){var t=e.left;return e.left=t.right,t.right=e,t}},{key:"rotationRR",value:function(e){var t=e.right;return e.right=t.left,t.left=e,t}},{key:"rotationLR",value:function(e){return e.left=this.rotationRR(e.left),this.rotationLL(e)}},{key:"rotationRL",value:function(e){return e.right=this.rotationLL(e.right),this.rotationRR(e)}},{key:"getBalanceFactor",value:function(e){var t=this.getNodeHeight(e.left)-this.getNodeHeight(e.right);switch(t){case-2:return l.UNBALANCED_RIGHT;case-1:return l.SLIGHTLY_UNBALANCED_RIGHT;case 1:return l.SLIGHTLY_UNBALANCED_LEFT;case 2:return l.UNBALANCED_LEFT;default:return l.BALANCED}}},{key:"insert",value:function(e){this.root=this.insertNode(this.root,e)}},{key:"insertNode",value:function(e,t){if(null==e)return new i.Node(t);if(this.compareFn(t,e.key)===n.Compare.LESS_THAN)e.left=this.insertNode(e.left,t);else{if(this.compareFn(t,e.key)!==n.Compare.BIGGER_THAN)return e;e.right=this.insertNode(e.right,t)}var r=this.getBalanceFactor(e);if(r===l.UNBALANCED_LEFT){if(this.compareFn(t,e.left.key)!==n.Compare.LESS_THAN)return this.rotationLR(e);e=this.rotationLL(e)}if(r===l.UNBALANCED_RIGHT){if(this.compareFn(t,e.right.key)!==n.Compare.BIGGER_THAN)return this.rotationRL(e);e=this.rotationRR(e)}return e}},{key:"removeNode",value:function(e,n){if(null==(e=function e(t,n,r){null===t&&(t=Function.prototype);var i=Object.getOwnPropertyDescriptor(t,n);if(void 0===i){var o=Object.getPrototypeOf(t);return null===o?void 0:e(o,n,r)}if("value"in i)return i.value;var u=i.get;return void 0!==u?u.call(r):void 0}(t.prototype.__proto__||Object.getPrototypeOf(t.prototype),"removeNode",this).call(this,e,n)))return e;var r=this.getBalanceFactor(e);if(r===l.UNBALANCED_LEFT){if(this.getBalanceFactor(e.left)===l.BALANCED||this.getBalanceFactor(e.left)===l.SLIGHTLY_UNBALANCED_LEFT)return this.rotationLL(e);if(this.getBalanceFactor(e.left)===l.SLIGHTLY_UNBALANCED_RIGHT)return this.rotationLR(e.left)}if(r===l.UNBALANCED_RIGHT){if(this.getBalanceFactor(e.right)===l.BALANCED||this.getBalanceFactor(e.right)===l.SLIGHTLY_UNBALANCED_RIGHT)return this.rotationRR(e);if(this.getBalanceFactor(e.right)===l.SLIGHTLY_UNBALANCED_LEFT)return this.rotationRL(e.right)}return e}}]),t}(u.default);t.default=f,e.exports=t.default})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t,n(0)],void 0===(o="function"==typeof(r=function(e,t){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.sequentialSearch=function(e,n){for(var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:t.defaultEquals,i=0;i2&&void 0!==arguments[2]?arguments[2]:t.defaultCompare,i=arguments.length>3&&void 0!==arguments[3]?arguments[3]:t.defaultEquals,o=arguments.length>4&&void 0!==arguments[4]?arguments[4]:t.defaultDiff,u=0,a=e.length-1,l=-1,f=-1;u<=a&&(0,t.biggerEquals)(n,e[u],r)&&(0,t.lesserEquals)(n,e[a],r);){if(f=o(n,e[u])/o(e[a],e[u]),l=u+Math.floor((a-u)*f),i(e[l],n))return l;r(e[l],n)===t.Compare.LESS_THAN?u=l+1:a=l-1}return t.DOES_NOT_EXIST}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t,n(0),n(7)],void 0===(o="function"==typeof(r=function(e,t,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.binarySearch=function(e,r){for(var i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:t.defaultCompare,o=(0,n.quickSort)(e),u=0,a=o.length-1;u<=a;){var l=Math.floor((u+a)/2),f=o[l];if(i(f,r)===t.Compare.LESS_THAN)u=l+1;else{if(i(f,r)!==t.Compare.BIGGER_THAN)return l;a=l-1}}return t.DOES_NOT_EXIST}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t,n(0)],void 0===(o="function"==typeof(r=function(e,t){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.shellSort=function(e){for(var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:t.defaultCompare,r=e.length/2;r>0;){for(var i=r;i=r&&n(e[o-r],u)===t.Compare.BIGGER_THAN;)e[o]=e[o-r],o-=r;e[o]=u}r=2===r?1:Math.floor(5*r/11)}return e}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t,n(0)],void 0===(o="function"==typeof(r=function(e,t){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.selectionSort=void 0,e.selectionSort=function(e){for(var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:t.defaultCompare,r=e.length,i=void 0,o=0;o1&&void 0!==arguments[1]?arguments[1]:10;if(e.length<2)return e;for(var i=(0,t.findMinValue)(e),o=(0,t.findMaxValue)(e),u=1;(o-i)/u>=1;)e=r(e,n,u,i),u*=n;return e};var n=function(e,t,n,r){return Math.floor((e-t)/n%r)},r=function(e,t,r,i){for(var o=void 0,u=[],a=[],l=0;l=0;c--)o=n(e[c],i,r,t),a[--u[o]]=e[c];for(var h=0;h1&&void 0!==arguments[1]?arguments[1]:t.defaultCompare;if(n.length>1){var i=n,o=i.length,u=Math.floor(o/2),a=e(n.slice(0,u),r),l=e(n.slice(u,o),r);n=function(e,n,r){for(var i=0,o=0,u=[];i0;)e[r++]=n,t--}),e}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t,n(8)],void 0===(o="function"==typeof(r=function(e,t){"use strict";function n(e){if(Array.isArray(e)){for(var t=0,n=Array(e.length);t1&&void 0!==arguments[1]?arguments[1]:5;return e.length<2?e:function(e){for(var r=[],i=0;ir&&(r=e[i]);for(var o=Math.floor((r-n)/t)+1,u=[],a=0;a1&&void 0!==arguments[1]?arguments[1]:t.defaultCompare,r=e.length,i=0;i1&&void 0!==arguments[1]?arguments[1]:t.defaultCompare,r=e.length,i=0;i0&&void 0!==arguments[0]&&arguments[0];!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.isDirected=t,this.vertices=[],this.adjList=new i.default}return o(e,[{key:"addVertex",value:function(e){this.vertices.includes(e)||(this.vertices.push(e),this.adjList.set(e,[]))}},{key:"addEdge",value:function(e,t){this.adjList.get(e)||this.addVertex(e),this.adjList.get(t)||this.addVertex(t),this.adjList.get(e).push(t),!0!==this.isDirected&&this.adjList.get(t).push(e)}},{key:"getVertices",value:function(){return this.vertices}},{key:"getAdjList",value:function(){return this.adjList}},{key:"toString",value:function(){for(var e="",t=0;t ";for(var n=this.adjList.get(this.vertices[t]),r=0;r0&&(u=a),l0&&(u=l),u!==t&&((0,n.swap)(e,t,u),r(e,u,i,o))}Object.defineProperty(t,"__esModule",{value:!0}),t.default=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:n.defaultCompare,i=e.length;for(!function(e,t){for(var n=Math.floor(e.length/2);n>=0;n-=1)r(e,n,e.length,t)}(e,t);i>1;)(0,n.swap)(e,0,--i),r(e,0,i,t);return e},e.exports=t.default})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t,n(0)],void 0===(o="function"==typeof(r=function(e,t){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0}),e.MaxHeap=e.MinHeap=void 0;var r=function(){function e(e,t){for(var n=0;n0&&void 0!==arguments[0]?arguments[0]:t.defaultCompare;n(this,e),this.compareFn=r,this.heap=[]}return r(e,[{key:"getLeftIndex",value:function(e){return 2*e+1}},{key:"getRightIndex",value:function(e){return 2*e+2}},{key:"getParentIndex",value:function(e){if(0!==e)return Math.floor((e-1)/2)}},{key:"size",value:function(){return this.heap.length}},{key:"isEmpty",value:function(){return this.size()<=0}},{key:"clear",value:function(){this.heap=[]}},{key:"findMinimum",value:function(){return this.isEmpty()?void 0:this.heap[0]}},{key:"insert",value:function(e){if(null!=e){var t=this.heap.length;return this.heap.push(e),this.siftUp(t),!0}return!1}},{key:"siftDown",value:function(e){var n=e,r=this.getLeftIndex(e),i=this.getRightIndex(e),o=this.size();r0&&this.compareFn(this.heap[n],this.heap[e])===t.Compare.BIGGER_THAN;)(0,t.swap)(this.heap,n,e),e=n,n=this.getParentIndex(e)}},{key:"extract",value:function(){if(!this.isEmpty()){if(1===this.size())return this.heap.shift();var e=this.heap.shift();return this.siftDown(0),e}}},{key:"heapify",value:function(e){e&&(this.heap=e);for(var t=Math.floor(this.size()/2)-1,n=0;n<=t;n++)this.siftDown(n);return this.heap}},{key:"getAsArray",value:function(){return this.heap}}]),e}();e.MaxHeap=function(e){function r(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:t.defaultCompare;n(this,r);var i=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}(this,(r.__proto__||Object.getPrototypeOf(r)).call(this,e));return i.compareFn=e,i.compareFn=(0,t.reverseCompare)(e),i}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}(r,e),r}(i)})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t],void 0===(o="function"==typeof(r=function(e){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.fibonacci=function e(t){return t<1?0:t<=2?1:e(t-1)+e(t-2)},e.fibonacciIterative=function(e){if(e<1)return 0;for(var t=0,n=1,r=e,i=2;i<=e;i++)r=n+t,t=n,n=r;return r},e.fibonacciMemoization=function(e){if(e<1)return 0;var t=[0,1];return function e(n){return null!=t[n]?t[n]:(t[n]=e(n-1)+e(n-2),t[n]=e(n-1)+e(n-2))}(e)}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t],void 0===(o="function"==typeof(r=function(e){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.factorialIterative=function(e){if(!(e<0)){for(var t=1,n=e;n>1;n--)t*=n;return t}},e.factorial=function e(t){if(!(t<0))return 1===t||0===t?1:t*e(t-1)}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t,n(1)],void 0===(o="function"==typeof(r=function(e,t){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.ValuePairLazy=void 0,e.ValuePairLazy=function(e){function t(e,n){var r=arguments.length>2&&void 0!==arguments[2]&&arguments[2];!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,t);var i=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e,n));return i.key=e,i.value=n,i.isDeleted=r,i}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}(t,e),t}(t.ValuePair)})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[e,t,n(0),n(38)],void 0===(o="function"==typeof(r=function(e,t,n,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var i=function(){function e(e,t){for(var n=0;n0&&void 0!==arguments[0]?arguments[0]:n.defaultToString;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.toStrFn=t,this.table={}}return i(e,[{key:"loseloseHashCode",value:function(e){if("number"==typeof e)return e;for(var t=this.toStrFn(e),n=0,r=0;r "+this.table[e[0]].toString()+"}",n=1;n "+this.table[e[n]].toString()+"}";return t}}]),e}();t.default=o,e.exports=t.default})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[e,t,n(0),n(1)],void 0===(o="function"==typeof(r=function(e,t,n,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var i=function(){function e(e,t){for(var n=0;n0&&void 0!==arguments[0]?arguments[0]:n.defaultToString;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.toStrFn=t,this.table={}}return i(e,[{key:"loseloseHashCode",value:function(e){if("number"==typeof e)return e;for(var t=this.toStrFn(e),n=0,r=0;r "+this.table[e[0]].toString()+"}",n=1;n "+this.table[e[n]].toString()+"}";return t}}]),e}();t.default=o,e.exports=t.default})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[e,t,n(0),n(2),n(1)],void 0===(o="function"==typeof(r=function(e,t,n,r,i){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var o,u=(o=r)&&o.__esModule?o:{default:o},a=function(){function e(e,t){for(var n=0;n0&&void 0!==arguments[0]?arguments[0]:n.defaultToString;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.toStrFn=t,this.table={}}return a(e,[{key:"loseloseHashCode",value:function(e){if("number"==typeof e)return e;for(var t=this.toStrFn(e),n=0,r=0;r "+this.table[e[0]].toString()+"}",n=1;n "+this.table[e[n]].toString()+"}";return t}}]),e}();t.default=l,e.exports=t.default})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[e,t,n(0),n(1)],void 0===(o="function"==typeof(r=function(e,t,n,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var i=function(){function e(e,t){for(var n=0;n0&&void 0!==arguments[0]?arguments[0]:n.defaultToString;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.toStrFn=t,this.table={}}return i(e,[{key:"loseloseHashCode",value:function(e){if("number"==typeof e)return e;for(var t=this.toStrFn(e),n=0,r=0;r "+this.table[e[0]].toString()+"}",n=1;n "+this.table[e[n]].toString()+"}";return t}}]),e}();t.default=o,e.exports=t.default})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[e,t],void 0===(o="function"==typeof(r=function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=function(){function e(e,t){for(var n=0;n0&&(o=i,u=r),u.forEach(function(e){o.includes(e)&&n.add(e)}),n}},{key:"difference",value:function(t){var n=new e;return this.values().forEach(function(e){t.has(e)||n.add(e)}),n}},{key:"isSubsetOf",value:function(e){if(this.size()>e.size())return!1;var t=!0;return this.values().every(function(n){return!!e.has(n)||(t=!1,!1)}),t}},{key:"isEmpty",value:function(){return 0===this.size()}},{key:"size",value:function(){return Object.keys(this.items).length}},{key:"clear",value:function(){this.items={}}},{key:"toString",value:function(){if(this.isEmpty())return"";for(var e=this.values(),t=""+e[0],n=1;n0&&void 0!==arguments[0]?arguments[0]:n.defaultEquals,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:n.defaultCompare;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,t);var i=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e));return i.equalsFn=e,i.compareFn=r,i}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}(t,e),u(t,[{key:"push",value:function(e){if(this.isEmpty())a(t.prototype.__proto__||Object.getPrototypeOf(t.prototype),"push",this).call(this,e);else{var n=this.getIndexNextSortedElement(e);a(t.prototype.__proto__||Object.getPrototypeOf(t.prototype),"insert",this).call(this,e,n)}}},{key:"insert",value:function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;if(this.isEmpty())return a(t.prototype.__proto__||Object.getPrototypeOf(t.prototype),"insert",this).call(this,e,0===n?n:0);var r=this.getIndexNextSortedElement(e);return a(t.prototype.__proto__||Object.getPrototypeOf(t.prototype),"insert",this).call(this,e,r)}},{key:"getIndexNextSortedElement",value:function(e){for(var t=this.head,r=0;r0&&void 0!==arguments[0]?arguments[0]:n.defaultEquals;return function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,t),function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e))}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}(t,e),a(t,[{key:"push",value:function(e){var t=new i.Node(e);null==this.head?this.head=t:this.getElementAt(this.size()-1).next=t,t.next=this.head,this.count++}},{key:"insert",value:function(e,t){if(t>=0&&t<=this.count){var n=new i.Node(e),r=this.head;if(0===t)null==this.head?(this.head=n,n.next=this.head):(n.next=r,r=this.getElementAt(this.size()),this.head=n,r.next=this.head);else{var o=this.getElementAt(t-1);n.next=o.next,o.next=n}return this.count++,!0}return!1}},{key:"removeAt",value:function(e){if(e>=0&&e1&&i;)o=t.removeFront(),u=t.removeBack(),o!==u&&(i=!1);return i};var n,r=(n=t)&&n.__esModule?n:{default:n}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t,n(6)],void 0===(o="function"==typeof(r=function(e,t){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.hotPotato=function(e,t){for(var n=new r.default,i=[],o=0;o1;){for(var u=0;u=0?t.push(o):t.isEmpty()?n=!1:(u=t.pop(),"([{".indexOf(u)!==")]}".indexOf(o)&&(n=!1)),i++;return!(!n||!t.isEmpty())};var n,r=(n=t)&&n.__esModule?n:{default:n}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t,n(3)],void 0===(o="function"==typeof(r=function(e,t){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.decimalToBinary=function(e){for(var t=new r.default,n=e,i=void 0,o="";n>0;)i=Math.floor(n%2),t.push(i),n=Math.floor(n/2);for(;!t.isEmpty();)o+=t.pop().toString();return o},e.baseConverter=function(e,t){var n=new r.default,i=e,o=void 0,u="";if(!(t>=2&&t<=36))return"";for(;i>0;)o=Math.floor(i%t),n.push(o),i=Math.floor(i/t);for(;!n.isEmpty();)u+="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[n.pop()];return u};var n,r=(n=t)&&n.__esModule?n:{default:n}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t,n(3)],void 0===(o="function"==typeof(r=function(e,t){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.hanoiStack=function(e){for(var t=new r.default,n=new r.default,i=new r.default,o=e;o>0;o--)t.push(o);return function e(t,n,r,i,o,u,a){var l=arguments.length>7&&void 0!==arguments[7]?arguments[7]:[];if(t<=0)return l;if(1===t){i.push(n.pop());var f={};f[o]=n.toString(),f[u]=r.toString(),f[a]=i.toString(),l.push(f)}else{e(t-1,n,i,r,o,a,u,l),i.push(n.pop());var s={};s[o]=n.toString(),s[u]=r.toString(),s[a]=i.toString(),l.push(s),e(t-1,r,n,i,u,o,a,l)}return l}(e,t,i,n,"source","helper","dest")},e.hanoi=function e(t,n,r,i){var o=arguments.length>4&&void 0!==arguments[4]?arguments[4]:[];return t<=0?o:(1===t?o.push([n,i]):(e(t-1,n,i,r,o),o.push([n,i]),e(t-1,r,n,i,o)),o)};var n,r=(n=t)&&n.__esModule?n:{default:n}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[e,t],void 0===(o="function"==typeof(r=function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=function(){function e(e,t){for(var n=0;n compareFn(b, a);\n}\n\nexport function defaultDiff(a, b) {\n return Number(a) - Number(b);\n}\n","export class ValuePair {\n constructor(key, value) {\n this.key = key;\n this.value = value;\n }\n toString() {\n return `[#${this.key}: ${this.value}]`;\n }\n}\n","import { defaultEquals } from '../util';\nimport { Node } from './models/linked-list-models';\n\nexport default class LinkedList {\n constructor(equalsFn = defaultEquals) {\n this.equalsFn = equalsFn;\n this.count = 0;\n this.head = undefined;\n }\n push(element) {\n const node = new Node(element);\n let current;\n if (this.head == null) {\n // catches null && undefined\n this.head = node;\n } else {\n current = this.head;\n while (current.next != null) {\n current = current.next;\n }\n current.next = node;\n }\n this.count++;\n }\n getElementAt(index) {\n if (index >= 0 && index <= this.count) {\n let node = this.head;\n for (let i = 0; i < index && node != null; i++) {\n node = node.next;\n }\n return node;\n }\n return undefined;\n }\n insert(element, index) {\n if (index >= 0 && index <= this.count) {\n const node = new Node(element);\n if (index === 0) {\n const current = this.head;\n node.next = current;\n this.head = node;\n } else {\n const previous = this.getElementAt(index - 1);\n node.next = previous.next;\n previous.next = node;\n }\n this.count++;\n return true;\n }\n return false;\n }\n removeAt(index) {\n if (index >= 0 && index < this.count) {\n let current = this.head;\n if (index === 0) {\n this.head = current.next;\n } else {\n const previous = this.getElementAt(index - 1);\n current = previous.next;\n previous.next = current.next;\n }\n this.count--;\n return current.element;\n }\n return undefined;\n }\n remove(element) {\n const index = this.indexOf(element);\n return this.removeAt(index);\n }\n indexOf(element) {\n let current = this.head;\n for (let i = 0; i < this.size() && current != null; i++) {\n if (this.equalsFn(element, current.element)) {\n return i;\n }\n current = current.next;\n }\n return -1;\n }\n isEmpty() {\n return this.size() === 0;\n }\n size() {\n return this.count;\n }\n getHead() {\n return this.head;\n }\n clear() {\n this.head = undefined;\n this.count = 0;\n }\n toString() {\n if (this.head == null) {\n return '';\n }\n let objString = `${this.head.element}`;\n let current = this.head.next;\n for (let i = 1; i < this.size() && current != null; i++) {\n objString = `${objString},${current.element}`;\n current = current.next;\n }\n return objString;\n }\n}\n","// @ts-check\n\nexport default class Stack {\n constructor() {\n this.count = 0;\n this.items = {};\n }\n push(element) {\n this.items[this.count] = element;\n this.count++;\n }\n pop() {\n if (this.isEmpty()) {\n return undefined;\n }\n this.count--;\n const result = this.items[this.count];\n delete this.items[this.count];\n return result;\n }\n peek() {\n if (this.isEmpty()) {\n return undefined;\n }\n return this.items[this.count - 1];\n }\n isEmpty() {\n return this.count === 0;\n }\n size() {\n return this.count;\n }\n clear() {\n /* while (!this.isEmpty()) {\n this.pop();\n } */\n this.items = {};\n this.count = 0;\n }\n toString() {\n if (this.isEmpty()) {\n return '';\n }\n let objString = `${this.items[0]}`;\n for (let i = 1; i < this.count; i++) {\n objString = `${objString},${this.items[i]}`;\n }\n return objString;\n }\n}\n","import { defaultCompare, Compare } from '../../util';\n\nexport function findMaxValue(array, compareFn = defaultCompare) {\n if (array && array.length > 0) {\n let max = array[0];\n for (let i = 1; i < array.length; i++) {\n if (compareFn(max, array[i]) === Compare.LESS_THAN) {\n max = array[i];\n }\n }\n return max;\n }\n return undefined;\n}\nexport function findMinValue(array, compareFn = defaultCompare) {\n if (array && array.length > 0) {\n let min = array[0];\n for (let i = 1; i < array.length; i++) {\n if (compareFn(min, array[i]) === Compare.BIGGER_THAN) {\n min = array[i];\n }\n }\n return min;\n }\n return undefined;\n}\n","export class Node {\n constructor(element, next) {\n this.element = element;\n this.next = next;\n }\n}\nexport class DoublyNode extends Node {\n constructor(element, next, prev) {\n super(element, next);\n this.prev = prev;\n }\n}\n","// @ts-check\n\nexport default class Queue {\n constructor() {\n this.count = 0;\n this.lowestCount = 0;\n this.items = {};\n }\n\n enqueue(element) {\n this.items[this.count] = element;\n this.count++;\n }\n\n dequeue() {\n if (this.isEmpty()) {\n return undefined;\n }\n const result = this.items[this.lowestCount];\n delete this.items[this.lowestCount];\n this.lowestCount++;\n return result;\n }\n\n peek() {\n if (this.isEmpty()) {\n return undefined;\n }\n return this.items[this.lowestCount];\n }\n\n isEmpty() {\n return this.size() === 0;\n }\n\n clear() {\n this.items = {};\n this.count = 0;\n this.lowestCount = 0;\n }\n\n size() {\n return this.count - this.lowestCount;\n }\n\n toString() {\n if (this.isEmpty()) {\n return '';\n }\n let objString = `${this.items[this.lowestCount]}`;\n for (let i = this.lowestCount + 1; i < this.count; i++) {\n objString = `${objString},${this.items[i]}`;\n }\n return objString;\n }\n}\n","import { Compare, defaultCompare, swap } from '../../util';\n\nfunction partition(array, left, right, compareFn) {\n const pivot = array[Math.floor((right + left) / 2)];\n let i = left;\n let j = right;\n // console.log('pivot is ' + pivot + '; left is ' + left + '; right is ' + right);\n while (i <= j) {\n while (compareFn(array[i], pivot) === Compare.LESS_THAN) {\n i++;\n // console.log('i = ' + i);\n }\n while (compareFn(array[j], pivot) === Compare.BIGGER_THAN) {\n j--;\n // console.log('j = ' + j);\n }\n if (i <= j) {\n // console.log('swap ' + array[i] + ' with ' + array[j]);\n swap(array, i, j);\n i++;\n j--;\n }\n }\n return i;\n}\nfunction quick(array, left, right, compareFn) {\n let index;\n if (array.length > 1) {\n index = partition(array, left, right, compareFn);\n if (left < index - 1) {\n quick(array, left, index - 1, compareFn);\n }\n if (index < right) {\n quick(array, index, right, compareFn);\n }\n }\n return array;\n}\nexport function quickSort(array, compareFn = defaultCompare) {\n return quick(array, 0, array.length - 1, compareFn);\n}\n","import { Compare, defaultCompare } from '../../util';\n\nexport const insertionSort = (array, compareFn = defaultCompare) => {\n const { length } = array;\n let temp;\n for (let i = 1; i < length; i++) {\n let j = i;\n temp = array[i];\n // console.log('to be inserted ' + temp);\n while (j > 0 && compareFn(array[j - 1], temp) === Compare.BIGGER_THAN) {\n // console.log('shift ' + array[j - 1]);\n array[j] = array[j - 1];\n j--;\n }\n // console.log('insert ' + temp);\n array[j] = temp;\n }\n return array;\n};\n","export class Node {\n constructor(key) {\n this.key = key;\n this.left = null;\n this.right = null;\n }\n toString() {\n return `${this.key}`;\n }\n}\n","import { Compare, defaultCompare } from '../util';\nimport { Node } from './models/node';\n\nexport default class BinarySearchTree {\n constructor(compareFn = defaultCompare) {\n this.compareFn = compareFn;\n this.root = null;\n }\n insert(key) {\n // special case: first key\n if (this.root == null) {\n this.root = new Node(key);\n } else {\n this.insertNode(this.root, key);\n }\n }\n insertNode(node, key) {\n if (this.compareFn(key, node.key) === Compare.LESS_THAN) {\n if (node.left == null) {\n node.left = new Node(key);\n } else {\n this.insertNode(node.left, key);\n }\n } else if (node.right == null) {\n node.right = new Node(key);\n } else {\n this.insertNode(node.right, key);\n }\n }\n getRoot() {\n return this.root;\n }\n search(key) {\n return this.searchNode(this.root, key);\n }\n searchNode(node, key) {\n if (node == null) {\n return false;\n }\n if (this.compareFn(key, node.key) === Compare.LESS_THAN) {\n return this.searchNode(node.left, key);\n } else if (this.compareFn(key, node.key) === Compare.BIGGER_THAN) {\n return this.searchNode(node.right, key);\n }\n return true;\n }\n inOrderTraverse(callback) {\n this.inOrderTraverseNode(this.root, callback);\n }\n inOrderTraverseNode(node, callback) {\n if (node != null) {\n this.inOrderTraverseNode(node.left, callback);\n callback(node.key);\n this.inOrderTraverseNode(node.right, callback);\n }\n }\n preOrderTraverse(callback) {\n this.preOrderTraverseNode(this.root, callback);\n }\n preOrderTraverseNode(node, callback) {\n if (node != null) {\n callback(node.key);\n this.preOrderTraverseNode(node.left, callback);\n this.preOrderTraverseNode(node.right, callback);\n }\n }\n postOrderTraverse(callback) {\n this.postOrderTraverseNode(this.root, callback);\n }\n postOrderTraverseNode(node, callback) {\n if (node != null) {\n this.postOrderTraverseNode(node.left, callback);\n this.postOrderTraverseNode(node.right, callback);\n callback(node.key);\n }\n }\n min() {\n return this.minNode(this.root);\n }\n minNode(node) {\n let current = node;\n while (current != null && current.left != null) {\n current = current.left;\n }\n return current;\n }\n max() {\n return this.maxNode(this.root);\n }\n maxNode(node) {\n let current = node;\n while (current != null && current.right != null) {\n current = current.right;\n }\n return current;\n }\n remove(key) {\n this.root = this.removeNode(this.root, key);\n }\n removeNode(node, key) {\n if (node == null) {\n return null;\n }\n if (this.compareFn(key, node.key) === Compare.LESS_THAN) {\n node.left = this.removeNode(node.left, key);\n return node;\n } else if (this.compareFn(key, node.key) === Compare.BIGGER_THAN) {\n node.right = this.removeNode(node.right, key);\n return node;\n }\n // key is equal to node.item\n // handle 3 special conditions\n // 1 - a leaf node\n // 2 - a node with only 1 child\n // 3 - a node with 2 children\n // case 1\n if (node.left == null && node.right == null) {\n node = null;\n return node;\n }\n // case 2\n if (node.left == null) {\n node = node.right;\n return node;\n } else if (node.right == null) {\n node = node.left;\n return node;\n }\n // case 3\n const aux = this.minNode(node.right);\n node.key = aux.key;\n node.right = this.removeNode(node.right, aux.key);\n return node;\n }\n}\n","import { defaultToString } from '../util';\nimport { ValuePair } from './models/value-pair';\n\nexport default class Dictionary {\n constructor(toStrFn = defaultToString) {\n this.toStrFn = toStrFn;\n this.table = {};\n }\n set(key, value) {\n if (key != null && value != null) {\n const tableKey = this.toStrFn(key);\n this.table[tableKey] = new ValuePair(key, value);\n return true;\n }\n return false;\n }\n get(key) {\n const valuePair = this.table[this.toStrFn(key)];\n return valuePair == null ? undefined : valuePair.value;\n }\n hasKey(key) {\n return this.table[this.toStrFn(key)] != null;\n }\n remove(key) {\n if (this.hasKey(key)) {\n delete this.table[this.toStrFn(key)];\n return true;\n }\n return false;\n }\n values() {\n return this.keyValues().map(valuePair => valuePair.value);\n }\n keys() {\n return this.keyValues().map(valuePair => valuePair.key);\n }\n keyValues() {\n return Object.values(this.table);\n }\n forEach(callbackFn) {\n const valuePairs = this.keyValues();\n for (let i = 0; i < valuePairs.length; i++) {\n const result = callbackFn(valuePairs[i].key, valuePairs[i].value);\n if (result === false) {\n break;\n }\n }\n }\n isEmpty() {\n return this.size() === 0;\n }\n size() {\n return Object.keys(this.table).length;\n }\n clear() {\n this.table = {};\n }\n toString() {\n if (this.isEmpty()) {\n return '';\n }\n const valuePairs = this.keyValues();\n let objString = `${valuePairs[0].toString()}`;\n for (let i = 1; i < valuePairs.length; i++) {\n objString = `${objString},${valuePairs[i].toString()}`;\n }\n return objString;\n }\n}\n","import { defaultEquals } from '../util';\nimport LinkedList from './linked-list';\nimport { DoublyNode } from './models/linked-list-models';\n\nexport default class DoublyLinkedList extends LinkedList {\n constructor(equalsFn = defaultEquals) {\n super(equalsFn);\n this.tail = undefined;\n }\n push(element) {\n const node = new DoublyNode(element);\n if (this.head == null) {\n this.head = node;\n this.tail = node; // NEW\n } else {\n // attach to the tail node // NEW\n this.tail.next = node;\n node.prev = this.tail;\n this.tail = node;\n }\n this.count++;\n }\n insert(element, index) {\n if (index >= 0 && index <= this.count) {\n const node = new DoublyNode(element);\n let current = this.head;\n if (index === 0) {\n if (this.head == null) { // NEW\n this.head = node;\n this.tail = node; // NEW\n } else {\n node.next = this.head;\n this.head.prev = node; // NEW\n this.head = node;\n }\n } else if (index === this.count) { // last item NEW\n current = this.tail;\n current.next = node;\n node.prev = current;\n this.tail = node;\n } else {\n const previous = this.getElementAt(index - 1);\n current = previous.next;\n node.next = current;\n previous.next = node;\n current.prev = node; // NEW\n node.prev = previous; // NEW\n }\n this.count++;\n return true;\n }\n return false;\n }\n removeAt(index) {\n if (index >= 0 && index < this.count) {\n let current = this.head;\n if (index === 0) {\n this.head = this.head.next;\n // if there is only one item, then we update tail as well //NEW\n if (this.count === 1) {\n // {2}\n this.tail = undefined;\n } else {\n this.head.prev = undefined;\n }\n } else if (index === this.count - 1) {\n // last item //NEW\n current = this.tail;\n this.tail = current.prev;\n this.tail.next = undefined;\n } else {\n current = this.getElementAt(index);\n const previous = current.prev;\n // link previous with current's next - skip it to remove\n previous.next = current.next;\n current.next.prev = previous; // NEW\n }\n this.count--;\n return current.element;\n }\n return undefined;\n }\n indexOf(element) {\n let current = this.head;\n let index = 0;\n while (current != null) {\n if (this.equalsFn(element, current.element)) {\n return index;\n }\n index++;\n current = current.next;\n }\n return -1;\n }\n getHead() {\n return this.head;\n }\n getTail() {\n return this.tail;\n }\n clear() {\n super.clear();\n this.tail = undefined;\n }\n toString() {\n if (this.head == null) {\n return '';\n }\n let objString = `${this.head.element}`;\n let current = this.head.next;\n while (current != null) {\n objString = `${objString},${current.element}`;\n current = current.next;\n }\n return objString;\n }\n inverseToString() {\n if (this.tail == null) {\n return '';\n }\n let objString = `${this.tail.element}`;\n let previous = this.tail.prev;\n while (previous != null) {\n objString = `${objString},${previous.element}`;\n previous = previous.prev;\n }\n return objString;\n }\n}\n","// @ts-check\n\nexport default class Deque {\n constructor() {\n this.count = 0;\n this.lowestCount = 0;\n this.items = {};\n }\n\n addFront(element) {\n if (this.isEmpty()) {\n this.addBack(element);\n } else if (this.lowestCount > 0) {\n this.lowestCount--;\n this.items[this.lowestCount] = element;\n } else {\n for (let i = this.count; i > 0; i--) {\n this.items[i] = this.items[i - 1];\n }\n this.count++;\n this.items[0] = element;\n }\n }\n\n addBack(element) {\n this.items[this.count] = element;\n this.count++;\n }\n\n removeFront() {\n if (this.isEmpty()) {\n return undefined;\n }\n const result = this.items[this.lowestCount];\n delete this.items[this.lowestCount];\n this.lowestCount++;\n return result;\n }\n\n removeBack() {\n if (this.isEmpty()) {\n return undefined;\n }\n this.count--;\n const result = this.items[this.count];\n delete this.items[this.count];\n return result;\n }\n\n peekFront() {\n if (this.isEmpty()) {\n return undefined;\n }\n return this.items[this.lowestCount];\n }\n\n peekBack() {\n if (this.isEmpty()) {\n return undefined;\n }\n return this.items[this.count - 1];\n }\n\n isEmpty() {\n return this.size() === 0;\n }\n\n clear() {\n this.items = {};\n this.count = 0;\n this.lowestCount = 0;\n }\n\n size() {\n return this.count - this.lowestCount;\n }\n\n toString() {\n if (this.isEmpty()) {\n return '';\n }\n let objString = `${this.items[this.lowestCount]}`;\n for (let i = this.lowestCount + 1; i < this.count; i++) {\n objString = `${objString},${this.items[i]}`;\n }\n return objString;\n }\n}\n","import { Compare, defaultCompare } from '../util';\nimport BinarySearchTree from './binary-search-tree';\nimport { Node } from './models/node';\n\nconst BalanceFactor = {\n UNBALANCED_RIGHT: 1,\n SLIGHTLY_UNBALANCED_RIGHT: 2,\n BALANCED: 3,\n SLIGHTLY_UNBALANCED_LEFT: 4,\n UNBALANCED_LEFT: 5\n};\n\nexport default class AVLTree extends BinarySearchTree {\n constructor(compareFn = defaultCompare) {\n super(compareFn);\n this.compareFn = compareFn;\n this.root = null;\n }\n getNodeHeight(node) {\n if (node == null) {\n return -1;\n }\n return Math.max(this.getNodeHeight(node.left), this.getNodeHeight(node.right)) + 1;\n }\n /**\n * Left left case: rotate right\n *\n * b a\n * / \\ / \\\n * a e -> rotationLL(b) -> c b\n * / \\ / \\\n * c d d e\n *\n * @param node Node\n */\n rotationLL(node) {\n const tmp = node.left;\n node.left = tmp.right;\n tmp.right = node;\n return tmp;\n }\n /**\n * Right right case: rotate left\n *\n * a b\n * / \\ / \\\n * c b -> rotationRR(a) -> a e\n * / \\ / \\\n * d e c d\n *\n * @param node Node\n */\n rotationRR(node) {\n const tmp = node.right;\n node.right = tmp.left;\n tmp.left = node;\n return tmp;\n }\n /**\n * Left right case: rotate left then right\n * @param node Node\n */\n rotationLR(node) {\n node.left = this.rotationRR(node.left);\n return this.rotationLL(node);\n }\n /**\n * Right left case: rotate right then left\n * @param node Node\n */\n rotationRL(node) {\n node.right = this.rotationLL(node.right);\n return this.rotationRR(node);\n }\n getBalanceFactor(node) {\n const heightDifference = this.getNodeHeight(node.left) - this.getNodeHeight(node.right);\n switch (heightDifference) {\n case -2:\n return BalanceFactor.UNBALANCED_RIGHT;\n case -1:\n return BalanceFactor.SLIGHTLY_UNBALANCED_RIGHT;\n case 1:\n return BalanceFactor.SLIGHTLY_UNBALANCED_LEFT;\n case 2:\n return BalanceFactor.UNBALANCED_LEFT;\n default:\n return BalanceFactor.BALANCED;\n }\n }\n insert(key) {\n this.root = this.insertNode(this.root, key);\n }\n insertNode(node, key) {\n if (node == null) {\n return new Node(key);\n } else if (this.compareFn(key, node.key) === Compare.LESS_THAN) {\n node.left = this.insertNode(node.left, key);\n } else if (this.compareFn(key, node.key) === Compare.BIGGER_THAN) {\n node.right = this.insertNode(node.right, key);\n } else {\n return node; // duplicated key\n }\n // verify if tree is balanced\n const balanceFactor = this.getBalanceFactor(node);\n if (balanceFactor === BalanceFactor.UNBALANCED_LEFT) {\n if (this.compareFn(key, node.left.key) === Compare.LESS_THAN) {\n // Left left case\n node = this.rotationLL(node);\n } else {\n // Left right case\n return this.rotationLR(node);\n }\n }\n if (balanceFactor === BalanceFactor.UNBALANCED_RIGHT) {\n if (this.compareFn(key, node.right.key) === Compare.BIGGER_THAN) {\n // Right right case\n node = this.rotationRR(node);\n } else {\n // Right left case\n return this.rotationRL(node);\n }\n }\n return node;\n }\n removeNode(node, key) {\n node = super.removeNode(node, key); // {1}\n if (node == null) {\n return node;\n }\n // verify if tree is balanced\n const balanceFactor = this.getBalanceFactor(node);\n if (balanceFactor === BalanceFactor.UNBALANCED_LEFT) {\n // Left left case\n if (\n this.getBalanceFactor(node.left) === BalanceFactor.BALANCED ||\n this.getBalanceFactor(node.left) === BalanceFactor.SLIGHTLY_UNBALANCED_LEFT\n ) {\n return this.rotationLL(node);\n }\n // Left right case\n if (this.getBalanceFactor(node.left) === BalanceFactor.SLIGHTLY_UNBALANCED_RIGHT) {\n return this.rotationLR(node.left);\n }\n }\n if (balanceFactor === BalanceFactor.UNBALANCED_RIGHT) {\n // Right right case\n if (\n this.getBalanceFactor(node.right) === BalanceFactor.BALANCED ||\n this.getBalanceFactor(node.right) === BalanceFactor.SLIGHTLY_UNBALANCED_RIGHT\n ) {\n return this.rotationRR(node);\n }\n // Right left case\n if (this.getBalanceFactor(node.right) === BalanceFactor.SLIGHTLY_UNBALANCED_LEFT) {\n return this.rotationRL(node.right);\n }\n }\n return node;\n }\n}\n","import { defaultEquals, DOES_NOT_EXIST } from '../../util';\n\nexport function sequentialSearch(array, value, equalsFn = defaultEquals) {\n for (let i = 0; i < array.length; i++) {\n if (equalsFn(value, array[i])) {\n return i;\n }\n }\n return DOES_NOT_EXIST;\n}\n","import {\n biggerEquals,\n Compare,\n defaultCompare,\n defaultEquals,\n defaultDiff,\n DOES_NOT_EXIST,\n lesserEquals\n} from '../../util';\n\nexport function interpolationSearch(\n array,\n value,\n compareFn = defaultCompare,\n equalsFn = defaultEquals,\n diffFn = defaultDiff\n) {\n const { length } = array;\n let low = 0;\n let high = length - 1;\n let position = -1;\n let delta = -1;\n while (\n low <= high &&\n biggerEquals(value, array[low], compareFn) &&\n lesserEquals(value, array[high], compareFn)\n ) {\n delta = diffFn(value, array[low]) / diffFn(array[high], array[low]);\n position = low + Math.floor((high - low) * delta);\n if (equalsFn(array[position], value)) {\n return position;\n }\n if (compareFn(array[position], value) === Compare.LESS_THAN) {\n low = position + 1;\n } else {\n high = position - 1;\n }\n }\n return DOES_NOT_EXIST;\n}\n","import { Compare, defaultCompare, DOES_NOT_EXIST } from '../../util';\nimport { quickSort } from '../sorting/quicksort';\n\nexport function binarySearch(array, value, compareFn = defaultCompare) {\n const sortedArray = quickSort(array);\n let low = 0;\n let high = sortedArray.length - 1;\n while (low <= high) {\n const mid = Math.floor((low + high) / 2);\n const element = sortedArray[mid];\n // console.log('mid element is ' + element);\n if (compareFn(element, value) === Compare.LESS_THAN) {\n low = mid + 1;\n // console.log('low is ' + low);\n } else if (compareFn(element, value) === Compare.BIGGER_THAN) {\n high = mid - 1;\n // console.log('high is ' + high);\n } else {\n // console.log('found it');\n return mid;\n }\n }\n return DOES_NOT_EXIST;\n}\n","import { Compare, defaultCompare } from '../../util';\n\nexport function shellSort(array, compareFn = defaultCompare) {\n let increment = array.length / 2;\n while (increment > 0) {\n for (let i = increment; i < array.length; i++) {\n let j = i;\n const temp = array[i];\n while (j >= increment && compareFn(array[j - increment], temp) === Compare.BIGGER_THAN) {\n array[j] = array[j - increment];\n j -= increment;\n }\n array[j] = temp;\n }\n if (increment === 2) {\n increment = 1;\n } else {\n increment = Math.floor((increment * 5) / 11);\n }\n }\n return array;\n}\n","import { Compare, defaultCompare, swap } from '../../util';\n\nexport const selectionSort = (array, compareFn = defaultCompare) => {\n const { length } = array;\n let indexMin;\n for (let i = 0; i < length - 1; i++) {\n indexMin = i;\n // console.log('index ' + array[i]);\n for (let j = i; j < length; j++) {\n if (compareFn(array[indexMin], array[j]) === Compare.BIGGER_THAN) {\n // console.log('new index min ' + array[j]);\n indexMin = j;\n }\n }\n if (i !== indexMin) {\n // console.log('swap ' + array[i] + ' with ' + array[indexMin]);\n swap(array, i, indexMin);\n }\n }\n return array;\n};\n","import { findMaxValue, findMinValue } from '../search/min-max-search';\n\nconst getBucketIndex = (value, minValue, significantDigit, radixBase) =>\n Math.floor(((value - minValue) / significantDigit) % radixBase);\n\nconst countingSortForRadix = (array, radixBase, significantDigit, minValue) => {\n let bucketsIndex;\n const buckets = [];\n const aux = [];\n for (let i = 0; i < radixBase; i++) {\n buckets[i] = 0;\n }\n for (let i = 0; i < array.length; i++) {\n bucketsIndex = getBucketIndex(array[i], minValue, significantDigit, radixBase);\n buckets[bucketsIndex]++;\n }\n for (let i = 1; i < radixBase; i++) {\n buckets[i] += buckets[i - 1];\n }\n for (let i = array.length - 1; i >= 0; i--) {\n bucketsIndex = getBucketIndex(array[i], minValue, significantDigit, radixBase);\n aux[--buckets[bucketsIndex]] = array[i];\n }\n for (let i = 0; i < array.length; i++) {\n array[i] = aux[i];\n }\n return array;\n};\nexport function radixSort(array, radixBase = 10) {\n if (array.length < 2) {\n return array;\n }\n const minValue = findMinValue(array);\n const maxValue = findMaxValue(array);\n // Perform counting sort for each significant digit, starting at 1\n let significantDigit = 1;\n while ((maxValue - minValue) / significantDigit >= 1) {\n // console.log('radix sort for digit ' + significantDigit);\n array = countingSortForRadix(array, radixBase, significantDigit, minValue);\n // console.log(array.join());\n significantDigit *= radixBase;\n }\n return array;\n}\n","import { Compare, defaultCompare } from '../../util';\n\nfunction merge(left, right, compareFn) {\n let i = 0;\n let j = 0;\n const result = [];\n while (i < left.length && j < right.length) {\n result.push(compareFn(left[i], right[j]) === Compare.LESS_THAN ? left[i++] : right[j++]);\n }\n return result.concat(i < left.length ? left.slice(i) : right.slice(j));\n}\nexport function mergeSort(array, compareFn = defaultCompare) {\n if (array.length > 1) {\n const { length } = array;\n const middle = Math.floor(length / 2);\n const left = mergeSort(array.slice(0, middle), compareFn);\n const right = mergeSort(array.slice(middle, length), compareFn);\n array = merge(left, right, compareFn);\n }\n return array;\n}\n","import { findMaxValue } from '../search/min-max-search';\n\nexport function countingSort(array) {\n if (array.length < 2) {\n return array;\n }\n const maxValue = findMaxValue(array);\n let sortedIndex = 0;\n const counts = new Array(maxValue + 1);\n array.forEach(element => {\n if (!counts[element]) {\n counts[element] = 0;\n }\n counts[element]++;\n });\n // console.log('Frequencies: ' + counts.join());\n counts.forEach((element, i) => {\n while (element > 0) {\n array[sortedIndex++] = i;\n element--;\n }\n });\n return array;\n}\n","import { insertionSort } from './insertion-sort';\n\nfunction createBuckets(array, bucketSize) {\n let minValue = array[0];\n let maxValue = array[0];\n for (let i = 1; i < array.length; i++) {\n if (array[i] < minValue) {\n minValue = array[i];\n } else if (array[i] > maxValue) {\n maxValue = array[i];\n }\n }\n const bucketCount = Math.floor((maxValue - minValue) / bucketSize) + 1;\n const buckets = [];\n for (let i = 0; i < bucketCount; i++) {\n buckets[i] = [];\n }\n for (let i = 0; i < array.length; i++) {\n buckets[Math.floor((array[i] - minValue) / bucketSize)].push(array[i]);\n }\n return buckets;\n}\nfunction sortBuckets(buckets) {\n const sortedArray = [];\n for (let i = 0; i < buckets.length; i++) {\n if (buckets[i] != null) {\n insertionSort(buckets[i]);\n sortedArray.push(...buckets[i]);\n }\n }\n return sortedArray;\n}\nexport function bucketSort(array, bucketSize = 5) {\n if (array.length < 2) {\n return array;\n }\n const buckets = createBuckets(array, bucketSize);\n return sortBuckets(buckets);\n}\n","import { Compare, defaultCompare, swap } from '../../util';\n\nexport function modifiedBubbleSort(array, compareFn = defaultCompare) {\n const { length } = array;\n for (let i = 0; i < length; i++) {\n // console.log('--- ');\n for (let j = 0; j < length - 1 - i; j++) {\n // console.log('compare ' + array[j] + ' with ' + array[j + 1]);\n if (compareFn(array[j], array[j + 1]) === Compare.BIGGER_THAN) {\n // console.log('swap ' + array[j] + ' with ' + array[j + 1]);\n swap(array, j, j + 1);\n }\n }\n }\n return array;\n}\n","import { Compare, defaultCompare, swap } from '../../util';\n\nexport function bubbleSort(array, compareFn = defaultCompare) {\n const { length } = array;\n for (let i = 0; i < length; i++) {\n // console.log('--- ');\n for (let j = 0; j < length - 1; j++) {\n // console.log('compare ' + array[j] + ' with ' + array[j + 1]);\n if (compareFn(array[j], array[j + 1]) === Compare.BIGGER_THAN) {\n // console.log('swap ' + array[j] + ' with ' + array[j + 1]);\n swap(array, j, j + 1);\n }\n }\n }\n return array;\n}\n","import { swap } from '../../util';\n\nexport function shuffle(array) {\n let currentIndex = array.length;\n while (currentIndex !== 0) {\n const randomIndex = Math.floor(Math.random() * currentIndex);\n currentIndex--;\n swap(array, currentIndex, randomIndex);\n }\n return array;\n}\n","const INF = Number.MAX_SAFE_INTEGER;\nconst find = (i, parent) => {\n while (parent[i]) {\n i = parent[i]; // eslint-disable-line prefer-destructuring\n }\n return i;\n};\nconst union = (i, j, parent) => {\n if (i !== j) {\n parent[j] = i;\n return true;\n }\n return false;\n};\nconst initializeCost = graph => {\n const cost = [];\n const { length } = graph;\n for (let i = 0; i < length; i++) {\n cost[i] = [];\n for (let j = 0; j < length; j++) {\n if (graph[i][j] === 0) {\n cost[i][j] = INF;\n } else {\n cost[i][j] = graph[i][j];\n }\n }\n }\n return cost;\n};\nexport const kruskal = graph => {\n const { length } = graph;\n const parent = [];\n let ne = 0;\n let a;\n let b;\n let u;\n let v;\n const cost = initializeCost(graph);\n while (ne < length - 1) {\n for (let i = 0, min = INF; i < length; i++) {\n for (let j = 0; j < length; j++) {\n if (cost[i][j] < min) {\n min = cost[i][j];\n a = u = i;\n b = v = j;\n }\n }\n }\n u = find(u, parent);\n v = find(v, parent);\n if (union(u, v, parent)) {\n ne++;\n }\n cost[a][b] = cost[b][a] = INF;\n }\n return parent;\n};\n","const INF = Number.MAX_SAFE_INTEGER;\nconst minKey = (graph, key, visited) => {\n // Initialize min value\n let min = INF;\n let minIndex = 0;\n for (let v = 0; v < graph.length; v++) {\n if (visited[v] === false && key[v] < min) {\n min = key[v];\n minIndex = v;\n }\n }\n return minIndex;\n};\nexport const prim = graph => {\n const parent = [];\n const key = [];\n const visited = [];\n const { length } = graph;\n for (let i = 0; i < length; i++) {\n key[i] = INF;\n visited[i] = false;\n }\n key[0] = 0;\n parent[0] = -1;\n for (let i = 0; i < length - 1; i++) {\n const u = minKey(graph, key, visited);\n visited[u] = true;\n for (let v = 0; v < length; v++) {\n if (graph[u][v] && !visited[v] && graph[u][v] < key[v]) {\n parent[v] = u;\n key[v] = graph[u][v];\n }\n }\n }\n return parent;\n};\n","export const floydWarshall = graph => {\n const dist = [];\n const { length } = graph;\n for (let i = 0; i < length; i++) {\n dist[i] = [];\n for (let j = 0; j < length; j++) {\n if (i === j) {\n dist[i][j] = 0;\n } else if (!isFinite(graph[i][j])) {\n dist[i][j] = Infinity;\n } else {\n dist[i][j] = graph[i][j];\n }\n }\n }\n for (let k = 0; k < length; k++) {\n for (let i = 0; i < length; i++) {\n for (let j = 0; j < length; j++) {\n if (dist[i][k] + dist[k][j] < dist[i][j]) {\n dist[i][j] = dist[i][k] + dist[k][j];\n }\n }\n }\n }\n return dist;\n};\n","const INF = Number.MAX_SAFE_INTEGER;\nconst minDistance = (dist, visited) => {\n let min = INF;\n let minIndex = -1;\n for (let v = 0; v < dist.length; v++) {\n if (visited[v] === false && dist[v] <= min) {\n min = dist[v];\n minIndex = v;\n }\n }\n return minIndex;\n};\nexport const dijkstra = (graph, src) => {\n const dist = [];\n const visited = [];\n const { length } = graph;\n for (let i = 0; i < length; i++) {\n dist[i] = INF;\n visited[i] = false;\n }\n dist[src] = 0;\n for (let i = 0; i < length - 1; i++) {\n const u = minDistance(dist, visited);\n visited[u] = true;\n for (let v = 0; v < length; v++) {\n if (!visited[v] && graph[u][v] !== 0 && dist[u] !== INF && dist[u] + graph[u][v] < dist[v]) {\n dist[v] = dist[u] + graph[u][v];\n }\n }\n }\n return dist;\n};\n","// import Graph from '../../data-structures/graph';\n\nconst Colors = {\n WHITE: 0,\n GREY: 1,\n BLACK: 2\n};\n\nconst initializeColor = vertices => {\n const color = {};\n for (let i = 0; i < vertices.length; i++) {\n color[vertices[i]] = Colors.WHITE;\n }\n return color;\n};\n\nconst depthFirstSearchVisit = (u, color, adjList, callback) => {\n color[u] = Colors.GREY;\n if (callback) {\n callback(u);\n }\n // console.log('Discovered ' + u);\n const neighbors = adjList.get(u);\n for (let i = 0; i < neighbors.length; i++) {\n const w = neighbors[i];\n if (color[w] === Colors.WHITE) {\n depthFirstSearchVisit(w, color, adjList, callback);\n }\n }\n color[u] = Colors.BLACK;\n // console.log('explored ' + u);\n};\n\nexport const depthFirstSearch = (graph, callback) => {\n const vertices = graph.getVertices();\n const adjList = graph.getAdjList();\n const color = initializeColor(vertices);\n\n for (let i = 0; i < vertices.length; i++) {\n if (color[vertices[i]] === Colors.WHITE) {\n depthFirstSearchVisit(vertices[i], color, adjList, callback);\n }\n }\n};\n\nconst DFSVisit = (u, color, d, f, p, time, adjList) => {\n // console.log('discovered ' + u);\n color[u] = Colors.GREY;\n d[u] = ++time.count;\n const neighbors = adjList.get(u);\n for (let i = 0; i < neighbors.length; i++) {\n const w = neighbors[i];\n if (color[w] === Colors.WHITE) {\n p[w] = u;\n DFSVisit(w, color, d, f, p, time, adjList);\n }\n }\n color[u] = Colors.BLACK;\n f[u] = ++time.count;\n // console.log('explored ' + u);\n};\n\nexport const DFS = graph => {\n const vertices = graph.getVertices();\n const adjList = graph.getAdjList();\n const color = initializeColor(vertices);\n const d = {};\n const f = {};\n const p = {};\n const time = { count: 0 };\n for (let i = 0; i < vertices.length; i++) {\n f[vertices[i]] = 0;\n d[vertices[i]] = 0;\n p[vertices[i]] = null;\n }\n for (let i = 0; i < vertices.length; i++) {\n if (color[vertices[i]] === Colors.WHITE) {\n DFSVisit(vertices[i], color, d, f, p, time, adjList);\n }\n }\n return {\n discovery: d,\n finished: f,\n predecessors: p\n };\n};\n","import Queue from '../../data-structures/queue';\n\nconst Colors = {\n WHITE: 0,\n GREY: 1,\n BLACK: 2\n};\n\nconst initializeColor = vertices => {\n const color = {};\n for (let i = 0; i < vertices.length; i++) {\n color[vertices[i]] = Colors.WHITE;\n }\n return color;\n};\n\nexport const breadthFirstSearch = (graph, startVertex, callback) => {\n const vertices = graph.getVertices();\n const adjList = graph.getAdjList();\n const color = initializeColor(vertices);\n const queue = new Queue();\n\n queue.enqueue(startVertex);\n\n while (!queue.isEmpty()) {\n const u = queue.dequeue();\n const neighbors = adjList.get(u);\n color[u] = Colors.GREY;\n for (let i = 0; i < neighbors.length; i++) {\n const w = neighbors[i];\n if (color[w] === Colors.WHITE) {\n color[w] = Colors.GREY;\n queue.enqueue(w);\n }\n }\n color[u] = Colors.BLACK;\n if (callback) {\n callback(u);\n }\n }\n};\n\nexport const BFS = (graph, startVertex) => {\n const vertices = graph.getVertices();\n const adjList = graph.getAdjList();\n const color = initializeColor(vertices);\n const queue = new Queue();\n const distances = {};\n const predecessors = {};\n queue.enqueue(startVertex);\n for (let i = 0; i < vertices.length; i++) {\n distances[vertices[i]] = 0;\n predecessors[vertices[i]] = null;\n }\n while (!queue.isEmpty()) {\n const u = queue.dequeue();\n const neighbors = adjList.get(u);\n color[u] = Colors.GREY;\n for (let i = 0; i < neighbors.length; i++) {\n const w = neighbors[i];\n if (color[w] === Colors.WHITE) {\n color[w] = Colors.GREY;\n distances[w] = distances[u] + 1;\n predecessors[w] = u;\n queue.enqueue(w);\n }\n }\n color[u] = Colors.BLACK;\n }\n return {\n distances,\n predecessors\n };\n};\n","import Dictionary from './dictionary';\n\nexport default class Graph {\n constructor(isDirected = false) {\n this.isDirected = isDirected;\n this.vertices = [];\n this.adjList = new Dictionary();\n }\n addVertex(v) {\n if (!this.vertices.includes(v)) {\n this.vertices.push(v);\n this.adjList.set(v, []); // initialize adjacency list with array as well;\n }\n }\n addEdge(a, b) {\n if (!this.adjList.get(a)) {\n this.addVertex(a);\n }\n if (!this.adjList.get(b)) {\n this.addVertex(b);\n }\n this.adjList.get(a).push(b);\n if (this.isDirected !== true) {\n this.adjList.get(b).push(a);\n }\n }\n getVertices() {\n return this.vertices;\n }\n getAdjList() {\n return this.adjList;\n }\n toString() {\n let s = '';\n for (let i = 0; i < this.vertices.length; i++) {\n s += `${this.vertices[i]} -> `;\n const neighbors = this.adjList.get(this.vertices[i]);\n for (let j = 0; j < neighbors.length; j++) {\n s += `${neighbors[j]} `;\n }\n s += '\\n';\n }\n return s;\n }\n}\n","import { defaultCompare, swap } from '../../util';\n\nfunction heapify(array, index, heapSize, compareFn) {\n let largest = index;\n const left = (2 * index) + 1;\n const right = (2 * index) + 2;\n if (left < heapSize && compareFn(array[left], array[index]) > 0) {\n largest = left;\n }\n if (right < heapSize && compareFn(array[right], array[largest]) > 0) {\n largest = right;\n }\n if (largest !== index) {\n swap(array, index, largest);\n heapify(array, largest, heapSize, compareFn);\n }\n}\n\nfunction buildMaxHeap(array, compareFn) {\n for (let i = Math.floor(array.length / 2); i >= 0; i -= 1) {\n heapify(array, i, array.length, compareFn);\n }\n return array;\n}\n\nexport default function heapSort(array, compareFn = defaultCompare) {\n let heapSize = array.length;\n buildMaxHeap(array, compareFn);\n while (heapSize > 1) {\n swap(array, 0, --heapSize);\n heapify(array, 0, heapSize, compareFn);\n }\n return array;\n}\n","import { Compare, defaultCompare, reverseCompare, swap } from '../util';\n\nexport class MinHeap {\n constructor(compareFn = defaultCompare) {\n this.compareFn = compareFn;\n this.heap = [];\n }\n getLeftIndex(index) {\n return (2 * index) + 1;\n }\n getRightIndex(index) {\n return (2 * index) + 2;\n }\n getParentIndex(index) {\n if (index === 0) {\n return undefined;\n }\n return Math.floor((index - 1) / 2);\n }\n size() {\n return this.heap.length;\n }\n isEmpty() {\n return this.size() <= 0;\n }\n clear() {\n this.heap = [];\n }\n findMinimum() {\n return this.isEmpty() ? undefined : this.heap[0];\n }\n insert(value) {\n if (value != null) {\n const index = this.heap.length;\n this.heap.push(value);\n this.siftUp(index);\n return true;\n }\n return false;\n }\n siftDown(index) {\n let element = index;\n const left = this.getLeftIndex(index);\n const right = this.getRightIndex(index);\n const size = this.size();\n if (\n left < size &&\n this.compareFn(this.heap[element], this.heap[left]) === Compare.BIGGER_THAN\n ) {\n element = left;\n }\n if (\n right < size &&\n this.compareFn(this.heap[element], this.heap[right]) === Compare.BIGGER_THAN\n ) {\n element = right;\n }\n if (index !== element) {\n swap(this.heap, index, element);\n this.siftDown(element);\n }\n }\n siftUp(index) {\n let parent = this.getParentIndex(index);\n while (\n index > 0 &&\n this.compareFn(this.heap[parent], this.heap[index]) === Compare.BIGGER_THAN\n ) {\n swap(this.heap, parent, index);\n index = parent;\n parent = this.getParentIndex(index);\n }\n }\n extract() {\n if (this.isEmpty()) {\n return undefined;\n }\n if (this.size() === 1) {\n return this.heap.shift();\n }\n const removedValue = this.heap.shift();\n this.siftDown(0);\n return removedValue;\n }\n heapify(array) {\n if (array) {\n this.heap = array;\n }\n const maxIndex = Math.floor(this.size() / 2) - 1;\n for (let i = 0; i <= maxIndex; i++) {\n this.siftDown(i);\n }\n return this.heap;\n }\n getAsArray() {\n return this.heap;\n }\n}\nexport class MaxHeap extends MinHeap {\n constructor(compareFn = defaultCompare) {\n super(compareFn);\n this.compareFn = compareFn;\n this.compareFn = reverseCompare(compareFn);\n }\n}\n","export function fibonacci(n) {\n if (n < 1) {\n return 0;\n }\n if (n <= 2) {\n return 1;\n }\n return fibonacci(n - 1) + fibonacci(n - 2);\n}\n\nexport function fibonacciIterative(n) {\n if (n < 1) { return 0; }\n let fibNMinus2 = 0;\n let fibNMinus1 = 1;\n let fibN = n;\n for (let i = 2; i <= n; i++) {\n fibN = fibNMinus1 + fibNMinus2;\n fibNMinus2 = fibNMinus1;\n fibNMinus1 = fibN;\n }\n return fibN;\n}\n\nexport function fibonacciMemoization(n) {\n if (n < 1) { return 0; }\n const memo = [0, 1];\n const fibonacciMem = num => {\n if (memo[num] != null) { return memo[num]; }\n memo[num] = fibonacciMem(num - 1) + fibonacciMem(num - 2);\n return (memo[num] = fibonacciMem(num - 1) + fibonacciMem(num - 2));\n };\n return fibonacciMem(n);\n}\n","export function factorialIterative(number) {\n if (number < 0) {\n return undefined;\n }\n let total = 1;\n for (let n = number; n > 1; n--) {\n total *= n;\n }\n return total;\n}\n\nexport function factorial(n) {\n if (n < 0) {\n return undefined;\n }\n if (n === 1 || n === 0) {\n return 1;\n }\n return n * factorial(n - 1);\n}\n","import { ValuePair } from './value-pair';\n\nexport class ValuePairLazy extends ValuePair {\n constructor(key, value, isDeleted = false) {\n super(key, value);\n this.key = key;\n this.value = value;\n this.isDeleted = isDeleted;\n }\n}\n","import { defaultToString } from '../util';\nimport { ValuePairLazy } from './models/value-pair-lazy';\n\nexport default class HashTableLinearProbingLazy {\n constructor(toStrFn = defaultToString) {\n this.toStrFn = toStrFn;\n this.table = {};\n }\n loseloseHashCode(key) {\n if (typeof key === 'number') {\n return key;\n }\n const tableKey = this.toStrFn(key);\n let hash = 0;\n for (let i = 0; i < tableKey.length; i++) {\n hash += tableKey.charCodeAt(i);\n }\n return hash % 37;\n }\n hashCode(key) {\n return this.loseloseHashCode(key);\n }\n put(key, value) {\n if (key != null && value != null) {\n const position = this.hashCode(key);\n if (\n this.table[position] == null ||\n (this.table[position] != null && this.table[position].isDeleted)\n ) {\n this.table[position] = new ValuePairLazy(key, value);\n } else {\n let index = position + 1;\n while (this.table[index] != null && !this.table[position].isDeleted) {\n index++;\n }\n this.table[index] = new ValuePairLazy(key, value);\n }\n return true;\n }\n return false;\n }\n get(key) {\n const position = this.hashCode(key);\n if (this.table[position] != null) {\n if (this.table[position].key === key && !this.table[position].isDeleted) {\n return this.table[position].value;\n }\n let index = position + 1;\n while (\n this.table[index] != null &&\n (this.table[index].key !== key || this.table[index].isDeleted)\n ) {\n if (this.table[index].key === key && this.table[index].isDeleted) {\n return undefined;\n }\n index++;\n }\n if (\n this.table[index] != null &&\n this.table[index].key === key &&\n !this.table[index].isDeleted\n ) {\n return this.table[position].value;\n }\n }\n return undefined;\n }\n remove(key) {\n const position = this.hashCode(key);\n if (this.table[position] != null) {\n if (this.table[position].key === key && !this.table[position].isDeleted) {\n this.table[position].isDeleted = true;\n return true;\n }\n let index = position + 1;\n while (\n this.table[index] != null &&\n (this.table[index].key !== key || this.table[index].isDeleted)\n ) {\n index++;\n }\n if (\n this.table[index] != null &&\n this.table[index].key === key &&\n !this.table[index].isDeleted\n ) {\n this.table[index].isDeleted = true;\n return true;\n }\n }\n return false;\n }\n isEmpty() {\n return this.size() === 0;\n }\n size() {\n let count = 0;\n Object.values(this.table).forEach(valuePair => {\n count += valuePair.isDeleted === true ? 0 : 1;\n });\n return count;\n }\n clear() {\n this.table = {};\n }\n getTable() {\n return this.table;\n }\n toString() {\n if (this.isEmpty()) {\n return '';\n }\n const keys = Object.keys(this.table);\n let objString = `{${keys[0]} => ${this.table[keys[0]].toString()}}`;\n for (let i = 1; i < keys.length; i++) {\n objString = `${objString},{${keys[i]} => ${this.table[\n keys[i]\n ].toString()}}`;\n }\n return objString;\n }\n}\n","import { defaultToString } from '../util';\nimport { ValuePair } from './models/value-pair';\n\nexport default class HashTableLinearProbing {\n constructor(toStrFn = defaultToString) {\n this.toStrFn = toStrFn;\n this.table = {};\n }\n loseloseHashCode(key) {\n if (typeof key === 'number') {\n return key;\n }\n const tableKey = this.toStrFn(key);\n let hash = 0;\n for (let i = 0; i < tableKey.length; i++) {\n hash += tableKey.charCodeAt(i);\n }\n return hash % 37;\n }\n hashCode(key) {\n return this.loseloseHashCode(key);\n }\n put(key, value) {\n if (key != null && value != null) {\n const position = this.hashCode(key);\n if (this.table[position] == null) {\n this.table[position] = new ValuePair(key, value);\n } else {\n let index = position + 1;\n while (this.table[index] != null) {\n index++;\n }\n this.table[index] = new ValuePair(key, value);\n }\n return true;\n }\n return false;\n }\n get(key) {\n const position = this.hashCode(key);\n if (this.table[position] != null) {\n if (this.table[position].key === key) {\n return this.table[position].value;\n }\n let index = position + 1;\n while (this.table[index] != null && this.table[index].key !== key) {\n index++;\n }\n if (this.table[index] != null && this.table[index].key === key) {\n return this.table[position].value;\n }\n }\n return undefined;\n }\n remove(key) {\n const position = this.hashCode(key);\n if (this.table[position] != null) {\n if (this.table[position].key === key) {\n delete this.table[position];\n this.verifyRemoveSideEffect(key, position);\n return true;\n }\n let index = position + 1;\n while (this.table[index] != null && this.table[index].key !== key) {\n index++;\n }\n if (this.table[index] != null && this.table[index].key === key) {\n delete this.table[index];\n this.verifyRemoveSideEffect(key, index);\n return true;\n }\n }\n return false;\n }\n verifyRemoveSideEffect(key, removedPosition) {\n const hash = this.hashCode(key);\n let index = removedPosition + 1;\n while (this.table[index] != null) {\n const posHash = this.hashCode(this.table[index].key);\n if (posHash <= hash || posHash <= removedPosition) {\n this.table[removedPosition] = this.table[index];\n delete this.table[index];\n removedPosition = index;\n }\n index++;\n }\n }\n isEmpty() {\n return this.size() === 0;\n }\n size() {\n return Object.keys(this.table).length;\n }\n clear() {\n this.table = {};\n }\n getTable() {\n return this.table;\n }\n toString() {\n if (this.isEmpty()) {\n return '';\n }\n const keys = Object.keys(this.table);\n let objString = `{${keys[0]} => ${this.table[keys[0]].toString()}}`;\n for (let i = 1; i < keys.length; i++) {\n objString = `${objString},{${keys[i]} => ${this.table[\n keys[i]\n ].toString()}}`;\n }\n return objString;\n }\n}\n","import { defaultToString } from '../util';\nimport LinkedList from './linked-list';\nimport { ValuePair } from './models/value-pair';\n\nexport default class HashTableSeparateChaining {\n constructor(toStrFn = defaultToString) {\n this.toStrFn = toStrFn;\n this.table = {};\n }\n loseloseHashCode(key) {\n if (typeof key === 'number') {\n return key;\n }\n const tableKey = this.toStrFn(key);\n let hash = 0;\n for (let i = 0; i < tableKey.length; i++) {\n hash += tableKey.charCodeAt(i);\n }\n return hash % 37;\n }\n hashCode(key) {\n return this.loseloseHashCode(key);\n }\n put(key, value) {\n if (key != null && value != null) {\n const position = this.hashCode(key);\n if (this.table[position] == null) {\n this.table[position] = new LinkedList();\n }\n this.table[position].push(new ValuePair(key, value));\n return true;\n }\n return false;\n }\n get(key) {\n const position = this.hashCode(key);\n const linkedList = this.table[position];\n if (linkedList != null && !linkedList.isEmpty()) {\n let current = linkedList.getHead();\n while (current != null) {\n if (current.element.key === key) {\n return current.element.value;\n }\n current = current.next;\n }\n }\n return undefined;\n }\n remove(key) {\n const position = this.hashCode(key);\n const linkedList = this.table[position];\n if (linkedList != null && !linkedList.isEmpty()) {\n let current = linkedList.getHead();\n while (current != null) {\n if (current.element.key === key) {\n linkedList.remove(current.element);\n if (linkedList.isEmpty()) {\n delete this.table[position];\n }\n return true;\n }\n current = current.next;\n }\n }\n return false;\n }\n isEmpty() {\n return this.size() === 0;\n }\n size() {\n let count = 0;\n Object.values(this.table).forEach(linkedList => {\n count += linkedList.size();\n });\n return count;\n }\n clear() {\n this.table = {};\n }\n getTable() {\n return this.table;\n }\n toString() {\n if (this.isEmpty()) {\n return '';\n }\n const keys = Object.keys(this.table);\n let objString = `{${keys[0]} => ${this.table[keys[0]].toString()}}`;\n for (let i = 1; i < keys.length; i++) {\n objString = `${objString},{${keys[i]} => ${this.table[\n keys[i]\n ].toString()}}`;\n }\n return objString;\n }\n}\n","import { defaultToString } from '../util';\nimport { ValuePair } from './models/value-pair';\n\nexport default class HashTable {\n constructor(toStrFn = defaultToString) {\n this.toStrFn = toStrFn;\n this.table = {};\n }\n loseloseHashCode(key) {\n if (typeof key === 'number') {\n return key;\n }\n const tableKey = this.toStrFn(key);\n let hash = 0;\n for (let i = 0; i < tableKey.length; i++) {\n hash += tableKey.charCodeAt(i);\n }\n return hash % 37;\n }\n /* djb2HashCode(key) {\n const tableKey = this.toStrFn(key);\n let hash = 5381;\n for (let i = 0; i < tableKey.length; i++) {\n hash = (hash * 33) + tableKey.charCodeAt(i);\n }\n return hash % 1013;\n } */\n hashCode(key) {\n return this.loseloseHashCode(key);\n }\n put(key, value) {\n if (key != null && value != null) {\n const position = this.hashCode(key);\n this.table[position] = new ValuePair(key, value);\n return true;\n }\n return false;\n }\n get(key) {\n const valuePair = this.table[this.hashCode(key)];\n return valuePair == null ? undefined : valuePair.value;\n }\n remove(key) {\n const hash = this.hashCode(key);\n const valuePair = this.table[hash];\n if (valuePair != null) {\n delete this.table[hash];\n return true;\n }\n return false;\n }\n getTable() {\n return this.table;\n }\n isEmpty() {\n return this.size() === 0;\n }\n size() {\n return Object.keys(this.table).length;\n }\n clear() {\n this.table = {};\n }\n toString() {\n if (this.isEmpty()) {\n return '';\n }\n const keys = Object.keys(this.table);\n let objString = `{${keys[0]} => ${this.table[keys[0]].toString()}}`;\n for (let i = 1; i < keys.length; i++) {\n objString = `${objString},{${keys[i]} => ${this.table[keys[i]].toString()}}`;\n }\n return objString;\n }\n}\n","export default class Set {\n constructor() {\n this.items = {};\n }\n add(element) {\n if (!this.has(element)) {\n this.items[element] = element;\n return true;\n }\n return false;\n }\n delete(element) {\n if (this.has(element)) {\n delete this.items[element];\n return true;\n }\n return false;\n }\n has(element) {\n return Object.prototype.hasOwnProperty.call(this.items, element);\n }\n values() {\n return Object.values(this.items);\n }\n union(otherSet) {\n const unionSet = new Set();\n this.values().forEach(value => unionSet.add(value));\n otherSet.values().forEach(value => unionSet.add(value));\n return unionSet;\n }\n intersection(otherSet) {\n const intersectionSet = new Set();\n const values = this.values();\n const otherValues = otherSet.values();\n let biggerSet = values;\n let smallerSet = otherValues;\n if (otherValues.length - values.length > 0) {\n biggerSet = otherValues;\n smallerSet = values;\n }\n smallerSet.forEach(value => {\n if (biggerSet.includes(value)) {\n intersectionSet.add(value);\n }\n });\n return intersectionSet;\n }\n difference(otherSet) {\n const differenceSet = new Set();\n this.values().forEach(value => {\n if (!otherSet.has(value)) {\n differenceSet.add(value);\n }\n });\n return differenceSet;\n }\n isSubsetOf(otherSet) {\n if (this.size() > otherSet.size()) {\n return false;\n }\n let isSubset = true;\n this.values().every(value => {\n if (!otherSet.has(value)) {\n isSubset = false;\n return false;\n }\n return true;\n });\n return isSubset;\n }\n isEmpty() {\n return this.size() === 0;\n }\n size() {\n return Object.keys(this.items).length;\n }\n clear() {\n this.items = {};\n }\n toString() {\n if (this.isEmpty()) {\n return '';\n }\n const values = this.values();\n let objString = `${values[0]}`;\n for (let i = 1; i < values.length; i++) {\n objString = `${objString},${values[i].toString()}`;\n }\n return objString;\n }\n}\n","import DoublyLinkedList from './doubly-linked-list';\n\nexport default class StackLinkedList {\n constructor() {\n this.items = new DoublyLinkedList();\n }\n push(element) {\n this.items.push(element);\n }\n pop() {\n if (this.isEmpty()) {\n return undefined;\n }\n const result = this.items.removeAt(this.size() - 1);\n return result;\n }\n peek() {\n if (this.isEmpty()) {\n return undefined;\n }\n return this.items.getElementAt(this.size() - 1).element;\n }\n isEmpty() {\n return this.items.isEmpty();\n }\n size() {\n return this.items.size();\n }\n clear() {\n this.items.clear();\n }\n toString() {\n return this.items.toString();\n }\n}\n","import { Compare, defaultCompare, defaultEquals } from '../util';\nimport LinkedList from './linked-list';\n\nexport default class SortedLinkedList extends LinkedList {\n constructor(equalsFn = defaultEquals, compareFn = defaultCompare) {\n super(equalsFn);\n this.equalsFn = equalsFn;\n this.compareFn = compareFn;\n }\n push(element) {\n if (this.isEmpty()) {\n super.push(element);\n } else {\n const index = this.getIndexNextSortedElement(element);\n super.insert(element, index);\n }\n }\n insert(element, index = 0) {\n if (this.isEmpty()) {\n return super.insert(element, index === 0 ? index : 0);\n }\n const pos = this.getIndexNextSortedElement(element);\n return super.insert(element, pos);\n }\n getIndexNextSortedElement(element) {\n let current = this.head;\n let i = 0;\n for (; i < this.size() && current; i++) {\n const comp = this.compareFn(element, current.element);\n if (comp === Compare.LESS_THAN) {\n return i;\n }\n current = current.next;\n }\n return i;\n }\n}\n","import { defaultEquals } from '../util';\nimport LinkedList from './linked-list';\nimport { Node } from './models/linked-list-models';\n\nexport default class CircularLinkedList extends LinkedList {\n constructor(equalsFn = defaultEquals) {\n super(equalsFn);\n }\n push(element) {\n const node = new Node(element);\n let current;\n if (this.head == null) {\n this.head = node;\n } else {\n current = this.getElementAt(this.size() - 1);\n current.next = node;\n }\n // set node.next to head - to have circular list\n node.next = this.head;\n this.count++;\n }\n insert(element, index) {\n if (index >= 0 && index <= this.count) {\n const node = new Node(element);\n let current = this.head;\n if (index === 0) {\n if (this.head == null) {\n // if no node in list\n this.head = node;\n node.next = this.head;\n } else {\n node.next = current;\n current = this.getElementAt(this.size());\n // update last element\n this.head = node;\n current.next = this.head;\n }\n } else {\n const previous = this.getElementAt(index - 1);\n node.next = previous.next;\n previous.next = node;\n }\n this.count++;\n return true;\n }\n return false;\n }\n removeAt(index) {\n if (index >= 0 && index < this.count) {\n let current = this.head;\n if (index === 0) {\n if (this.size() === 1) {\n this.head = undefined;\n } else {\n const removed = this.head;\n current = this.getElementAt(this.size() - 1);\n this.head = this.head.next;\n current.next = this.head;\n current = removed;\n }\n } else {\n // no need to update last element for circular list\n const previous = this.getElementAt(index - 1);\n current = previous.next;\n previous.next = current.next;\n }\n this.count--;\n return current.element;\n }\n return undefined;\n }\n}\n","import Deque from '../data-structures/deque';\n\nexport function palindromeChecker(aString) {\n if (\n aString === undefined ||\n aString === null ||\n (aString !== null && aString.length === 0)\n ) {\n return false;\n }\n const deque = new Deque();\n const lowerString = aString.toLocaleLowerCase().split(' ').join('');\n let isEqual = true;\n let firstChar;\n let lastChar;\n\n for (let i = 0; i < lowerString.length; i++) {\n deque.addBack(lowerString.charAt(i));\n }\n\n while (deque.size() > 1 && isEqual) {\n firstChar = deque.removeFront();\n lastChar = deque.removeBack();\n if (firstChar !== lastChar) {\n isEqual = false;\n }\n }\n\n return isEqual;\n}\n","import Queue from '../data-structures/queue';\n\nexport function hotPotato(elementsList, num) {\n const queue = new Queue();\n const elimitatedList = [];\n\n for (let i = 0; i < elementsList.length; i++) {\n queue.enqueue(elementsList[i]);\n }\n\n while (queue.size() > 1) {\n for (let i = 0; i < num; i++) {\n queue.enqueue(queue.dequeue());\n }\n elimitatedList.push(queue.dequeue());\n }\n\n return {\n eliminated: elimitatedList,\n winner: queue.dequeue()\n };\n}\n","// @ts-check\nimport Stack from '../data-structures/stack';\n\nexport function parenthesesChecker(symbols) {\n const stack = new Stack();\n const opens = '([{';\n const closers = ')]}';\n let balanced = true;\n let index = 0;\n let symbol;\n let top;\n\n while (index < symbols.length && balanced) {\n symbol = symbols.charAt(index);\n if (opens.indexOf(symbol) >= 0) {\n stack.push(symbol);\n } else if (stack.isEmpty()) {\n balanced = false;\n } else {\n top = stack.pop();\n if (!(opens.indexOf(top) === closers.indexOf(symbol))) {\n balanced = false;\n }\n }\n index++;\n }\n if (balanced && stack.isEmpty()) {\n return true;\n }\n return false;\n}\n","// @ts-check\nimport Stack from '../data-structures/stack';\n\nexport function decimalToBinary(decNumber) {\n const remStack = new Stack();\n let number = decNumber;\n let rem;\n let binaryString = '';\n\n while (number > 0) {\n rem = Math.floor(number % 2);\n remStack.push(rem);\n number = Math.floor(number / 2);\n }\n\n while (!remStack.isEmpty()) {\n binaryString += remStack.pop().toString();\n }\n\n return binaryString;\n}\n\nexport function baseConverter(decNumber, base) {\n const remStack = new Stack();\n const digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';\n let number = decNumber;\n let rem;\n let baseString = '';\n\n if (!(base >= 2 && base <= 36)) {\n return '';\n }\n\n while (number > 0) {\n rem = Math.floor(number % base);\n remStack.push(rem);\n number = Math.floor(number / base);\n }\n\n while (!remStack.isEmpty()) {\n baseString += digits[remStack.pop()];\n }\n\n return baseString;\n}\n","// @ts-check\nimport Stack from '../data-structures/stack';\n\nfunction towerOfHanoi(plates, source, helper, dest, sourceName, helperName, destName, moves = []) {\n if (plates <= 0) {\n return moves;\n }\n if (plates === 1) {\n dest.push(source.pop());\n const move = {};\n move[sourceName] = source.toString();\n move[helperName] = helper.toString();\n move[destName] = dest.toString();\n moves.push(move);\n } else {\n towerOfHanoi(plates - 1, source, dest, helper, sourceName, destName, helperName, moves);\n dest.push(source.pop());\n const move = {};\n move[sourceName] = source.toString();\n move[helperName] = helper.toString();\n move[destName] = dest.toString();\n moves.push(move);\n towerOfHanoi(plates - 1, helper, source, dest, helperName, sourceName, destName, moves);\n }\n return moves;\n}\n\nexport function hanoiStack(plates) {\n const source = new Stack();\n const dest = new Stack();\n const helper = new Stack();\n\n for (let i = plates; i > 0; i--) {\n source.push(i);\n }\n\n return towerOfHanoi(plates, source, helper, dest, 'source', 'helper', 'dest');\n}\n\nexport function hanoi(plates, source, helper, dest, moves = []) {\n if (plates <= 0) {\n return moves;\n }\n if (plates === 1) {\n moves.push([source, dest]);\n } else {\n hanoi(plates - 1, source, dest, helper, moves);\n moves.push([source, dest]);\n hanoi(plates - 1, helper, source, dest, moves);\n }\n return moves;\n}\n","// @ts-check\n\nexport default class StackArray {\n constructor() {\n this.items = [];\n }\n push(element) {\n this.items.push(element);\n }\n\n pop() {\n return this.items.pop();\n }\n\n peek() {\n return this.items[this.items.length - 1];\n }\n\n isEmpty() {\n return this.items.length === 0;\n }\n\n size() {\n return this.items.length;\n }\n\n clear() {\n this.items = [];\n }\n\n toArray() {\n return this.items;\n }\n\n toString() {\n return this.items.toString();\n }\n}\n","import * as _util from './util';\n\n// chapters 05 and 07\nexport const util = _util;\n\n// chapter 03\nexport { default as StackArray } from './data-structures/stack-array';\nexport { default as Stack } from './data-structures/stack';\nexport { default as hanoi } from './others/hanoi';\nexport { default as hanoiStack } from './others/hanoi';\nexport { default as baseConverter } from './others/base-converter';\nexport { default as decimalToBinary } from './others/base-converter';\nexport { default as parenthesesChecker } from './others/balanced-symbols';\n\n// chapter 04\nexport { default as Queue } from './data-structures/queue';\nexport { default as Deque } from './data-structures/deque';\nexport { default as hotPotato } from './others/hot-potato';\nexport { default as palindromeChecker } from './others/palindrome-checker';\n\n// chapter 05\nexport { default as LinkedList } from './data-structures/linked-list';\nexport { default as DoublyLinkedList } from './data-structures/doubly-linked-list';\nexport { default as CircularLinkedList } from './data-structures/circular-linked-list';\nexport { default as SortedLinkedList } from './data-structures/sorted-linked-list';\nexport { default as StackLinkedList } from './data-structures/stack-linked-list';\n\n// chapter 06\nexport { default as Set } from './data-structures/set';\n\n// chapter 07\nexport { default as Dictionary } from './data-structures/dictionary';\nexport { default as HashTable } from './data-structures/hash-table';\nexport { default as HashTableSeparateChaining } from './data-structures/hash-table-separate-chaining';\nexport { default as HashTableLinearProbing } from './data-structures/hash-table-linear-probing';\nexport { default as HashTableLinearProbingLazy } from './data-structures/hash-table-linear-probing-lazy';\n\n// chapter 08\nexport { default as factorialIterative } from './others/factorial';\nexport { default as factorial } from './others/factorial';\nexport { default as fibonacci } from './others/fibonacci';\nexport { default as fibonacciIterative } from './others/fibonacci';\nexport { default as fibonacciMemoization } from './others/fibonacci';\n\n// chapter 09\nexport { default as BinarySearchTree } from './data-structures/binary-search-tree';\nexport { default as AVLTree } from './data-structures/avl-tree';\n\n// chapter 10\nexport { MinHeap } from './data-structures/heap';\nexport { MaxHeap } from './data-structures/heap';\nexport { default as heapSort } from './algorithms/sorting/heap-sort';\n\n// chapter 11\nexport { default as Graph } from './data-structures/graph';\nexport { breadthFirstSearch } from './algorithms/graph/breadth-first-search';\nexport { BFS } from './algorithms/graph/breadth-first-search';\nexport { depthFirstSearch } from './algorithms/graph/depth-first-search';\nexport { DFS } from './algorithms/graph/depth-first-search';\nexport { dijkstra } from './algorithms/graph/dijkstra';\nexport { floydWarshall } from './algorithms/graph/floyd-warshall';\nexport { prim } from './algorithms/graph/prim';\nexport { kruskal } from './algorithms/graph/kruskal';\n\n// chapter 12\nexport { shuffle } from './algorithms/shuffle/fisher–yates';\n\nexport { bubbleSort } from './algorithms/sorting/bubble-sort';\nexport { modifiedBubbleSort } from './algorithms/sorting/bubble-sort-improved';\nexport { bucketSort } from './algorithms/sorting/bucket-sort';\nexport { countingSort } from './algorithms/sorting/counting-sort';\nexport { insertionSort } from './algorithms/sorting/insertion-sort';\nexport { mergeSort } from './algorithms/sorting/merge-sort';\nexport { quickSort } from './algorithms/sorting/quicksort';\nexport { radixSort } from './algorithms/sorting/radix-sort';\nexport { selectionSort } from './algorithms/sorting/selection-sort';\nexport { shellSort } from './algorithms/sorting/shell-sort';\n\nexport { binarySearch } from './algorithms/search/binary-search';\nexport { interpolationSearch } from './algorithms/search/interpolation-search';\nexport { sequentialSearch } from './algorithms/search/sequential-search';\nexport { findMaxValue } from './algorithms/search/min-max-search';\nexport { findMinValue } from './algorithms/search/min-max-search';\n"],"sourceRoot":""} \ No newline at end of file diff --git a/examples/chapter10/01-UsingMinHeap.html b/examples/chapter10/01-UsingMinHeap.html new file mode 100644 index 00000000..83814ba1 --- /dev/null +++ b/examples/chapter10/01-UsingMinHeap.html @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/examples/chapter10/01-UsingMinHeap.js b/examples/chapter10/01-UsingMinHeap.js new file mode 100644 index 00000000..71f12f77 --- /dev/null +++ b/examples/chapter10/01-UsingMinHeap.js @@ -0,0 +1,25 @@ +const { MinHeap } = PacktDataStructuresAlgorithms; + +const heap = new MinHeap(); + +heap.insert(2); +heap.insert(3); +heap.insert(4); +heap.insert(5); + +heap.insert(1); + +console.log(heap.getAsArray()); + +console.log('Heap size: ', heap.size()); // 5 +console.log('Heap is empty: ', heap.isEmpty()); // false +console.log('Heap min value: ', heap.findMinimum()); // 1 + +heap.insert(6); +heap.insert(7); + +console.log(heap.getAsArray()); + +console.log('Extract minimum: ', heap.extract()); +console.log(heap.getAsArray()); + diff --git a/examples/chapter10/02-UsingMaxHeap.html b/examples/chapter10/02-UsingMaxHeap.html new file mode 100644 index 00000000..9d573bda --- /dev/null +++ b/examples/chapter10/02-UsingMaxHeap.html @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/examples/chapter10/02-UsingMaxHeap.js b/examples/chapter10/02-UsingMaxHeap.js new file mode 100644 index 00000000..f1a5b97d --- /dev/null +++ b/examples/chapter10/02-UsingMaxHeap.js @@ -0,0 +1,27 @@ +const { MaxHeap } = PacktDataStructuresAlgorithms; + +const maxHeap = new MaxHeap(); + +maxHeap.insert(2); +maxHeap.insert(3); +maxHeap.insert(4); +maxHeap.insert(5); + +maxHeap.insert(1); + +console.log(maxHeap.getAsArray()); + +console.log('Heap size: ', maxHeap.size()); // 5 +console.log('Heap is empty: ', maxHeap.isEmpty()); // false +console.log('Heap min value: ', maxHeap.findMinimum()); // 5 + +maxHeap.insert(6); +maxHeap.insert(9); +maxHeap.insert(10); +maxHeap.insert(14); + +console.log(maxHeap.getAsArray()); + +console.log('Extract minimum: ', maxHeap.extract()); +console.log(maxHeap.getAsArray()); + diff --git a/examples/chapter10/03-HeapSort.html b/examples/chapter10/03-HeapSort.html new file mode 100644 index 00000000..e52c43f4 --- /dev/null +++ b/examples/chapter10/03-HeapSort.html @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/examples/chapter10/03-HeapSort.js b/examples/chapter10/03-HeapSort.js new file mode 100644 index 00000000..9d9b1ac0 --- /dev/null +++ b/examples/chapter10/03-HeapSort.js @@ -0,0 +1,7 @@ +const { heapSort } = PacktDataStructuresAlgorithms; + +console.log('********** Heap Sort **********'); +const array = [7, 6, 3, 5, 4, 1, 2]; + +console.log('Before sorting: ', array); +console.log('After sorting: ', heapSort(array)); diff --git a/examples/chapter11/03-DFS.js b/examples/chapter11/03-DFS.js index c29686eb..5c440a74 100644 --- a/examples/chapter11/03-DFS.js +++ b/examples/chapter11/03-DFS.js @@ -46,9 +46,9 @@ graph.addEdge('C', 'F'); graph.addEdge('F', 'E'); const result = DFS(graph); -console.log(result.discovery); -console.log(result.finished); -console.log(result.predecessors); +console.log('discovery', result.discovery); +console.log('finished', result.finished); +console.log('predecessors', result.predecessors); const fTimes = result.finished; s = ''; diff --git a/examples/index.html b/examples/index.html index 5a9bb80c..4e1f4cb3 100644 --- a/examples/index.html +++ b/examples/index.html @@ -190,6 +190,19 @@ +
+ +
diff --git a/package.json b/package.json index 88846051..1f3edd60 100644 --- a/package.json +++ b/package.json @@ -75,6 +75,7 @@ "tslint": "^5.9.1", "typescript": "^2.7.2", "webpack": "^4.1.1", + "webpack-cli": "^2.0.12", "yargs": "^11.0.0" } } diff --git a/src/js/algorithms/graph/depth-first-search.js b/src/js/algorithms/graph/depth-first-search.js index 208925ce..8427659f 100644 --- a/src/js/algorithms/graph/depth-first-search.js +++ b/src/js/algorithms/graph/depth-first-search.js @@ -46,7 +46,7 @@ export const depthFirstSearch = (graph, callback) => { const DFSVisit = (u, color, d, f, p, time, adjList) => { // console.log('discovered ' + u); color[u] = Colors.GREY; - d[u] = ++time; + d[u] = ++time.count; const neighbors = adjList.get(u); for (let i = 0; i < neighbors.length; i++) { const w = neighbors[i]; @@ -56,7 +56,7 @@ const DFSVisit = (u, color, d, f, p, time, adjList) => { } } color[u] = Colors.BLACK; - f[u] = ++time; + f[u] = ++time.count; // console.log('explored ' + u); }; @@ -67,7 +67,7 @@ export const DFS = graph => { const d = {}; const f = {}; const p = {}; - const time = 0; + const time = { count: 0 }; for (let i = 0; i < vertices.length; i++) { f[vertices[i]] = 0; d[vertices[i]] = 0; diff --git a/src/js/data-structures/graph.js b/src/js/data-structures/graph.js index d838c4f6..d8c92044 100644 --- a/src/js/data-structures/graph.js +++ b/src/js/data-structures/graph.js @@ -20,7 +20,7 @@ export default class Graph { this.addVertex(b); } this.adjList.get(a).push(b); - if (!this.isDirected) { + if (this.isDirected !== true) { this.adjList.get(b).push(a); } } diff --git a/src/js/data-structures/heap.js b/src/js/data-structures/heap.js index edce1608..136d50df 100644 --- a/src/js/data-structures/heap.js +++ b/src/js/data-structures/heap.js @@ -26,7 +26,7 @@ export class MinHeap { clear() { this.heap = []; } - find() { + findMinimum() { return this.isEmpty() ? undefined : this.heap[0]; } insert(value) { @@ -43,12 +43,15 @@ export class MinHeap { const left = this.getLeftIndex(index); const right = this.getRightIndex(index); const size = this.size(); - if (left < size && this.compareFn(this.heap[element], this.heap[left]) > Compare.BIGGER_THAN) { + if ( + left < size && + this.compareFn(this.heap[element], this.heap[left]) === Compare.BIGGER_THAN + ) { element = left; } if ( right < size && - this.compareFn(this.heap[element], this.heap[right]) > Compare.BIGGER_THAN + this.compareFn(this.heap[element], this.heap[right]) === Compare.BIGGER_THAN ) { element = right; } @@ -59,7 +62,10 @@ export class MinHeap { } siftUp(index) { let parent = this.getParentIndex(index); - while (index > 0 && this.compareFn(this.heap[parent], this.heap[index]) > Compare.BIGGER_THAN) { + while ( + index > 0 && + this.compareFn(this.heap[parent], this.heap[index]) === Compare.BIGGER_THAN + ) { swap(this.heap, parent, index); index = parent; parent = this.getParentIndex(index); @@ -79,11 +85,15 @@ export class MinHeap { heapify(array) { if (array) { this.heap = array; - this.heap.unshift(null); // remove all null elements } - for (let i = this.size() - 1; i > 0; i--) { + const maxIndex = Math.floor(this.size() / 2) - 1; + for (let i = 0; i <= maxIndex; i++) { this.siftDown(i); } + return this.heap; + } + getAsArray() { + return this.heap; } } export class MaxHeap extends MinHeap { diff --git a/src/ts/data-structures/heap.ts b/src/ts/data-structures/heap.ts index 6dc06585..f2b269b3 100644 --- a/src/ts/data-structures/heap.ts +++ b/src/ts/data-structures/heap.ts @@ -103,6 +103,10 @@ export class MinHeap { return this.heap; } + + getAsArray() { + return this.heap; + } } export class MaxHeap extends MinHeap { diff --git a/test/ts/data-structures/heap.spec.ts b/test/ts/data-structures/heap.spec.ts index 7296bf63..abfdedc9 100644 --- a/test/ts/data-structures/heap.spec.ts +++ b/test/ts/data-structures/heap.spec.ts @@ -1,5 +1,5 @@ import 'mocha'; -// import { expect } from 'chai'; +import { expect } from 'chai'; import { MinHeap } from '../../../src/ts/index'; import { MaxHeap } from '../../../src/ts/data-structures/heap'; import heapSort from '../../../src/ts/algorithms/sorting/heap-sort'; @@ -15,12 +15,27 @@ describe('Heap', () => { }); - it('inserts elements in the AVLTree', () => { + it('inserts elements in the Heap', () => { + + let min = 2; - heap.insert(3); heap.insert(2); - heap.insert(1); + expect(heap.findMinimum()).to.equal(min); + heap.insert(3); + expect(heap.findMinimum()).to.equal(min); heap.insert(4); + expect(heap.findMinimum()).to.equal(min); + heap.insert(5); + expect(heap.findMinimum()).to.equal(min); + + heap.insert(1); + min = 1; + expect(heap.findMinimum()).to.equal(min); + + heap.insert(6); + heap.insert(9); + heap.insert(10); + heap.insert(14); heap.extract(); heap.extract(); diff --git a/webpack.config.js b/webpack.config.js index f7cdebe0..6d033ef4 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,7 +1,7 @@ // @ts-check /* eslint-disable */ const webpack = require('webpack'); -const UglifyJsPlugin = webpack.optimize.UglifyJsPlugin; +const UglifyJsPlugin = require('uglifyjs-webpack-plugin');; const path = require('path'); const env = require('yargs').argv.env; @@ -11,7 +11,7 @@ let plugins = [], outputFile; if (env === 'build') { - plugins.push(new UglifyJsPlugin({ minimize: true })); + // plugins.push(new UglifyJsPlugin({ minimize: true })); outputFile = libraryName + '.min.js'; } else { outputFile = libraryName + '.js'; @@ -40,7 +40,17 @@ const config = { modules: [path.resolve('./node_modules'), path.resolve('./src/js')], extensions: ['.json', '.js'] }, - plugins: plugins + optimization: { + minimizer: [ + // we specify a custom UglifyJsPlugin here to get source maps in production + new UglifyJsPlugin({ + cache: true, + parallel: true, + sourceMap: true + }) + ] + } + // plugins: plugins }; module.exports = config;