UNPKG

@clarketm/superavltree

Version:
10 lines • 7.41 kB
/** * Copyright (c) 2018, Travis Clarke * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.AVLTree={})}(this,function(t){"use strict";function e(t,e){return t-e}function r(t){return function(e,r){return 0===t(e,r)}}function n(t){return function(e,r){return t(e,r)<0}}function i(t){return function(e,r){return t(e,r)>0}}var u=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},o=function(){function t(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}return function(e,r,n){return r&&t(e.prototype,r),n&&t(e,n),e}}(),a=function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)},f=function(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e},l=function(t){if(Array.isArray(t)){for(var e=0,r=Array(t.length);e<t.length;e++)r[e]=t[e];return r}return Array.from(t)},h=function(){function t(e){u(this,t),this._value=e,this._left=null,this._right=null}return o(t,[{key:"value",get:function(){return this._value},set:function(t){this._value=t}},{key:"left",get:function(){return this._left},set:function(t){this._left=t}},{key:"right",get:function(){return this._right},set:function(t){this._right=t}}]),t}(),c=function(){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];u(this,t),this._queue=[].concat(l(e))}return o(t,[{key:"isEmpty",value:function(){return 0===this._queue.length}},{key:"clear",value:function(){this._queue.length=0}},{key:"enqueue",value:function(t){return this._queue.push(t)}},{key:"dequeue",value:function(){return this._queue.shift()}},{ key:"toArray",value:function(){return this._queue.slice(0)}},{key:"size",get:function(){return this._queue.length}},{key:"front",get:function(){return this._queue[0]}},{key:"rear",get:function(){return this._queue[this._queue.length-1]}}]),t}(),s=function(){function t(){var o=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],a=arguments[1];u(this,t),this._root=null,this._compareEqual=r(a||e),this._compareLessThan=n(a||e),this._compareGreaterThan=i(a||e);var f=!0,l=!1,h=void 0;try{for(var c,s=o[Symbol.iterator]();!(f=(c=s.next()).done);f=!0){this.insert(c.value)}}catch(t){l=!0,h=t}finally{try{!f&&s.return&&s.return()}finally{if(l)throw h}}}return o(t,[{key:"getHeight",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.root;return function t(e){return e?Math.max(t(e.left),t(e.right))+1:0}(t)}},{key:"findMin",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.root;return t.left?this.findMin(t.left):t}},{key:"findMax",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.root;return t.right?this.findMax(t.right):t}},{key:"insert",value:function(t){var e=this,r=new h(t);this._root=function t(n){return n?(e._compareLessThan(r.value,n.value)?n.left=t(n.left):n.right=t(n.right),n):r}(this.root)}},{key:"search",value:function(t){var e=this;if(!t)return null;return function r(n){return n?e._compareEqual(t,n.value)?n:e._compareLessThan(t,n.value)?r(n.left):e._compareGreaterThan(t,n.value)?r(n.right):void 0:null}(this.root)}},{key:"remove",value:function(t){var e=this;this._root=function t(r,n){if(!r)return null;if(e._compareEqual(r.value,n)){if(r.left||r.right){if(r.left){if(r.right){var i=e.findMin(r.right);return r.value=i.value,r.right=t(r.right,i.value),r}return r.left}return r.right}return null}return e._compareLessThan(n,r.value)?(r.left=t(r.left,n),r):e._compareGreaterThan(n,r.value)?(r.right=t(r.right,n),r):void 0}(this.root,t)}},{key:"preOrder",value:function(){function t(e){e&&(r=[].concat(l(r),[e]),t(e.left), t(e.right))}var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.root,r=[];return t(e),r}},{key:"inOrder",value:function(){function t(e){e&&(t(e.left),r=[].concat(l(r),[e]),t(e.right))}var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.root,r=[];return t(e),r}},{key:"postOrder",value:function(){function t(e){e&&(t(e.left),t(e.right),r=[].concat(l(r),[e]))}var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.root,r=[];return t(e),r}},{key:"levelOrder",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.root,e=[],r=new c;for(r.enqueue(t);!r.isEmpty();){var n=r.dequeue();e.push(n),n.left&&r.enqueue(n.left),n.right&&r.enqueue(n.right)}return e}},{key:"toArray",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]&&arguments[1],r=void 0;switch(t){default:case"pre":r=this.preOrder(this.root);break;case"in":r=this.inOrder(this.root);break;case"post":r=this.postOrder(this.root);break;case"level":r=this.levelOrder(this.root)}return e?r.map(function(t){return t.value}):r}},{key:"root",get:function(){return this._root}},{key:"height",get:function(){return this.getHeight(this.root)}}],[{key:"_defaultComparator",value:function(t,e){return t.value>e.value}}]),t}();t.AVLTree=function(t){function e(){return u(this,e),f(this,(e.__proto__||Object.getPrototypeOf(e)).apply(this,arguments))}return a(e,t),o(e,[{key:"_getBalance",value:function(t){return this.getHeight(t.left)-this.getHeight(t.right)}},{key:"_rotateLeft",value:function(t){var e=t.right;return t.right=e.left,e.left=t,e}},{key:"_rotateRight",value:function(t){var e=t.left;return t.left=e.right,e.right=t,e}},{key:"insert",value:function(t){var e=this,r=new h(t);this._root=function t(n){if(!n)return r;e._compareLessThan(r.value,n.value)?n.left=t(n.left):n.right=t(n.right);var i=e._getBalance(n);if(i>1){if(n.left&&e._compareLessThan(r.value,n.left.value))return e._rotateRight(n);if(n.left&&e._compareGreaterThan(r.value,n.left.value))return n.left=e._rotateLeft(n.left),e._rotateRight(n)} if(i<-1){if(n.right&&e._compareGreaterThan(r.value,n.right.value))return e._rotateLeft(n);if(n.right&&e._compareLessThan(r.value,n.right.value))return n.right=e._rotateRight(n.right),e._rotateLeft(n)}return n}(this.root)}},{key:"remove",value:function(t){var e=this;this._root=function t(r,n){if(!r)return null;if(e._compareEqual(r.value,n)){if(!r.left&&!r.right)return null;if(!r.left)return r.right;if(!r.right)return r.left;var i=e.findMin(r.right);r.value=i.value,r.right=t(r.right,i.value)}else e._compareLessThan(n,r.value)?r.left=t(r.left,n):e._compareGreaterThan(n,r.value)&&(r.right=t(r.right,n));var u=e._getBalance(r);if(u>1){if(r.left&&e._getBalance(r.left)>=0)return e._rotateRight(r);if(r.left&&e._getBalance(r.left)<0)return r.left=e._rotateLeft(r.left),e._rotateRight(r)}if(u<-1){if(r.right&&e._getBalance(r.left)<=0)return e._rotateLeft(r);if(r.right&&e._getBalance(r.left)>0)return r.right=e._rotateRight(r.right),e._rotateLeft(r)}return r}(this.root,t)}},{key:"balanced",get:function(){return Math.abs(this._getBalance(this.root))<=1}}]),e}(s),Object.defineProperty(t,"__esModule",{value:!0})});