@dsinjs/binary-tree
Version:
Binary Trees for your DS in JS
1 lines • 8.84 kB
JavaScript
!function o(n,i,u){function l(t,e){if(!i[t]){if(!n[t]){var r="function"==typeof require&&require;if(!e&&r)return r(t,!0);if(a)return a(t,!0);throw(r=new Error("Cannot find module '"+t+"'")).code="MODULE_NOT_FOUND",r}r=i[t]={exports:{}},n[t][0].call(r.exports,function(e){return l(n[t][1][e]||e)},r,r.exports,o,n,i,u)}return i[t].exports}for(var a="function"==typeof require&&require,e=0;e<u.length;e++)l(u[e]);return l}({1:[function(e,t,r){"use strict";var o,n=this&&this.__extends||(o=function(e,t){return(o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r])})(e,t)},function(e,t){function r(){this.constructor=e}o(e,t),e.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}),s=this&&this.__values||function(e){var t="function"==typeof Symbol&&Symbol.iterator,r=t&&e[t],o=0;if(r)return r.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&o>=e.length&&(e=void 0),{value:e&&e[o++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")},h=this&&this.__read||function(e,t){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var o,n,i=r.call(e),u=[];try{for(;(void 0===t||0<t--)&&!(o=i.next()).done;)u.push(o.value)}catch(e){n={error:e}}finally{try{o&&!o.done&&(r=i.return)&&r.call(i)}finally{if(n)throw n.error}}return u};Object.defineProperty(r,"__esModule",{value:!0}),r.BTree=void 0;var i,f=e("./btreenode"),p=(i=Error,n(u,i),u);function u(e){e=i.call(this,e)||this;return e.name="UnreachableError",e}var l,a=(l=Error,n(d,l),d);function d(e){e=l.call(this,e)||this;return e.name="FilteredRootError",e}"undefined"!=typeof window&&"undefined"==typeof Symbol&&((c=window).Symbol={},c.Symbol.iterator="==iterator==");var c=(v.prototype.toString=function(){return this.root.toString()},v.prototype.toJSON=function(){return this.root.toJSON()},v.prototype.toArray=function(){var r=[];return this.each(function(e,t){r.push(e)}),r},v.prototype.toFlatArray=function(){var r=[];return this.each(function(e,t){r.push(e.value)}),r},v.prototype.insert=function(e){return this.insertLeftMost(e)},v.prototype.insertLeftMost=function(e){for(var t=this.root;null!=t.lNode;)t=t.lNode;return t.lNode=new f.BTreeNode({value:e}),this.depth=this.root.getDepth(),t.lNode},v.prototype.insertRightMost=function(e){for(var t=this.root;null!=t.rNode;)t=t.rNode;return t.rNode=new f.BTreeNode({value:e}),this.depth=this.root.getDepth(),t.rNode},v.prototype.delete=function(o){var n=function(e){if(null==e)return e;var t=e.lNode,r=e.rNode;if(null==t&&null==r)return null;if(e.lNode&&e.lNode.value===o)return e.lNode=null,t;if(e.rNode&&e.rNode.value===o)return e.rNode=null,r;r=n(e.lNode),e=r?null:n(e.rNode);return r||e},e=n(this.root);return this.depth=this.root.getDepth(),e},v.prototype.insertAt=function(e,t){var r,o,n=v.getPathFromIndex(t),i=this.root;try{for(var u=s(n.entries()),l=u.next();!l.done;l=u.next()){var a=h(l.value,2),d=a[0],a=a[1];if("L"===a){if(null==i.lNode&&n.length!==d+1)throw new p("Given index cannot be reached");null==i.lNode?i.lNode=new f.BTreeNode({value:e}):i=i.lNode}if("R"===a){if(null==i.rNode&&n.length!==d+1)throw new p("Given index cannot be reached");null==i.rNode?i.rNode=new f.BTreeNode({value:e}):i=i.rNode}}}catch(e){r={error:e}}finally{try{l&&!l.done&&(o=u.return)&&o.call(u)}finally{if(r)throw r.error}}this.depth=this.root.getDepth()},v.prototype.traverseBFS=function(n){var i=[],u=function(e,t){var r,o;if(null!=e&&((r=JSON.parse(JSON.stringify(t))).push("L"),(o=JSON.parse(JSON.stringify(t))).push("R"),i.push({node:e.lNode,path:r}),i.push({node:e.rNode,path:o}),n(e,v.getIndexFromPath(t))),i.length){t=i.splice(0,1)[0];return u(t.node,t.path)}};u(this.root,["U"])},v.prototype.traverseDFS=function(o){var n=function(e,t){var r;null!==e&&(o(e,v.getIndexFromPath(t)),null!==e.lNode&&((r=JSON.parse(JSON.stringify(t))).push("L"),n(e.lNode,r)),null!==e.rNode&&((t=JSON.parse(JSON.stringify(t))).push("R"),n(e.rNode,t)))};n(this.root,["U"])},v.prototype.each=function(e){return this.traverseBFS(e)},v.prototype.forEach=function(e){return this.traverseBFS(e)},v.prototype[Symbol.iterator]=function(){var e=-1,t=this.toArray();return{next:function(){return{value:void 0===t[++e]?void 0:t[e],done:!(e!==t.length)}}}},v.prototype.entries=function(){return this.toArray().entries()},v.prototype.map=function(r){var o=new v(r(this.root.value));return this.each(function(e,t){1!==t&&(e=r(e.value),o.insertAt(e,t))}),o},v.prototype.filter=function(r){if(!r(this.root.value))throw new a("Root node cannot be filtered. If you want to filter out root node, you can use emptry BTree instance.");var o=new v(this.root.value);return this.each(function(e,t){1!==t&&r(e.value)&&o.insertAt(e.value,t)}),o},v.prototype.reduce=function(r,e){var o=this;void 0===e&&(e=0);var n=e;return this.each(function(e,t){n=r(n,e.value,t,o)}),n},v.prototype.reverse=function(){var r=function(e){var t;null!==e&&(t=e.lNode,e.lNode=e.rNode,e.rNode=t,r(e.lNode),r(e.rNode))};return r(this.root),this},v.prototype.indexOf=function(r){var o=-1;return this.each(function(e,t){e.value===r&&-1===o&&(o=t)}),o},v.prototype.includes=function(e){return-1!==this.indexOf(e)},v.prototype.exists=function(e){return-1!==this.indexOf(e)},v.prototype.has=function(e){return-1!==this.indexOf(e)},v.prototype.sort=function(r,e){if(void 0===r&&(r=function(e,t){return void 0===e&&(e=0),void 0===t&&(t=0),e<t?-1:e==t?0:1}),void 0===e&&(e=!1),e){var o=function(e){var t;null!==e&&(0<r(e.lNode,e.rNode)&&(t=e.lNode,e.lNode=e.rNode,e.rNode=t),o(e.lNode),o(e.rNode))};o(this.root)}else{var t=[],n=[],i=0,u=[],l=function(e){null!==e&&null!==e.lNode&&u.push(e.lNode),null!==e&&null!==e.rNode&&u.push(e.rNode),null!==e&&(n.push(e),t.push(e.value)),0!==u.length&&(e=u[0],u.splice(0,1),l(e))};for(l(this.root);0!==t.length;){for(var a=t[0],d=0,s=1;s<t.length;s++)0<r(a,t[s])&&(a=t[s],d=s);n[i].value=a,t.splice(d,1),i++}}},v.prototype.print=function(){var o=!1;this.traverseDFS(function(e,t){var r=v.getPathFromIndex(t).length,r=o?" |-".repeat(r-1):"";console.log(r,e.value,"("+t+")"),o=!0})},v.prototype.find=function(r){var o=null;return this.each(function(e,t){e.value===r&&null===o&&(o=e)}),o},v.getIndexFromPath=function(e){e=JSON.parse(JSON.stringify(e));for(var t=1;0!=e.length;)"U"===e[0]?e.splice(0,1):"L"===e[0]?(t*=2,e.splice(0,1)):"R"===e[0]&&(t=2*t+1,e.splice(0,1));return t},v.getPathFromIndex=function(e){for(var t=[];1!=e;)e%2==0?t.push("L"):(t.push("R"),--e),e/=2;return t.push("U"),t=t.reverse()},v.fromArray=function(e){var t,r,o=new v(JSON.parse(JSON.stringify(e))[0]);try{for(var n=s(e.entries()),i=n.next();!i.done;i=n.next()){var u=h(i.value,2),l=u[0],u=u[1];0!==l&&o.insertAt(u,l+1)}}catch(e){t={error:e}}finally{try{i&&!i.done&&(r=n.return)&&r.call(n)}finally{if(t)throw t.error}}return o},v);function v(e){this.depth=1,"object"==typeof e&&void 0!==e.root?this.root=new f.BTreeNode({value:e.root}):"object"==typeof e&&void 0!==e.value?this.root=new f.BTreeNode({value:e.value}):this.root=new f.BTreeNode({value:e}),this.depth=this.root.getDepth()}r.BTree=c,void 0!==t&&(t.exports={BTree:c,BTreeNode:f.BTreeNode}),"undefined"!=typeof window&&(window.DSinJS=window.DSinJS||{},window.DSinJS.BTree=c)},{"./btreenode":2}],2:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.BTreeNode=void 0;var o=(n.prototype.validate=function(){return null!=this.value||null!=this.value},n.prototype.toJSON=function(){var e=this.value;return{value:this.value&&"function"==typeof e.toJSON?e.toJSON():this.value,lNode:null===this.lNode?null:this.lNode.toJSON(),rNode:null===this.rNode?null:this.rNode.toJSON()}},n.prototype.toString=function(){var e=this.value,t=null===this.lNode?"":this.lNode.toString(),r=null===this.rNode?"":this.rNode.toString();return("function"==typeof e.toString?e.toString():String(this.value))+t+r},n.prototype.getDepth=function(){var e=null===this.lNode?1:this.lNode.getDepth()+1,t=null===this.rNode?1:this.rNode.getDepth()+1;return Math.max(e,t)},n);function n(e){if(this.value=e.value||null,this.lNode=e.lNode||null,!this.lNode instanceof n&&(this.lNode=new n(this.lNode)),this.rNode=e.rNode||null,!this.rNode instanceof n&&(this.rNode=new n(this.rNode)),!this.validate())throw new Error("A BTree node must have a valid value, cannot be null or undefined")}r.BTreeNode=o,void 0!==t&&(t.exports={BTreeNode:o}),"undefined"!=typeof window&&(window.DSinJS=window.DSinJS||{},window.DSinJS.BTreeNode=o)},{}],3:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});var o=e("./btreenode");Object.defineProperty(r,"BTreeNode",{enumerable:!0,get:function(){return o.BTreeNode}});var n=e("./btree");Object.defineProperty(r,"BTree",{enumerable:!0,get:function(){return n.BTree}})},{"./btree":1,"./btreenode":2}]},{},[3]);