avl-tree-typed 2.2.3 → 2.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. package/dist/cjs/index.cjs +341 -75
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +343 -75
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +341 -75
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +343 -75
  8. package/dist/esm-legacy/index.mjs.map +1 -1
  9. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -2
  10. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
  11. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
  12. package/dist/types/data-structures/binary-tree/bst.d.ts +142 -28
  13. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +2 -2
  14. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
  15. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -5
  16. package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
  17. package/dist/umd/avl-tree-typed.js +343 -75
  18. package/dist/umd/avl-tree-typed.js.map +1 -1
  19. package/dist/umd/avl-tree-typed.min.js +3 -3
  20. package/dist/umd/avl-tree-typed.min.js.map +1 -1
  21. package/package.json +2 -2
  22. package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -2
  23. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +9 -8
  24. package/src/data-structures/binary-tree/avl-tree.ts +4 -5
  25. package/src/data-structures/binary-tree/bst.ts +495 -85
  26. package/src/data-structures/binary-tree/red-black-tree.ts +1 -2
  27. package/src/data-structures/binary-tree/tree-counter.ts +5 -7
  28. package/src/data-structures/binary-tree/tree-multi-map.ts +7 -8
  29. package/src/types/data-structures/binary-tree/bst.ts +5 -5
@@ -1,8 +1,8 @@
1
- "use strict";var avlTreeTyped=(()=>{var M=Object.defineProperty;var G=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var q=Object.prototype.hasOwnProperty;var W=(h,s,e)=>s in h?M(h,s,{enumerable:!0,configurable:!0,writable:!0,value:e}):h[s]=e;var X=(h,s)=>{for(var e in s)M(h,e,{get:s[e],enumerable:!0})},J=(h,s,e,t)=>{if(s&&typeof s=="object"||typeof s=="function")for(let n of j(s))!q.call(h,n)&&n!==e&&M(h,n,{get:()=>s[n],enumerable:!(t=G(s,n))||t.enumerable});return h};var Y=h=>J(M({},"__esModule",{value:!0}),h);var c=(h,s,e)=>W(h,typeof s!="symbol"?s+"":s,e);var ee={};X(ee,{AVLTree:()=>P,AVLTreeNode:()=>S,BST:()=>L,BSTNode:()=>I,BinaryTree:()=>C,BinaryTreeNode:()=>E});function A(h){let s=typeof h;return s==="number"?!0:s==="bigint"||s==="string"||s==="boolean"}function H(h){if(typeof h.valueOf=="function"){let s=h.valueOf();if(s!==h){if(A(s))return s;if(typeof s=="object"&&s!==null)return H(s)}}if(typeof h.toString=="function"){let s=h.toString();if(s!=="[object Object]")return s}return null}function g(h,s=!1){if(h==null)return!1;if(A(h))return!0;if(typeof h!="object")return!1;if(h instanceof Date||s)return!0;let e=H(h);return e==null?!1:A(e)}var D=h=>({isThunk:!0,fn:h}),Z=h=>typeof h=="object"&&h!==null&&"isThunk"in h&&h.isThunk;function $(h){let s=h;for(;Z(s);)s=s.fn();return s}function F(h){return(...s)=>$(h(...s))}var x=class{constructor(s){c(this,"_toElementFn");if(s){let{toElementFn:e}=s;if(typeof e=="function")this._toElementFn=e;else if(e)throw new TypeError("toElementFn must be a function type")}}get toElementFn(){return this._toElementFn}*[Symbol.iterator](...s){yield*this._getIterator(...s)}*values(){for(let s of this)yield s}every(s,e){let t=0;for(let n of this)if(e===void 0){if(!s(n,t++,this))return!1}else if(!s.call(e,n,t++,this))return!1;return!0}some(s,e){let t=0;for(let n of this)if(e===void 0){if(s(n,t++,this))return!0}else if(s.call(e,n,t++,this))return!0;return!1}forEach(s,e){let t=0;for(let n of this)e===void 0?s(n,t++,this):s.call(e,n,t++,this)}find(s,e){let t=0;for(let n of this)if(e===void 0){if(s(n,t++,this))return n}else if(s.call(e,n,t++,this))return n}has(s){for(let e of this)if(e===s)return!0;return!1}reduce(s,e){let t=0,n=this[Symbol.iterator](),i;if(arguments.length>=2)i=e;else{let r=n.next();if(r.done)throw new TypeError("Reduce of empty structure with no initial value");i=r.value,t=1}for(let r of n)i=s(i,r,t++,this);return i}toArray(){return[...this]}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var w=class h extends x{constructor(e){super(e);c(this,"_maxLen",-1);if(e){let{maxLen:t}=e;typeof t=="number"&&t>0&&t%1===0&&(this._maxLen=t)}}get maxLen(){return this._maxLen}indexOf(e,t=0){if(this.length===0)return-1;t<0&&(t=this.length+t),t<0&&(t=0);for(let n=t;n<this.length;n++)if(this.at(n)===e)return n;return-1}lastIndexOf(e,t=this.length-1){if(this.length===0)return-1;t>=this.length&&(t=this.length-1),t<0&&(t=this.length+t);for(let n=t;n>=0;n--)if(this.at(n)===e)return n;return-1}findIndex(e,t){for(let n=0;n<this.length;n++){let i=this.at(n);if(i!==void 0&&e.call(t,i,n,this))return n}return-1}concat(...e){let t=this.clone();for(let n of e)n instanceof h?t.pushMany(n):t.push(n);return t}sort(e){let t=this.toArray();t.sort(e),this.clear();for(let n of t)this.push(n);return this}splice(e,t=0,...n){let i=this._createInstance();e=e<0?this.length+e:e,e=Math.max(0,Math.min(e,this.length)),t=Math.max(0,Math.min(t,this.length-e));for(let r=0;r<t;r++){let o=this.deleteAt(e);o!==void 0&&i.push(o)}for(let r=0;r<n.length;r++)this.addAt(e+r,n[r]);return i}join(e=","){return this.toArray().join(e)}toReversedArray(){let e=[];for(let t=this.length-1;t>=0;t--)e.push(this.at(t));return e}reduceRight(e,t){let n=t!=null?t:0;for(let i=this.length-1;i>=0;i--)n=e(n,this.at(i),i,this);return n}slice(e=0,t=this.length){e=e<0?this.length+e:e,t=t<0?this.length+t:t;let n=this._createInstance();for(let i=e;i<t;i++)n.push(this.at(i));return n}fill(e,t=0,n=this.length){if(t=t<0?this.length+t:t,n=n<0?this.length+n:n,t<0&&(t=0),n>this.length&&(n=this.length),t>=n)return this;for(let i=t;i<n;i++)this.setAt(i,e);return this}};var m=class h extends w{constructor(e=[],t){super(t);c(this,"_elements",[]);c(this,"_offset",0);c(this,"_autoCompactRatio",.5);if(t){let{autoCompactRatio:n=.5}=t;this._autoCompactRatio=n}this.pushMany(e)}get elements(){return this._elements}get offset(){return this._offset}get autoCompactRatio(){return this._autoCompactRatio}set autoCompactRatio(e){this._autoCompactRatio=e}get length(){return this.elements.length-this._offset}get first(){return this.length>0?this.elements[this._offset]:void 0}get last(){return this.length>0?this.elements[this.elements.length-1]:void 0}static fromArray(e){return new h(e)}isEmpty(){return this.length===0}push(e){return this.elements.push(e),this._maxLen>0&&this.length>this._maxLen&&this.shift(),!0}pushMany(e){let t=[];for(let n of e)this.toElementFn?t.push(this.push(this.toElementFn(n))):t.push(this.push(n));return t}shift(){if(this.length===0)return;let e=this.first;return this._offset+=1,this.elements.length>0&&this.offset/this.elements.length>this.autoCompactRatio&&this.compact(),e}delete(e){for(let t=this._offset;t<this.elements.length;t++)if(Object.is(this.elements[t],e))return this.elements.splice(t,1),!0;return!1}at(e){if(!(e<0||e>=this.length))return this._elements[this._offset+e]}deleteAt(e){if(e<0||e>=this.length)return;let t=this._offset+e,[n]=this.elements.splice(t,1);return n}addAt(e,t){return e<0||e>this.length?!1:(this._elements.splice(this._offset+e,0,t),!0)}setAt(e,t){return e<0||e>=this.length?!1:(this._elements[this._offset+e]=t,!0)}reverse(){return this._elements=this.elements.slice(this._offset).reverse(),this._offset=0,this}clear(){this._elements=[],this._offset=0}compact(){return this._elements=this.elements.slice(this._offset),this._offset=0,!0}splice(e,t=0,...n){e=Math.max(0,Math.min(e,this.length)),t=Math.max(0,Math.min(t,this.length-e));let i=this._offset+e,r=this._elements.splice(i,t,...n);this.elements.length>0&&this.offset/this.elements.length>this.autoCompactRatio&&this.compact();let o=this._createInstance({toElementFn:this.toElementFn,maxLen:this._maxLen});return o._setAutoCompactRatio(this._autoCompactRatio),o.pushMany(r),o}clone(){let e=this._createInstance({toElementFn:this.toElementFn,maxLen:this._maxLen});e._setAutoCompactRatio(this._autoCompactRatio);for(let t=this._offset;t<this.elements.length;t++)e.push(this.elements[t]);return e}filter(e,t){let n=this._createInstance({toElementFn:this.toElementFn,maxLen:this._maxLen});n._setAutoCompactRatio(this._autoCompactRatio);let i=0;for(let r of this)e.call(t,r,i,this)&&n.push(r),i++;return n}map(e,t,n){var o,l;let i=new this.constructor([],{toElementFn:t==null?void 0:t.toElementFn,maxLen:(o=t==null?void 0:t.maxLen)!=null?o:this._maxLen,autoCompactRatio:(l=t==null?void 0:t.autoCompactRatio)!=null?l:this._autoCompactRatio}),r=0;for(let u of this)i.push(n===void 0?e(u,r++,this):e.call(n,u,r++,this));return i}mapSame(e,t){var o;let n=this.constructor,i=new n([],{toElementFn:this.toElementFn,maxLen:this._maxLen,autoCompactRatio:this._autoCompactRatio});(o=i._setAutoCompactRatio)==null||o.call(i,this._autoCompactRatio);let r=0;for(let l of this){let u=t===void 0?e(l,r++,this):e.call(t,l,r++,this);i.push(u)}return i}_setAutoCompactRatio(e){this._autoCompactRatio=e}*_getIterator(){for(let e=this._offset;e<this.elements.length;e++)yield this.elements[e]}*_getReverseIterator(){for(let e=this.length-1;e>=0;e--){let t=this.at(e);t!==void 0&&(yield t)}}_createInstance(e){let t=this.constructor;return new t([],e)}_createLike(e=[],t){let n=this.constructor;return new n(e,t)}};var v=class{*[Symbol.iterator](...s){yield*this._getIterator(...s)}*entries(){for(let s of this)yield s}*keys(){for(let s of this)yield s[0]}*values(){for(let s of this)yield s[1]}every(s,e){let t=0;for(let n of this)if(!s.call(e,n[1],n[0],t++,this))return!1;return!0}some(s,e){let t=0;for(let n of this)if(s.call(e,n[1],n[0],t++,this))return!0;return!1}forEach(s,e){let t=0;for(let n of this){let[i,r]=n;s.call(e,r,i,t++,this)}}find(s,e){let t=0;for(let n of this){let[i,r]=n;if(s.call(e,r,i,t++,this))return n}}has(s){for(let e of this){let[t]=e;if(t===s)return!0}return!1}hasValue(s){for(let[,e]of this)if(e===s)return!0;return!1}get(s){for(let e of this){let[t,n]=e;if(t===s)return n}}reduce(s,e){let t=e,n=0;for(let i of this){let[r,o]=i;t=s(t,o,r,n++,this)}return t}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var _=class{constructor(s,e,t=!0,n=!0){this.low=s;this.high=e;this.includeLow=t;this.includeHigh=n;if(!(g(s)&&g(e)))throw new RangeError("low or high is not comparable");if(s>e)throw new RangeError("low must be less than or equal to high")}isInRange(s,e){let t=this.includeLow?e(s,this.low)>=0:e(s,this.low)>0,n=this.includeHigh?e(s,this.high)<=0:e(s,this.high)<0;return t&&n}};var E=class{constructor(s,e){c(this,"key");c(this,"value");c(this,"parent");c(this,"_left");c(this,"_right");c(this,"_height",0);c(this,"_color","BLACK");c(this,"_count",1);this.key=s,this.value=e}get left(){return this._left}set left(s){s&&(s.parent=this),this._left=s}get right(){return this._right}set right(s){s&&(s.parent=this),this._right=s}get height(){return this._height}set height(s){this._height=s}get color(){return this._color}set color(s){this._color=s}get count(){return this._count}set count(s){this._count=s}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},C=class extends v{constructor(e=[],t){super();c(this,"iterationType","ITERATIVE");c(this,"_isMapMode",!0);c(this,"_isDuplicate",!1);c(this,"_store",new Map);c(this,"_root");c(this,"_size",0);c(this,"_NIL",new E(NaN));c(this,"_toEntryFn");c(this,"_DEFAULT_NODE_CALLBACK",e=>e?e.key:void 0);if(t){let{iterationType:n,toEntryFn:i,isMapMode:r,isDuplicate:o}=t;if(n&&(this.iterationType=n),r!==void 0&&(this._isMapMode=r),o!==void 0&&(this._isDuplicate=o),typeof i=="function")this._toEntryFn=i;else if(i)throw TypeError("toEntryFn must be a function type")}e&&this.addMany(e)}get isMapMode(){return this._isMapMode}get isDuplicate(){return this._isDuplicate}get store(){return this._store}get root(){return this._root}get size(){return this._size}get NIL(){return this._NIL}get toEntryFn(){return this._toEntryFn}createNode(e,t){return new E(e,this._isMapMode?void 0:t)}createTree(e){return this._createInstance(e)}ensureNode(e,t=this.iterationType){if(e===null)return null;if(e!==void 0&&e!==this._NIL){if(this.isNode(e))return e;if(this.isEntry(e)){let n=e[0];return n===null?null:n===void 0?void 0:this.getNode(n,this._root,t)}return this.getNode(e,this._root,t)}}isNode(e){return e instanceof E}isRaw(e){return this._toEntryFn!==void 0&&typeof e=="object"}isRealNode(e){return e===this._NIL||e===null||e===void 0?!1:this.isNode(e)}isRealNodeOrNull(e){return e===null||this.isRealNode(e)}isNIL(e){return e===this._NIL}isRange(e){return e instanceof _}isLeaf(e){return e=this.ensureNode(e),e===void 0?!1:e===null?!0:!this.isRealNode(e.left)&&!this.isRealNode(e.right)}isEntry(e){return Array.isArray(e)&&e.length===2}isValidKey(e){return e===null?!0:g(e)}add(e,t){let[n,i]=this._keyValueNodeOrEntryToNodeAndValue(e,t);if(n===void 0)return!1;if(!this._root)return this._setRoot(n),this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size=1,!0;let r=new m([this._root]),o;for(;r.length>0;){let l=r.shift();if(l){if(!this._isDuplicate&&n!==null&&l.key===n.key)return this._replaceNode(l,n),this._isMapMode&&this._setValue(l.key,i),!0;o===void 0&&(l.left===void 0||l.right===void 0)&&(o=l),l.left!==null&&l.left&&r.push(l.left),l.right!==null&&l.right&&r.push(l.right)}}return o?(o.left===void 0?o.left=n:o.right===void 0&&(o.right=n),this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0):!1}set(e,t){return this.add(e,t)}addMany(e,t){let n=[],i;t&&(i=t[Symbol.iterator]());for(let r of e){let o;if(i){let l=i.next();l.done||(o=l.value)}this.isRaw(r)&&(r=this._toEntryFn(r)),n.push(this.add(r,o))}return n}setMany(e,t){return this.addMany(e,t)}merge(e){this.addMany(e,[])}refill(e,t){this.clear(),this.addMany(e,t)}delete(e){let t=[];if(!this._root)return t;let n=this.getNode(e);if(!n)return t;let i=n==null?void 0:n.parent,r,o=n;if(!n.left&&!n.right&&!i)this._setRoot(void 0);else if(n.left){let l=this.getRightMost(u=>u,n.left);if(l){let u=l.parent;o=this._swapProperties(n,l),u&&(u.right===l?u.right=l.left:u.left=l.left,r=u)}}else if(i){let{familyPosition:l}=n;l==="LEFT"||l==="ROOT_LEFT"?i.left=n.right:(l==="RIGHT"||l==="ROOT_RIGHT")&&(i.right=n.right),r=i}else this._setRoot(n.right),n.right=void 0;return this._size=this._size-1,t.push({deleted:o,needBalanced:r}),this._isMapMode&&o&&this._store.delete(o.key),t}search(e,t=!1,n=this._DEFAULT_NODE_CALLBACK,i=this._root,r=this.iterationType){if(e===void 0)return[];if(e===null)return[];if(i=this.ensureNode(i),!i)return[];let o=this._ensurePredicate(e),l=[];if(r==="RECURSIVE"){let u=d=>{o(d)&&(l.push(n(d)),t)||!this.isRealNode(d.left)&&!this.isRealNode(d.right)||(this.isRealNode(d.left)&&u(d.left),this.isRealNode(d.right)&&u(d.right))};u(i)}else{let u=[i];for(;u.length>0;){let d=u.pop();if(this.isRealNode(d)){if(o(d)&&(l.push(n(d)),t))return l;this.isRealNode(d.left)&&u.push(d.left),this.isRealNode(d.right)&&u.push(d.right)}}}return l}getNodes(e,t=!1,n=this._root,i=this.iterationType){return this.search(e,t,r=>r,n,i)}getNode(e,t=this._root,n=this.iterationType){return this.search(e,!0,i=>i,t,n)[0]}get(e,t=this._root,n=this.iterationType){var i;if(this._isMapMode){let r=this._extractKey(e);return r==null?void 0:this._store.get(r)}return(i=this.getNode(e,t,n))==null?void 0:i.value}has(e,t=this._root,n=this.iterationType){return this.search(e,!0,i=>i,t,n).length>0}clear(){this._clearNodes(),this._isMapMode&&this._clearValues()}isEmpty(){return this._size===0}isPerfectlyBalanced(e=this._root){return this.getMinHeight(e)+1>=this.getHeight(e)}isBST(e=this._root,t=this.iterationType){let n=this.ensureNode(e);if(!n)return!0;if(t==="RECURSIVE"){let i=(l,u,d)=>{if(!this.isRealNode(l))return!0;let a=Number(l.key);return a<=u||a>=d?!1:i(l.left,u,a)&&i(l.right,a,d)},r=i(n,Number.MIN_SAFE_INTEGER,Number.MAX_SAFE_INTEGER),o=i(n,Number.MAX_SAFE_INTEGER,Number.MIN_SAFE_INTEGER);return r||o}else{let i=(l=!1)=>{let u=[],d=l?Number.MAX_SAFE_INTEGER:Number.MIN_SAFE_INTEGER,a=n;for(;this.isRealNode(a)||u.length>0;){for(;this.isRealNode(a);)u.push(a),a=a.left;a=u.pop();let T=Number(a.key);if(!this.isRealNode(a)||!l&&d>=T||l&&d<=T)return!1;d=T,a=a.right}return!0},r=i(!1),o=i(!0);return r||o}}getDepth(e,t=this._root){let n=this.ensureNode(e),i=this.ensureNode(t),r=0;for(;n!=null&&n.parent;){if(n===i)return r;r++,n=n.parent}return r}getHeight(e=this._root,t=this.iterationType){if(e=this.ensureNode(e),!this.isRealNode(e))return-1;if(t==="RECURSIVE"){let n=i=>{if(!this.isRealNode(i))return-1;let r=n(i.left),o=n(i.right);return Math.max(r,o)+1};return n(e)}else{let n=[{node:e,depth:0}],i=0;for(;n.length>0;){let{node:r,depth:o}=n.pop();this.isRealNode(r.left)&&n.push({node:r.left,depth:o+1}),this.isRealNode(r.right)&&n.push({node:r.right,depth:o+1}),i=Math.max(i,o)}return i}}getMinHeight(e=this._root,t=this.iterationType){if(e=this.ensureNode(e),!e)return-1;if(t==="RECURSIVE"){let n=i=>{if(!this.isRealNode(i)||!this.isRealNode(i.left)&&!this.isRealNode(i.right))return 0;let r=n(i.left),o=n(i.right);return Math.min(r,o)+1};return n(e)}else{let n=[],i=e,r=null,o=new Map;for(;n.length>0||i;)if(this.isRealNode(i))n.push(i),i=i.left;else if(i=n[n.length-1],!this.isRealNode(i.right)||r===i.right){if(i=n.pop(),this.isRealNode(i)){let l=this.isRealNode(i.left)?o.get(i.left):-1,u=this.isRealNode(i.right)?o.get(i.right):-1;o.set(i,1+Math.min(l,u)),r=i,i=null}}else i=i.right;return o.get(e)}}getPathToRoot(e,t=this._DEFAULT_NODE_CALLBACK,n=!1){let i=[],r=this.ensureNode(e);if(!r)return i;for(;r.parent;)i.push(t(r)),r=r.parent;return i.push(t(r)),n?i.reverse():i}getLeftMost(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){if(this.isNIL(t))return e(void 0);let i=this.ensureNode(t);if(!this.isRealNode(i))return e(void 0);if(n==="RECURSIVE"){let r=o=>{let{left:l}=o;return this.isRealNode(l)?r(l):o};return e(r(i))}else{let r=F(o=>{let{left:l}=o;return this.isRealNode(l)?D(()=>r(l)):o});return e(r(i))}}getRightMost(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){if(this.isNIL(t)||(t=this.ensureNode(t),!t))return e(void 0);if(n==="RECURSIVE"){let i=r=>{let{right:o}=r;return this.isRealNode(o)?i(o):r};return e(i(t))}else{let i=F(r=>{let{right:o}=r;return this.isRealNode(o)?D(()=>i(o)):r});return e(i(t))}}getPredecessor(e){if(this.isRealNode(e.left)){let t=e.left;for(;!this.isRealNode(t)||this.isRealNode(t.right)&&t.right!==e;)this.isRealNode(t)&&(t=t.right);return t}else return e}getSuccessor(e){if(e=this.ensureNode(e),!this.isRealNode(e))return;if(this.isRealNode(e.right))return this.getLeftMost(n=>n,e.right);let t=e.parent;for(;this.isRealNode(t)&&e===t.right;)e=t,t=t.parent;return t}dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,r=this.iterationType,o=!1){return i=this.ensureNode(i),i?this._dfs(e,t,n,i,r,o):[]}bfs(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType,i=!1){if(t=this.ensureNode(t),!t)return[];let r=[];if(n==="RECURSIVE"){let o=new m([t]),l=u=>{if(o.length===0)return;let d=o.shift();r.push(e(d)),i?(d&&this.isRealNodeOrNull(d.left)&&o.push(d.left),d&&this.isRealNodeOrNull(d.right)&&o.push(d.right)):(this.isRealNode(d.left)&&o.push(d.left),this.isRealNode(d.right)&&o.push(d.right)),l(u+1)};l(0)}else{let o=new m([t]);for(;o.length>0;){let l=o.length;for(let u=0;u<l;u++){let d=o.shift();r.push(e(d)),i?(d&&this.isRealNodeOrNull(d.left)&&o.push(d.left),d&&this.isRealNodeOrNull(d.right)&&o.push(d.right)):(this.isRealNode(d.left)&&o.push(d.left),this.isRealNode(d.right)&&o.push(d.right))}}}return r}leaves(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){t=this.ensureNode(t);let i=[];if(!this.isRealNode(t))return[];if(n==="RECURSIVE"){let r=o=>{this.isLeaf(o)&&i.push(e(o)),!(!this.isRealNode(o.left)&&!this.isRealNode(o.right))&&(this.isRealNode(o.left)&&r(o.left),this.isRealNode(o.right)&&r(o.right))};r(t)}else{let r=new m([t]);for(;r.length>0;){let o=r.shift();this.isRealNode(o)&&(this.isLeaf(o)&&i.push(e(o)),this.isRealNode(o.left)&&r.push(o.left),this.isRealNode(o.right)&&r.push(o.right))}}return i}listLevels(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType,i=!1){t=this.ensureNode(t);let r=[];if(!t)return r;if(n==="RECURSIVE"){let o=(l,u)=>{r[u]||(r[u]=[]),r[u].push(e(l)),i?(l&&this.isRealNodeOrNull(l.left)&&o(l.left,u+1),l&&this.isRealNodeOrNull(l.right)&&o(l.right,u+1)):(l&&l.left&&o(l.left,u+1),l&&l.right&&o(l.right,u+1))};o(t,0)}else{let o=[[t,0]];for(;o.length>0;){let l=o.pop(),[u,d]=l;r[d]||(r[d]=[]),r[d].push(e(u)),i?(u&&this.isRealNodeOrNull(u.right)&&o.push([u.right,d+1]),u&&this.isRealNodeOrNull(u.left)&&o.push([u.left,d+1])):(u&&u.right&&o.push([u.right,d+1]),u&&u.left&&o.push([u.left,d+1]))}}return r}morris(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=this._root){if(n=this.ensureNode(n),!n)return[];let i=[],r=n,o=u=>{let d=null,a=null;for(;u;)a=u.right,u.right=d,d=u,u=a;return d},l=u=>{let d=o(u),a=d;for(;a;)i.push(e(a)),a=a.right;o(d)};switch(t){case"IN":for(;r;){if(r.left){let u=this.getPredecessor(r);if(u.right)u.right=null;else{u.right=r,r=r.left;continue}}i.push(e(r)),r=r.right}break;case"PRE":for(;r;){if(r.left){let u=this.getPredecessor(r);if(u.right)u.right=null;else{u.right=r,i.push(e(r)),r=r.left;continue}}else i.push(e(r));r=r.right}break;case"POST":for(;r;){if(r.left){let u=this.getPredecessor(r);if(u.right===null){u.right=r,r=r.left;continue}else u.right=null,l(r.left)}r=r.right}l(n);break}return i}clone(){let e=this._createInstance();return this._clone(e),e}filter(e,t){let n=this._createInstance(),i=0;for(let[r,o]of this)e.call(t,o,r,i++,this)&&n.add([r,o]);return n}map(e,t,n){let i=this._createLike([],t),r=0;for(let[o,l]of this)i.add(e.call(n,l,o,r++,this));return i}toVisual(e=this._root,t){let n={isShowUndefined:!1,isShowNull:!0,isShowRedBlackNIL:!1,...t};e=this.ensureNode(e);let i="";return e&&(n.isShowUndefined&&(i+=`U for undefined
1
+ "use strict";var avlTreeTyped=(()=>{var k=Object.defineProperty;var G=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var q=Object.prototype.hasOwnProperty;var W=(h,o,e)=>o in h?k(h,o,{enumerable:!0,configurable:!0,writable:!0,value:e}):h[o]=e;var X=(h,o)=>{for(var e in o)k(h,e,{get:o[e],enumerable:!0})},J=(h,o,e,t)=>{if(o&&typeof o=="object"||typeof o=="function")for(let n of j(o))!q.call(h,n)&&n!==e&&k(h,n,{get:()=>o[n],enumerable:!(t=G(o,n))||t.enumerable});return h};var Y=h=>J(k({},"__esModule",{value:!0}),h);var c=(h,o,e)=>W(h,typeof o!="symbol"?o+"":o,e);var ee={};X(ee,{AVLTree:()=>U,AVLTreeNode:()=>L,BST:()=>I,BSTNode:()=>S,BinaryTree:()=>C,BinaryTreeNode:()=>E});function O(h){let o=typeof h;return o==="number"?!0:o==="bigint"||o==="string"||o==="boolean"}function H(h){if(typeof h.valueOf=="function"){let o=h.valueOf();if(o!==h){if(O(o))return o;if(typeof o=="object"&&o!==null)return H(o)}}if(typeof h.toString=="function"){let o=h.toString();if(o!=="[object Object]")return o}return null}function g(h,o=!1){if(h==null)return!1;if(O(h))return!0;if(typeof h!="object")return!1;if(h instanceof Date||o)return!0;let e=H(h);return e==null?!1:O(e)}var D=h=>({isThunk:!0,fn:h}),Z=h=>typeof h=="object"&&h!==null&&"isThunk"in h&&h.isThunk;function $(h){let o=h;for(;Z(o);)o=o.fn();return o}function F(h){return(...o)=>$(h(...o))}var x=class{constructor(o){c(this,"_toElementFn");if(o){let{toElementFn:e}=o;if(typeof e=="function")this._toElementFn=e;else if(e)throw new TypeError("toElementFn must be a function type")}}get toElementFn(){return this._toElementFn}*[Symbol.iterator](...o){yield*this._getIterator(...o)}*values(){for(let o of this)yield o}every(o,e){let t=0;for(let n of this)if(e===void 0){if(!o(n,t++,this))return!1}else if(!o.call(e,n,t++,this))return!1;return!0}some(o,e){let t=0;for(let n of this)if(e===void 0){if(o(n,t++,this))return!0}else if(o.call(e,n,t++,this))return!0;return!1}forEach(o,e){let t=0;for(let n of this)e===void 0?o(n,t++,this):o.call(e,n,t++,this)}find(o,e){let t=0;for(let n of this)if(e===void 0){if(o(n,t++,this))return n}else if(o.call(e,n,t++,this))return n}has(o){for(let e of this)if(e===o)return!0;return!1}reduce(o,e){let t=0,n=this[Symbol.iterator](),i;if(arguments.length>=2)i=e;else{let r=n.next();if(r.done)throw new TypeError("Reduce of empty structure with no initial value");i=r.value,t=1}for(let r of n)i=o(i,r,t++,this);return i}toArray(){return[...this]}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var w=class h extends x{constructor(e){super(e);c(this,"_maxLen",-1);if(e){let{maxLen:t}=e;typeof t=="number"&&t>0&&t%1===0&&(this._maxLen=t)}}get maxLen(){return this._maxLen}indexOf(e,t=0){if(this.length===0)return-1;t<0&&(t=this.length+t),t<0&&(t=0);for(let n=t;n<this.length;n++)if(this.at(n)===e)return n;return-1}lastIndexOf(e,t=this.length-1){if(this.length===0)return-1;t>=this.length&&(t=this.length-1),t<0&&(t=this.length+t);for(let n=t;n>=0;n--)if(this.at(n)===e)return n;return-1}findIndex(e,t){for(let n=0;n<this.length;n++){let i=this.at(n);if(i!==void 0&&e.call(t,i,n,this))return n}return-1}concat(...e){let t=this.clone();for(let n of e)n instanceof h?t.pushMany(n):t.push(n);return t}sort(e){let t=this.toArray();t.sort(e),this.clear();for(let n of t)this.push(n);return this}splice(e,t=0,...n){let i=this._createInstance();e=e<0?this.length+e:e,e=Math.max(0,Math.min(e,this.length)),t=Math.max(0,Math.min(t,this.length-e));for(let r=0;r<t;r++){let s=this.deleteAt(e);s!==void 0&&i.push(s)}for(let r=0;r<n.length;r++)this.addAt(e+r,n[r]);return i}join(e=","){return this.toArray().join(e)}toReversedArray(){let e=[];for(let t=this.length-1;t>=0;t--)e.push(this.at(t));return e}reduceRight(e,t){let n=t!=null?t:0;for(let i=this.length-1;i>=0;i--)n=e(n,this.at(i),i,this);return n}slice(e=0,t=this.length){e=e<0?this.length+e:e,t=t<0?this.length+t:t;let n=this._createInstance();for(let i=e;i<t;i++)n.push(this.at(i));return n}fill(e,t=0,n=this.length){if(t=t<0?this.length+t:t,n=n<0?this.length+n:n,t<0&&(t=0),n>this.length&&(n=this.length),t>=n)return this;for(let i=t;i<n;i++)this.setAt(i,e);return this}};var m=class h extends w{constructor(e=[],t){super(t);c(this,"_elements",[]);c(this,"_offset",0);c(this,"_autoCompactRatio",.5);if(t){let{autoCompactRatio:n=.5}=t;this._autoCompactRatio=n}this.pushMany(e)}get elements(){return this._elements}get offset(){return this._offset}get autoCompactRatio(){return this._autoCompactRatio}set autoCompactRatio(e){this._autoCompactRatio=e}get length(){return this.elements.length-this._offset}get first(){return this.length>0?this.elements[this._offset]:void 0}get last(){return this.length>0?this.elements[this.elements.length-1]:void 0}static fromArray(e){return new h(e)}isEmpty(){return this.length===0}push(e){return this.elements.push(e),this._maxLen>0&&this.length>this._maxLen&&this.shift(),!0}pushMany(e){let t=[];for(let n of e)this.toElementFn?t.push(this.push(this.toElementFn(n))):t.push(this.push(n));return t}shift(){if(this.length===0)return;let e=this.first;return this._offset+=1,this.elements.length>0&&this.offset/this.elements.length>this.autoCompactRatio&&this.compact(),e}delete(e){for(let t=this._offset;t<this.elements.length;t++)if(Object.is(this.elements[t],e))return this.elements.splice(t,1),!0;return!1}at(e){if(!(e<0||e>=this.length))return this._elements[this._offset+e]}deleteAt(e){if(e<0||e>=this.length)return;let t=this._offset+e,[n]=this.elements.splice(t,1);return n}addAt(e,t){return e<0||e>this.length?!1:(this._elements.splice(this._offset+e,0,t),!0)}setAt(e,t){return e<0||e>=this.length?!1:(this._elements[this._offset+e]=t,!0)}reverse(){return this._elements=this.elements.slice(this._offset).reverse(),this._offset=0,this}clear(){this._elements=[],this._offset=0}compact(){return this._elements=this.elements.slice(this._offset),this._offset=0,!0}splice(e,t=0,...n){e=Math.max(0,Math.min(e,this.length)),t=Math.max(0,Math.min(t,this.length-e));let i=this._offset+e,r=this._elements.splice(i,t,...n);this.elements.length>0&&this.offset/this.elements.length>this.autoCompactRatio&&this.compact();let s=this._createInstance({toElementFn:this.toElementFn,maxLen:this._maxLen});return s._setAutoCompactRatio(this._autoCompactRatio),s.pushMany(r),s}clone(){let e=this._createInstance({toElementFn:this.toElementFn,maxLen:this._maxLen});e._setAutoCompactRatio(this._autoCompactRatio);for(let t=this._offset;t<this.elements.length;t++)e.push(this.elements[t]);return e}filter(e,t){let n=this._createInstance({toElementFn:this.toElementFn,maxLen:this._maxLen});n._setAutoCompactRatio(this._autoCompactRatio);let i=0;for(let r of this)e.call(t,r,i,this)&&n.push(r),i++;return n}map(e,t,n){var s,l;let i=new this.constructor([],{toElementFn:t==null?void 0:t.toElementFn,maxLen:(s=t==null?void 0:t.maxLen)!=null?s:this._maxLen,autoCompactRatio:(l=t==null?void 0:t.autoCompactRatio)!=null?l:this._autoCompactRatio}),r=0;for(let d of this)i.push(n===void 0?e(d,r++,this):e.call(n,d,r++,this));return i}mapSame(e,t){var s;let n=this.constructor,i=new n([],{toElementFn:this.toElementFn,maxLen:this._maxLen,autoCompactRatio:this._autoCompactRatio});(s=i._setAutoCompactRatio)==null||s.call(i,this._autoCompactRatio);let r=0;for(let l of this){let d=t===void 0?e(l,r++,this):e.call(t,l,r++,this);i.push(d)}return i}_setAutoCompactRatio(e){this._autoCompactRatio=e}*_getIterator(){for(let e=this._offset;e<this.elements.length;e++)yield this.elements[e]}*_getReverseIterator(){for(let e=this.length-1;e>=0;e--){let t=this.at(e);t!==void 0&&(yield t)}}_createInstance(e){let t=this.constructor;return new t([],e)}_createLike(e=[],t){let n=this.constructor;return new n(e,t)}};var v=class{*[Symbol.iterator](...o){yield*this._getIterator(...o)}*entries(){for(let o of this)yield o}*keys(){for(let o of this)yield o[0]}*values(){for(let o of this)yield o[1]}every(o,e){let t=0;for(let n of this)if(!o.call(e,n[1],n[0],t++,this))return!1;return!0}some(o,e){let t=0;for(let n of this)if(o.call(e,n[1],n[0],t++,this))return!0;return!1}forEach(o,e){let t=0;for(let n of this){let[i,r]=n;o.call(e,r,i,t++,this)}}find(o,e){let t=0;for(let n of this){let[i,r]=n;if(o.call(e,r,i,t++,this))return n}}has(o){for(let e of this){let[t]=e;if(t===o)return!0}return!1}hasValue(o){for(let[,e]of this)if(e===o)return!0;return!1}get(o){for(let e of this){let[t,n]=e;if(t===o)return n}}reduce(o,e){let t=e,n=0;for(let i of this){let[r,s]=i;t=o(t,s,r,n++,this)}return t}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var _=class{constructor(o,e,t=!0,n=!0){this.low=o;this.high=e;this.includeLow=t;this.includeHigh=n;if(!(g(o)&&g(e)))throw new RangeError("low or high is not comparable");if(o>e)throw new RangeError("low must be less than or equal to high")}isInRange(o,e){let t=this.includeLow?e(o,this.low)>=0:e(o,this.low)>0,n=this.includeHigh?e(o,this.high)<=0:e(o,this.high)<0;return t&&n}};var E=class{constructor(o,e){c(this,"key");c(this,"value");c(this,"parent");c(this,"_left");c(this,"_right");c(this,"_height",0);c(this,"_color","BLACK");c(this,"_count",1);this.key=o,this.value=e}get left(){return this._left}set left(o){o&&(o.parent=this),this._left=o}get right(){return this._right}set right(o){o&&(o.parent=this),this._right=o}get height(){return this._height}set height(o){this._height=o}get color(){return this._color}set color(o){this._color=o}get count(){return this._count}set count(o){this._count=o}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},C=class extends v{constructor(e=[],t){super();c(this,"iterationType","ITERATIVE");c(this,"_isMapMode",!0);c(this,"_isDuplicate",!1);c(this,"_store",new Map);c(this,"_root");c(this,"_size",0);c(this,"_NIL",new E(NaN));c(this,"_toEntryFn");c(this,"_DEFAULT_NODE_CALLBACK",e=>e?e.key:void 0);if(t){let{iterationType:n,toEntryFn:i,isMapMode:r,isDuplicate:s}=t;if(n&&(this.iterationType=n),r!==void 0&&(this._isMapMode=r),s!==void 0&&(this._isDuplicate=s),typeof i=="function")this._toEntryFn=i;else if(i)throw TypeError("toEntryFn must be a function type")}e&&this.addMany(e)}get isMapMode(){return this._isMapMode}get isDuplicate(){return this._isDuplicate}get store(){return this._store}get root(){return this._root}get size(){return this._size}get NIL(){return this._NIL}get toEntryFn(){return this._toEntryFn}createNode(e,t){return new E(e,this._isMapMode?void 0:t)}createTree(e){return this._createInstance(e)}ensureNode(e,t=this.iterationType){if(e===null)return null;if(e!==void 0&&e!==this._NIL){if(this.isNode(e))return e;if(this.isEntry(e)){let n=e[0];return n===null?null:n===void 0?void 0:this.getNode(n,this._root,t)}return this.getNode(e,this._root,t)}}isNode(e){return e instanceof E}isRaw(e){return this._toEntryFn!==void 0&&typeof e=="object"}isRealNode(e){return e===this._NIL||e===null||e===void 0?!1:this.isNode(e)}isRealNodeOrNull(e){return e===null||this.isRealNode(e)}isNIL(e){return e===this._NIL}isRange(e){return e instanceof _}isLeaf(e){return e=this.ensureNode(e),e===void 0?!1:e===null?!0:!this.isRealNode(e.left)&&!this.isRealNode(e.right)}isEntry(e){return Array.isArray(e)&&e.length===2}isValidKey(e){return e===null?!0:g(e)}add(e,t){let[n,i]=this._keyValueNodeOrEntryToNodeAndValue(e,t);if(n===void 0)return!1;if(!this._root)return this._setRoot(n),this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size=1,!0;let r=new m([this._root]),s;for(;r.length>0;){let l=r.shift();if(l){if(!this._isDuplicate&&n!==null&&l.key===n.key)return this._replaceNode(l,n),this._isMapMode&&this._setValue(l.key,i),!0;s===void 0&&(l.left===void 0||l.right===void 0)&&(s=l),l.left!==null&&l.left&&r.push(l.left),l.right!==null&&l.right&&r.push(l.right)}}return s?(s.left===void 0?s.left=n:s.right===void 0&&(s.right=n),this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0):!1}set(e,t){return this.add(e,t)}addMany(e,t){let n=[],i;t&&(i=t[Symbol.iterator]());for(let r of e){let s;if(i){let l=i.next();l.done||(s=l.value)}this.isRaw(r)&&(r=this._toEntryFn(r)),n.push(this.add(r,s))}return n}setMany(e,t){return this.addMany(e,t)}merge(e){this.addMany(e,[])}refill(e,t){this.clear(),this.addMany(e,t)}delete(e){let t=[];if(!this._root)return t;let n=this.getNode(e);if(!n)return t;let i=n==null?void 0:n.parent,r,s=n;if(!n.left&&!n.right&&!i)this._setRoot(void 0);else if(n.left){let l=this.getRightMost(d=>d,n.left);if(l){let d=l.parent;s=this._swapProperties(n,l),d&&(d.right===l?d.right=l.left:d.left=l.left,r=d)}}else if(i){let{familyPosition:l}=n;l==="LEFT"||l==="ROOT_LEFT"?i.left=n.right:(l==="RIGHT"||l==="ROOT_RIGHT")&&(i.right=n.right),r=i}else this._setRoot(n.right),n.right=void 0;return this._size=this._size-1,t.push({deleted:s,needBalanced:r}),this._isMapMode&&s&&this._store.delete(s.key),t}search(e,t=!1,n=this._DEFAULT_NODE_CALLBACK,i=this._root,r=this.iterationType){if(e===void 0)return[];if(e===null)return[];if(i=this.ensureNode(i),!i)return[];let s=this._ensurePredicate(e),l=[];if(r==="RECURSIVE"){let d=u=>{s(u)&&(l.push(n(u)),t)||!this.isRealNode(u.left)&&!this.isRealNode(u.right)||(this.isRealNode(u.left)&&d(u.left),this.isRealNode(u.right)&&d(u.right))};d(i)}else{let d=[i];for(;d.length>0;){let u=d.pop();if(this.isRealNode(u)){if(s(u)&&(l.push(n(u)),t))return l;this.isRealNode(u.left)&&d.push(u.left),this.isRealNode(u.right)&&d.push(u.right)}}}return l}getNodes(e,t=!1,n=this._root,i=this.iterationType){return this.search(e,t,r=>r,n,i)}getNode(e,t=this._root,n=this.iterationType){return this.search(e,!0,i=>i,t,n)[0]}get(e,t=this._root,n=this.iterationType){var i;if(this._isMapMode){let r=this._extractKey(e);return r==null?void 0:this._store.get(r)}return(i=this.getNode(e,t,n))==null?void 0:i.value}has(e,t=this._root,n=this.iterationType){return this.search(e,!0,i=>i,t,n).length>0}clear(){this._clearNodes(),this._isMapMode&&this._clearValues()}isEmpty(){return this._size===0}isPerfectlyBalanced(e=this._root){return this.getMinHeight(e)+1>=this.getHeight(e)}isBST(e=this._root,t=this.iterationType){let n=this.ensureNode(e);if(!n)return!0;if(t==="RECURSIVE"){let i=(l,d,u)=>{if(!this.isRealNode(l))return!0;let a=Number(l.key);return a<=d||a>=u?!1:i(l.left,d,a)&&i(l.right,a,u)},r=i(n,Number.MIN_SAFE_INTEGER,Number.MAX_SAFE_INTEGER),s=i(n,Number.MAX_SAFE_INTEGER,Number.MIN_SAFE_INTEGER);return r||s}else{let i=(l=!1)=>{let d=[],u=l?Number.MAX_SAFE_INTEGER:Number.MIN_SAFE_INTEGER,a=n;for(;this.isRealNode(a)||d.length>0;){for(;this.isRealNode(a);)d.push(a),a=a.left;a=d.pop();let p=Number(a.key);if(!this.isRealNode(a)||!l&&u>=p||l&&u<=p)return!1;u=p,a=a.right}return!0},r=i(!1),s=i(!0);return r||s}}getDepth(e,t=this._root){let n=this.ensureNode(e),i=this.ensureNode(t),r=0;for(;n!=null&&n.parent;){if(n===i)return r;r++,n=n.parent}return r}getHeight(e=this._root,t=this.iterationType){if(e=this.ensureNode(e),!this.isRealNode(e))return-1;if(t==="RECURSIVE"){let n=i=>{if(!this.isRealNode(i))return-1;let r=n(i.left),s=n(i.right);return Math.max(r,s)+1};return n(e)}else{let n=[{node:e,depth:0}],i=0;for(;n.length>0;){let{node:r,depth:s}=n.pop();this.isRealNode(r.left)&&n.push({node:r.left,depth:s+1}),this.isRealNode(r.right)&&n.push({node:r.right,depth:s+1}),i=Math.max(i,s)}return i}}getMinHeight(e=this._root,t=this.iterationType){if(e=this.ensureNode(e),!e)return-1;if(t==="RECURSIVE"){let n=i=>{if(!this.isRealNode(i)||!this.isRealNode(i.left)&&!this.isRealNode(i.right))return 0;let r=n(i.left),s=n(i.right);return Math.min(r,s)+1};return n(e)}else{let n=[],i=e,r=null,s=new Map;for(;n.length>0||i;)if(this.isRealNode(i))n.push(i),i=i.left;else if(i=n[n.length-1],!this.isRealNode(i.right)||r===i.right){if(i=n.pop(),this.isRealNode(i)){let l=this.isRealNode(i.left)?s.get(i.left):-1,d=this.isRealNode(i.right)?s.get(i.right):-1;s.set(i,1+Math.min(l,d)),r=i,i=null}}else i=i.right;return s.get(e)}}getPathToRoot(e,t=this._DEFAULT_NODE_CALLBACK,n=!1){let i=[],r=this.ensureNode(e);if(!r)return i;for(;r.parent;)i.push(t(r)),r=r.parent;return i.push(t(r)),n?i.reverse():i}getLeftMost(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){if(this.isNIL(t))return e(void 0);let i=this.ensureNode(t);if(!this.isRealNode(i))return e(void 0);if(n==="RECURSIVE"){let r=s=>{let{left:l}=s;return this.isRealNode(l)?r(l):s};return e(r(i))}else{let r=F(s=>{let{left:l}=s;return this.isRealNode(l)?D(()=>r(l)):s});return e(r(i))}}getRightMost(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){if(this.isNIL(t)||(t=this.ensureNode(t),!t))return e(void 0);if(n==="RECURSIVE"){let i=r=>{let{right:s}=r;return this.isRealNode(s)?i(s):r};return e(i(t))}else{let i=F(r=>{let{right:s}=r;return this.isRealNode(s)?D(()=>i(s)):r});return e(i(t))}}getPredecessor(e){if(this.isRealNode(e.left)){let t=e.left;for(;!this.isRealNode(t)||this.isRealNode(t.right)&&t.right!==e;)this.isRealNode(t)&&(t=t.right);return t}else return e}getSuccessor(e){if(e=this.ensureNode(e),!this.isRealNode(e))return;if(this.isRealNode(e.right))return this.getLeftMost(n=>n,e.right);let t=e.parent;for(;this.isRealNode(t)&&e===t.right;)e=t,t=t.parent;return t}dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,r=this.iterationType,s=!1){return i=this.ensureNode(i),i?this._dfs(e,t,n,i,r,s):[]}bfs(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType,i=!1){if(t=this.ensureNode(t),!t)return[];let r=[];if(n==="RECURSIVE"){let s=new m([t]),l=d=>{if(s.length===0)return;let u=s.shift();r.push(e(u)),i?(u&&this.isRealNodeOrNull(u.left)&&s.push(u.left),u&&this.isRealNodeOrNull(u.right)&&s.push(u.right)):(this.isRealNode(u.left)&&s.push(u.left),this.isRealNode(u.right)&&s.push(u.right)),l(d+1)};l(0)}else{let s=new m([t]);for(;s.length>0;){let l=s.length;for(let d=0;d<l;d++){let u=s.shift();r.push(e(u)),i?(u&&this.isRealNodeOrNull(u.left)&&s.push(u.left),u&&this.isRealNodeOrNull(u.right)&&s.push(u.right)):(this.isRealNode(u.left)&&s.push(u.left),this.isRealNode(u.right)&&s.push(u.right))}}}return r}leaves(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){t=this.ensureNode(t);let i=[];if(!this.isRealNode(t))return[];if(n==="RECURSIVE"){let r=s=>{this.isLeaf(s)&&i.push(e(s)),!(!this.isRealNode(s.left)&&!this.isRealNode(s.right))&&(this.isRealNode(s.left)&&r(s.left),this.isRealNode(s.right)&&r(s.right))};r(t)}else{let r=new m([t]);for(;r.length>0;){let s=r.shift();this.isRealNode(s)&&(this.isLeaf(s)&&i.push(e(s)),this.isRealNode(s.left)&&r.push(s.left),this.isRealNode(s.right)&&r.push(s.right))}}return i}listLevels(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType,i=!1){t=this.ensureNode(t);let r=[];if(!t)return r;if(n==="RECURSIVE"){let s=(l,d)=>{r[d]||(r[d]=[]),r[d].push(e(l)),i?(l&&this.isRealNodeOrNull(l.left)&&s(l.left,d+1),l&&this.isRealNodeOrNull(l.right)&&s(l.right,d+1)):(l&&l.left&&s(l.left,d+1),l&&l.right&&s(l.right,d+1))};s(t,0)}else{let s=[[t,0]];for(;s.length>0;){let l=s.pop(),[d,u]=l;r[u]||(r[u]=[]),r[u].push(e(d)),i?(d&&this.isRealNodeOrNull(d.right)&&s.push([d.right,u+1]),d&&this.isRealNodeOrNull(d.left)&&s.push([d.left,u+1])):(d&&d.right&&s.push([d.right,u+1]),d&&d.left&&s.push([d.left,u+1]))}}return r}morris(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=this._root){if(n=this.ensureNode(n),!n)return[];let i=[],r=n,s=d=>{let u=null,a=null;for(;d;)a=d.right,d.right=u,u=d,d=a;return u},l=d=>{let u=s(d),a=u;for(;a;)i.push(e(a)),a=a.right;s(u)};switch(t){case"IN":for(;r;){if(r.left){let d=this.getPredecessor(r);if(d.right)d.right=null;else{d.right=r,r=r.left;continue}}i.push(e(r)),r=r.right}break;case"PRE":for(;r;){if(r.left){let d=this.getPredecessor(r);if(d.right)d.right=null;else{d.right=r,i.push(e(r)),r=r.left;continue}}else i.push(e(r));r=r.right}break;case"POST":for(;r;){if(r.left){let d=this.getPredecessor(r);if(d.right===null){d.right=r,r=r.left;continue}else d.right=null,l(r.left)}r=r.right}l(n);break}return i}clone(){let e=this._createInstance();return this._clone(e),e}filter(e,t){let n=this._createInstance(),i=0;for(let[r,s]of this)e.call(t,s,r,i++,this)&&n.add([r,s]);return n}map(e,t,n){let i=this._createLike([],t),r=0;for(let[s,l]of this)i.add(e.call(n,l,s,r++,this));return i}toVisual(e=this._root,t){let n={isShowUndefined:!1,isShowNull:!0,isShowRedBlackNIL:!1,...t};e=this.ensureNode(e);let i="";return e&&(n.isShowUndefined&&(i+=`U for undefined
2
2
  `),n.isShowNull&&(i+=`N for null
3
3
  `),n.isShowRedBlackNIL&&(i+=`S for Sentinel Node(NIL)
4
- `),(o=>{let[l]=this._displayAux(o,n),u="";for(let d of l)u+=d+`
5
- `;i+=u})(e)),i}print(e,t=this._root){console.log(this.toVisual(t,e))}_dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,r=this.iterationType,o=!1,l=T=>!!T,u=T=>!!T,d=T=>o?this.isRealNodeOrNull(T):this.isRealNode(T),a=T=>this.isRealNodeOrNull(T)){if(i=this.ensureNode(i),!i)return[];let T=[];if(r==="RECURSIVE"){let p=f=>{if(!d(f))return;let V=()=>{l(f)&&(f==null?void 0:f.left)!==void 0&&p(f==null?void 0:f.left)},y=()=>{u(f)&&(f==null?void 0:f.right)!==void 0&&p(f==null?void 0:f.right)};switch(t){case"IN":if(V(),a(f)&&(T.push(e(f)),n))return;y();break;case"PRE":if(a(f)&&(T.push(e(f)),n))return;V(),y();break;case"POST":if(V(),y(),a(f)&&(T.push(e(f)),n))return;break}};p(i)}else{let p=[{opt:0,node:i}],f=K=>{var N;l(K.node)&&p.push({opt:0,node:(N=K.node)==null?void 0:N.left})},V=K=>{var N;u(K.node)&&p.push({opt:0,node:(N=K.node)==null?void 0:N.right})},y=K=>{d(K.node)&&p.push({opt:1,node:K.node})};for(;p.length>0;){let K=p.pop();if(K!==void 0&&d(K.node))if(K.opt===1){if(a(K.node)&&K.node!==void 0&&(T.push(e(K.node)),n))return T}else switch(t){case"IN":V(K),y(K),f(K);break;case"PRE":V(K),f(K),y(K);break;case"POST":y(K),V(K),f(K);break}}}return T}*_getIterator(e=this._root){if(e)if(this.iterationType==="ITERATIVE"){let t=[],n=e;for(;n||t.length>0;){for(;this.isRealNode(n);)t.push(n),n=n.left;n=t.pop(),this.isRealNode(n)&&(this._isMapMode?yield[n.key,this._store.get(n.key)]:yield[n.key,n.value],n=n.right)}}else e.left&&this.isRealNode(e)&&(yield*this[Symbol.iterator](e.left)),this._isMapMode?yield[e.key,this._store.get(e.key)]:yield[e.key,e.value],e.right&&this.isRealNode(e)&&(yield*this[Symbol.iterator](e.right))}_snapshotOptions(){return{iterationType:this.iterationType,toEntryFn:this.toEntryFn,isMapMode:this.isMapMode,isDuplicate:this.isDuplicate}}_createInstance(e){let t=this.constructor;return new t([],{...this._snapshotOptions(),...e!=null?e:{}})}_createLike(e=[],t){let n=this.constructor;return new n(e,{...this._snapshotOptions(),...t!=null?t:{}})}_keyValueNodeOrEntryToNodeAndValue(e,t){if(e===void 0)return[void 0,void 0];if(e===null)return[null,void 0];if(this.isNode(e))return[e,t];if(this.isEntry(e)){let[n,i]=e;if(n===void 0)return[void 0,void 0];if(n===null)return[null,void 0];let r=t!=null?t:i;return[this.createNode(n,r),r]}return[this.createNode(e,t),t]}_clone(e){this.bfs(t=>{t===null?e.add(null):this._isMapMode?e.add([t.key,this._store.get(t.key)]):e.add([t.key,t.value])},this._root,this.iterationType,!0),this._isMapMode&&(e._store=this._store)}_displayAux(e,t){let{isShowNull:n,isShowUndefined:i,isShowRedBlackNIL:r}=t,o=[["\u2500"],1,0,0];if(e===null&&!n)return o;if(e===void 0&&!i)return o;if(this.isNIL(e)&&!r)return o;if(e!=null){let u=e.key,d=this.isNIL(e)?"S":String(u),a=d.length;return l(d,a,this._displayAux(e.left,t),this._displayAux(e.right,t))}else{let u=e===void 0?"U":"N",d=u.length;return l(u,d,[[""],1,0,0],[[""],1,0,0])}function l(u,d,a,T){let[p,f,V,y]=a,[K,N,R,b]=T,k=" ".repeat(Math.max(0,y+1))+"_".repeat(Math.max(0,f-y-1))+u+"_".repeat(Math.max(0,b))+" ".repeat(Math.max(0,N-b)),O=(V>0?" ".repeat(y)+"/"+" ".repeat(f-y-1):" ".repeat(f))+" ".repeat(d)+(R>0?" ".repeat(b)+"\\"+" ".repeat(N-b-1):" ".repeat(N)),U=[k,O];for(let B=0;B<Math.max(V,R);B++){let z=B<V?p[B]:" ".repeat(f),Q=B<R?K[B]:" ".repeat(N);U.push(z+" ".repeat(d)+Q)}return[U,f+d+N,Math.max(V,R)+2,f+Math.floor(d/2)]}}_swapProperties(e,t){if(e=this.ensureNode(e),t=this.ensureNode(t),e&&t){let{key:n,value:i}=t,r=this.createNode(n,i);return r&&(t.key=e.key,this._isMapMode||(t.value=e.value),e.key=r.key,this._isMapMode||(e.value=r.value)),t}}_replaceNode(e,t){return e.parent&&(e.parent.left===e?e.parent.left=t:e.parent.right===e&&(e.parent.right=t)),t.left=e.left,t.right=e.right,t.parent=e.parent,this._root===e&&this._setRoot(t),t}_setRoot(e){e&&(e.parent=void 0),this._root=e}_ensurePredicate(e){if(e==null)return t=>!1;if(this._isPredicate(e))return e;if(this.isRealNode(e))return t=>t===e;if(this.isEntry(e)){let[t]=e;return n=>n?n.key===t:!1}return t=>t?t.key===e:!1}_isPredicate(e){return typeof e=="function"}_extractKey(e){if(e===null)return null;if(e!==void 0&&e!==this._NIL)return this.isNode(e)?e.key:this.isEntry(e)?e[0]:e}_setValue(e,t){return e==null||t===void 0?!1:this._store.set(e,t)}_clearNodes(){this._setRoot(void 0),this._size=0}_clearValues(){this._store.clear()}};var I=class{constructor(s,e){c(this,"key");c(this,"value");c(this,"parent");c(this,"_left");c(this,"_right");c(this,"_height",0);c(this,"_color","BLACK");c(this,"_count",1);this.key=s,this.value=e}get left(){return this._left}set left(s){s&&(s.parent=this),this._left=s}get right(){return this._right}set right(s){s&&(s.parent=this),this._right=s}get height(){return this._height}set height(s){this._height=s}get color(){return this._color}set color(s){this._color=s}get count(){return this._count}set count(s){this._count=s}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},L=class extends C{constructor(e=[],t){super([],t);c(this,"_root");c(this,"_isReverse",!1);c(this,"_comparator",(e,t)=>{if(g(e)&&g(t))return e>t?1:e<t?-1:0;if(this._specifyComparable){let n=this._specifyComparable(e),i=this._specifyComparable(t);return n>i?1:n<i?-1:0}if(typeof e=="object"||typeof t=="object")throw TypeError("When comparing object types, a custom specifyComparable must be defined in the constructor's options.");return 0});c(this,"_specifyComparable");if(t){let{specifyComparable:n,isReverse:i}=t;typeof n=="function"&&(this._specifyComparable=n),i!==void 0&&(this._isReverse=i)}e&&this.addMany(e)}get root(){return this._root}get isReverse(){return this._isReverse}get comparator(){return this._comparator}get specifyComparable(){return this._specifyComparable}createNode(e,t){return new I(e,this._isMapMode?void 0:t)}ensureNode(e,t=this.iterationType){var n;return(n=super.ensureNode(e,t))!=null?n:void 0}isNode(e){return e instanceof I}isValidKey(e){return g(e,this._specifyComparable!==void 0)}dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,r=this.iterationType){return super.dfs(e,t,n,i,r)}bfs(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){return super.bfs(e,t,n,!1)}listLevels(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){return super.listLevels(e,t,n,!1)}getNode(e,t=this._root,n=this.iterationType){var i;return(i=this.getNodes(e,!0,t,n)[0])!=null?i:void 0}search(e,t=!1,n=this._DEFAULT_NODE_CALLBACK,i=this._root,r=this.iterationType){if(e===void 0)return[];if(e===null)return[];if(i=this.ensureNode(i),!i)return[];let o,l=this.isRange(e);l?o=a=>a?e.isInRange(a.key,this._comparator):!1:o=this._ensurePredicate(e);let u=a=>{if(!a||!this.isRealNode(a.left))return!1;if(l){let T=e,p=this.isReverse?T.high:T.low,f=this.isReverse?T.includeHigh:T.includeLow;return f&&this._compare(a.key,p)>=0||!f&&this._compare(a.key,p)>0}if(!l&&!this._isPredicate(e)){let T=this._extractKey(e);return T!=null&&this._compare(a.key,T)>0}return!0},d=a=>{if(!a||!this.isRealNode(a.right))return!1;if(l){let T=e,p=this.isReverse?T.low:T.high,f=this.isReverse?T.includeLow:T.includeHigh;return f&&this._compare(a.key,p)<=0||!f&&this._compare(a.key,p)<0}if(!l&&!this._isPredicate(e)){let T=this._extractKey(e);return T!=null&&this._compare(a.key,T)<0}return!0};return super._dfs(n,"IN",t,i,r,!1,u,d,()=>!0,a=>!!a&&o(a))}rangeSearch(e,t=this._DEFAULT_NODE_CALLBACK,n=this._root,i=this.iterationType){let r=e instanceof _?e:new _(e[0],e[1]);return this.search(r,!1,t,n,i)}add(e,t){let[n,i]=this._keyValueNodeOrEntryToNodeAndValue(e,t);if(n===void 0)return!1;if(this._root===void 0)return this._setRoot(n),this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0;let r=this._root;for(;r!==void 0;){if(this._compare(r.key,n.key)===0)return this._replaceNode(r,n),this._isMapMode&&this._setValue(r.key,i),!0;if(this._compare(r.key,n.key)>0){if(r.left===void 0)return r.left=n,this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0;r.left!==null&&(r=r.left)}else{if(r.right===void 0)return r.right=n,this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0;r.right!==null&&(r=r.right)}}return!1}addMany(e,t,n=!0,i=this.iterationType){let r=[],o=t==null?void 0:t[Symbol.iterator]();if(!n){for(let p of e){let f=o==null?void 0:o.next().value;this.isRaw(p)&&(p=this._toEntryFn(p)),r.push(this.add(p,f))}return r}let l=[],u=0;for(let p of e)l.push({key:p,value:o==null?void 0:o.next().value,orgIndex:u++});let d=l.sort(({key:p},{key:f})=>{let V,y;return this.isRaw(p)?V=this._toEntryFn(p)[0]:this.isEntry(p)?V=p[0]:this.isRealNode(p)?V=p.key:V=p,this.isRaw(f)?y=this._toEntryFn(f)[0]:this.isEntry(f)?y=f[0]:this.isRealNode(f)?y=f.key:y=f,V!=null&&y!=null?this._compare(V,y):0}),a=p=>{if(p.length===0)return;let f=Math.floor((p.length-1)/2),{key:V,value:y,orgIndex:K}=p[f];if(this.isRaw(V)){let N=this._toEntryFn(V);r[K]=this.add(N)}else r[K]=this.add(V,y);a(p.slice(0,f)),a(p.slice(f+1))};return i==="RECURSIVE"?a(d):(()=>{let f=[[0,d.length-1]];for(;f.length>0;){let V=f.pop();if(!V)continue;let[y,K]=V;if(y>K)continue;let N=y+Math.floor((K-y)/2),{key:R,value:b,orgIndex:k}=d[N];if(this.isRaw(R)){let O=this._toEntryFn(R);r[k]=this.add(O)}else r[k]=this.add(R,b);f.push([N+1,K]),f.push([y,N-1])}})(),r}lowerBound(e,t=this.iterationType){return this._bound(e,!0,t)}upperBound(e,t=this.iterationType){return this._bound(e,!1,t)}lesserOrGreaterTraverse(e=this._DEFAULT_NODE_CALLBACK,t=-1,n=this._root,i=this.iterationType){let r=this.ensureNode(n),o=[];if(!this._root||!r)return o;let l=r.key;if(i==="RECURSIVE"){let u=d=>{let a=this._compare(d.key,l);Math.sign(a)==t&&o.push(e(d)),this.isRealNode(d.left)&&u(d.left),this.isRealNode(d.right)&&u(d.right)};return u(this._root),o}else{let u=new m([this._root]);for(;u.length>0;){let d=u.shift();if(this.isRealNode(d)){let a=this._compare(d.key,l);Math.sign(a)==t&&o.push(e(d)),this.isRealNode(d.left)&&u.push(d.left),this.isRealNode(d.right)&&u.push(d.right)}}return o}}perfectlyBalance(e=this.iterationType){let t=this.dfs(o=>o,"IN",!1,this._root,e),n=t.length;if(this._clearNodes(),n===0)return!1;let i=(o,l,u)=>{if(o>l)return;let d=o+(l-o>>1),a=t[d],T=i(o,d-1,a),p=i(d+1,l,a);return a.left=T,a.right=p,a.parent=u,a},r=i(0,n-1,void 0);return this._setRoot(r),this._size=n,!0}isAVLBalanced(e=this.iterationType){if(!this._root)return!0;let t=!0;if(e==="RECURSIVE"){let n=i=>{if(!i)return 0;let r=n(i.left),o=n(i.right);return Math.abs(r-o)>1&&(t=!1),Math.max(r,o)+1};n(this._root)}else{let n=[],i=this._root,r,o=new Map;for(;n.length>0||i;)if(i)n.push(i),i.left!==null&&(i=i.left);else if(i=n[n.length-1],!i.right||r===i.right){if(i=n.pop(),i){let l=i.left?o.get(i.left):-1,u=i.right?o.get(i.right):-1;if(Math.abs(l-u)>1)return!1;o.set(i,1+Math.max(l,u)),r=i,i=void 0}}else i=i.right}return t}map(e,t,n){let i=this._createLike([],t),r=0;for(let[o,l]of this)i.add(e.call(n,l,o,r++,this));return i}deleteWhere(e){let t=[],n=this._root,i=0;for(;t.length>0||n!==void 0;){for(;n!=null;)t.push(n),n=n.left;let r=t.pop();if(!r)break;let o=r.key,l=r.value;if(e(o,l,i++,this))return this._deleteByKey(o);n=r.right}return!1}_bound(e,t,n){if(e==null)return;if(this._isPredicate(e))return this._boundByPredicate(e,n);let i;if(this.isNode(e))i=e.key;else if(this.isEntry(e)){let r=e[0];if(r==null)return;i=r}else i=e;if(i!==void 0)return this._boundByKey(i,t,n)}_boundByKey(e,t,n){var i,r;if(n==="RECURSIVE"){let o=l=>{if(!this.isRealNode(l))return;let u=this.comparator(l.key,e);if(t?u>=0:u>0){let a=o(l.left);return a!=null?a:l}else return o(l.right)};return o(this.root)}else{let o=this.root,l;for(;this.isRealNode(o);){let u=this.comparator(o.key,e);(t?u>=0:u>0)?(l=o,o=(i=o.left)!=null?i:void 0):o=(r=o.right)!=null?r:void 0}return l}}_boundByPredicate(e,t){if(t==="RECURSIVE"){let n,i=r=>{n||!this.isRealNode(r)||(this.isRealNode(r.left)&&i(r.left),!n&&e(r)&&(n=r),!n&&this.isRealNode(r.right)&&i(r.right))};return i(this.root),n}else{let n=[],i=this.root;for(;n.length>0||this.isRealNode(i);)if(this.isRealNode(i))n.push(i),i=i.left;else{let r=n.pop();if(!this.isRealNode(r))break;if(e(r))return r;i=r.right}return}}_createInstance(e){let t=this.constructor;return new t([],{...this._snapshotOptions(),...e!=null?e:{}})}_createLike(e=[],t){let n=this.constructor;return new n(e,{...this._snapshotOptions(),...t!=null?t:{}})}_snapshotOptions(){return{...super._snapshotOptions(),specifyComparable:this.specifyComparable,isReverse:this.isReverse}}_keyValueNodeOrEntryToNodeAndValue(e,t){let[n,i]=super._keyValueNodeOrEntryToNodeAndValue(e,t);return n===null?[void 0,void 0]:[n,t!=null?t:i]}_setRoot(e){e&&(e.parent=void 0),this._root=e}_compare(e,t){return this._isReverse?-this._comparator(e,t):this._comparator(e,t)}_deleteByKey(e){var r;let t=this._root;for(;t;){let o=this._compare(t.key,e);if(o===0)break;t=o>0?t.left:t.right}if(!t)return!1;let n=(o,l)=>{let u=o==null?void 0:o.parent;u?u.left===o?u.left=l:u.right=l:this._setRoot(l),l&&(l.parent=u)},i=o=>{if(o){for(;o.left!==void 0&&o.left!==null;)o=o.left;return o}};if(t.left===void 0)n(t,t.right);else if(t.right===void 0)n(t,t.left);else{let o=i(t.right);o.parent!==t&&(n(o,o.right),o.right=t.right,o.right&&(o.right.parent=o)),n(t,o),o.left=t.left,o.left&&(o.left.parent=o)}return this._size=Math.max(0,((r=this._size)!=null?r:0)-1),!0}};var S=class{constructor(s,e){c(this,"key");c(this,"value");c(this,"parent");c(this,"_left");c(this,"_right");c(this,"_height",0);c(this,"_color","BLACK");c(this,"_count",1);this.key=s,this.value=e}get left(){return this._left}set left(s){s&&(s.parent=this),this._left=s}get right(){return this._right}set right(s){s&&(s.parent=this),this._right=s}get height(){return this._height}set height(s){this._height=s}get color(){return this._color}set color(s){this._color=s}get count(){return this._count}set count(s){this._count=s}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},P=class extends L{constructor(s=[],e){super([],e),s&&super.addMany(s)}createNode(s,e){return new S(s,this._isMapMode?void 0:e)}isNode(s){return s instanceof S}add(s,e){if(s===null)return!1;let t=super.add(s,e);return t&&this._balancePath(s),t}delete(s){let e=super.delete(s);for(let{needBalanced:t}of e)t&&this._balancePath(t);return e}perfectlyBalance(s=this.iterationType){let e=this.dfs(r=>r,"IN",!1,this._root,s),t=e.length;if(t===0)return!1;this._clearNodes();let n=(r,o,l)=>{if(r>o)return;let u=r+(o-r>>1),d=e[u];d.left=n(r,u-1,d),d.right=n(u+1,o,d),d.parent=l;let a=d.left?d.left.height:-1,T=d.right?d.right.height:-1;return d.height=Math.max(a,T)+1,d},i=n(0,t-1,void 0);return this._setRoot(i),this._size=t,!0}map(s,e,t){let n=this._createLike([],e),i=0;for(let[r,o]of this)n.add(s.call(t,o,r,i++,this));return n}_createInstance(s){let e=this.constructor;return new e([],{...this._snapshotOptions(),...s!=null?s:{}})}_createLike(s=[],e){let t=this.constructor;return new t(s,{...this._snapshotOptions(),...e!=null?e:{}})}_swapProperties(s,e){let t=this.ensureNode(s),n=this.ensureNode(e);if(t&&n){let{key:i,value:r,height:o}=n,l=this.createNode(i,r);return l&&(l.height=o,n.key=t.key,this._isMapMode||(n.value=t.value),n.height=t.height,t.key=l.key,this._isMapMode||(t.value=l.value),t.height=l.height),n}}_balanceFactor(s){let e=s.left?s.left.height:-1;return(s.right?s.right.height:-1)-e}_updateHeight(s){let e=s.left?s.left.height:-1,t=s.right?s.right.height:-1;s.height=1+Math.max(e,t)}_balanceLL(s){let e=s.parent,t=s.left;t!==null&&(s.parent=t),t&&t.right&&(t.right.parent=s),t&&(t.parent=e),s===this.root?t&&this._setRoot(t):(e==null?void 0:e.left)===s?e.left=t:e&&(e.right=t),t&&(s.left=t.right,t.right=s),this._updateHeight(s),t&&this._updateHeight(t)}_balanceLR(s){let e=s.parent,t=s.left,n;t&&(n=t.right),s&&n!==null&&(s.parent=n),t&&n!==null&&(t.parent=n),n&&(n.left&&t!==null&&(n.left.parent=t),n.right&&(n.right.parent=s),n.parent=e),s===this.root?n&&this._setRoot(n):e&&(e.left===s?e.left=n:e.right=n),n&&(s.left=n.right,t&&(t.right=n.left),n.left=t,n.right=s),this._updateHeight(s),t&&this._updateHeight(t),n&&this._updateHeight(n)}_balanceRR(s){let e=s.parent,t=s.right;t!==null&&(s.parent=t),t&&(t.left&&(t.left.parent=s),t.parent=e),s===this.root?t&&this._setRoot(t):e&&(e.left===s?e.left=t:e.right=t),t&&(s.right=t.left,t.left=s),this._updateHeight(s),t&&this._updateHeight(t)}_balanceRL(s){let e=s.parent,t=s.right,n;t&&(n=t.left),n!==null&&(s.parent=n),t&&n!==null&&(t.parent=n),n&&(n.left&&(n.left.parent=s),n.right&&t!==null&&(n.right.parent=t),n.parent=e),s===this.root?n&&this._setRoot(n):e&&(e.left===s?e.left=n:e.right=n),n&&(s.right=n.left),t&&n&&(t.left=n.right),n&&(n.left=s),n&&(n.right=t),this._updateHeight(s),t&&this._updateHeight(t),n&&this._updateHeight(n)}_balancePath(s){s=this.ensureNode(s);let e=this.getPathToRoot(s,t=>t,!1);for(let t=0;t<e.length;t++){let n=e[t];if(n)switch(this._updateHeight(n),this._balanceFactor(n)){case-2:n&&n.left&&(this._balanceFactor(n.left)<=0?this._balanceLL(n):this._balanceLR(n));break;case 2:n&&n.right&&(this._balanceFactor(n.right)>=0?this._balanceRR(n):this._balanceRL(n))}}}_replaceNode(s,e){return e.height=s.height,super._replaceNode(s,e)}};return Y(ee);})();
4
+ `),(s=>{let[l]=this._displayAux(s,n),d="";for(let u of l)d+=u+`
5
+ `;i+=d})(e)),i}print(e,t=this._root){console.log(this.toVisual(t,e))}_dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,r=this.iterationType,s=!1,l=p=>!!p,d=p=>!!p,u=p=>s?this.isRealNodeOrNull(p):this.isRealNode(p),a=p=>this.isRealNodeOrNull(p)){if(i=this.ensureNode(i),!i)return[];let p=[];if(r==="RECURSIVE"){let T=f=>{if(!u(f))return;let V=()=>{l(f)&&(f==null?void 0:f.left)!==void 0&&T(f==null?void 0:f.left)},N=()=>{d(f)&&(f==null?void 0:f.right)!==void 0&&T(f==null?void 0:f.right)};switch(t){case"IN":if(V(),a(f)&&(p.push(e(f)),n))return;N();break;case"PRE":if(a(f)&&(p.push(e(f)),n))return;V(),N();break;case"POST":if(V(),N(),a(f)&&(p.push(e(f)),n))return;break}};T(i)}else{let T=[{opt:0,node:i}],f=K=>{var y;l(K.node)&&T.push({opt:0,node:(y=K.node)==null?void 0:y.left})},V=K=>{var y;d(K.node)&&T.push({opt:0,node:(y=K.node)==null?void 0:y.right})},N=K=>{u(K.node)&&T.push({opt:1,node:K.node})};for(;T.length>0;){let K=T.pop();if(K!==void 0&&u(K.node))if(K.opt===1){if(a(K.node)&&K.node!==void 0&&(p.push(e(K.node)),n))return p}else switch(t){case"IN":V(K),N(K),f(K);break;case"PRE":V(K),f(K),N(K);break;case"POST":N(K),V(K),f(K);break}}}return p}*_getIterator(e=this._root){if(e)if(this.iterationType==="ITERATIVE"){let t=[],n=e;for(;n||t.length>0;){for(;this.isRealNode(n);)t.push(n),n=n.left;n=t.pop(),this.isRealNode(n)&&(this._isMapMode?yield[n.key,this._store.get(n.key)]:yield[n.key,n.value],n=n.right)}}else e.left&&this.isRealNode(e)&&(yield*this[Symbol.iterator](e.left)),this._isMapMode?yield[e.key,this._store.get(e.key)]:yield[e.key,e.value],e.right&&this.isRealNode(e)&&(yield*this[Symbol.iterator](e.right))}_snapshotOptions(){return{iterationType:this.iterationType,toEntryFn:this.toEntryFn,isMapMode:this.isMapMode,isDuplicate:this.isDuplicate}}_createInstance(e){let t=this.constructor;return new t([],{...this._snapshotOptions(),...e!=null?e:{}})}_createLike(e=[],t){let n=this.constructor;return new n(e,{...this._snapshotOptions(),...t!=null?t:{}})}_keyValueNodeOrEntryToNodeAndValue(e,t){if(e===void 0)return[void 0,void 0];if(e===null)return[null,void 0];if(this.isNode(e))return[e,t];if(this.isEntry(e)){let[n,i]=e;if(n===void 0)return[void 0,void 0];if(n===null)return[null,void 0];let r=t!=null?t:i;return[this.createNode(n,r),r]}return[this.createNode(e,t),t]}_clone(e){this.bfs(t=>{t===null?e.add(null):this._isMapMode?e.add([t.key,this._store.get(t.key)]):e.add([t.key,t.value])},this._root,this.iterationType,!0),this._isMapMode&&(e._store=this._store)}_displayAux(e,t){let{isShowNull:n,isShowUndefined:i,isShowRedBlackNIL:r}=t,s=[["\u2500"],1,0,0];if(e===null&&!n)return s;if(e===void 0&&!i)return s;if(this.isNIL(e)&&!r)return s;if(e!=null){let d=e.key,u=this.isNIL(e)?"S":String(d),a=u.length;return l(u,a,this._displayAux(e.left,t),this._displayAux(e.right,t))}else{let d=e===void 0?"U":"N",u=d.length;return l(d,u,[[""],1,0,0],[[""],1,0,0])}function l(d,u,a,p){let[T,f,V,N]=a,[K,y,R,B]=p,M=" ".repeat(Math.max(0,N+1))+"_".repeat(Math.max(0,f-N-1))+d+"_".repeat(Math.max(0,B))+" ".repeat(Math.max(0,y-B)),A=(V>0?" ".repeat(N)+"/"+" ".repeat(f-N-1):" ".repeat(f))+" ".repeat(u)+(R>0?" ".repeat(B)+"\\"+" ".repeat(y-B-1):" ".repeat(y)),P=[M,A];for(let b=0;b<Math.max(V,R);b++){let z=b<V?T[b]:" ".repeat(f),Q=b<R?K[b]:" ".repeat(y);P.push(z+" ".repeat(u)+Q)}return[P,f+u+y,Math.max(V,R)+2,f+Math.floor(u/2)]}}_swapProperties(e,t){if(e=this.ensureNode(e),t=this.ensureNode(t),e&&t){let{key:n,value:i}=t,r=this.createNode(n,i);return r&&(t.key=e.key,this._isMapMode||(t.value=e.value),e.key=r.key,this._isMapMode||(e.value=r.value)),t}}_replaceNode(e,t){return e.parent&&(e.parent.left===e?e.parent.left=t:e.parent.right===e&&(e.parent.right=t)),t.left=e.left,t.right=e.right,t.parent=e.parent,this._root===e&&this._setRoot(t),t}_setRoot(e){e&&(e.parent=void 0),this._root=e}_ensurePredicate(e){if(e==null)return t=>!1;if(this._isPredicate(e))return e;if(this.isRealNode(e))return t=>t===e;if(this.isEntry(e)){let[t]=e;return n=>n?n.key===t:!1}return t=>t?t.key===e:!1}_isPredicate(e){return typeof e=="function"}_extractKey(e){if(e===null)return null;if(e!==void 0&&e!==this._NIL)return this.isNode(e)?e.key:this.isEntry(e)?e[0]:e}_setValue(e,t){return e==null||t===void 0?!1:this._store.set(e,t)}_clearNodes(){this._setRoot(void 0),this._size=0}_clearValues(){this._store.clear()}};var S=class{constructor(o,e){c(this,"key");c(this,"value");c(this,"parent");c(this,"_left");c(this,"_right");c(this,"_height",0);c(this,"_color","BLACK");c(this,"_count",1);this.key=o,this.value=e}get left(){return this._left}set left(o){o&&(o.parent=this),this._left=o}get right(){return this._right}set right(o){o&&(o.parent=this),this._right=o}get height(){return this._height}set height(o){this._height=o}get color(){return this._color}set color(o){this._color=o}get count(){return this._count}set count(o){this._count=o}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},I=class extends C{constructor(e=[],t){super([],t);c(this,"_root");c(this,"_comparator");t?"comparator"in t&&t.comparator!==void 0?this._comparator=t.comparator:this._comparator=this._createDefaultComparator():this._comparator=this._createDefaultComparator(),e&&this.addMany(e)}get root(){return this._root}get comparator(){return this._comparator}createNode(e,t){return new S(e,this._isMapMode?void 0:t)}ensureNode(e,t=this.iterationType){var n;return(n=super.ensureNode(e,t))!=null?n:void 0}isNode(e){return e instanceof S}isValidKey(e){return g(e)}dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,r=this.iterationType){return super.dfs(e,t,n,i,r)}bfs(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){return super.bfs(e,t,n,!1)}listLevels(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){return super.listLevels(e,t,n,!1)}getNode(e,t=this._root,n=this.iterationType){var i;return(i=this.getNodes(e,!0,t,n)[0])!=null?i:void 0}search(e,t=!1,n=this._DEFAULT_NODE_CALLBACK,i=this._root,r=this.iterationType){if(e===void 0)return[];if(e===null)return[];if(i=this.ensureNode(i),!i)return[];let s,l=this.isRange(e);l?s=a=>a?e.isInRange(a.key,this._comparator):!1:s=this._ensurePredicate(e);let d=a=>{if(!a||!this.isRealNode(a.left))return!1;if(l){let p=e,T=p.low,f=p.includeLow;return f&&this._compare(a.key,T)>=0||!f&&this._compare(a.key,T)>0}if(!l&&!this._isPredicate(e)){let p=this._extractKey(e);return p!=null&&this._compare(a.key,p)>0}return!0},u=a=>{if(!a||!this.isRealNode(a.right))return!1;if(l){let p=e,T=p.high,f=p.includeHigh;return f&&this._compare(a.key,T)<=0||!f&&this._compare(a.key,T)<0}if(!l&&!this._isPredicate(e)){let p=this._extractKey(e);return p!=null&&this._compare(a.key,p)<0}return!0};return super._dfs(n,"IN",t,i,r,!1,d,u,()=>!0,a=>!!a&&s(a))}rangeSearch(e,t=this._DEFAULT_NODE_CALLBACK,n=this._root,i=this.iterationType){let r=e instanceof _?e:new _(e[0],e[1]);return this.search(r,!1,t,n,i)}add(e,t){let[n,i]=this._keyValueNodeOrEntryToNodeAndValue(e,t);if(n===void 0)return!1;if(this._root===void 0)return this._setRoot(n),this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0;let r=this._root;for(;r!==void 0;){if(this._compare(r.key,n.key)===0)return this._replaceNode(r,n),this._isMapMode&&this._setValue(r.key,i),!0;if(this._compare(r.key,n.key)>0){if(r.left===void 0)return r.left=n,this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0;r.left!==null&&(r=r.left)}else{if(r.right===void 0)return r.right=n,this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0;r.right!==null&&(r=r.right)}}return!1}addMany(e,t,n=!0,i=this.iterationType){let r=[],s=t==null?void 0:t[Symbol.iterator]();if(!n){for(let T of e){let f=s==null?void 0:s.next().value;this.isRaw(T)&&(T=this._toEntryFn(T)),r.push(this.add(T,f))}return r}let l=[],d=0;for(let T of e)l.push({key:T,value:s==null?void 0:s.next().value,orgIndex:d++});let u=l.sort(({key:T},{key:f})=>{let V,N;return this.isRaw(T)?V=this._toEntryFn(T)[0]:this.isEntry(T)?V=T[0]:this.isRealNode(T)?V=T.key:V=T,this.isRaw(f)?N=this._toEntryFn(f)[0]:this.isEntry(f)?N=f[0]:this.isRealNode(f)?N=f.key:N=f,V!=null&&N!=null?this._compare(V,N):0}),a=T=>{if(T.length===0)return;let f=Math.floor((T.length-1)/2),{key:V,value:N,orgIndex:K}=T[f];if(this.isRaw(V)){let y=this._toEntryFn(V);r[K]=this.add(y)}else r[K]=this.add(V,N);a(T.slice(0,f)),a(T.slice(f+1))};return i==="RECURSIVE"?a(u):(()=>{let f=[[0,u.length-1]];for(;f.length>0;){let V=f.pop();if(!V)continue;let[N,K]=V;if(N>K)continue;let y=N+Math.floor((K-N)/2),{key:R,value:B,orgIndex:M}=u[y];if(this.isRaw(R)){let A=this._toEntryFn(R);r[M]=this.add(A)}else r[M]=this.add(R,B);f.push([y+1,K]),f.push([N,y-1])}})(),r}lowerBound(e,t=this.iterationType){return this._bound(e,!0,t)}upperBound(e,t=this.iterationType){return this._bound(e,!1,t)}ceilingEntry(e,t=this.iterationType){return this.lowerBound(e,t)}higherEntry(e,t=this.iterationType){return this.upperBound(e,t)}floorEntry(e,t=this.iterationType){if(e==null)return;if(this._isPredicate(e))return this._floorByPredicate(e,t);let n;if(this.isNode(e))n=e.key;else if(this.isEntry(e)){let i=e[0];if(i==null)return;n=i}else n=e;if(n!==void 0)return this._floorByKey(n,t)}lowerEntry(e,t=this.iterationType){if(e==null)return;if(this._isPredicate(e))return this._lowerByPredicate(e,t);let n;if(this.isNode(e))n=e.key;else if(this.isEntry(e)){let i=e[0];if(i==null)return;n=i}else n=e;if(n!==void 0)return this._lowerByKey(n,t)}lesserOrGreaterTraverse(e=this._DEFAULT_NODE_CALLBACK,t=-1,n=this._root,i=this.iterationType){let r=this.ensureNode(n),s=[];if(!this._root||!r)return s;let l=r.key;if(i==="RECURSIVE"){let d=u=>{let a=this._compare(u.key,l);Math.sign(a)==t&&s.push(e(u)),this.isRealNode(u.left)&&d(u.left),this.isRealNode(u.right)&&d(u.right)};return d(this._root),s}else{let d=new m([this._root]);for(;d.length>0;){let u=d.shift();if(this.isRealNode(u)){let a=this._compare(u.key,l);Math.sign(a)==t&&s.push(e(u)),this.isRealNode(u.left)&&d.push(u.left),this.isRealNode(u.right)&&d.push(u.right)}}return s}}perfectlyBalance(e=this.iterationType){let t=this.dfs(s=>s,"IN",!1,this._root,e),n=t.length;if(this._clearNodes(),n===0)return!1;let i=(s,l,d)=>{if(s>l)return;let u=s+(l-s>>1),a=t[u],p=i(s,u-1,a),T=i(u+1,l,a);return a.left=p,a.right=T,a.parent=d,a},r=i(0,n-1,void 0);return this._setRoot(r),this._size=n,!0}isAVLBalanced(e=this.iterationType){if(!this._root)return!0;let t=!0;if(e==="RECURSIVE"){let n=i=>{if(!i)return 0;let r=n(i.left),s=n(i.right);return Math.abs(r-s)>1&&(t=!1),Math.max(r,s)+1};n(this._root)}else{let n=[],i=this._root,r,s=new Map;for(;n.length>0||i;)if(i)n.push(i),i.left!==null&&(i=i.left);else if(i=n[n.length-1],!i.right||r===i.right){if(i=n.pop(),i){let l=i.left?s.get(i.left):-1,d=i.right?s.get(i.right):-1;if(Math.abs(l-d)>1)return!1;s.set(i,1+Math.max(l,d)),r=i,i=void 0}}else i=i.right}return t}map(e,t,n){let i=this._createLike([],t),r=0;for(let[s,l]of this)i.add(e.call(n,l,s,r++,this));return i}deleteWhere(e,t=!1,n=this._root,i=this.iterationType){let r=this.search(e,t,l=>l,n,i),s=[];for(let l of r){let d=this.delete(l);s=s.concat(d)}return s}_createDefaultComparator(){return(e,t)=>{debugger;if(g(e)&&g(t))return e>t?1:e<t?-1:0;if(typeof e=="object"||typeof t=="object")throw TypeError("When comparing object type keys, a custom comparator must be provided in the constructor's options!");return 0}}_floorByKey(e,t){var n,i;if(t==="RECURSIVE"){let r=s=>{if(!this.isRealNode(s))return;if(this.comparator(s.key,e)<=0){let d=r(s.right);return d!=null?d:s}else return r(s.left)};return r(this.root)}else{let r=this.root,s;for(;this.isRealNode(r);)this.comparator(r.key,e)<=0?(s=r,r=(n=r.right)!=null?n:void 0):r=(i=r.left)!=null?i:void 0;return s}}_floorByPredicate(e,t){if(t==="RECURSIVE"){let n,i=r=>{this.isRealNode(r)&&(this.isRealNode(r.left)&&i(r.left),e(r)&&(n=r),this.isRealNode(r.right)&&i(r.right))};return i(this.root),n}else{let n=[],i=this.root,r;for(;n.length>0||this.isRealNode(i);)if(this.isRealNode(i))n.push(i),i=i.left;else{let s=n.pop();if(!this.isRealNode(s))break;e(s)&&(r=s),i=s.right}return r}}_lowerByKey(e,t){var n,i;if(t==="RECURSIVE"){let r=s=>{if(!this.isRealNode(s))return;if(this.comparator(s.key,e)<0){let d=r(s.right);return d!=null?d:s}else return r(s.left)};return r(this.root)}else{let r=this.root,s;for(;this.isRealNode(r);)this.comparator(r.key,e)<0?(s=r,r=(n=r.right)!=null?n:void 0):r=(i=r.left)!=null?i:void 0;return s}}_lowerByPredicate(e,t){if(t==="RECURSIVE"){let n,i=r=>{this.isRealNode(r)&&(this.isRealNode(r.left)&&i(r.left),e(r)&&(n=r),this.isRealNode(r.right)&&i(r.right))};return i(this.root),n}else{let n=[],i=this.root,r;for(;n.length>0||this.isRealNode(i);)if(this.isRealNode(i))n.push(i),i=i.left;else{let s=n.pop();if(!this.isRealNode(s))break;e(s)&&(r=s),i=s.right}return r}}_bound(e,t,n){if(e==null)return;if(this._isPredicate(e))return this._boundByPredicate(e,n);let i;if(this.isNode(e))i=e.key;else if(this.isEntry(e)){let r=e[0];if(r==null)return;i=r}else i=e;if(i!==void 0)return this._boundByKey(i,t,n)}_boundByKey(e,t,n){var i,r;if(n==="RECURSIVE"){let s=l=>{if(!this.isRealNode(l))return;let d=this.comparator(l.key,e);if(t?d>=0:d>0){let a=s(l.left);return a!=null?a:l}else return s(l.right)};return s(this.root)}else{let s=this.root,l;for(;this.isRealNode(s);){let d=this.comparator(s.key,e);(t?d>=0:d>0)?(l=s,s=(i=s.left)!=null?i:void 0):s=(r=s.right)!=null?r:void 0}return l}}_boundByPredicate(e,t){if(t==="RECURSIVE"){let n,i=r=>{n||!this.isRealNode(r)||(this.isRealNode(r.left)&&i(r.left),!n&&e(r)&&(n=r),!n&&this.isRealNode(r.right)&&i(r.right))};return i(this.root),n}else{let n=[],i=this.root;for(;n.length>0||this.isRealNode(i);)if(this.isRealNode(i))n.push(i),i=i.left;else{let r=n.pop();if(!this.isRealNode(r))break;if(e(r))return r;i=r.right}return}}_createInstance(e){let t=this.constructor;return new t([],{...this._snapshotOptions(),...e!=null?e:{}})}_createLike(e=[],t){let n=this.constructor;return new n(e,{...this._snapshotOptions(),...t!=null?t:{}})}_snapshotOptions(){return{...super._snapshotOptions(),comparator:this._comparator}}_keyValueNodeOrEntryToNodeAndValue(e,t){let[n,i]=super._keyValueNodeOrEntryToNodeAndValue(e,t);return n===null?[void 0,void 0]:[n,t!=null?t:i]}_setRoot(e){e&&(e.parent=void 0),this._root=e}_compare(e,t){return this._comparator(e,t)}_deleteByKey(e){var r;let t=this._root;for(;t;){let s=this._compare(t.key,e);if(s===0)break;t=s>0?t.left:t.right}if(!t)return!1;let n=(s,l)=>{let d=s==null?void 0:s.parent;d?d.left===s?d.left=l:d.right=l:this._setRoot(l),l&&(l.parent=d)},i=s=>{if(s){for(;s.left!==void 0&&s.left!==null;)s=s.left;return s}};if(t.left===void 0)n(t,t.right);else if(t.right===void 0)n(t,t.left);else{let s=i(t.right);s.parent!==t&&(n(s,s.right),s.right=t.right,s.right&&(s.right.parent=s)),n(t,s),s.left=t.left,s.left&&(s.left.parent=s)}return this._size=Math.max(0,((r=this._size)!=null?r:0)-1),!0}};var L=class{constructor(o,e){c(this,"key");c(this,"value");c(this,"parent");c(this,"_left");c(this,"_right");c(this,"_height",0);c(this,"_color","BLACK");c(this,"_count",1);this.key=o,this.value=e}get left(){return this._left}set left(o){o&&(o.parent=this),this._left=o}get right(){return this._right}set right(o){o&&(o.parent=this),this._right=o}get height(){return this._height}set height(o){this._height=o}get color(){return this._color}set color(o){this._color=o}get count(){return this._count}set count(o){this._count=o}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},U=class extends I{constructor(o=[],e){super([],e),o&&super.addMany(o)}createNode(o,e){return new L(o,this._isMapMode?void 0:e)}isNode(o){return o instanceof L}add(o,e){if(o===null)return!1;let t=super.add(o,e);return t&&this._balancePath(o),t}delete(o){let e=super.delete(o);for(let{needBalanced:t}of e)t&&this._balancePath(t);return e}perfectlyBalance(o=this.iterationType){let e=this.dfs(r=>r,"IN",!1,this._root,o),t=e.length;if(t===0)return!1;this._clearNodes();let n=(r,s,l)=>{if(r>s)return;let d=r+(s-r>>1),u=e[d];u.left=n(r,d-1,u),u.right=n(d+1,s,u),u.parent=l;let a=u.left?u.left.height:-1,p=u.right?u.right.height:-1;return u.height=Math.max(a,p)+1,u},i=n(0,t-1,void 0);return this._setRoot(i),this._size=t,!0}map(o,e,t){let n=this._createLike([],e),i=0;for(let[r,s]of this)n.add(o.call(t,s,r,i++,this));return n}_createInstance(o){let e=this.constructor;return new e([],{...this._snapshotOptions(),...o!=null?o:{}})}_createLike(o=[],e){let t=this.constructor;return new t(o,{...this._snapshotOptions(),...e!=null?e:{}})}_swapProperties(o,e){let t=this.ensureNode(o),n=this.ensureNode(e);if(t&&n){let{key:i,value:r,height:s}=n,l=this.createNode(i,r);return l&&(l.height=s,n.key=t.key,this._isMapMode||(n.value=t.value),n.height=t.height,t.key=l.key,this._isMapMode||(t.value=l.value),t.height=l.height),n}}_balanceFactor(o){let e=o.left?o.left.height:-1;return(o.right?o.right.height:-1)-e}_updateHeight(o){let e=o.left?o.left.height:-1,t=o.right?o.right.height:-1;o.height=1+Math.max(e,t)}_balanceLL(o){let e=o.parent,t=o.left;t!==null&&(o.parent=t),t&&t.right&&(t.right.parent=o),t&&(t.parent=e),o===this.root?t&&this._setRoot(t):(e==null?void 0:e.left)===o?e.left=t:e&&(e.right=t),t&&(o.left=t.right,t.right=o),this._updateHeight(o),t&&this._updateHeight(t)}_balanceLR(o){let e=o.parent,t=o.left,n;t&&(n=t.right),o&&n!==null&&(o.parent=n),t&&n!==null&&(t.parent=n),n&&(n.left&&t!==null&&(n.left.parent=t),n.right&&(n.right.parent=o),n.parent=e),o===this.root?n&&this._setRoot(n):e&&(e.left===o?e.left=n:e.right=n),n&&(o.left=n.right,t&&(t.right=n.left),n.left=t,n.right=o),this._updateHeight(o),t&&this._updateHeight(t),n&&this._updateHeight(n)}_balanceRR(o){let e=o.parent,t=o.right;t!==null&&(o.parent=t),t&&(t.left&&(t.left.parent=o),t.parent=e),o===this.root?t&&this._setRoot(t):e&&(e.left===o?e.left=t:e.right=t),t&&(o.right=t.left,t.left=o),this._updateHeight(o),t&&this._updateHeight(t)}_balanceRL(o){let e=o.parent,t=o.right,n;t&&(n=t.left),n!==null&&(o.parent=n),t&&n!==null&&(t.parent=n),n&&(n.left&&(n.left.parent=o),n.right&&t!==null&&(n.right.parent=t),n.parent=e),o===this.root?n&&this._setRoot(n):e&&(e.left===o?e.left=n:e.right=n),n&&(o.right=n.left),t&&n&&(t.left=n.right),n&&(n.left=o),n&&(n.right=t),this._updateHeight(o),t&&this._updateHeight(t),n&&this._updateHeight(n)}_balancePath(o){o=this.ensureNode(o);let e=this.getPathToRoot(o,t=>t,!1);for(let t=0;t<e.length;t++){let n=e[t];if(n)switch(this._updateHeight(n),this._balanceFactor(n)){case-2:n&&n.left&&(this._balanceFactor(n.left)<=0?this._balanceLL(n):this._balanceLR(n));break;case 2:n&&n.right&&(this._balanceFactor(n.right)>=0?this._balanceRR(n):this._balanceRL(n))}}}_replaceNode(o,e){return e.height=o.height,super._replaceNode(o,e)}};return Y(ee);})();
6
6
  /**
7
7
  * data-structure-typed
8
8
  *