@js-labs/data-structures
Version:
There are neither a lot of resources on internet nor any book which guides and dictates best practices in the implementation of popular Data Structures using Javascript. The purpose of this library is to provide cooked implementation of populare data stru
2 lines • 17.9 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("ds",[],t):"object"==typeof exports?exports.ds=t():e.ds=t()}(window,function(){return function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=3)}([function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}();var o=Symbol("queue");t.Queue=function(){function e(){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this[o]=[]}return n(e,[{key:"enqueue",value:function(e){this[o].unshift(e)}},{key:"dequeue",value:function(){if(this.isEmpty())return new Error("The Queue is Empty");this[o].pop()}},{key:"size",value:function(){return this[o].length}},{key:"isEmpty",value:function(){return 0===this.size()}},{key:"top",value:function(){return this.isEmpty()?new Error("The Queue is Empty"):this[o][this.size()-1]}},{key:"clear",value:function(){this[o].length=0}},{key:"queue",get:function(){return this[o]}}]),e}()},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.BST=t.BSTNode=void 0;var n=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),o=r(0);function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var u=t.BSTNode=function(){function e(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null,o=arguments.length>3&&void 0!==arguments[3]?arguments[3]:null;i(this,e),this._key=t,this._details=r,this._left=n,this._right=o}return n(e,[{key:"key",get:function(){return this._key},set:function(e){this._key=e}},{key:"details",get:function(){return this._details},set:function(e){this._details=e}},{key:"left",get:function(){return this._left},set:function(e){this._left=e}},{key:"right",get:function(){return this._right},set:function(e){this._right=e}}]),e}(),a=Symbol("inorder"),c=Symbol("preorder"),s=Symbol("postorder"),f=Symbol("levelorder"),l=Symbol("length");t.BST=function(){function e(){i(this,e),this.root=null,this[l]=0}return n(e,[{key:"insert",value:function(e){var t=this,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,n=new u(e,r);this.root?function r(){var o=arguments.length>0&&void 0!==arguments[0]?arguments[0]:t.root;o.key>e&&!o.left?(o.left=n,t[l]++):o.key>e?r(o.left):o.key<e&&!o.right?(o.right=n,t[l]++):o.key<e&&r(o.right)}():(this.root=n,this[l]++)}},{key:"delete",value:function(e){if(!this.root)return new Error("BST is empty. Cannot delete from empty BST");var t=this.lookup(e);if(!t.hasVal)return new Error("Node not found.");if(t.currentNode.left||t.currentNode.right){if(!!t.currentNode.left^!!t.currentNode.right){var r=t.parentNode.key>e?"left":"right",n=t.currentNode.left?"left":"right";t.parentNode[r]=t.currentNode[n],this[l]--}else if(t.currentNode.left&&t.currentNode.right){var o=this.findMin(t.currentNode.right);t.currentNode.key=o.subtree.key,o.parent.left=null,this[l]--}}else{if(null===t.parentNode)this.root=null;else{var i=t.parentNode.key>e?"left":"right";t.parentNode[i]=null}this[l]--}}},{key:"findMin",value:function(){for(var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.root,t=void 0;e.left;)t=e,e=e.left;return{subtree:e,parent:t}}},{key:"lookup",value:function(e){var t=this,r={hasVal:!1,currentNode:null,parentNode:null};return function n(){var o=arguments.length>0&&void 0!==arguments[0]?arguments[0]:t.root,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;o&&(o.key===e?(r.hasVal=!0,r.currentNode=o,r.parentNode=i):o.left&&o.key>e?n(o.left,o):o.right&&o.key<e&&n(o.right,o))}(),r}},{key:"height",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.root;return null===e?-1:Math.max(this.height(e.left),this.height(e.right))+1}},{key:"traverse",value:function(e){var t=void 0;switch(e){case"inOrder":t=this[a]();break;case"preOrder":t=this[c]();break;case"postOrder":t=this[s]();break;case"levelOrder":t=this[f]();break;default:t=new Error("Type should be one of inOrder, preOrder, postOrder or levelOrder")}return t}},{key:a,value:function(){var e=[];return function t(r){r&&(t(r.left),e.push(r.key),t(r.right))}(arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.root),e}},{key:c,value:function(){var e=[];return function t(r){r&&(e.push(r.key),t(r.left),t(r.right))}(arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.root),e}},{key:s,value:function(){var e=[];return function t(r){r&&(t(r.left),t(r.right),e.push(r.key))}(arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.root),e}},{key:f,value:function(){var e=[],t=new o.Queue;for(null!==this.root&&t.enqueue(this.root);!t.isEmpty();){var r=t.top();r.left&&t.enqueue(r.left),r.right&&t.enqueue(r.right),e.push(r.key),t.dequeue()}return e}},{key:"len",get:function(){return this[l]}}]),e}()},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.AdjacencyList=void 0;var n=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),o=r(1);t.AdjacencyList=function(){function e(t){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.diGraph=t,this.vertices=new Map}return n(e,[{key:"addNode",value:function(e){this.vertices.get(e)||this.vertices.set(e,new o.BST)}},{key:"removeNode",value:function(e){var t=this;this.vertices.forEach(function(r,n){t.removeEdge(n,e)}),this.vertices.delete(e)}},{key:"addEdge",value:function(e,t,r){this.vertices.has(e)||this.addNode(e),this.vertices.has(t)||this.addNode(t),this.vertices.get(e).insert(t,{weight:r}),this.diGraph||this.vertices.get(t).insert(e,{weight:r})}},{key:"removeEdge",value:function(e,t){if(!this.vertices.has(e))return new Error("No edge present between "+e+" and "+t);var r=this.vertices.get(e).delete(t);if(r&&r.constructor===Error)return new Error("No edge present between "+e+" and "+t);this.diGraph||this.removeEdge.stopRecursion?this.removeEdge.stopRecursion&&(this.removeEdge.stopRecursion=void 0):(this.removeEdge.stopRecursion=!0,this.removeEdge(t,e))}},{key:"getEdgeWeight",value:function(e,t){var r=void 0;if(this.vertices.has(e)){var n=this.vertices.get(e).lookup(t);r=n.hasVal?n.currentNode.details.weight:void 0}return r||new Error("Edge not found between "+e+" and "+t)}}]),e}()},function(e,t,r){r(4),e.exports=r(5)},function(e,t){!function(t){"use strict";var r,n=Object.prototype,o=n.hasOwnProperty,i="function"==typeof Symbol?Symbol:{},u=i.iterator||"@@iterator",a=i.asyncIterator||"@@asyncIterator",c=i.toStringTag||"@@toStringTag",s="object"==typeof e,f=t.regeneratorRuntime;if(f)s&&(e.exports=f);else{(f=t.regeneratorRuntime=s?e.exports:{}).wrap=k;var l="suspendedStart",h="suspendedYield",d="executing",v="completed",y={},p={};p[u]=function(){return this};var g=Object.getPrototypeOf,m=g&&g(g(T([])));m&&m!==n&&o.call(m,u)&&(p=m);var b=E.prototype=_.prototype=Object.create(p);x.prototype=b.constructor=E,E.constructor=x,E[c]=x.displayName="GeneratorFunction",f.isGeneratorFunction=function(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===x||"GeneratorFunction"===(t.displayName||t.name))},f.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,E):(e.__proto__=E,c in e||(e[c]="GeneratorFunction")),e.prototype=Object.create(b),e},f.awrap=function(e){return{__await:e}},j(O.prototype),O.prototype[a]=function(){return this},f.AsyncIterator=O,f.async=function(e,t,r,n){var o=new O(k(e,t,r,n));return f.isGeneratorFunction(t)?o:o.next().then(function(e){return e.done?e.value:o.next()})},j(b),b[c]="Generator",b[u]=function(){return this},b.toString=function(){return"[object Generator]"},f.keys=function(e){var t=[];for(var r in e)t.push(r);return t.reverse(),function r(){for(;t.length;){var n=t.pop();if(n in e)return r.value=n,r.done=!1,r}return r.done=!0,r}},f.values=T,S.prototype={constructor:S,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=r,this.done=!1,this.delegate=null,this.method="next",this.arg=r,this.tryEntries.forEach(P),!e)for(var t in this)"t"===t.charAt(0)&&o.call(this,t)&&!isNaN(+t.slice(1))&&(this[t]=r)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var t=this;function n(n,o){return a.type="throw",a.arg=e,t.next=n,o&&(t.method="next",t.arg=r),!!o}for(var i=this.tryEntries.length-1;i>=0;--i){var u=this.tryEntries[i],a=u.completion;if("root"===u.tryLoc)return n("end");if(u.tryLoc<=this.prev){var c=o.call(u,"catchLoc"),s=o.call(u,"finallyLoc");if(c&&s){if(this.prev<u.catchLoc)return n(u.catchLoc,!0);if(this.prev<u.finallyLoc)return n(u.finallyLoc)}else if(c){if(this.prev<u.catchLoc)return n(u.catchLoc,!0)}else{if(!s)throw new Error("try statement without catch or finally");if(this.prev<u.finallyLoc)return n(u.finallyLoc)}}}},abrupt:function(e,t){for(var r=this.tryEntries.length-1;r>=0;--r){var n=this.tryEntries[r];if(n.tryLoc<=this.prev&&o.call(n,"finallyLoc")&&this.prev<n.finallyLoc){var i=n;break}}i&&("break"===e||"continue"===e)&&i.tryLoc<=t&&t<=i.finallyLoc&&(i=null);var u=i?i.completion:{};return u.type=e,u.arg=t,i?(this.method="next",this.next=i.finallyLoc,y):this.complete(u)},complete:function(e,t){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&t&&(this.next=t),y},finish:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.finallyLoc===e)return this.complete(r.completion,r.afterLoc),P(r),y}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.tryLoc===e){var n=r.completion;if("throw"===n.type){var o=n.arg;P(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:T(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=r),y}}}function k(e,t,r,n){var o=t&&t.prototype instanceof _?t:_,i=Object.create(o.prototype),u=new S(n||[]);return i._invoke=function(e,t,r){var n=l;return function(o,i){if(n===d)throw new Error("Generator is already running");if(n===v){if("throw"===o)throw i;return M()}for(r.method=o,r.arg=i;;){var u=r.delegate;if(u){var a=N(u,r);if(a){if(a===y)continue;return a}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if(n===l)throw n=v,r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n=d;var c=w(e,t,r);if("normal"===c.type){if(n=r.done?v:h,c.arg===y)continue;return{value:c.arg,done:r.done}}"throw"===c.type&&(n=v,r.method="throw",r.arg=c.arg)}}}(e,r,u),i}function w(e,t,r){try{return{type:"normal",arg:e.call(t,r)}}catch(e){return{type:"throw",arg:e}}}function _(){}function x(){}function E(){}function j(e){["next","throw","return"].forEach(function(t){e[t]=function(e){return this._invoke(t,e)}})}function O(e){var t;this._invoke=function(r,n){function i(){return new Promise(function(t,i){!function t(r,n,i,u){var a=w(e[r],e,n);if("throw"!==a.type){var c=a.arg,s=c.value;return s&&"object"==typeof s&&o.call(s,"__await")?Promise.resolve(s.__await).then(function(e){t("next",e,i,u)},function(e){t("throw",e,i,u)}):Promise.resolve(s).then(function(e){c.value=e,i(c)},function(e){return t("throw",e,i,u)})}u(a.arg)}(r,n,t,i)})}return t=t?t.then(i,i):i()}}function N(e,t){var n=e.iterator[t.method];if(n===r){if(t.delegate=null,"throw"===t.method){if(e.iterator.return&&(t.method="return",t.arg=r,N(e,t),"throw"===t.method))return y;t.method="throw",t.arg=new TypeError("The iterator does not provide a 'throw' method")}return y}var o=w(n,e.iterator,t.arg);if("throw"===o.type)return t.method="throw",t.arg=o.arg,t.delegate=null,y;var i=o.arg;return i?i.done?(t[e.resultName]=i.value,t.next=e.nextLoc,"return"!==t.method&&(t.method="next",t.arg=r),t.delegate=null,y):i:(t.method="throw",t.arg=new TypeError("iterator result is not an object"),t.delegate=null,y)}function L(e){var t={tryLoc:e[0]};1 in e&&(t.catchLoc=e[1]),2 in e&&(t.finallyLoc=e[2],t.afterLoc=e[3]),this.tryEntries.push(t)}function P(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function S(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(L,this),this.reset(!0)}function T(e){if(e){var t=e[u];if(t)return t.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var n=-1,i=function t(){for(;++n<e.length;)if(o.call(e,n))return t.value=e[n],t.done=!1,t;return t.value=r,t.done=!0,t};return i.next=i}}return{next:M}}function M(){return{value:r,done:!0}}}(function(){return this||"object"==typeof self&&self}()||Function("return this")())},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=r(0);Object.keys(n).forEach(function(e){"default"!==e&&"__esModule"!==e&&Object.defineProperty(t,e,{enumerable:!0,get:function(){return n[e]}})});var o=r(1);Object.keys(o).forEach(function(e){"default"!==e&&"__esModule"!==e&&Object.defineProperty(t,e,{enumerable:!0,get:function(){return o[e]}})});var i=r(6);Object.keys(i).forEach(function(e){"default"!==e&&"__esModule"!==e&&Object.defineProperty(t,e,{enumerable:!0,get:function(){return i[e]}})});var u=r(2);Object.keys(u).forEach(function(e){"default"!==e&&"__esModule"!==e&&Object.defineProperty(t,e,{enumerable:!0,get:function(){return u[e]}})});var a=r(7);Object.keys(a).forEach(function(e){"default"!==e&&"__esModule"!==e&&Object.defineProperty(t,e,{enumerable:!0,get:function(){return a[e]}})})},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.Graph=void 0;var n=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),o=r(2);t.Graph=function(){function e(t){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.diGraph="directed"===t}return n(e,[{key:"createGraph",value:function(e){var t=void 0;return"adjList"!==e?new Error("Graph Type not allowed."):((t=new o.AdjacencyList(this.diGraph)).type=e,t)}}]),e}()},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}();function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var i=t.Node=function(){function e(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;o(this,e),this._key=t,this._next=r}return n(e,[{key:"key",get:function(){return this._key}},{key:"next",get:function(){return this._next},set:function(e){this._next=e}}]),e}(),u=Symbol("size"),a=Symbol("head");t.LinkedList=function(){function e(){o(this,e),this[u]=0,this[a]=null}return n(e,[{key:Symbol.iterator,value:regeneratorRuntime.mark(function e(){var t;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:t=this.head;case 1:if(null===t){e.next=7;break}return e.next=4,t.key;case 4:t=t.next,e.next=1;break;case 7:case"end":return e.stop()}},e,this)})},{key:"size",value:function(){return this[u]}},{key:"isEmpty",value:function(){return 0===this.size()}},{key:"search",value:function(e){return this._search(e).found}},{key:"insert",value:function(e){this.isEmpty()?this.insertFirst(e):this.insertLast(e),this[u]+=1}},{key:"insertLast",value:function(e){for(var t=this[a];t&&null!==t.next;)t=t.next;t.next=new i(e)}},{key:"insertFirst",value:function(e){this[a]=new i(e)}},{key:"insertBefore",value:function(e,t){var r=this._search(t),n=r.prev,o=r.curr;if(r.found){var c=new i(e);null===n?this[a]=c:n.next=c,c.next=o,this[u]+=1}}},{key:"insertAfter",value:function(e,t){var r=this._search(t),n=r.curr,o=r.next;r.found&&(n.next=new i(e),n.next.next=o,this[u]+=1)}},{key:"remove",value:function(e){var t=this._search(e),r=t.prev,n=t.next;t.found&&(null===r?this[a]=n:r.next=n,this[u]-=1)}},{key:"_search",value:function(e){for(var t=null,r=this.head,n=null,o=!1;null!==r&&!o;){if(r.key===e){n=r.next,o=!0;break}t=r,r=r.next}return{prev:t,curr:r,next:n,found:o}}},{key:"head",get:function(){return this[a]}}]),e}()}])});
//# sourceMappingURL=ds.min.js.map