red-black-tree-typed 2.2.0 → 2.2.2

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 (31) hide show
  1. package/dist/cjs/index.cjs +8 -8
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +8 -8
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +8 -8
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +8 -8
  8. package/dist/esm-legacy/index.mjs.map +1 -1
  9. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -1
  10. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
  11. package/dist/types/data-structures/binary-tree/bst.d.ts +1 -0
  12. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +1 -0
  13. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -0
  14. package/dist/types/types/data-structures/base/base.d.ts +1 -1
  15. package/dist/umd/red-black-tree-typed.js +8 -8
  16. package/dist/umd/red-black-tree-typed.js.map +1 -1
  17. package/dist/umd/red-black-tree-typed.min.js +2 -2
  18. package/dist/umd/red-black-tree-typed.min.js.map +1 -1
  19. package/package.json +2 -2
  20. package/src/data-structures/base/iterable-entry-base.ts +4 -4
  21. package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -1
  22. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
  23. package/src/data-structures/binary-tree/avl-tree.ts +4 -2
  24. package/src/data-structures/binary-tree/binary-tree.ts +3 -2
  25. package/src/data-structures/binary-tree/bst.ts +2 -1
  26. package/src/data-structures/binary-tree/red-black-tree.ts +2 -1
  27. package/src/data-structures/binary-tree/tree-counter.ts +1 -1
  28. package/src/data-structures/binary-tree/tree-multi-map.ts +2 -1
  29. package/src/data-structures/graph/abstract-graph.ts +3 -3
  30. package/src/data-structures/hash/hash-map.ts +4 -4
  31. package/src/types/data-structures/base/base.ts +1 -1
@@ -1,8 +1,8 @@
1
- "use strict";var redBlackTreeTyped=(()=>{var S=Object.defineProperty;var q=Object.getOwnPropertyDescriptor;var W=Object.getOwnPropertyNames;var X=Object.prototype.hasOwnProperty;var J=(h,o,e)=>o in h?S(h,o,{enumerable:!0,configurable:!0,writable:!0,value:e}):h[o]=e;var Y=(h,o)=>{for(var e in o)S(h,e,{get:o[e],enumerable:!0})},Z=(h,o,e,t)=>{if(o&&typeof o=="object"||typeof o=="function")for(let n of W(o))!X.call(h,n)&&n!==e&&S(h,n,{get:()=>o[n],enumerable:!(t=q(o,n))||t.enumerable});return h};var $=h=>Z(S({},"__esModule",{value:!0}),h);var c=(h,o,e)=>J(h,typeof o!="symbol"?o+"":o,e);var te={};Y(te,{BST:()=>I,BSTNode:()=>k,BinaryTree:()=>C,BinaryTreeNode:()=>E,DFSOperation:()=>P,Range:()=>_,RedBlackTree:()=>U,RedBlackTreeNode:()=>L});function v(h){let o=typeof h;return o==="number"?!0:o==="bigint"||o==="string"||o==="boolean"}function Q(h){if(typeof h.valueOf=="function"){let o=h.valueOf();if(o!==h){if(v(o))return o;if(typeof o=="object"&&o!==null)return Q(o)}}if(typeof h.toString=="function"){let o=h.toString();if(o!=="[object Object]")return o}return null}function R(h,o=!1){if(h==null)return!1;if(v(h))return!0;if(typeof h!="object")return!1;if(h instanceof Date||o)return!0;let e=Q(h);return e==null?!1:v(e)}var D=h=>({isThunk:!0,fn:h}),z=h=>typeof h=="object"&&h!==null&&"isThunk"in h&&h.isThunk;function ee(h){let o=h;for(;z(o);)o=o.fn();return o}function F(h){return(...o)=>ee(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 s=n.next();if(s.done)throw new TypeError("Reduce of empty structure with no initial value");i=s.value,t=1}for(let s of n)i=o(i,s,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 s=0;s<t;s++){let r=this.deleteAt(e);r!==void 0&&i.push(r)}for(let s=0;s<n.length;s++)this.addAt(e+s,n[s]);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,s=this._elements.splice(i,t,...n);this.elements.length>0&&this.offset/this.elements.length>this.autoCompactRatio&&this.compact();let r=this._createInstance({toElementFn:this.toElementFn,maxLen:this._maxLen});return r._setAutoCompactRatio(this._autoCompactRatio),r.pushMany(s),r}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 s of this)e.call(t,s,i,this)&&n.push(s),i++;return n}map(e,t,n){var r,l;let i=new this.constructor([],{toElementFn:t==null?void 0:t.toElementFn,maxLen:(r=t==null?void 0:t.maxLen)!=null?r:this._maxLen,autoCompactRatio:(l=t==null?void 0:t.autoCompactRatio)!=null?l:this._autoCompactRatio}),s=0;for(let a of this)i.push(n===void 0?e(a,s++,this):e.call(n,a,s++,this));return i}mapSame(e,t){var r;let n=this.constructor,i=new n([],{toElementFn:this.toElementFn,maxLen:this._maxLen,autoCompactRatio:this._autoCompactRatio});(r=i._setAutoCompactRatio)==null||r.call(i,this._autoCompactRatio);let s=0;for(let l of this){let a=t===void 0?e(l,s++,this):e.call(t,l,s++,this);i.push(a)}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 A=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[0],n[1],t++,this))return!1;return!0}some(o,e){let t=0;for(let n of this)if(o.call(e,n[0],n[1],t++,this))return!0;return!1}forEach(o,e){let t=0;for(let n of this){let[i,s]=n;o.call(e,i,s,t++,this)}}find(o,e){let t=0;for(let n of this){let[i,s]=n;if(o.call(e,i,s,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[s,r]=i;t=o(t,r,s,n++,this)}return t}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var P=(e=>(e[e.VISIT=0]="VISIT",e[e.PROCESS=1]="PROCESS",e))(P||{}),_=class{constructor(o,e,t=!0,n=!0){this.low=o;this.high=e;this.includeLow=t;this.includeHigh=n;if(!(R(o)&&R(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 A{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:s,isDuplicate:r}=t;if(n&&(this.iterationType=n),s!==void 0&&(this._isMapMode=s),r!==void 0&&(this._isDuplicate=r),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:R(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 s=new m([this._root]),r;for(;s.length>0;){let l=s.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;r===void 0&&(l.left===void 0||l.right===void 0)&&(r=l),l.left!==null&&l.left&&s.push(l.left),l.right!==null&&l.right&&s.push(l.right)}}return r?(r.left===void 0?r.left=n:r.right===void 0&&(r.right=n),this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0):!1}addMany(e,t){let n=[],i;t&&(i=t[Symbol.iterator]());for(let s of e){let r;if(i){let l=i.next();l.done||(r=l.value)}this.isRaw(s)&&(s=this._toEntryFn(s)),n.push(this.add(s,r))}return n}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,s,r=n;if(!n.left&&!n.right&&!i)this._setRoot(void 0);else if(n.left){let l=this.getRightMost(a=>a,n.left);if(l){let a=l.parent;r=this._swapProperties(n,l),a&&(a.right===l?a.right=l.left:a.left=l.left,s=a)}}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),s=i}else this._setRoot(n.right),n.right=void 0;return this._size=this._size-1,t.push({deleted:r,needBalanced:s}),this._isMapMode&&r&&this._store.delete(r.key),t}search(e,t=!1,n=this._DEFAULT_NODE_CALLBACK,i=this._root,s=this.iterationType){if(e===void 0)return[];if(e===null)return[];if(i=this.ensureNode(i),!i)return[];let r=this._ensurePredicate(e),l=[];if(s==="RECURSIVE"){let a=u=>{r(u)&&(l.push(n(u)),t)||!this.isRealNode(u.left)&&!this.isRealNode(u.right)||(this.isRealNode(u.left)&&a(u.left),this.isRealNode(u.right)&&a(u.right))};a(i)}else{let a=[i];for(;a.length>0;){let u=a.pop();if(this.isRealNode(u)){if(r(u)&&(l.push(n(u)),t))return l;this.isRealNode(u.left)&&a.push(u.left),this.isRealNode(u.right)&&a.push(u.right)}}}return l}getNodes(e,t=!1,n=this._root,i=this.iterationType){return this.search(e,t,s=>s,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 s=this._extractKey(e);return s==null?void 0:this._store.get(s)}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,a,u)=>{if(!this.isRealNode(l))return!0;let d=Number(l.key);return d<=a||d>=u?!1:i(l.left,a,d)&&i(l.right,d,u)},s=i(n,Number.MIN_SAFE_INTEGER,Number.MAX_SAFE_INTEGER),r=i(n,Number.MAX_SAFE_INTEGER,Number.MIN_SAFE_INTEGER);return s||r}else{let i=(l=!1)=>{let a=[],u=l?Number.MAX_SAFE_INTEGER:Number.MIN_SAFE_INTEGER,d=n;for(;this.isRealNode(d)||a.length>0;){for(;this.isRealNode(d);)a.push(d),d=d.left;d=a.pop();let p=Number(d.key);if(!this.isRealNode(d)||!l&&u>=p||l&&u<=p)return!1;u=p,d=d.right}return!0},s=i(!1),r=i(!0);return s||r}}getDepth(e,t=this._root){let n=this.ensureNode(e),i=this.ensureNode(t),s=0;for(;n!=null&&n.parent;){if(n===i)return s;s++,n=n.parent}return s}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 s=n(i.left),r=n(i.right);return Math.max(s,r)+1};return n(e)}else{let n=[{node:e,depth:0}],i=0;for(;n.length>0;){let{node:s,depth:r}=n.pop();this.isRealNode(s.left)&&n.push({node:s.left,depth:r+1}),this.isRealNode(s.right)&&n.push({node:s.right,depth:r+1}),i=Math.max(i,r)}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 s=n(i.left),r=n(i.right);return Math.min(s,r)+1};return n(e)}else{let n=[],i=e,s=null,r=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)||s===i.right){if(i=n.pop(),this.isRealNode(i)){let l=this.isRealNode(i.left)?r.get(i.left):-1,a=this.isRealNode(i.right)?r.get(i.right):-1;r.set(i,1+Math.min(l,a)),s=i,i=null}}else i=i.right;return r.get(e)}}getPathToRoot(e,t=this._DEFAULT_NODE_CALLBACK,n=!1){let i=[],s=this.ensureNode(e);if(!s)return i;for(;s.parent;)i.push(t(s)),s=s.parent;return i.push(t(s)),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 s=r=>{let{left:l}=r;return this.isRealNode(l)?s(l):r};return e(s(i))}else{let s=F(r=>{let{left:l}=r;return this.isRealNode(l)?D(()=>s(l)):r});return e(s(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=s=>{let{right:r}=s;return this.isRealNode(r)?i(r):s};return e(i(t))}else{let i=F(s=>{let{right:r}=s;return this.isRealNode(r)?D(()=>i(r)):s});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,s=this.iterationType,r=!1){return i=this.ensureNode(i),i?this._dfs(e,t,n,i,s,r):[]}bfs(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType,i=!1){if(t=this.ensureNode(t),!t)return[];let s=[];if(n==="RECURSIVE"){let r=new m([t]),l=a=>{if(r.length===0)return;let u=r.shift();s.push(e(u)),i?(u&&this.isRealNodeOrNull(u.left)&&r.push(u.left),u&&this.isRealNodeOrNull(u.right)&&r.push(u.right)):(this.isRealNode(u.left)&&r.push(u.left),this.isRealNode(u.right)&&r.push(u.right)),l(a+1)};l(0)}else{let r=new m([t]);for(;r.length>0;){let l=r.length;for(let a=0;a<l;a++){let u=r.shift();s.push(e(u)),i?(u&&this.isRealNodeOrNull(u.left)&&r.push(u.left),u&&this.isRealNodeOrNull(u.right)&&r.push(u.right)):(this.isRealNode(u.left)&&r.push(u.left),this.isRealNode(u.right)&&r.push(u.right))}}}return s}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 s=r=>{this.isLeaf(r)&&i.push(e(r)),!(!this.isRealNode(r.left)&&!this.isRealNode(r.right))&&(this.isRealNode(r.left)&&s(r.left),this.isRealNode(r.right)&&s(r.right))};s(t)}else{let s=new m([t]);for(;s.length>0;){let r=s.shift();this.isRealNode(r)&&(this.isLeaf(r)&&i.push(e(r)),this.isRealNode(r.left)&&s.push(r.left),this.isRealNode(r.right)&&s.push(r.right))}}return i}listLevels(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType,i=!1){t=this.ensureNode(t);let s=[];if(!t)return s;if(n==="RECURSIVE"){let r=(l,a)=>{s[a]||(s[a]=[]),s[a].push(e(l)),i?(l&&this.isRealNodeOrNull(l.left)&&r(l.left,a+1),l&&this.isRealNodeOrNull(l.right)&&r(l.right,a+1)):(l&&l.left&&r(l.left,a+1),l&&l.right&&r(l.right,a+1))};r(t,0)}else{let r=[[t,0]];for(;r.length>0;){let l=r.pop(),[a,u]=l;s[u]||(s[u]=[]),s[u].push(e(a)),i?(a&&this.isRealNodeOrNull(a.right)&&r.push([a.right,u+1]),a&&this.isRealNodeOrNull(a.left)&&r.push([a.left,u+1])):(a&&a.right&&r.push([a.right,u+1]),a&&a.left&&r.push([a.left,u+1]))}}return s}morris(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=this._root){if(n=this.ensureNode(n),!n)return[];let i=[],s=n,r=a=>{let u=null,d=null;for(;a;)d=a.right,a.right=u,u=a,a=d;return u},l=a=>{let u=r(a),d=u;for(;d;)i.push(e(d)),d=d.right;r(u)};switch(t){case"IN":for(;s;){if(s.left){let a=this.getPredecessor(s);if(a.right)a.right=null;else{a.right=s,s=s.left;continue}}i.push(e(s)),s=s.right}break;case"PRE":for(;s;){if(s.left){let a=this.getPredecessor(s);if(a.right)a.right=null;else{a.right=s,i.push(e(s)),s=s.left;continue}}else i.push(e(s));s=s.right}break;case"POST":for(;s;){if(s.left){let a=this.getPredecessor(s);if(a.right===null){a.right=s,s=s.left;continue}else a.right=null,l(s.left)}s=s.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[s,r]of this)e.call(t,s,r,i++,this)&&n.add([s,r]);return n}map(e,t,n){let i=this._createLike([],t),s=0;for(let[r,l]of this)i.add(e.call(n,r,l,s++,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 redBlackTreeTyped=(()=>{var S=Object.defineProperty;var q=Object.getOwnPropertyDescriptor;var W=Object.getOwnPropertyNames;var X=Object.prototype.hasOwnProperty;var J=(h,o,e)=>o in h?S(h,o,{enumerable:!0,configurable:!0,writable:!0,value:e}):h[o]=e;var Y=(h,o)=>{for(var e in o)S(h,e,{get:o[e],enumerable:!0})},Z=(h,o,e,t)=>{if(o&&typeof o=="object"||typeof o=="function")for(let n of W(o))!X.call(h,n)&&n!==e&&S(h,n,{get:()=>o[n],enumerable:!(t=q(o,n))||t.enumerable});return h};var $=h=>Z(S({},"__esModule",{value:!0}),h);var c=(h,o,e)=>J(h,typeof o!="symbol"?o+"":o,e);var te={};Y(te,{BST:()=>I,BSTNode:()=>k,BinaryTree:()=>C,BinaryTreeNode:()=>E,DFSOperation:()=>P,Range:()=>_,RedBlackTree:()=>U,RedBlackTreeNode:()=>L});function v(h){let o=typeof h;return o==="number"?!0:o==="bigint"||o==="string"||o==="boolean"}function Q(h){if(typeof h.valueOf=="function"){let o=h.valueOf();if(o!==h){if(v(o))return o;if(typeof o=="object"&&o!==null)return Q(o)}}if(typeof h.toString=="function"){let o=h.toString();if(o!=="[object Object]")return o}return null}function R(h,o=!1){if(h==null)return!1;if(v(h))return!0;if(typeof h!="object")return!1;if(h instanceof Date||o)return!0;let e=Q(h);return e==null?!1:v(e)}var D=h=>({isThunk:!0,fn:h}),z=h=>typeof h=="object"&&h!==null&&"isThunk"in h&&h.isThunk;function ee(h){let o=h;for(;z(o);)o=o.fn();return o}function F(h){return(...o)=>ee(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 s=n.next();if(s.done)throw new TypeError("Reduce of empty structure with no initial value");i=s.value,t=1}for(let s of n)i=o(i,s,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 s=0;s<t;s++){let r=this.deleteAt(e);r!==void 0&&i.push(r)}for(let s=0;s<n.length;s++)this.addAt(e+s,n[s]);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,s=this._elements.splice(i,t,...n);this.elements.length>0&&this.offset/this.elements.length>this.autoCompactRatio&&this.compact();let r=this._createInstance({toElementFn:this.toElementFn,maxLen:this._maxLen});return r._setAutoCompactRatio(this._autoCompactRatio),r.pushMany(s),r}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 s of this)e.call(t,s,i,this)&&n.push(s),i++;return n}map(e,t,n){var r,l;let i=new this.constructor([],{toElementFn:t==null?void 0:t.toElementFn,maxLen:(r=t==null?void 0:t.maxLen)!=null?r:this._maxLen,autoCompactRatio:(l=t==null?void 0:t.autoCompactRatio)!=null?l:this._autoCompactRatio}),s=0;for(let a of this)i.push(n===void 0?e(a,s++,this):e.call(n,a,s++,this));return i}mapSame(e,t){var r;let n=this.constructor,i=new n([],{toElementFn:this.toElementFn,maxLen:this._maxLen,autoCompactRatio:this._autoCompactRatio});(r=i._setAutoCompactRatio)==null||r.call(i,this._autoCompactRatio);let s=0;for(let l of this){let a=t===void 0?e(l,s++,this):e.call(t,l,s++,this);i.push(a)}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 A=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,s]=n;o.call(e,s,i,t++,this)}}find(o,e){let t=0;for(let n of this){let[i,s]=n;if(o.call(e,s,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[s,r]=i;t=o(t,r,s,n++,this)}return t}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var P=(e=>(e[e.VISIT=0]="VISIT",e[e.PROCESS=1]="PROCESS",e))(P||{}),_=class{constructor(o,e,t=!0,n=!0){this.low=o;this.high=e;this.includeLow=t;this.includeHigh=n;if(!(R(o)&&R(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 A{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:s,isDuplicate:r}=t;if(n&&(this.iterationType=n),s!==void 0&&(this._isMapMode=s),r!==void 0&&(this._isDuplicate=r),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:R(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 s=new m([this._root]),r;for(;s.length>0;){let l=s.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;r===void 0&&(l.left===void 0||l.right===void 0)&&(r=l),l.left!==null&&l.left&&s.push(l.left),l.right!==null&&l.right&&s.push(l.right)}}return r?(r.left===void 0?r.left=n:r.right===void 0&&(r.right=n),this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0):!1}addMany(e,t){let n=[],i;t&&(i=t[Symbol.iterator]());for(let s of e){let r;if(i){let l=i.next();l.done||(r=l.value)}this.isRaw(s)&&(s=this._toEntryFn(s)),n.push(this.add(s,r))}return n}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,s,r=n;if(!n.left&&!n.right&&!i)this._setRoot(void 0);else if(n.left){let l=this.getRightMost(a=>a,n.left);if(l){let a=l.parent;r=this._swapProperties(n,l),a&&(a.right===l?a.right=l.left:a.left=l.left,s=a)}}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),s=i}else this._setRoot(n.right),n.right=void 0;return this._size=this._size-1,t.push({deleted:r,needBalanced:s}),this._isMapMode&&r&&this._store.delete(r.key),t}search(e,t=!1,n=this._DEFAULT_NODE_CALLBACK,i=this._root,s=this.iterationType){if(e===void 0)return[];if(e===null)return[];if(i=this.ensureNode(i),!i)return[];let r=this._ensurePredicate(e),l=[];if(s==="RECURSIVE"){let a=u=>{r(u)&&(l.push(n(u)),t)||!this.isRealNode(u.left)&&!this.isRealNode(u.right)||(this.isRealNode(u.left)&&a(u.left),this.isRealNode(u.right)&&a(u.right))};a(i)}else{let a=[i];for(;a.length>0;){let u=a.pop();if(this.isRealNode(u)){if(r(u)&&(l.push(n(u)),t))return l;this.isRealNode(u.left)&&a.push(u.left),this.isRealNode(u.right)&&a.push(u.right)}}}return l}getNodes(e,t=!1,n=this._root,i=this.iterationType){return this.search(e,t,s=>s,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 s=this._extractKey(e);return s==null?void 0:this._store.get(s)}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,a,u)=>{if(!this.isRealNode(l))return!0;let d=Number(l.key);return d<=a||d>=u?!1:i(l.left,a,d)&&i(l.right,d,u)},s=i(n,Number.MIN_SAFE_INTEGER,Number.MAX_SAFE_INTEGER),r=i(n,Number.MAX_SAFE_INTEGER,Number.MIN_SAFE_INTEGER);return s||r}else{let i=(l=!1)=>{let a=[],u=l?Number.MAX_SAFE_INTEGER:Number.MIN_SAFE_INTEGER,d=n;for(;this.isRealNode(d)||a.length>0;){for(;this.isRealNode(d);)a.push(d),d=d.left;d=a.pop();let p=Number(d.key);if(!this.isRealNode(d)||!l&&u>=p||l&&u<=p)return!1;u=p,d=d.right}return!0},s=i(!1),r=i(!0);return s||r}}getDepth(e,t=this._root){let n=this.ensureNode(e),i=this.ensureNode(t),s=0;for(;n!=null&&n.parent;){if(n===i)return s;s++,n=n.parent}return s}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 s=n(i.left),r=n(i.right);return Math.max(s,r)+1};return n(e)}else{let n=[{node:e,depth:0}],i=0;for(;n.length>0;){let{node:s,depth:r}=n.pop();this.isRealNode(s.left)&&n.push({node:s.left,depth:r+1}),this.isRealNode(s.right)&&n.push({node:s.right,depth:r+1}),i=Math.max(i,r)}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 s=n(i.left),r=n(i.right);return Math.min(s,r)+1};return n(e)}else{let n=[],i=e,s=null,r=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)||s===i.right){if(i=n.pop(),this.isRealNode(i)){let l=this.isRealNode(i.left)?r.get(i.left):-1,a=this.isRealNode(i.right)?r.get(i.right):-1;r.set(i,1+Math.min(l,a)),s=i,i=null}}else i=i.right;return r.get(e)}}getPathToRoot(e,t=this._DEFAULT_NODE_CALLBACK,n=!1){let i=[],s=this.ensureNode(e);if(!s)return i;for(;s.parent;)i.push(t(s)),s=s.parent;return i.push(t(s)),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 s=r=>{let{left:l}=r;return this.isRealNode(l)?s(l):r};return e(s(i))}else{let s=F(r=>{let{left:l}=r;return this.isRealNode(l)?D(()=>s(l)):r});return e(s(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=s=>{let{right:r}=s;return this.isRealNode(r)?i(r):s};return e(i(t))}else{let i=F(s=>{let{right:r}=s;return this.isRealNode(r)?D(()=>i(r)):s});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,s=this.iterationType,r=!1){return i=this.ensureNode(i),i?this._dfs(e,t,n,i,s,r):[]}bfs(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType,i=!1){if(t=this.ensureNode(t),!t)return[];let s=[];if(n==="RECURSIVE"){let r=new m([t]),l=a=>{if(r.length===0)return;let u=r.shift();s.push(e(u)),i?(u&&this.isRealNodeOrNull(u.left)&&r.push(u.left),u&&this.isRealNodeOrNull(u.right)&&r.push(u.right)):(this.isRealNode(u.left)&&r.push(u.left),this.isRealNode(u.right)&&r.push(u.right)),l(a+1)};l(0)}else{let r=new m([t]);for(;r.length>0;){let l=r.length;for(let a=0;a<l;a++){let u=r.shift();s.push(e(u)),i?(u&&this.isRealNodeOrNull(u.left)&&r.push(u.left),u&&this.isRealNodeOrNull(u.right)&&r.push(u.right)):(this.isRealNode(u.left)&&r.push(u.left),this.isRealNode(u.right)&&r.push(u.right))}}}return s}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 s=r=>{this.isLeaf(r)&&i.push(e(r)),!(!this.isRealNode(r.left)&&!this.isRealNode(r.right))&&(this.isRealNode(r.left)&&s(r.left),this.isRealNode(r.right)&&s(r.right))};s(t)}else{let s=new m([t]);for(;s.length>0;){let r=s.shift();this.isRealNode(r)&&(this.isLeaf(r)&&i.push(e(r)),this.isRealNode(r.left)&&s.push(r.left),this.isRealNode(r.right)&&s.push(r.right))}}return i}listLevels(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType,i=!1){t=this.ensureNode(t);let s=[];if(!t)return s;if(n==="RECURSIVE"){let r=(l,a)=>{s[a]||(s[a]=[]),s[a].push(e(l)),i?(l&&this.isRealNodeOrNull(l.left)&&r(l.left,a+1),l&&this.isRealNodeOrNull(l.right)&&r(l.right,a+1)):(l&&l.left&&r(l.left,a+1),l&&l.right&&r(l.right,a+1))};r(t,0)}else{let r=[[t,0]];for(;r.length>0;){let l=r.pop(),[a,u]=l;s[u]||(s[u]=[]),s[u].push(e(a)),i?(a&&this.isRealNodeOrNull(a.right)&&r.push([a.right,u+1]),a&&this.isRealNodeOrNull(a.left)&&r.push([a.left,u+1])):(a&&a.right&&r.push([a.right,u+1]),a&&a.left&&r.push([a.left,u+1]))}}return s}morris(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=this._root){if(n=this.ensureNode(n),!n)return[];let i=[],s=n,r=a=>{let u=null,d=null;for(;a;)d=a.right,a.right=u,u=a,a=d;return u},l=a=>{let u=r(a),d=u;for(;d;)i.push(e(d)),d=d.right;r(u)};switch(t){case"IN":for(;s;){if(s.left){let a=this.getPredecessor(s);if(a.right)a.right=null;else{a.right=s,s=s.left;continue}}i.push(e(s)),s=s.right}break;case"PRE":for(;s;){if(s.left){let a=this.getPredecessor(s);if(a.right)a.right=null;else{a.right=s,i.push(e(s)),s=s.left;continue}}else i.push(e(s));s=s.right}break;case"POST":for(;s;){if(s.left){let a=this.getPredecessor(s);if(a.right===null){a.right=s,s=s.left;continue}else a.right=null,l(s.left)}s=s.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[s,r]of this)e.call(t,r,s,i++,this)&&n.add([s,r]);return n}map(e,t,n){let i=this._createLike([],t),s=0;for(let[r,l]of this)i.add(e.call(n,l,r,s++,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
4
  `),(r=>{let[l]=this._displayAux(r,n),a="";for(let u of l)a+=u+`
5
- `;i+=a})(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,s=this.iterationType,r=!1,l=p=>!!p,a=p=>!!p,u=p=>r?this.isRealNodeOrNull(p):this.isRealNode(p),d=p=>this.isRealNodeOrNull(p)){if(i=this.ensureNode(i),!i)return[];let p=[];if(s==="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)},y=()=>{a(f)&&(f==null?void 0:f.right)!==void 0&&T(f==null?void 0:f.right)};switch(t){case"IN":if(V(),d(f)&&(p.push(e(f)),n))return;y();break;case"PRE":if(d(f)&&(p.push(e(f)),n))return;V(),y();break;case"POST":if(V(),y(),d(f)&&(p.push(e(f)),n))return;break}};T(i)}else{let T=[{opt:0,node:i}],f=K=>{var N;l(K.node)&&T.push({opt:0,node:(N=K.node)==null?void 0:N.left})},V=K=>{var N;a(K.node)&&T.push({opt:0,node:(N=K.node)==null?void 0:N.right})},y=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(d(K.node)&&K.node!==void 0&&(p.push(e(K.node)),n))return p}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 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 s=t!=null?t:i;return[this.createNode(n,s),s]}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:s}=t,r=[["\u2500"],1,0,0];if(e===null&&!n)return r;if(e===void 0&&!i)return r;if(this.isNIL(e)&&!s)return r;if(e!=null){let a=e.key,u=this.isNIL(e)?"S":String(a),d=u.length;return l(u,d,this._displayAux(e.left,t),this._displayAux(e.right,t))}else{let a=e===void 0?"U":"N",u=a.length;return l(a,u,[[""],1,0,0],[[""],1,0,0])}function l(a,u,d,p){let[T,f,V,y]=d,[K,N,g,B]=p,M=" ".repeat(Math.max(0,y+1))+"_".repeat(Math.max(0,f-y-1))+a+"_".repeat(Math.max(0,B))+" ".repeat(Math.max(0,N-B)),O=(V>0?" ".repeat(y)+"/"+" ".repeat(f-y-1):" ".repeat(f))+" ".repeat(u)+(g>0?" ".repeat(B)+"\\"+" ".repeat(N-B-1):" ".repeat(N)),H=[M,O];for(let b=0;b<Math.max(V,g);b++){let G=b<V?T[b]:" ".repeat(f),j=b<g?K[b]:" ".repeat(N);H.push(G+" ".repeat(u)+j)}return[H,f+u+N,Math.max(V,g)+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,s=this.createNode(n,i);return s&&(t.key=e.key,this._isMapMode||(t.value=e.value),e.key=s.key,this._isMapMode||(e.value=s.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 k=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,"_isReverse",!1);c(this,"_comparator",(e,t)=>{if(R(e)&&R(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 k(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 k}isValidKey(e){return R(e,this._specifyComparable!==void 0)}dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,s=this.iterationType){return super.dfs(e,t,n,i,s)}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,s=this.iterationType){if(e===void 0)return[];if(e===null)return[];if(i=this.ensureNode(i),!i)return[];let r,l=this.isRange(e);l?r=d=>d?e.isInRange(d.key,this._comparator):!1:r=this._ensurePredicate(e);let a=d=>{if(!d||!this.isRealNode(d.left))return!1;if(l){let p=e,T=this.isReverse?p.high:p.low,f=this.isReverse?p.includeHigh:p.includeLow;return f&&this._compare(d.key,T)>=0||!f&&this._compare(d.key,T)>0}if(!l&&!this._isPredicate(e)){let p=this._extractKey(e);return p!=null&&this._compare(d.key,p)>0}return!0},u=d=>{if(!d||!this.isRealNode(d.right))return!1;if(l){let p=e,T=this.isReverse?p.low:p.high,f=this.isReverse?p.includeLow:p.includeHigh;return f&&this._compare(d.key,T)<=0||!f&&this._compare(d.key,T)<0}if(!l&&!this._isPredicate(e)){let p=this._extractKey(e);return p!=null&&this._compare(d.key,p)<0}return!0};return super._dfs(n,"IN",t,i,s,!1,a,u,()=>!0,d=>!!d&&r(d))}rangeSearch(e,t=this._DEFAULT_NODE_CALLBACK,n=this._root,i=this.iterationType){let s=e instanceof _?e:new _(e[0],e[1]);return this.search(s,!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 s=this._root;for(;s!==void 0;){if(this._compare(s.key,n.key)===0)return this._replaceNode(s,n),this._isMapMode&&this._setValue(s.key,i),!0;if(this._compare(s.key,n.key)>0){if(s.left===void 0)return s.left=n,this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0;s.left!==null&&(s=s.left)}else{if(s.right===void 0)return s.right=n,this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0;s.right!==null&&(s=s.right)}}return!1}addMany(e,t,n=!0,i=this.iterationType){let s=[],r=t==null?void 0:t[Symbol.iterator]();if(!n){for(let T of e){let f=r==null?void 0:r.next().value;this.isRaw(T)&&(T=this._toEntryFn(T)),s.push(this.add(T,f))}return s}let l=[],a=0;for(let T of e)l.push({key:T,value:r==null?void 0:r.next().value,orgIndex:a++});let u=l.sort(({key:T},{key:f})=>{let V,y;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)?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}),d=T=>{if(T.length===0)return;let f=Math.floor((T.length-1)/2),{key:V,value:y,orgIndex:K}=T[f];if(this.isRaw(V)){let N=this._toEntryFn(V);s[K]=this.add(N)}else s[K]=this.add(V,y);d(T.slice(0,f)),d(T.slice(f+1))};return i==="RECURSIVE"?d(u):(()=>{let f=[[0,u.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:g,value:B,orgIndex:M}=u[N];if(this.isRaw(g)){let O=this._toEntryFn(g);s[M]=this.add(O)}else s[M]=this.add(g,B);f.push([N+1,K]),f.push([y,N-1])}})(),s}lesserOrGreaterTraverse(e=this._DEFAULT_NODE_CALLBACK,t=-1,n=this._root,i=this.iterationType){let s=this.ensureNode(n),r=[];if(!this._root||!s)return r;let l=s.key;if(i==="RECURSIVE"){let a=u=>{let d=this._compare(u.key,l);Math.sign(d)==t&&r.push(e(u)),this.isRealNode(u.left)&&a(u.left),this.isRealNode(u.right)&&a(u.right)};return a(this._root),r}else{let a=new m([this._root]);for(;a.length>0;){let u=a.shift();if(this.isRealNode(u)){let d=this._compare(u.key,l);Math.sign(d)==t&&r.push(e(u)),this.isRealNode(u.left)&&a.push(u.left),this.isRealNode(u.right)&&a.push(u.right)}}return r}}perfectlyBalance(e=this.iterationType){let t=this.dfs(r=>r,"IN",!1,this._root,e),n=t.length;if(this._clearNodes(),n===0)return!1;let i=(r,l,a)=>{if(r>l)return;let u=r+(l-r>>1),d=t[u],p=i(r,u-1,d),T=i(u+1,l,d);return d.left=p,d.right=T,d.parent=a,d},s=i(0,n-1,void 0);return this._setRoot(s),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 s=n(i.left),r=n(i.right);return Math.abs(s-r)>1&&(t=!1),Math.max(s,r)+1};n(this._root)}else{let n=[],i=this._root,s,r=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||s===i.right){if(i=n.pop(),i){let l=i.left?r.get(i.left):-1,a=i.right?r.get(i.right):-1;if(Math.abs(l-a)>1)return!1;r.set(i,1+Math.max(l,a)),s=i,i=void 0}}else i=i.right}return t}map(e,t,n){let i=this._createLike([],t),s=0;for(let[r,l]of this)i.add(e.call(n,r,l,s++,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 s=t.pop();if(!s)break;let r=s.key,l=s.value;if(e(r,l,i++,this))return this._deleteByKey(r);n=s.right}return!1}_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 s;let t=this._root;for(;t;){let r=this._compare(t.key,e);if(r===0)break;t=r>0?t.left:t.right}if(!t)return!1;let n=(r,l)=>{let a=r==null?void 0:r.parent;a?a.left===r?a.left=l:a.right=l:this._setRoot(l),l&&(l.parent=a)},i=r=>{if(r){for(;r.left!==void 0&&r.left!==null;)r=r.left;return r}};if(t.left===void 0)n(t,t.right);else if(t.right===void 0)n(t,t.left);else{let r=i(t.right);r.parent!==t&&(n(r,r.right),r.right=t.right,r.right&&(r.right.parent=r)),n(t,r),r.left=t.left,r.left&&(r.left.parent=r)}return this._size=Math.max(0,((s=this._size)!=null?s:0)-1),!0}};var L=class{constructor(o,e,t="BLACK"){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,this.color=t}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(e=[],t){super([],t);c(this,"_root");this._root=this.NIL,e&&this.addMany(e)}get root(){return this._root}createNode(e,t,n="BLACK"){return new L(e,this._isMapMode?void 0:t,n)}isNode(e){return e instanceof L}clear(){super.clear(),this._root=this.NIL}add(e,t){let[n,i]=this._keyValueNodeOrEntryToNodeAndValue(e,t);if(!this.isRealNode(n))return!1;let s=this._insert(n);if(s==="CREATED"){if(this.isRealNode(this._root))this._root.color="BLACK";else return!1;return this._isMapMode&&this._setValue(n.key,i),this._size++,!0}return s==="UPDATED"?(this._isMapMode&&this._setValue(n.key,i),!0):!1}delete(e){if(e===null)return[];let t=[],n;if(this._isPredicate(e)?n=this.getNode(e):n=this.isRealNode(e)?e:this.getNode(e),!n)return t;let i=n.color,s;if(!this.isRealNode(n.left))n.right!==null&&(s=n.right,this._transplant(n,n.right));else if(!this.isRealNode(n.right))s=n.left,this._transplant(n,n.left);else{let r=this.getLeftMost(l=>l,n.right);r&&(i=r.color,r.right!==null&&(s=r.right),r.parent===n?this.isRealNode(s)&&(s.parent=r):(r.right!==null&&(this._transplant(r,r.right),r.right=n.right),this.isRealNode(r.right)&&(r.right.parent=r)),this._transplant(n,r),r.left=n.left,this.isRealNode(r.left)&&(r.left.parent=r),r.color=n.color)}return this._isMapMode&&this._store.delete(n.key),this._size--,i==="BLACK"&&this._deleteFixup(s),t.push({deleted:n,needBalanced:void 0}),t}map(e,t,n){let i=this._createLike([],t),s=0;for(let[r,l]of this)i.add(e.call(n,r,l,s++,this));return i}_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:{}})}_setRoot(e){e&&(e.parent=void 0),this._root=e}_replaceNode(e,t){return t.color=e.color,super._replaceNode(e,t)}_insert(e){var i,s,r;let t=(i=this.root)!=null?i:this.NIL,n;for(;t!==this.NIL;){n=t;let l=this._compare(e.key,t.key);if(l<0)t=(s=t.left)!=null?s:this.NIL;else if(l>0)t=(r=t.right)!=null?r:this.NIL;else return this._replaceNode(t,e),"UPDATED"}return e.parent=n,n?this._compare(e.key,n.key)<0?n.left=e:n.right=e:this._setRoot(e),e.left=this.NIL,e.right=this.NIL,e.color="RED",this._insertFixup(e),"CREATED"}_transplant(e,t){e.parent?e===e.parent.left?e.parent.left=t:e.parent.right=t:this._setRoot(t),t&&(t.parent=e.parent)}_insertFixup(e){var t,n,i,s,r;for(;((t=e==null?void 0:e.parent)==null?void 0:t.color)==="RED";)if(e.parent===((n=e.parent.parent)==null?void 0:n.left)){let l=e.parent.parent.right;(l==null?void 0:l.color)==="RED"?(e.parent.color="BLACK",l.color="BLACK",e.parent.parent.color="RED",e=e.parent.parent):(e===e.parent.right&&(e=e.parent,this._leftRotate(e)),e&&e.parent&&e.parent.parent&&(e.parent.color="BLACK",e.parent.parent.color="RED",this._rightRotate(e.parent.parent)))}else{let l=(r=(s=(i=e==null?void 0:e.parent)==null?void 0:i.parent)==null?void 0:s.left)!=null?r:void 0;(l==null?void 0:l.color)==="RED"?(e.parent.color="BLACK",l.color="BLACK",e.parent.parent.color="RED",e=e.parent.parent):(e===e.parent.left&&(e=e.parent,this._rightRotate(e)),e&&e.parent&&e.parent.parent&&(e.parent.color="BLACK",e.parent.parent.color="RED",this._leftRotate(e.parent.parent)))}this.isRealNode(this._root)&&(this._root.color="BLACK")}_deleteFixup(e){var t,n,i,s;if(!e||e===this.root||e.color==="BLACK"){e&&(e.color="BLACK");return}for(;e&&e!==this.root&&e.color==="BLACK";){let r=e.parent;if(!r)break;if(e===r.left){let l=r.right;(l==null?void 0:l.color)==="RED"&&(l.color="BLACK",r.color="RED",this._leftRotate(r),l=r.right),((n=(t=l==null?void 0:l.left)==null?void 0:t.color)!=null?n:"BLACK")==="BLACK"?(l&&(l.color="RED"),e=r):(l!=null&&l.left&&(l.left.color="BLACK"),l&&(l.color=r.color),r.color="BLACK",this._rightRotate(r),e=this.root)}else{let l=r.left;(l==null?void 0:l.color)==="RED"&&(l.color="BLACK",r&&(r.color="RED"),this._rightRotate(r),r&&(l=r.left)),((s=(i=l==null?void 0:l.right)==null?void 0:i.color)!=null?s:"BLACK")==="BLACK"?(l&&(l.color="RED"),e=r):(l!=null&&l.right&&(l.right.color="BLACK"),l&&(l.color=r.color),r&&(r.color="BLACK"),this._leftRotate(r),e=this.root)}}e&&(e.color="BLACK")}_leftRotate(e){if(!e||!e.right)return;let t=e.right;e.right=t.left,t.left&&t.left!==this.NIL&&(t.left.parent=e),t.parent=e.parent,e.parent?e===e.parent.left?e.parent.left=t:e.parent.right=t:this._setRoot(t),t.left=e,e.parent=t}_rightRotate(e){if(!e||!e.left)return;let t=e.left;e.left=t.right,t.right&&t.right!==this.NIL&&(t.right.parent=e),t.parent=e.parent,e.parent?e===e.parent.left?e.parent.left=t:e.parent.right=t:this._setRoot(t),t.right=e,e.parent=t}};return $(te);})();
5
+ `;i+=a})(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,s=this.iterationType,r=!1,l=p=>!!p,a=p=>!!p,u=p=>r?this.isRealNodeOrNull(p):this.isRealNode(p),d=p=>this.isRealNodeOrNull(p)){if(i=this.ensureNode(i),!i)return[];let p=[];if(s==="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)},y=()=>{a(f)&&(f==null?void 0:f.right)!==void 0&&T(f==null?void 0:f.right)};switch(t){case"IN":if(V(),d(f)&&(p.push(e(f)),n))return;y();break;case"PRE":if(d(f)&&(p.push(e(f)),n))return;V(),y();break;case"POST":if(V(),y(),d(f)&&(p.push(e(f)),n))return;break}};T(i)}else{let T=[{opt:0,node:i}],f=K=>{var N;l(K.node)&&T.push({opt:0,node:(N=K.node)==null?void 0:N.left})},V=K=>{var N;a(K.node)&&T.push({opt:0,node:(N=K.node)==null?void 0:N.right})},y=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(d(K.node)&&K.node!==void 0&&(p.push(e(K.node)),n))return p}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 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 s=t!=null?t:i;return[this.createNode(n,s),s]}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:s}=t,r=[["\u2500"],1,0,0];if(e===null&&!n)return r;if(e===void 0&&!i)return r;if(this.isNIL(e)&&!s)return r;if(e!=null){let a=e.key,u=this.isNIL(e)?"S":String(a),d=u.length;return l(u,d,this._displayAux(e.left,t),this._displayAux(e.right,t))}else{let a=e===void 0?"U":"N",u=a.length;return l(a,u,[[""],1,0,0],[[""],1,0,0])}function l(a,u,d,p){let[T,f,V,y]=d,[K,N,g,B]=p,M=" ".repeat(Math.max(0,y+1))+"_".repeat(Math.max(0,f-y-1))+a+"_".repeat(Math.max(0,B))+" ".repeat(Math.max(0,N-B)),O=(V>0?" ".repeat(y)+"/"+" ".repeat(f-y-1):" ".repeat(f))+" ".repeat(u)+(g>0?" ".repeat(B)+"\\"+" ".repeat(N-B-1):" ".repeat(N)),H=[M,O];for(let b=0;b<Math.max(V,g);b++){let G=b<V?T[b]:" ".repeat(f),j=b<g?K[b]:" ".repeat(N);H.push(G+" ".repeat(u)+j)}return[H,f+u+N,Math.max(V,g)+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,s=this.createNode(n,i);return s&&(t.key=e.key,this._isMapMode||(t.value=e.value),e.key=s.key,this._isMapMode||(e.value=s.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 k=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,"_isReverse",!1);c(this,"_comparator",(e,t)=>{if(R(e)&&R(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 k(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 k}isValidKey(e){return R(e,this._specifyComparable!==void 0)}dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,s=this.iterationType){return super.dfs(e,t,n,i,s)}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,s=this.iterationType){if(e===void 0)return[];if(e===null)return[];if(i=this.ensureNode(i),!i)return[];let r,l=this.isRange(e);l?r=d=>d?e.isInRange(d.key,this._comparator):!1:r=this._ensurePredicate(e);let a=d=>{if(!d||!this.isRealNode(d.left))return!1;if(l){let p=e,T=this.isReverse?p.high:p.low,f=this.isReverse?p.includeHigh:p.includeLow;return f&&this._compare(d.key,T)>=0||!f&&this._compare(d.key,T)>0}if(!l&&!this._isPredicate(e)){let p=this._extractKey(e);return p!=null&&this._compare(d.key,p)>0}return!0},u=d=>{if(!d||!this.isRealNode(d.right))return!1;if(l){let p=e,T=this.isReverse?p.low:p.high,f=this.isReverse?p.includeLow:p.includeHigh;return f&&this._compare(d.key,T)<=0||!f&&this._compare(d.key,T)<0}if(!l&&!this._isPredicate(e)){let p=this._extractKey(e);return p!=null&&this._compare(d.key,p)<0}return!0};return super._dfs(n,"IN",t,i,s,!1,a,u,()=>!0,d=>!!d&&r(d))}rangeSearch(e,t=this._DEFAULT_NODE_CALLBACK,n=this._root,i=this.iterationType){let s=e instanceof _?e:new _(e[0],e[1]);return this.search(s,!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 s=this._root;for(;s!==void 0;){if(this._compare(s.key,n.key)===0)return this._replaceNode(s,n),this._isMapMode&&this._setValue(s.key,i),!0;if(this._compare(s.key,n.key)>0){if(s.left===void 0)return s.left=n,this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0;s.left!==null&&(s=s.left)}else{if(s.right===void 0)return s.right=n,this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0;s.right!==null&&(s=s.right)}}return!1}addMany(e,t,n=!0,i=this.iterationType){let s=[],r=t==null?void 0:t[Symbol.iterator]();if(!n){for(let T of e){let f=r==null?void 0:r.next().value;this.isRaw(T)&&(T=this._toEntryFn(T)),s.push(this.add(T,f))}return s}let l=[],a=0;for(let T of e)l.push({key:T,value:r==null?void 0:r.next().value,orgIndex:a++});let u=l.sort(({key:T},{key:f})=>{let V,y;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)?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}),d=T=>{if(T.length===0)return;let f=Math.floor((T.length-1)/2),{key:V,value:y,orgIndex:K}=T[f];if(this.isRaw(V)){let N=this._toEntryFn(V);s[K]=this.add(N)}else s[K]=this.add(V,y);d(T.slice(0,f)),d(T.slice(f+1))};return i==="RECURSIVE"?d(u):(()=>{let f=[[0,u.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:g,value:B,orgIndex:M}=u[N];if(this.isRaw(g)){let O=this._toEntryFn(g);s[M]=this.add(O)}else s[M]=this.add(g,B);f.push([N+1,K]),f.push([y,N-1])}})(),s}lesserOrGreaterTraverse(e=this._DEFAULT_NODE_CALLBACK,t=-1,n=this._root,i=this.iterationType){let s=this.ensureNode(n),r=[];if(!this._root||!s)return r;let l=s.key;if(i==="RECURSIVE"){let a=u=>{let d=this._compare(u.key,l);Math.sign(d)==t&&r.push(e(u)),this.isRealNode(u.left)&&a(u.left),this.isRealNode(u.right)&&a(u.right)};return a(this._root),r}else{let a=new m([this._root]);for(;a.length>0;){let u=a.shift();if(this.isRealNode(u)){let d=this._compare(u.key,l);Math.sign(d)==t&&r.push(e(u)),this.isRealNode(u.left)&&a.push(u.left),this.isRealNode(u.right)&&a.push(u.right)}}return r}}perfectlyBalance(e=this.iterationType){let t=this.dfs(r=>r,"IN",!1,this._root,e),n=t.length;if(this._clearNodes(),n===0)return!1;let i=(r,l,a)=>{if(r>l)return;let u=r+(l-r>>1),d=t[u],p=i(r,u-1,d),T=i(u+1,l,d);return d.left=p,d.right=T,d.parent=a,d},s=i(0,n-1,void 0);return this._setRoot(s),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 s=n(i.left),r=n(i.right);return Math.abs(s-r)>1&&(t=!1),Math.max(s,r)+1};n(this._root)}else{let n=[],i=this._root,s,r=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||s===i.right){if(i=n.pop(),i){let l=i.left?r.get(i.left):-1,a=i.right?r.get(i.right):-1;if(Math.abs(l-a)>1)return!1;r.set(i,1+Math.max(l,a)),s=i,i=void 0}}else i=i.right}return t}map(e,t,n){let i=this._createLike([],t),s=0;for(let[r,l]of this)i.add(e.call(n,l,r,s++,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 s=t.pop();if(!s)break;let r=s.key,l=s.value;if(e(r,l,i++,this))return this._deleteByKey(r);n=s.right}return!1}_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 s;let t=this._root;for(;t;){let r=this._compare(t.key,e);if(r===0)break;t=r>0?t.left:t.right}if(!t)return!1;let n=(r,l)=>{let a=r==null?void 0:r.parent;a?a.left===r?a.left=l:a.right=l:this._setRoot(l),l&&(l.parent=a)},i=r=>{if(r){for(;r.left!==void 0&&r.left!==null;)r=r.left;return r}};if(t.left===void 0)n(t,t.right);else if(t.right===void 0)n(t,t.left);else{let r=i(t.right);r.parent!==t&&(n(r,r.right),r.right=t.right,r.right&&(r.right.parent=r)),n(t,r),r.left=t.left,r.left&&(r.left.parent=r)}return this._size=Math.max(0,((s=this._size)!=null?s:0)-1),!0}};var L=class{constructor(o,e,t="BLACK"){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,this.color=t}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(e=[],t){super([],t);c(this,"_root");this._root=this.NIL,e&&this.addMany(e)}get root(){return this._root}createNode(e,t,n="BLACK"){return new L(e,this._isMapMode?void 0:t,n)}isNode(e){return e instanceof L}clear(){super.clear(),this._root=this.NIL}add(e,t){let[n,i]=this._keyValueNodeOrEntryToNodeAndValue(e,t);if(!this.isRealNode(n))return!1;let s=this._insert(n);if(s==="CREATED"){if(this.isRealNode(this._root))this._root.color="BLACK";else return!1;return this._isMapMode&&this._setValue(n.key,i),this._size++,!0}return s==="UPDATED"?(this._isMapMode&&this._setValue(n.key,i),!0):!1}delete(e){if(e===null)return[];let t=[],n;if(this._isPredicate(e)?n=this.getNode(e):n=this.isRealNode(e)?e:this.getNode(e),!n)return t;let i=n.color,s;if(!this.isRealNode(n.left))n.right!==null&&(s=n.right,this._transplant(n,n.right));else if(!this.isRealNode(n.right))s=n.left,this._transplant(n,n.left);else{let r=this.getLeftMost(l=>l,n.right);r&&(i=r.color,r.right!==null&&(s=r.right),r.parent===n?this.isRealNode(s)&&(s.parent=r):(r.right!==null&&(this._transplant(r,r.right),r.right=n.right),this.isRealNode(r.right)&&(r.right.parent=r)),this._transplant(n,r),r.left=n.left,this.isRealNode(r.left)&&(r.left.parent=r),r.color=n.color)}return this._isMapMode&&this._store.delete(n.key),this._size--,i==="BLACK"&&this._deleteFixup(s),t.push({deleted:n,needBalanced:void 0}),t}map(e,t,n){let i=this._createLike([],t),s=0;for(let[r,l]of this)i.add(e.call(n,l,r,s++,this));return i}_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:{}})}_setRoot(e){e&&(e.parent=void 0),this._root=e}_replaceNode(e,t){return t.color=e.color,super._replaceNode(e,t)}_insert(e){var i,s,r;let t=(i=this.root)!=null?i:this.NIL,n;for(;t!==this.NIL;){n=t;let l=this._compare(e.key,t.key);if(l<0)t=(s=t.left)!=null?s:this.NIL;else if(l>0)t=(r=t.right)!=null?r:this.NIL;else return this._replaceNode(t,e),"UPDATED"}return e.parent=n,n?this._compare(e.key,n.key)<0?n.left=e:n.right=e:this._setRoot(e),e.left=this.NIL,e.right=this.NIL,e.color="RED",this._insertFixup(e),"CREATED"}_transplant(e,t){e.parent?e===e.parent.left?e.parent.left=t:e.parent.right=t:this._setRoot(t),t&&(t.parent=e.parent)}_insertFixup(e){var t,n,i,s,r;for(;((t=e==null?void 0:e.parent)==null?void 0:t.color)==="RED";)if(e.parent===((n=e.parent.parent)==null?void 0:n.left)){let l=e.parent.parent.right;(l==null?void 0:l.color)==="RED"?(e.parent.color="BLACK",l.color="BLACK",e.parent.parent.color="RED",e=e.parent.parent):(e===e.parent.right&&(e=e.parent,this._leftRotate(e)),e&&e.parent&&e.parent.parent&&(e.parent.color="BLACK",e.parent.parent.color="RED",this._rightRotate(e.parent.parent)))}else{let l=(r=(s=(i=e==null?void 0:e.parent)==null?void 0:i.parent)==null?void 0:s.left)!=null?r:void 0;(l==null?void 0:l.color)==="RED"?(e.parent.color="BLACK",l.color="BLACK",e.parent.parent.color="RED",e=e.parent.parent):(e===e.parent.left&&(e=e.parent,this._rightRotate(e)),e&&e.parent&&e.parent.parent&&(e.parent.color="BLACK",e.parent.parent.color="RED",this._leftRotate(e.parent.parent)))}this.isRealNode(this._root)&&(this._root.color="BLACK")}_deleteFixup(e){var t,n,i,s;if(!e||e===this.root||e.color==="BLACK"){e&&(e.color="BLACK");return}for(;e&&e!==this.root&&e.color==="BLACK";){let r=e.parent;if(!r)break;if(e===r.left){let l=r.right;(l==null?void 0:l.color)==="RED"&&(l.color="BLACK",r.color="RED",this._leftRotate(r),l=r.right),((n=(t=l==null?void 0:l.left)==null?void 0:t.color)!=null?n:"BLACK")==="BLACK"?(l&&(l.color="RED"),e=r):(l!=null&&l.left&&(l.left.color="BLACK"),l&&(l.color=r.color),r.color="BLACK",this._rightRotate(r),e=this.root)}else{let l=r.left;(l==null?void 0:l.color)==="RED"&&(l.color="BLACK",r&&(r.color="RED"),this._rightRotate(r),r&&(l=r.left)),((s=(i=l==null?void 0:l.right)==null?void 0:i.color)!=null?s:"BLACK")==="BLACK"?(l&&(l.color="RED"),e=r):(l!=null&&l.right&&(l.right.color="BLACK"),l&&(l.color=r.color),r&&(r.color="BLACK"),this._leftRotate(r),e=this.root)}}e&&(e.color="BLACK")}_leftRotate(e){if(!e||!e.right)return;let t=e.right;e.right=t.left,t.left&&t.left!==this.NIL&&(t.left.parent=e),t.parent=e.parent,e.parent?e===e.parent.left?e.parent.left=t:e.parent.right=t:this._setRoot(t),t.left=e,e.parent=t}_rightRotate(e){if(!e||!e.left)return;let t=e.left;e.left=t.right,t.right&&t.right!==this.NIL&&(t.right.parent=e),t.parent=e.parent,e.parent?e===e.parent.left?e.parent.left=t:e.parent.right=t:this._setRoot(t),t.right=e,e.parent=t}};return $(te);})();
6
6
  /**
7
7
  * data-structure-typed
8
8
  *