UNPKG

avl-treemap

Version:

A TS/JS implemenation of a self-balancing binary tree with mapped data nodes

1 lines 7.47 kB
(()=>{"use strict";var t,r,e=(t=function(r,e){return(t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,r){t.__proto__=r}||function(t,r){for(var e in r)Object.prototype.hasOwnProperty.call(r,e)&&(t[e]=r[e])})(r,e)},function(r,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=r}t(r,e),r.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}),n=function(){return(n=Object.assign||function(t){for(var r,e=1,n=arguments.length;e<n;e++)for(var o in r=arguments[e])Object.prototype.hasOwnProperty.call(r,o)&&(t[o]=r[o]);return t}).apply(this,arguments)};!function(t){t[t.DFS=0]="DFS",t[t.BFS=1]="BFS"}(r||(r={}));var o=function(){function t(t,r,e,n,o){void 0===e&&(e=null),void 0===n&&(n=null),void 0===o&&(o=null),this.key=t,this.data=r,this.parent=e,this.left=n,this.right=o}return Object.defineProperty(t.prototype,"height",{get:function(){var t,r;return null===this.left&&null===this.right?0:1+Math.max((null===(t=this.left)||void 0===t?void 0:t.height)||0,(null===(r=this.right)||void 0===r?void 0:r.height)||0)},enumerable:!1,configurable:!0}),t.prototype.setParent=function(t){return this.parent=t||null,this},t.prototype.strip=function(){return this.parent=null,this.left=null,this.right=null,this},t.prototype.clone=function(){return new t(this.key,this.data,this.parent,this.left,this.right)},t.prototype.toString=function(){return this.key+"='"+this.data+"'"},t}(),i=function(t){function r(e){var n=t.call(this,e||r.name)||this;return"captureStackTrace"in Error?Error.captureStackTrace(n,r):n.stack=(new Error).stack,n}return e(r,t),r}(Error);!function(){function t(){this.root=null,this.defaultAlgorithm=r.DFS}t.prototype.first=function(){var t=this.dfTraversal((function(t,r){throw r.push(t.data),new i("Found First Node's Data!")}));return t.length>0&&t[0]},t.prototype.firstKey=function(){var t=this.dfTraversal((function(t,r){throw r.push(t.key),new i("Found First Node's Key!")}));return t.length>0&&t[0]},t.prototype.last=function(){return this.dfsValues().pop()||!1},t.prototype.lastKey=function(){return this.dfsKeys().pop()||!1},t.prototype.fetch=function(r){var e=new o(r,null),n=this.root?t.binarySearch(this.compare,this.root,e):null;return null!=n?n.data:null},t.prototype.isKey=function(r){var e=new o(r,null);return null!=(this.root?t.binarySearch(this.compare,this.root,e):null)},t.prototype.keys=function(){return this.defaultAlgorithm===r.DFS?this.dfsKeys():this.bfsKeys()},t.prototype.dfsKeys=function(){return this.dfTraversal((function(t,r){r.push(t.key)}))},t.prototype.bfsKeys=function(){return this.bfTraversal((function(t,r){r.push(t.key)}))},t.prototype.values=function(){return this.defaultAlgorithm===r.DFS?this.dfsValues():this.bfsValues()},t.prototype.dfsValues=function(){return this.dfTraversal((function(t,r){r.push(t.data)}))},t.prototype.bfsValues=function(){return this.bfTraversal((function(t,r){r.push(t.data)}))},t.prototype.allEntries=function(){return this.defaultAlgorithm===r.DFS?this.dfsEntries():this.bfsEntries()},t.prototype.dfsEntries=function(){return this.dfTraversal((function(t,r){r.push([t.key,t.data])}))},t.prototype.bfsEntries=function(){return this.bfTraversal((function(t,r){r.push([t.key,t.data])}))},t.prototype.size=function(){return this.keys().length},t.prototype.height=function(){return this.root?this.root.height+1:0},t.binarySearch=function(r,e,n){if(!e)return null;switch(r(e,n)){case-1:return t.binarySearch(r,e.left,n);case 1:return t.binarySearch(r,e.right,n);default:return e}},t.insert=function(r,e,n){return r.root?function r(e,o,i){var u=o,l=e.compare(u,i);if(l>0)null!==u.right?r(e,u.right,i):(u.right=i,i.setParent(u));else{if(!(l<0))return u.data=i.data,!1;null!==u.left?r(e,u.left,i):(u.left=i,i.setParent(u))}return!n&&t.balanceTree(e,u)}(r,r.root,e):(t.setRoot(r,e),!0)},t.prototype.add=function(r,e){var n=new o(r,e);return t.insert(this,n,!1),this},t.prototype.merge=function(r){return Object.getPrototypeOf(r)===Object.getPrototypeOf(this)&&t.insertSubtree(this,r)},t.prototype.remove=function(r){var e=new o(r,null),n=t.binarySearch(this.compare,this.root,e);if(null===n)return!1;for(;null!==n.left||null!==n.right;)t.calcBalanceFactor(n)>0?t.rotationRight(this,n,!0):t.rotationLeft(this,n,!0);var i=n.parent;if(null===i)return this.root=null,n.data;if(i.left===n?i.left=null:i.right=null,i.height>0&&t.balanceTree(this,i),null==i.parent)t.setRoot(this,i);else for(;null!=i.parent;)i=i.parent,t.balanceTree(this,i);return n.data},t.prototype.removeAll=function(){return t.setRoot(this,null)},t.prototype.dfTraversal=function(t){var r=[];if("function"!=typeof t||null===this.root)return r;try{!function e(n,o){null==o.left&&null==o.right?t.call(n,o,r):(null!=o.left&&e(n,o.left),t.call(n,o,r),null!=o.right&&e(n,o.right))}(this,this.root)}catch(t){if(!(t instanceof i))throw t}return r},t.prototype.bfTraversal=function(t){var r=[];if("function"!=typeof t||null==this.root)return r;try{!function e(n,o,i){if(void 0===i)for(var u=0;u<=o.height;)e(n,o,u++);else 0===i?t.call(n,o,r,i):(null!=o.left&&e(n,o.left,i-1),null!=o.right&&e(n,o.right,i-1))}(this,this.root)}catch(t){if(!(t instanceof i))throw t}return r},t.prototype.sliceTree=function(r){var e=t.nakedClone(this);return t.setRoot(e,r)},t.prototype.subtree=function(r){var e=t.binarySearch(this.compare,this.root,new o(r,null));return null!==e&&this.sliceTree(e)},t.setRoot=function(t,r){return t.root=r?r.setParent():null,t},t.calcBalanceFactor=function(t){return t?(null!=(r=t).left?r.left.height+1:0)-(null!=r.right?r.right.height+1:0):0;var r},t.balanceTree=function(r,e){var n=e||r.root,o=t.calcBalanceFactor(n);return!(-1<=o&&o<=1||null===n)&&(2===o?(1===t.calcBalanceFactor(n.left)||(n.left=t.rotationLeft(r,n.left)),t.rotationRight(r,n)):-2===o&&(1===t.calcBalanceFactor(n.right)?(n.right=t.rotationRight(r,n.right),t.rotationLeft(r,n)):t.rotationLeft(r,n)),!0)},t.rotate=function(r,e,n,o){var i=e,u=i.parent,l=r.sliceTree(n);i.left===n?i.left=null:i.right=null;var a=r.sliceTree(i);o||t.balanceTree(a),t.insert(l,a.root,o);var s=l.root.setParent(u);return null!==u?u.left===i?u.left=s:u.right=s:t.setRoot(r,s),s},t.rotationLeft=function(r,e,n){if(void 0===n&&(n=!1),!e.right)throw new Error("RotateLeft() should not be happening if right child is null.");return t.rotate(r,e,e.right,n)},t.rotationRight=function(r,e,n){if(void 0===n&&(n=!1),!e.left)throw new Error("RotateRight() should not be happening if left child is null.");return t.rotate(r,e,e.left,n)},t.insertSubtree=function(r,e){return e.bfTraversal((function(e){var n=new o(e.key,e.data);t.insert(r,n,!1)})),r},t.prototype.compare=function(r,e){return t.compare(r,e)},t.compare=function(t,r){return Number.isNaN(t.key)||Number.isNaN(r.key)?"string"==typeof t.key||t.key instanceof String?t.key>r.key?-1:t.key<r.key?1:0:1:t.key>r.key?-1:t.key<r.key?1:0},t.nakedClone=function(t){var r=Object.create(Object.getPrototypeOf(t));return Object.getOwnPropertyNames(t).forEach((function(e){if("function"==typeof t[e]){var o=n({},Object.getOwnPropertyDescriptor(t,e));Object.defineProperty(r,e,o)}})),r.defaultAlgorithm=t.defaultAlgorithm,r},t.prototype.toString=function(){if(!this.root)return"TreeMap:{ root: NULL }";var t=this.dfTraversal((function(t,r){r.push(t.toString())})).join(", ");return"TreeMap:{ root: ["+this.root.toString()+"], df:["+t+"] }"},t.prototype.print=function(){console.log(this.toString())}}()})();