UNPKG

js-data-structures-and-algorithms

Version:
3 lines (2 loc) 18 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).jsDsa={})}(this,(function(t){"use strict";function e(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function n(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 i(t,e,i){return e&&n(t.prototype,e),i&&n(t,i),Object.defineProperty(t,"prototype",{writable:!1}),t}function r(t){return function(t){if(Array.isArray(t))return o(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 o(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 o(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 o(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 l=function(){function t(){e(this,t),this.items=[],this.length=0}return i(t,[{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}}]),t}(),a=function(t,e){var n=new l;return t.enumerate().forEach((function(t){e.has(t)&&n.add(t)})),e.enumerate().forEach((function(e){t.has(e)&&n.add(e)})),n},u=function(t,e){var n=new l;return t.enumerate().forEach((function(t){e.has(t)||n.add(t)})),n},s=function(t,e){var n=new l;return t.enumerate().forEach((function(t){return n.add(t)})),e.enumerate().forEach((function(t){return n.add(t)})),n},h=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},c=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},f=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")),c(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},v=i((function t(n){e(this,t),this.val=n,this.left=null,this.right=null})),g=function(){function t(){e(this,t),this.root=null}return i(t,[{key:"insert",value:function(t){var e=new v(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)}}]),t}(),d=i((function t(n){var i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null;e(this,t),this.val=n,this.next=i,this.prev=r})),y=function(){function t(){e(this,t),this.head=null,this.tail=null,this.length=0}return i(t,[{key:"insertAtBeginning",value:function(t){var e=new d(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 d(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 d(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}}]),t}(),m=i((function t(n){var i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;e(this,t),this.val=n,this.next=i})),k=function(){function t(){e(this,t),this.head=null,this.length=0}return i(t,[{key:"insertAtBeginning",value:function(t){var e=new m(t);return e.next=this.head,this.head=e,this.length++,this.head}},{key:"insertAtEnd",value:function(t){var e=new m(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 m(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}}]),t}(),p=function(){function t(){e(this,t),this.items=[],this.length=0}return i(t,[{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}}]),t}(),x=function(){function t(){e(this,t),this.items=[],this.length=0}return i(t,[{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}}]),t}(),b=function(){function t(){e(this,t),this.queue=new y}return i(t,[{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(),[]}}]),t}(),A=function(){function t(){e(this,t),this.items=[],this.length=0}return i(t,[{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}}]),t}(),w=function(){function t(){e(this,t),this.stack=new k}return i(t,[{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(),[]}}]),t}();t.BinarySearchTree=g,t.BinarySearchTreeNode=v,t.DoublyLinkedList=y,t.DoublyLinkedListNode=d,t.LinkedList=k,t.LinkedListNode=m,t.PriorityQueue=p,t.Queue=x,t.QueueFromDoublyLinkedList=b,t.Set=l,t.Stack=A,t.StackFromLinkedList=w,t.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},t.boyerMooreHorspoolSearch=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},t.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},t.countingSort=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},t.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},t.intersection=a,t.linearSearch=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},t.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 i=Math.floor(e.length/2),r=t(e.slice(0,i),n),o=t(e.slice(i),n);return h(r,o,n)},t.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 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},t.quickSort=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=f(e,n,i,r);return n<o-1&&t(e,n,o-1,r),o<i&&t(e,o,i,r),e},t.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},t.setDifference=u,t.symmetricDifference=function(t,e){var n=a(t,e),i=s(t,e);return u(i,n)},t.union=s,Object.defineProperty(t,"__esModule",{value:!0})})); //# sourceMappingURL=main.umd.min.js.map