UNPKG

js-data-structures-and-algorithms

Version:
3 lines (2 loc) 17.7 kB
function t(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function e(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}function n(t,n,i){return n&&e(t.prototype,n),i&&e(t,i),Object.defineProperty(t,"prototype",{writable:!1}),t}function i(t){return function(t){if(Array.isArray(t))return r(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||function(t,e){if(!t)return;if("string"==typeof t)return r(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);"Object"===n&&t.constructor&&(n=t.constructor.name);if("Map"===n||"Set"===n)return Array.from(t);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return r(t,e)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function r(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,i=new Array(e);n<e;n++)i[n]=t[n];return i}var o=function(t,e,n){if(!(t instanceof Array)||null==e)return-1;for(var r=i(t),o=1,l=0;r.length>0;){var a=Math.floor(r.length/2);if(l+=a,n&&console.log("iteration ".concat(o,": midpoint index: ").concat(l,"; array to search: ").concat(r.join(", "),"; ").concat(e," === ").concat(r[a]," ? ... ").concat(e===r[a],"!")),r[a]===e)return l;r[a]>e?(l-=a,r=r.slice(0,a)):(l+=1,r=r.slice(a+1)),o++}return-1},l=function(t,e,n){if(!(t instanceof Array)||null==e)return-1;for(var i=0;i<t.length;i++)if(n&&console.log("iteration ".concat(i+1,": ").concat(e," === ").concat(t[i]," ? ... ").concat(e===t[i],"!")),e===t[i])return i;return-1},a=function(t,e,n){if("string"!=typeof t||"string"!=typeof e)return n&&console.log("bad input, exiting early"),-1;var i=e.length,r=t.length;if(i>r)return n&&console.log("needle is longer than the haystack, exiting early"),-1;for(var o,l=i-1,a={},u=0;u<i;u++)a[e[u]]=l-u;for(var s=0,h=0;r>=i;){for(h=l;t[s+h]===e[h];h--)if(0===h)return s;r-=o=a[t[s+l]]||i,s+=o}return-1},u=function(t,e,n){if("string"!=typeof t||"string"!=typeof e)return n&&console.log("bad input, exiting early"),-1;if(e.length>t.length)return n&&console.log("needle is longer than the haystack, exiting early"),-1;for(var i=0;i<t.length;i++)for(var r=0;r<e.length&&t[i+r]===e[r];r++)if(r===e.length-1)return i;return-1},s=function(){function e(){t(this,e),this.items=[],this.length=0}return n(e,[{key:"add",value:function(t){return-1===this.items.indexOf(t)&&(this.items.push(t),this.length++),t}},{key:"remove",value:function(t){var e=this.items.indexOf(t);return-1!==e&&(this.items.splice(e,1),this.length--),t}},{key:"has",value:function(t){return-1!==this.items.indexOf(t)}},{key:"isEmpty",value:function(){return 0===this.length}},{key:"size",value:function(){return this.length}},{key:"enumerate",value:function(){return this.items}},{key:"clear",value:function(){return this.length=0,this.items=[],this.items}}]),e}(),h=function(t,e){var n=new s;return t.enumerate().forEach((function(t){e.has(t)&&n.add(t)})),e.enumerate().forEach((function(e){t.has(e)&&n.add(e)})),n},c=function(t,e){var n=new s;return t.enumerate().forEach((function(t){e.has(t)||n.add(t)})),n},f=function(t,e){var n=new s;return t.enumerate().forEach((function(t){return n.add(t)})),e.enumerate().forEach((function(t){return n.add(t)})),n},v=function(t,e){var n=h(t,e),i=f(t,e);return c(i,n)},g=function(t,e){var n=i(t),r=!1,o=0;e&&console.log("starting array: ".concat(n.join(" ")));do{r=!1,o++;for(var l=0;l<n.length-1;l++)if(n[l]>n[l+1]){var a=n[l];n[l]=n[l+1],n[l+1]=a,r=!0}e&&console.log("iteration ".concat(o,": array: ").concat(n.join(" ")))}while(r);return n},d=function(t,e,n,i){var r=[],o=[],l=e,a=n;if(i&&console.log("starting array: ".concat(t.join(" "))),void 0===l||void 0===a){i&&console.log("getting min and max values of the input array");for(var u=0;u<t.length;u++){var s=t[u];0===u&&(l=s,a=s),s<l&&(l=s),s>a&&(a=s)}}i&&console.log("min value: ".concat(l,"; max value: ").concat(a));for(var h=a-l+1,c=0;c<h;c++)o.push(0);i&&console.log("initialized count array: ".concat(o.join(" ")));for(var f=0;f<t.length;f++){o[t[f]-l]++}i&&console.log("populated count array: ".concat(o.join(" ")));for(var v=0;v<o.length;v++)for(;o[v]>0;)r.push(v+l),o[v]--;return i&&console.log("sorted output array: ".concat(r.join(" "))),r},y=function(t,e){var n=i(t);e&&console.log("starting array: ".concat(n.join(" ")));for(var r=1;r<n.length;r++){for(var o=n.splice(r,1)[0],l=!1,a=r;a>=1;a--)if(e&&console.log(" comparing ".concat(o," with ").concat(n[a-1])),o>n[a-1]){e&&console.log(" inserting ".concat(o," after ").concat(n[a-1])),n=[].concat(i(n.slice(0,a)),[o],i(n.slice(a))),l=!0;break}l||(e&&console.log(" inserting ".concat(o," at first index")),n=[o].concat(i(n))),e&&console.log("iteration ".concat(r+1,": array: ").concat(n.join(" ")))}return n},m=function t(e,n){if(n&&console.log("current array: ".concat(e.join(" "))),e.length<2)return n&&console.log(" array only has 0 or 1 items, so nothing to sort!"),e;n&&console.log(" splitting the array down the middle!");var i=Math.floor(e.length/2),r=t(e.slice(0,i),n),o=t(e.slice(i),n);return k(r,o,n)},k=function(t,e,n){n&&console.log("merging arrays [".concat(t.join(" "),"] and [").concat(e.join(" "),"]"));for(var i=[];t.length>0&&e.length>0;)i.push(t[0]<e[0]?t.shift():e.shift()),n&&console.log(" current merge result from the two sub-arrays: ".concat(i.join(" ")));var r=i.concat(t.length?t:e);return n&&console.log(" final merge result from the two sub-arrays: ".concat(r.join(" "))),r},p=function t(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:e.length-1,r=arguments.length>3?arguments[3]:void 0;if(r&&console.log("current array: ".concat(e.join(" "))),e.length<2)return r&&console.log(" array only has 0 or 1 items, so nothing to sort!"),e;var o=A(e,n,i,r);return n<o-1&&t(e,n,o-1,r),o<i&&t(e,o,i,r),e},x=function(t,e,n,i){i&&console.log(" swapping elements with values ".concat(t[e]," and ").concat(t[n]));var r=t[e];t[e]=t[n],t[n]=r},A=function(t,e,n,i){var r=t[Math.floor((n+e)/2)];i&&console.log(" pivot element chosen is ".concat(r));var o=e,l=n;for(i&&console.log(" current left element: ".concat(t[o],", current right element: ").concat(t[l]));o<=l;){for(;t[o]<r;)i&&console.log(" left element ".concat(t[o]," is less than ").concat(r,", moving right by one element to ").concat(t[o+1])),o++;for(i&&console.log(" left element ".concat(t[o]," is greater than or equal to ").concat(r,", so ").concat(t[o]," is eligible to be swapped"));t[l]>r;)i&&console.log(" right element ".concat(t[l]," is greater than ").concat(r,", moving left by one element to ").concat(t[l-1])),l--;i&&console.log(" right element ".concat(t[l]," is less than or equal to ").concat(r,", so ").concat(t[l]," is eligible to be swapped")),o<=l?(i&&console.log(" left element ".concat(t[o]," is less than or equal to right element ").concat(t[l],", so we can swap these two elements")),x(t,o,l,i),o++,l--,i&&console.log(" resulting array: ".concat(t.join(" ")))):i&&console.log(" left element ".concat(t[o]," is NOT less than or equal to right element ").concat(t[l],", so we will NOT swap these two elements"))}return o},w=function(t,e){var n=i(t);e&&console.log("starting array: ".concat(n.join(" ")));for(var r=0;r<n.length-1;r++){for(var o=r,l=r;l<n.length;l++)n[l]<n[o]&&(o=l);var a=n[r];n[r]=n[o],n[o]=a,e&&console.log("iteration ".concat(r+1,": array: ").concat(n.join(" ")))}return n},b=n((function e(n){t(this,e),this.val=n,this.left=null,this.right=null})),N=function(){function e(){t(this,e),this.root=null}return n(e,[{key:"insert",value:function(t){var e=new b(t);return null===this.root?(this.root=e,this.root):(this._insertNode(this.root,e),e)}},{key:"_insertNode",value:function(t,e){return t.val>e.val&&null===t.left?(t.left=e,e):t.val>e.val?this._insertNode(t.left,e):(t.val<e.val||t.val===e.val)&&null===t.right?(t.right=e,e):this._insertNode(t.right,e)}},{key:"contains",value:function(t){var e=!1;if(null===this.root)return e;return function n(i){i.val===t?e=!0:null!==i.left&&t<i.val?n(i.left):null!==i.right&&t>i.val&&n(i.right)}(this.root),e}},{key:"remove",value:function(t){return this.root=this._removeNode(this.root,t),this.root}},{key:"_removeNode",value:function(t,e){if(null===t)return null;if(e<t.val)return t.left=this._removeNode(t.left,e),t;if(e>t.val)return t.right=this._removeNode(t.right,e),t;if(null===t.left&&null===t.right)return t=null;if(null===t.left)return t=t.right;if(null===t.right)return t=t.left;var n=this._findMinNode(t.right);return t.val=n.val,t.right=this._removeNode(t.right,n.val),t}},{key:"inOrderTraversal",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.root,e=arguments.length>1?arguments[1]:void 0;null!==t&&(this.inOrderTraversal(t.left,e),"function"==typeof e&&e(t),this.inOrderTraversal(t.right,e))}},{key:"preOrderTraversal",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.root,e=arguments.length>1?arguments[1]:void 0;null!==t&&("function"==typeof e&&e(t),this.preOrderTraversal(t.left,e),this.preOrderTraversal(t.right,e))}},{key:"postOrderTraversal",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.root,e=arguments.length>1?arguments[1]:void 0;null!==t&&(this.postOrderTraversal(t.left,e),this.postOrderTraversal(t.right,e),"function"==typeof e&&e(t))}},{key:"isEmpty",value:function(){return null===this.root}},{key:"clear",value:function(){return this.root=null,this.root}},{key:"_findMinNode",value:function(t){return null===t.left?t:this._findMinNode(t.left)}}]),e}(),j=n((function e(n){var i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null;t(this,e),this.val=n,this.next=i,this.prev=r})),E=function(){function e(){t(this,e),this.head=null,this.tail=null,this.length=0}return n(e,[{key:"insertAtBeginning",value:function(t){var e=new j(t);return this.head?(this.head.prev=e,e.next=this.head,this.head=e,this.length++,this.head):(this.head=e,this.tail=e,this.length++,this.head)}},{key:"insertAtEnd",value:function(t){var e=new j(t);return this.head?(e.prev=this.tail,this.tail.next=e,this.tail=e,this.length++,this.head):this.insertAtBeginning(t)}},{key:"insertAt",value:function(t,e){if(!this.head)return this.insertAtBeginning(t);if(0===e)return this.insertAtBeginning(t);if(e>=this.length)return this.insertAtEnd(t);var n=this.getAt(e-1),i=new j(t);return i.next=n.next,i.prev=n,n.next=i,this.length++,this.head}},{key:"getAt",value:function(t){for(var e=0,n=this.head;n;){if(e===t)return n;e++,n=n.next}return null}},{key:"deleteFirstNode",value:function(){return this.head?this.head.next?(this.head=this.head.next,this.head.prev=null,this.length--,this.head):(this.head=null,this.tail=null,this.length--,this.head):this.head}},{key:"deleteLastNode",value:function(){return this.head?this.head.next?(this.tail=this.tail.prev,this.tail.next=null,this.length--,this.head):(this.head=null,this.tail=null,this.length--,this.head):this.head}},{key:"deleteAt",value:function(t){if(!this.head)return this.head;if(0===t)return this.deleteFirstNode();if(t===this.length-1)return this.deleteLastNode();var e=this.getAt(t-1);return e&&e.next?(e.next=e.next.next,e.next.prev=e,this.length--,this.head):this.head}},{key:"reverse",value:function(){for(var t=this.head,e=null;null!==t;){var n=t.next;t.next=e,t.prev=n,e=t,t=n}this.tail=this.head,this.head=e}},{key:"traverse",value:function(t){if("function"!=typeof t)return!1;for(var e=this.head;null!==e;)t(e),e=e.next;return!0}},{key:"traverseReverse",value:function(t){if("function"!=typeof t)return!1;for(var e=this.tail;null!==e;)t(e),e=e.prev;return!0}},{key:"isEmpty",value:function(){return 0===this.length}},{key:"size",value:function(){return this.length}},{key:"enumerate",value:function(){for(var t=[],e=this.head;e;)t.push(e.val),e=e.next;return t}},{key:"clear",value:function(){return this.head=null,this.tail=null,this.length=0,this.head}}]),e}(),q=n((function e(n){var i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;t(this,e),this.val=n,this.next=i})),O=function(){function e(){t(this,e),this.head=null,this.length=0}return n(e,[{key:"insertAtBeginning",value:function(t){var e=new q(t);return e.next=this.head,this.head=e,this.length++,this.head}},{key:"insertAtEnd",value:function(t){var e=new q(t);if(!this.head)return this.head=e,this.length++,this.head;for(var n=this.head;null!==n.next;)n=n.next;return n.next=e,this.length++,this.head}},{key:"insertAt",value:function(t,e){if(!this.head)return this.insertAtBeginning(t);if(0===e)return this.insertAtBeginning(t);if(e>=this.length)return this.insertAtEnd(t);var n=this.getAt(e-1),i=new q(t);return i.next=n.next,n.next=i,this.length++,this.head}},{key:"getAt",value:function(t){for(var e=0,n=this.head;n;){if(e===t)return n;e++,n=n.next}return null}},{key:"deleteFirstNode",value:function(){return this.head?(this.head=this.head.next,this.length--,this.head):this.head}},{key:"deleteLastNode",value:function(){if(!this.head)return this.head;if(!this.head.next)return this.head=null,this.length--,this.head;for(var t=this.head,e=this.head.next;null!==e.next;)t=e,e=e.next;return t.next=null,this.length--,this.head}},{key:"deleteAt",value:function(t){if(!this.head)return this.head;if(0===t)return this.deleteFirstNode();var e=this.getAt(t-1);return e&&e.next?(e.next=e.next.next,this.length--,this.head):this.head}},{key:"reverse",value:function(){for(var t=this.head,e=null;null!==t;){var n=t.next;t.next=e,e=t,t=n}return this.head=e,!0}},{key:"traverse",value:function(t){if("function"!=typeof t)return!1;for(var e=this.head;null!==e;)t(e),e=e.next;return!0}},{key:"isEmpty",value:function(){return 0===this.length}},{key:"size",value:function(){return this.length}},{key:"enumerate",value:function(){for(var t=[],e=this.head;e;)t.push(e.val),e=e.next;return t}},{key:"clear",value:function(){return this.head=null,this.length=0,this.head}}]),e}(),T=function(){function e(){t(this,e),this.items=[],this.length=0}return n(e,[{key:"enqueue",value:function(t){for(var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,n=0;n<this.length;n++)if(e<this.items[n].priority){var i={value:t,priority:e};return this.items.splice(n,0,i),this.length++,i}var r={value:t,priority:e};return this.items.push(r),this.length++,r}},{key:"dequeue",value:function(){return this.length?(this.length--,this.items.shift()):null}},{key:"peek",value:function(){return this.items[0]||null}},{key:"isEmpty",value:function(){return 0===this.length}},{key:"size",value:function(){return this.length}},{key:"enumerate",value:function(){return this.items}},{key:"clear",value:function(){return this.length=0,this.items=[],this.items}}]),e}(),_=function(){function e(){t(this,e),this.items=[],this.length=0}return n(e,[{key:"enqueue",value:function(t){return this.items.push(t),this.length++,t}},{key:"dequeue",value:function(){return this.length?(this.length--,this.items.shift()):null}},{key:"peek",value:function(){return this.items[0]||null}},{key:"isEmpty",value:function(){return 0===this.length}},{key:"size",value:function(){return this.length}},{key:"enumerate",value:function(){return this.items}},{key:"clear",value:function(){return this.length=0,this.items=[],this.items}}]),e}(),z=function(){function e(){t(this,e),this.queue=new E}return n(e,[{key:"enqueue",value:function(t){return this.queue.insertAtEnd(t),t}},{key:"dequeue",value:function(){var t=this.queue.getAt(0);return this.queue.deleteFirstNode(),t?t.val:null}},{key:"peek",value:function(){var t=this.queue.getAt(0);return t?t.val:null}},{key:"isEmpty",value:function(){return this.queue.isEmpty()}},{key:"size",value:function(){return this.queue.size()}},{key:"enumerate",value:function(){return this.queue.enumerate()}},{key:"clear",value:function(){return this.queue.clear(),[]}}]),e}(),B=function(){function e(){t(this,e),this.items=[],this.length=0}return n(e,[{key:"push",value:function(t){return this.items.unshift(t),this.length++,t}},{key:"pop",value:function(){return this.length?(this.length--,this.items.shift()):null}},{key:"peek",value:function(){return this.items[0]||null}},{key:"isEmpty",value:function(){return 0===this.length}},{key:"size",value:function(){return this.length}},{key:"enumerate",value:function(){return this.items}},{key:"clear",value:function(){return this.length=0,this.items=[],this.items}}]),e}(),L=function(){function e(){t(this,e),this.stack=new O}return n(e,[{key:"push",value:function(t){return this.stack.insertAtBeginning(t),t}},{key:"pop",value:function(){var t=this.stack.getAt(0);return this.stack.deleteFirstNode(),t?t.val:null}},{key:"peek",value:function(){var t=this.stack.getAt(0);return t?t.val:null}},{key:"isEmpty",value:function(){return this.stack.isEmpty()}},{key:"size",value:function(){return this.stack.size()}},{key:"enumerate",value:function(){return this.stack.enumerate()}},{key:"clear",value:function(){return this.stack.clear(),[]}}]),e}();export{N as BinarySearchTree,b as BinarySearchTreeNode,E as DoublyLinkedList,j as DoublyLinkedListNode,O as LinkedList,q as LinkedListNode,T as PriorityQueue,_ as Queue,z as QueueFromDoublyLinkedList,s as Set,B as Stack,L as StackFromLinkedList,o as binarySearch,a as boyerMooreHorspoolSearch,g as bubbleSort,d as countingSort,y as insertionSort,h as intersection,l as linearSearch,m as mergeSort,u as naiveSearch,p as quickSort,w as selectionSort,c as setDifference,v as symmetricDifference,f as union}; //# sourceMappingURL=main.esm.min.js.map