UNPKG

js-data-structures-and-algorithms

Version:
3 lines (2 loc) 17.9 kB
"use strict";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 r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}function n(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),Object.defineProperty(t,"prototype",{writable:!1}),t}function r(t){return function(t){if(Array.isArray(t))return i(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 i(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 i(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 i(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}Object.defineProperty(exports,"__esModule",{value:!0});var o=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}(),l=function(t,e){var n=new o;return t.enumerate().forEach((function(t){e.has(t)&&n.add(t)})),e.enumerate().forEach((function(e){t.has(e)&&n.add(e)})),n},a=function(t,e){var n=new o;return t.enumerate().forEach((function(t){e.has(t)||n.add(t)})),n},s=function(t,e){var n=new o;return t.enumerate().forEach((function(t){return n.add(t)})),e.enumerate().forEach((function(t){return n.add(t)})),n},u=function(t,e,n){n&&console.log("merging arrays [".concat(t.join(" "),"] and [").concat(e.join(" "),"]"));for(var r=[];t.length>0&&e.length>0;)r.push(t[0]<e[0]?t.shift():e.shift()),n&&console.log(" current merge result from the two sub-arrays: ".concat(r.join(" ")));var i=r.concat(t.length?t:e);return n&&console.log(" final merge result from the two sub-arrays: ".concat(i.join(" "))),i},h=function(t,e,n,r){r&&console.log(" swapping elements with values ".concat(t[e]," and ").concat(t[n]));var i=t[e];t[e]=t[n],t[n]=i},c=function(t,e,n,r){var i=t[Math.floor((n+e)/2)];r&&console.log(" pivot element chosen is ".concat(i));var o=e,l=n;for(r&&console.log(" current left element: ".concat(t[o],", current right element: ").concat(t[l]));o<=l;){for(;t[o]<i;)r&&console.log(" left element ".concat(t[o]," is less than ").concat(i,", moving right by one element to ").concat(t[o+1])),o++;for(r&&console.log(" left element ".concat(t[o]," is greater than or equal to ").concat(i,", so ").concat(t[o]," is eligible to be swapped"));t[l]>i;)r&&console.log(" right element ".concat(t[l]," is greater than ").concat(i,", moving left by one element to ").concat(t[l-1])),l--;r&&console.log(" right element ".concat(t[l]," is less than or equal to ").concat(i,", so ").concat(t[l]," is eligible to be swapped")),o<=l?(r&&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")),h(t,o,l,r),o++,l--,r&&console.log(" resulting array: ".concat(t.join(" ")))):r&&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},f=n((function e(n){t(this,e),this.val=n,this.left=null,this.right=null})),v=function(){function e(){t(this,e),this.root=null}return n(e,[{key:"insert",value:function(t){var e=new f(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(r){r.val===t?e=!0:null!==r.left&&t<r.val?n(r.left):null!==r.right&&t>r.val&&n(r.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}(),g=n((function e(n){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null;t(this,e),this.val=n,this.next=r,this.prev=i})),d=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 g(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 g(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),r=new g(t);return r.next=n.next,r.prev=n,n.next=r,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}(),y=n((function e(n){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;t(this,e),this.val=n,this.next=r})),p=function(){function e(){t(this,e),this.head=null,this.length=0}return n(e,[{key:"insertAtBeginning",value:function(t){var e=new y(t);return e.next=this.head,this.head=e,this.length++,this.head}},{key:"insertAtEnd",value:function(t){var e=new y(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),r=new y(t);return r.next=n.next,n.next=r,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}(),m=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 r={value:t,priority:e};return this.items.splice(n,0,r),this.length++,r}var i={value:t,priority:e};return this.items.push(i),this.length++,i}},{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}(),k=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}(),x=function(){function e(){t(this,e),this.queue=new d}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}(),A=function(){function e(){t(this,e),this.stack=new p}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}();exports.BinarySearchTree=v,exports.BinarySearchTreeNode=f,exports.DoublyLinkedList=d,exports.DoublyLinkedListNode=g,exports.LinkedList=p,exports.LinkedListNode=y,exports.PriorityQueue=m,exports.Queue=k,exports.QueueFromDoublyLinkedList=x,exports.Set=o,exports.Stack=b,exports.StackFromLinkedList=A,exports.binarySearch=function(t,e,n){if(!(t instanceof Array)||null==e)return-1;for(var i=r(t),o=1,l=0;i.length>0;){var a=Math.floor(i.length/2);if(l+=a,n&&console.log("iteration ".concat(o,": midpoint index: ").concat(l,"; array to search: ").concat(i.join(", "),"; ").concat(e," === ").concat(i[a]," ? ... ").concat(e===i[a],"!")),i[a]===e)return l;i[a]>e?(l-=a,i=i.slice(0,a)):(l+=1,i=i.slice(a+1)),o++}return-1},exports.boyerMooreHorspoolSearch=function(t,e,n){if("string"!=typeof t||"string"!=typeof e)return n&&console.log("bad input, exiting early"),-1;var r=e.length,i=t.length;if(r>i)return n&&console.log("needle is longer than the haystack, exiting early"),-1;for(var o,l=r-1,a={},s=0;s<r;s++)a[e[s]]=l-s;for(var u=0,h=0;i>=r;){for(h=l;t[u+h]===e[h];h--)if(0===h)return u;i-=o=a[t[u+l]]||r,u+=o}return-1},exports.bubbleSort=function(t,e){var n=r(t),i=!1,o=0;e&&console.log("starting array: ".concat(n.join(" ")));do{i=!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,i=!0}e&&console.log("iteration ".concat(o,": array: ").concat(n.join(" ")))}while(i);return n},exports.countingSort=function(t,e,n,r){var i=[],o=[],l=e,a=n;if(r&&console.log("starting array: ".concat(t.join(" "))),void 0===l||void 0===a){r&&console.log("getting min and max values of the input array");for(var s=0;s<t.length;s++){var u=t[s];0===s&&(l=u,a=u),u<l&&(l=u),u>a&&(a=u)}}r&&console.log("min value: ".concat(l,"; max value: ").concat(a));for(var h=a-l+1,c=0;c<h;c++)o.push(0);r&&console.log("initialized count array: ".concat(o.join(" ")));for(var f=0;f<t.length;f++){o[t[f]-l]++}r&&console.log("populated count array: ".concat(o.join(" ")));for(var v=0;v<o.length;v++)for(;o[v]>0;)i.push(v+l),o[v]--;return r&&console.log("sorted output array: ".concat(i.join(" "))),i},exports.insertionSort=function(t,e){var n=r(t);e&&console.log("starting array: ".concat(n.join(" ")));for(var i=1;i<n.length;i++){for(var o=n.splice(i,1)[0],l=!1,a=i;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(r(n.slice(0,a)),[o],r(n.slice(a))),l=!0;break}l||(e&&console.log(" inserting ".concat(o," at first index")),n=[o].concat(r(n))),e&&console.log("iteration ".concat(i+1,": array: ").concat(n.join(" ")))}return n},exports.intersection=l,exports.linearSearch=function(t,e,n){if(!(t instanceof Array)||null==e)return-1;for(var r=0;r<t.length;r++)if(n&&console.log("iteration ".concat(r+1,": ").concat(e," === ").concat(t[r]," ? ... ").concat(e===t[r],"!")),e===t[r])return r;return-1},exports.mergeSort=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 r=Math.floor(e.length/2),i=t(e.slice(0,r),n),o=t(e.slice(r),n);return u(i,o,n)},exports.naiveSearch=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 r=0;r<t.length;r++)for(var i=0;i<e.length&&t[r+i]===e[i];i++)if(i===e.length-1)return r;return-1},exports.quickSort=function t(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:e.length-1,i=arguments.length>3?arguments[3]:void 0;if(i&&console.log("current array: ".concat(e.join(" "))),e.length<2)return i&&console.log(" array only has 0 or 1 items, so nothing to sort!"),e;var o=c(e,n,r,i);return n<o-1&&t(e,n,o-1,i),o<r&&t(e,o,r,i),e},exports.selectionSort=function(t,e){var n=r(t);e&&console.log("starting array: ".concat(n.join(" ")));for(var i=0;i<n.length-1;i++){for(var o=i,l=i;l<n.length;l++)n[l]<n[o]&&(o=l);var a=n[i];n[i]=n[o],n[o]=a,e&&console.log("iteration ".concat(i+1,": array: ").concat(n.join(" ")))}return n},exports.setDifference=a,exports.symmetricDifference=function(t,e){var n=l(t,e),r=s(t,e);return a(r,n)},exports.union=s; //# sourceMappingURL=main.cjs.min.js.map