bst-typed 2.2.7 → 2.3.0

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 (30) hide show
  1. package/README.md +5 -4
  2. package/dist/cjs/index.cjs +56 -59
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs-legacy/index.cjs +56 -59
  5. package/dist/cjs-legacy/index.cjs.map +1 -1
  6. package/dist/esm/index.mjs +56 -59
  7. package/dist/esm/index.mjs.map +1 -1
  8. package/dist/esm-legacy/index.mjs +56 -59
  9. package/dist/esm-legacy/index.mjs.map +1 -1
  10. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +1 -1
  11. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
  12. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +10 -10
  13. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +22 -24
  14. package/dist/types/data-structures/binary-tree/bst.d.ts +11 -11
  15. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +1 -1
  16. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +1 -1
  17. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
  18. package/dist/umd/bst-typed.js +56 -59
  19. package/dist/umd/bst-typed.js.map +1 -1
  20. package/dist/umd/bst-typed.min.js +2 -2
  21. package/dist/umd/bst-typed.min.js.map +1 -1
  22. package/package.json +2 -2
  23. package/src/data-structures/binary-tree/avl-tree-counter.ts +6 -6
  24. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +13 -13
  25. package/src/data-structures/binary-tree/avl-tree.ts +15 -15
  26. package/src/data-structures/binary-tree/binary-tree.ts +52 -55
  27. package/src/data-structures/binary-tree/bst.ts +21 -22
  28. package/src/data-structures/binary-tree/red-black-tree.ts +3 -3
  29. package/src/data-structures/binary-tree/tree-counter.ts +4 -4
  30. package/src/data-structures/binary-tree/tree-multi-map.ts +13 -13
@@ -1,8 +1,8 @@
1
- "use strict";var bstTyped=(()=>{var C=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var W=Object.getOwnPropertyNames;var Q=Object.prototype.hasOwnProperty;var j=(f,d,e)=>d in f?C(f,d,{enumerable:!0,configurable:!0,writable:!0,value:e}):f[d]=e;var G=(f,d)=>{for(var e in d)C(f,e,{get:d[e],enumerable:!0})},X=(f,d,e,t)=>{if(d&&typeof d=="object"||typeof d=="function")for(let n of W(d))!Q.call(f,n)&&n!==e&&C(f,n,{get:()=>d[n],enumerable:!(t=H(d,n))||t.enumerable});return f};var J=f=>X(C({},"__esModule",{value:!0}),f);var p=(f,d,e)=>j(f,typeof d!="symbol"?d+"":d,e);var te={};G(te,{BST:()=>F,BSTNode:()=>S,BinaryTree:()=>L,BinaryTreeNode:()=>R,DFSOperation:()=>O,Range:()=>E});function w(f){let d=typeof f;return d==="number"?!0:d==="bigint"||d==="string"||d==="boolean"}function U(f){if(typeof f.valueOf=="function"){let d=f.valueOf();if(d!==f){if(w(d))return d;if(typeof d=="object"&&d!==null)return U(d)}}if(typeof f.toString=="function"){let d=f.toString();if(d!=="[object Object]")return d}return null}function k(f,d=!1){if(f==null)return!1;if(w(f))return!0;if(typeof f!="object")return!1;if(f instanceof Date||d)return!0;let e=U(f);return e==null?!1:w(e)}var D=f=>({isThunk:!0,fn:f}),Y=f=>typeof f=="object"&&f!==null&&"isThunk"in f&&f.isThunk;function Z(f){let d=f;for(;Y(d);)d=d.fn();return d}function A(f){return(...d)=>Z(f(...d))}var x=class{constructor(d){p(this,"_toElementFn");if(d){let{toElementFn:e}=d;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](...d){yield*this._getIterator(...d)}*values(){for(let d of this)yield d}every(d,e){let t=0;for(let n of this)if(e===void 0){if(!d(n,t++,this))return!1}else if(!d.call(e,n,t++,this))return!1;return!0}some(d,e){let t=0;for(let n of this)if(e===void 0){if(d(n,t++,this))return!0}else if(d.call(e,n,t++,this))return!0;return!1}forEach(d,e){let t=0;for(let n of this)e===void 0?d(n,t++,this):d.call(e,n,t++,this)}find(d,e){let t=0;for(let n of this)if(e===void 0){if(d(n,t++,this))return n}else if(d.call(e,n,t++,this))return n}has(d){for(let e of this)if(e===d)return!0;return!1}reduce(d,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=d(i,r,t++,this);return i}toArray(){return[...this]}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var B=class f extends x{constructor(e){super(e);p(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 f?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 K=class f extends B{constructor(e=[],t){super(t);p(this,"_elements",[]);p(this,"_offset",0);p(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 f(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,o;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:(o=t==null?void 0:t.autoCompactRatio)!=null?o:this._autoCompactRatio}),r=0;for(let l of this)i.push(n===void 0?e(l,r++,this):e.call(n,l,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 o of this){let l=t===void 0?e(o,r++,this):e.call(t,o,r++,this);i.push(l)}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](...d){yield*this._getIterator(...d)}*entries(){for(let d of this)yield d}*keys(){for(let d of this)yield d[0]}*values(){for(let d of this)yield d[1]}every(d,e){let t=0;for(let n of this)if(!d.call(e,n[1],n[0],t++,this))return!1;return!0}some(d,e){let t=0;for(let n of this)if(d.call(e,n[1],n[0],t++,this))return!0;return!1}forEach(d,e){let t=0;for(let n of this){let[i,r]=n;d.call(e,r,i,t++,this)}}find(d,e){let t=0;for(let n of this){let[i,r]=n;if(d.call(e,r,i,t++,this))return n}}has(d){for(let e of this){let[t]=e;if(t===d)return!0}return!1}hasValue(d){for(let[,e]of this)if(e===d)return!0;return!1}get(d){for(let e of this){let[t,n]=e;if(t===d)return n}}reduce(d,e){let t=e,n=0;for(let i of this){let[r,s]=i;t=d(t,s,r,n++,this)}return t}toArray(){return[...this]}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var O=(e=>(e[e.VISIT=0]="VISIT",e[e.PROCESS=1]="PROCESS",e))(O||{}),E=class{constructor(d,e,t=!0,n=!0){this.low=d;this.high=e;this.includeLow=t;this.includeHigh=n}isInRange(d,e){let t=this.includeLow?e(d,this.low)>=0:e(d,this.low)>0,n=this.includeHigh?e(d,this.high)<=0:e(d,this.high)<0;return t&&n}};var R=class{constructor(d,e){p(this,"key");p(this,"value");p(this,"parent");p(this,"_left");p(this,"_right");p(this,"_height",0);p(this,"_color","BLACK");p(this,"_count",1);this.key=d,this.value=e}get left(){return this._left}set left(d){d&&(d.parent=this),this._left=d}get right(){return this._right}set right(d){d&&(d.parent=this),this._right=d}get height(){return this._height}set height(d){this._height=d}get color(){return this._color}set color(d){this._color=d}get count(){return this._count}set count(d){this._count=d}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 v{constructor(e=[],t){super();p(this,"iterationType","ITERATIVE");p(this,"_isMapMode",!0);p(this,"_isDuplicate",!1);p(this,"_store",new Map);p(this,"_root");p(this,"_size",0);p(this,"_NIL",new R(NaN));p(this,"_toEntryFn");p(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 R(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 R}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 E}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:k(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 K([this._root]),s;for(;r.length>0;){let o=r.shift();if(o){if(!this._isDuplicate&&n!==null&&o.key===n.key)return this._replaceNode(o,n),this._isMapMode&&this._setValue(o.key,i),!0;s===void 0&&(o.left===void 0||o.right===void 0)&&(s=o),o.left!==null&&o.left&&r.push(o.left),o.right!==null&&o.right&&r.push(o.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 o=i.next();o.done||(s=o.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 o=this.getRightMost(l=>l,n.left);if(o){let l=o.parent;s=this._swapProperties(n,o),l&&(l.right===o?l.right=o.left:l.left=o.left,r=l)}}else if(i){let{familyPosition:o}=n;o==="LEFT"||o==="ROOT_LEFT"?i.left=n.right:(o==="RIGHT"||o==="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),o=[];if(r==="RECURSIVE"){let l=u=>{s(u)&&(o.push(n(u)),t)||!this.isRealNode(u.left)&&!this.isRealNode(u.right)||(this.isRealNode(u.left)&&l(u.left),this.isRealNode(u.right)&&l(u.right))};l(i)}else{let l=[i];for(;l.length>0;){let u=l.pop();if(this.isRealNode(u)){if(s(u)&&(o.push(n(u)),t))return o;this.isRealNode(u.left)&&l.push(u.left),this.isRealNode(u.right)&&l.push(u.right)}}}return o}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=(o,l,u)=>{if(!this.isRealNode(o))return!0;let a=Number(o.key);return a<=l||a>=u?!1:i(o.left,l,a)&&i(o.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=(o=!1)=>{let l=[],u=o?Number.MAX_SAFE_INTEGER:Number.MIN_SAFE_INTEGER,a=n;for(;this.isRealNode(a)||l.length>0;){for(;this.isRealNode(a);)l.push(a),a=a.left;a=l.pop();let _=Number(a.key);if(!this.isRealNode(a)||!o&&u>=_||o&&u<=_)return!1;u=_,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 o=this.isRealNode(i.left)?s.get(i.left):-1,l=this.isRealNode(i.right)?s.get(i.right):-1;s.set(i,1+Math.min(o,l)),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:o}=s;return this.isRealNode(o)?r(o):s};return e(r(i))}else{let r=A(s=>{let{left:o}=s;return this.isRealNode(o)?D(()=>r(o)):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=A(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 K([t]),o=l=>{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)),o(l+1)};o(0)}else{let s=new K([t]);for(;s.length>0;){let o=s.length;for(let l=0;l<o;l++){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 K([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=(o,l)=>{r[l]||(r[l]=[]),r[l].push(e(o)),i?(o&&this.isRealNodeOrNull(o.left)&&s(o.left,l+1),o&&this.isRealNodeOrNull(o.right)&&s(o.right,l+1)):(o&&o.left&&s(o.left,l+1),o&&o.right&&s(o.right,l+1))};s(t,0)}else{let s=[[t,0]];for(;s.length>0;){let o=s.pop(),[l,u]=o;r[u]||(r[u]=[]),r[u].push(e(l)),i?(l&&this.isRealNodeOrNull(l.right)&&s.push([l.right,u+1]),l&&this.isRealNodeOrNull(l.left)&&s.push([l.left,u+1])):(l&&l.right&&s.push([l.right,u+1]),l&&l.left&&s.push([l.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=l=>{let u=null,a=null;for(;l;)a=l.right,l.right=u,u=l,l=a;return u},o=l=>{let u=s(l),a=u;for(;a;)i.push(e(a)),a=a.right;s(u)};switch(t){case"IN":for(;r;){if(r.left){let l=this.getPredecessor(r);if(l.right)l.right=null;else{l.right=r,r=r.left;continue}}i.push(e(r)),r=r.right}break;case"PRE":for(;r;){if(r.left){let l=this.getPredecessor(r);if(l.right)l.right=null;else{l.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 l=this.getPredecessor(r);if(l.right===null){l.right=r,r=r.left;continue}else l.right=null,o(r.left)}r=r.right}o(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,o]of this)i.add(e.call(n,o,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
1
+ "use strict";var bstTyped=(()=>{var C=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var W=Object.getOwnPropertyNames;var Q=Object.prototype.hasOwnProperty;var j=(f,d,e)=>d in f?C(f,d,{enumerable:!0,configurable:!0,writable:!0,value:e}):f[d]=e;var G=(f,d)=>{for(var e in d)C(f,e,{get:d[e],enumerable:!0})},X=(f,d,e,t)=>{if(d&&typeof d=="object"||typeof d=="function")for(let n of W(d))!Q.call(f,n)&&n!==e&&C(f,n,{get:()=>d[n],enumerable:!(t=H(d,n))||t.enumerable});return f};var J=f=>X(C({},"__esModule",{value:!0}),f);var p=(f,d,e)=>j(f,typeof d!="symbol"?d+"":d,e);var te={};G(te,{BST:()=>F,BSTNode:()=>S,BinaryTree:()=>L,BinaryTreeNode:()=>R,DFSOperation:()=>O,Range:()=>E});function w(f){let d=typeof f;return d==="number"?!0:d==="bigint"||d==="string"||d==="boolean"}function U(f){if(typeof f.valueOf=="function"){let d=f.valueOf();if(d!==f){if(w(d))return d;if(typeof d=="object"&&d!==null)return U(d)}}if(typeof f.toString=="function"){let d=f.toString();if(d!=="[object Object]")return d}return null}function k(f,d=!1){if(f==null)return!1;if(w(f))return!0;if(typeof f!="object")return!1;if(f instanceof Date||d)return!0;let e=U(f);return e==null?!1:w(e)}var D=f=>({isThunk:!0,fn:f}),Y=f=>typeof f=="object"&&f!==null&&"isThunk"in f&&f.isThunk;function Z(f){let d=f;for(;Y(d);)d=d.fn();return d}function A(f){return(...d)=>Z(f(...d))}var x=class{constructor(d){p(this,"_toElementFn");if(d){let{toElementFn:e}=d;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](...d){yield*this._getIterator(...d)}*values(){for(let d of this)yield d}every(d,e){let t=0;for(let n of this)if(e===void 0){if(!d(n,t++,this))return!1}else if(!d.call(e,n,t++,this))return!1;return!0}some(d,e){let t=0;for(let n of this)if(e===void 0){if(d(n,t++,this))return!0}else if(d.call(e,n,t++,this))return!0;return!1}forEach(d,e){let t=0;for(let n of this)e===void 0?d(n,t++,this):d.call(e,n,t++,this)}find(d,e){let t=0;for(let n of this)if(e===void 0){if(d(n,t++,this))return n}else if(d.call(e,n,t++,this))return n}has(d){for(let e of this)if(e===d)return!0;return!1}reduce(d,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=d(i,r,t++,this);return i}toArray(){return[...this]}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var B=class f extends x{constructor(e){super(e);p(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 f?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 K=class f extends B{constructor(e=[],t){super(t);p(this,"_elements",[]);p(this,"_offset",0);p(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 f(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,o;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:(o=t==null?void 0:t.autoCompactRatio)!=null?o:this._autoCompactRatio}),r=0;for(let l of this)i.push(n===void 0?e(l,r++,this):e.call(n,l,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 o of this){let l=t===void 0?e(o,r++,this):e.call(t,o,r++,this);i.push(l)}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](...d){yield*this._getIterator(...d)}*entries(){for(let d of this)yield d}*keys(){for(let d of this)yield d[0]}*values(){for(let d of this)yield d[1]}every(d,e){let t=0;for(let n of this)if(!d.call(e,n[1],n[0],t++,this))return!1;return!0}some(d,e){let t=0;for(let n of this)if(d.call(e,n[1],n[0],t++,this))return!0;return!1}forEach(d,e){let t=0;for(let n of this){let[i,r]=n;d.call(e,r,i,t++,this)}}find(d,e){let t=0;for(let n of this){let[i,r]=n;if(d.call(e,r,i,t++,this))return n}}has(d){for(let e of this){let[t]=e;if(t===d)return!0}return!1}hasValue(d){for(let[,e]of this)if(e===d)return!0;return!1}get(d){for(let e of this){let[t,n]=e;if(t===d)return n}}reduce(d,e){let t=e,n=0;for(let i of this){let[r,s]=i;t=d(t,s,r,n++,this)}return t}toArray(){return[...this]}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var O=(e=>(e[e.VISIT=0]="VISIT",e[e.PROCESS=1]="PROCESS",e))(O||{}),E=class{constructor(d,e,t=!0,n=!0){this.low=d;this.high=e;this.includeLow=t;this.includeHigh=n}isInRange(d,e){let t=this.includeLow?e(d,this.low)>=0:e(d,this.low)>0,n=this.includeHigh?e(d,this.high)<=0:e(d,this.high)<0;return t&&n}};var R=class{constructor(d,e){p(this,"key");p(this,"value");p(this,"parent");p(this,"_left");p(this,"_right");p(this,"_height",0);p(this,"_color","BLACK");p(this,"_count",1);this.key=d,this.value=e}get left(){return this._left}set left(d){d&&(d.parent=this),this._left=d}get right(){return this._right}set right(d){d&&(d.parent=this),this._right=d}get height(){return this._height}set height(d){this._height=d}get color(){return this._color}set color(d){this._color=d}get count(){return this._count}set count(d){this._count=d}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 v{constructor(e=[],t){super();p(this,"iterationType","ITERATIVE");p(this,"_isMapMode",!0);p(this,"_isDuplicate",!1);p(this,"_store",new Map);p(this,"_root");p(this,"_size",0);p(this,"_NIL",new R(NaN));p(this,"_toEntryFn");p(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.setMany(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 R(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 R}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 E}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:k(e)}add(e){return this.set(e)}set(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 K([this._root]),s;for(;r.length>0;){let o=r.shift();if(o){if(!this._isDuplicate&&n!==null&&o.key===n.key)return this._replaceNode(o,n),this._isMapMode&&this._setValue(o.key,i),!0;s===void 0&&(o.left===void 0||o.right===void 0)&&(s=o),o.left!==null&&o.left&&r.push(o.left),o.right!==null&&o.right&&r.push(o.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}addMany(e){return this.setMany(e)}setMany(e,t){let n=[],i;t&&(i=t[Symbol.iterator]());for(let r of e){let s;if(i){let o=i.next();o.done||(s=o.value)}this.isRaw(r)&&(r=this._toEntryFn(r)),n.push(this.set(r,s))}return n}merge(e){this.setMany(e,[])}refill(e,t){this.clear(),this.setMany(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 o=this.getRightMost(l=>l,n.left);if(o){let l=o.parent;s=this._swapProperties(n,o),l&&(l.right===o?l.right=o.left:l.left=o.left,r=l)}}else if(i){let{familyPosition:o}=n;o==="LEFT"||o==="ROOT_LEFT"?i.left=n.right:(o==="RIGHT"||o==="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),o=[];if(r==="RECURSIVE"){let l=u=>{s(u)&&(o.push(n(u)),t)||!this.isRealNode(u.left)&&!this.isRealNode(u.right)||(this.isRealNode(u.left)&&l(u.left),this.isRealNode(u.right)&&l(u.right))};l(i)}else{let l=[i];for(;l.length>0;){let u=l.pop();if(this.isRealNode(u)){if(s(u)&&(o.push(n(u)),t))return o;this.isRealNode(u.left)&&l.push(u.left),this.isRealNode(u.right)&&l.push(u.right)}}}return o}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=(o,l,u)=>{if(!this.isRealNode(o))return!0;let a=Number(o.key);return a<=l||a>=u?!1:i(o.left,l,a)&&i(o.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=(o=!1)=>{let l=[],u=o?Number.MAX_SAFE_INTEGER:Number.MIN_SAFE_INTEGER,a=n;for(;this.isRealNode(a)||l.length>0;){for(;this.isRealNode(a);)l.push(a),a=a.left;a=l.pop();let _=Number(a.key);if(!this.isRealNode(a)||!o&&u>=_||o&&u<=_)return!1;u=_,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 o=this.isRealNode(i.left)?s.get(i.left):-1,l=this.isRealNode(i.right)?s.get(i.right):-1;s.set(i,1+Math.min(o,l)),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:o}=s;return this.isRealNode(o)?r(o):s};return e(r(i))}else{let r=A(s=>{let{left:o}=s;return this.isRealNode(o)?D(()=>r(o)):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=A(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 K([t]),o=l=>{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)),o(l+1)};o(0)}else{let s=new K([t]);for(;s.length>0;){let o=s.length;for(let l=0;l<o;l++){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 K([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=(o,l)=>{r[l]||(r[l]=[]),r[l].push(e(o)),i?(o&&this.isRealNodeOrNull(o.left)&&s(o.left,l+1),o&&this.isRealNodeOrNull(o.right)&&s(o.right,l+1)):(o&&o.left&&s(o.left,l+1),o&&o.right&&s(o.right,l+1))};s(t,0)}else{let s=[[t,0]];for(;s.length>0;){let o=s.pop(),[l,u]=o;r[u]||(r[u]=[]),r[u].push(e(l)),i?(l&&this.isRealNodeOrNull(l.right)&&s.push([l.right,u+1]),l&&this.isRealNodeOrNull(l.left)&&s.push([l.left,u+1])):(l&&l.right&&s.push([l.right,u+1]),l&&l.left&&s.push([l.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=l=>{let u=null,a=null;for(;l;)a=l.right,l.right=u,u=l,l=a;return u},o=l=>{let u=s(l),a=u;for(;a;)i.push(e(a)),a=a.right;s(u)};switch(t){case"IN":for(;r;){if(r.left){let l=this.getPredecessor(r);if(l.right)l.right=null;else{l.right=r,r=r.left;continue}}i.push(e(r)),r=r.right}break;case"PRE":for(;r;){if(r.left){let l=this.getPredecessor(r);if(l.right)l.right=null;else{l.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 l=this.getPredecessor(r);if(l.right===null){l.right=r,r=r.left;continue}else l.right=null,o(r.left)}r=r.right}o(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.set([r,s]);return n}map(e,t,n){let i=this._createLike([],t),r=0;for(let[s,o]of this)i.set(e.call(n,o,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
4
  `),(s=>{let[o]=this._displayAux(s,n),l="";for(let u of o)l+=u+`
5
- `;i+=l})(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,o=_=>!!_,l=_=>!!_,u=_=>s?this.isRealNodeOrNull(_):this.isRealNode(_),a=_=>this.isRealNodeOrNull(_)){if(i=this.ensureNode(i),!i)return[];let _=[];if(r==="RECURSIVE"){let c=h=>{if(!u(h))return;let T=()=>{o(h)&&(h==null?void 0:h.left)!==void 0&&c(h==null?void 0:h.left)},N=()=>{l(h)&&(h==null?void 0:h.right)!==void 0&&c(h==null?void 0:h.right)};switch(t){case"IN":if(T(),a(h)&&(_.push(e(h)),n))return;N();break;case"PRE":if(a(h)&&(_.push(e(h)),n))return;T(),N();break;case"POST":if(T(),N(),a(h)&&(_.push(e(h)),n))return;break}};c(i)}else{let c=[{opt:0,node:i}],h=y=>{var b;o(y.node)&&c.push({opt:0,node:(b=y.node)==null?void 0:b.left})},T=y=>{var b;l(y.node)&&c.push({opt:0,node:(b=y.node)==null?void 0:b.right})},N=y=>{u(y.node)&&c.push({opt:1,node:y.node})};for(;c.length>0;){let y=c.pop();if(y!==void 0&&u(y.node))if(y.opt===1){if(a(y.node)&&y.node!==void 0&&(_.push(e(y.node)),n))return _}else switch(t){case"IN":T(y),N(y),h(y);break;case"PRE":T(y),h(y),N(y);break;case"POST":N(y),T(y),h(y);break}}}return _}*_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 l=e.key,u=this.isNIL(e)?"S":String(l),a=u.length;return o(u,a,this._displayAux(e.left,t),this._displayAux(e.right,t))}else{let l=e===void 0?"U":"N",u=l.length;return o(l,u,[[""],1,0,0],[[""],1,0,0])}function o(l,u,a,_){let[c,h,T,N]=a,[y,b,g,m]=_,I=" ".repeat(Math.max(0,N+1))+"_".repeat(Math.max(0,h-N-1))+l+"_".repeat(Math.max(0,m))+" ".repeat(Math.max(0,b-m)),M=(T>0?" ".repeat(N)+"/"+" ".repeat(h-N-1):" ".repeat(h))+" ".repeat(u)+(g>0?" ".repeat(m)+"\\"+" ".repeat(b-m-1):" ".repeat(b)),P=[I,M];for(let V=0;V<Math.max(T,g);V++){let z=V<T?c[V]:" ".repeat(h),q=V<g?y[V]:" ".repeat(b);P.push(z+" ".repeat(u)+q)}return[P,h+u+b,Math.max(T,g)+2,h+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(d,e){p(this,"key");p(this,"value");p(this,"parent");p(this,"_left");p(this,"_right");p(this,"_height",0);p(this,"_color","BLACK");p(this,"_count",1);this.key=d,this.value=e}get left(){return this._left}set left(d){d&&(d.parent=this),this._left=d}get right(){return this._right}set right(d){d&&(d.parent=this),this._right=d}get height(){return this._height}set height(d){this._height=d}get color(){return this._color}set color(d){this._color=d}get count(){return this._count}set count(d){this._count=d}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"}},F=class extends L{constructor(e=[],t){super([],t);p(this,"_root");p(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 k(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,o=this.isRange(e);o?s=a=>a?e.isInRange(a.key,this._comparator):!1:s=this._ensurePredicate(e);let l=a=>{if(!a||!this.isRealNode(a.left))return!1;if(o){let _=e,c=_.low,h=_.includeLow;return h&&this._compare(a.key,c)>=0||!h&&this._compare(a.key,c)>0}if(!o&&!this._isPredicate(e)){let _=this._extractKey(e);return _!=null&&this._compare(a.key,_)>0}return!0},u=a=>{if(!a||!this.isRealNode(a.right))return!1;if(o){let _=e,c=_.high,h=_.includeHigh;return h&&this._compare(a.key,c)<=0||!h&&this._compare(a.key,c)<0}if(!o&&!this._isPredicate(e)){let _=this._extractKey(e);return _!=null&&this._compare(a.key,_)<0}return!0};return super._dfs(n,"IN",t,i,r,!1,l,u,()=>!0,a=>!!a&&s(a))}rangeSearch(e,t=this._DEFAULT_NODE_CALLBACK,n=this._root,i=this.iterationType){let r=e instanceof E?e:new E(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 c of e){let h=s==null?void 0:s.next().value;this.isRaw(c)&&(c=this._toEntryFn(c)),r.push(this.add(c,h))}return r}let o=[],l=0;for(let c of e)o.push({key:c,value:s==null?void 0:s.next().value,orgIndex:l++});let u=o.sort(({key:c},{key:h})=>{let T,N;return this.isRaw(c)?T=this._toEntryFn(c)[0]:this.isEntry(c)?T=c[0]:this.isRealNode(c)?T=c.key:T=c,this.isRaw(h)?N=this._toEntryFn(h)[0]:this.isEntry(h)?N=h[0]:this.isRealNode(h)?N=h.key:N=h,T!=null&&N!=null?this._compare(T,N):0}),a=c=>{if(c.length===0)return;let h=Math.floor((c.length-1)/2),{key:T,value:N,orgIndex:y}=c[h];if(this.isRaw(T)){let b=this._toEntryFn(T);r[y]=this.add(b)}else r[y]=this.add(T,N);a(c.slice(0,h)),a(c.slice(h+1))};return i==="RECURSIVE"?a(u):(()=>{let h=[[0,u.length-1]];for(;h.length>0;){let T=h.pop();if(!T)continue;let[N,y]=T;if(N>y)continue;let b=N+Math.floor((y-N)/2),{key:g,value:m,orgIndex:I}=u[b];if(this.isRaw(g)){let M=this._toEntryFn(g);r[I]=this.add(M)}else r[I]=this.add(g,m);h.push([b+1,y]),h.push([N,b-1])}})(),r}ceiling(e,t=this._DEFAULT_NODE_CALLBACK,n){let i,r=this.iterationType;typeof t=="string"?r=t:t&&(i=t,n&&(r=n));let s=this._bound(e,!0,r);return i?s?i(s):void 0:s==null?void 0:s.key}higher(e,t=this._DEFAULT_NODE_CALLBACK,n){let i,r=this.iterationType;typeof t=="string"?r=t:t&&(i=t,n&&(r=n));let s=this._bound(e,!1,r);return i?s?i(s):void 0:s==null?void 0:s.key}floor(e,t=this._DEFAULT_NODE_CALLBACK,n){if(e==null)return void 0;let i,r=this.iterationType;if(typeof t=="string"?r=t:t&&(i=t,n&&(r=n)),this._isPredicate(e)){let o=this._floorByPredicate(e,r);return i?o?i(o):void 0:o==null?void 0:o.key}let s;if(this.isNode(e))s=e.key;else if(this.isEntry(e)){let o=e[0];if(o==null)return void 0;s=o}else s=e;if(s!==void 0){let o=this._floorByKey(s,r);return i?o?i(o):void 0:o==null?void 0:o.key}}lower(e,t,n){if(e==null)return void 0;let i,r=this.iterationType;if(typeof t=="string"?r=t:t&&(i=t,n&&(r=n)),this._isPredicate(e)){let o=this._lowerByPredicate(e,r);return i?o?i(o):void 0:o==null?void 0:o.key}let s;if(this.isNode(e))s=e.key;else if(this.isEntry(e)){let o=e[0];if(o==null)return void 0;s=o}else s=e;if(s!==void 0){let o=this._lowerByKey(s,r);return i?o?i(o):void 0:o==null?void 0:o.key}}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 o=r.key;if(i==="RECURSIVE"){let l=u=>{let a=this._compare(u.key,o);Math.sign(a)==t&&s.push(e(u)),this.isRealNode(u.left)&&l(u.left),this.isRealNode(u.right)&&l(u.right)};return l(this._root),s}else{let l=new K([this._root]);for(;l.length>0;){let u=l.shift();if(this.isRealNode(u)){let a=this._compare(u.key,o);Math.sign(a)==t&&s.push(e(u)),this.isRealNode(u.left)&&l.push(u.left),this.isRealNode(u.right)&&l.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,o,l)=>{if(s>o)return;let u=s+(o-s>>1),a=t[u],_=i(s,u-1,a),c=i(u+1,o,a);return a.left=_,a.right=c,a.parent=l,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 o=i.left?s.get(i.left):-1,l=i.right?s.get(i.right):-1;if(Math.abs(o-l)>1)return!1;s.set(i,1+Math.max(o,l)),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,o]of this)i.add(e.call(n,o,s,r++,this));return i}deleteWhere(e,t=!1,n=this._root,i=this.iterationType){let r=this.search(e,t,o=>o,n,i),s=[];for(let o of r){let l=this.delete(o);s=s.concat(l)}return s}_createDefaultComparator(){return(e,t)=>{debugger;if(k(e)&&k(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 l=r(s.right);return l!=null?l: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 l=r(s.right);return l!=null?l: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=o=>{if(!this.isRealNode(o))return;let l=this.comparator(o.key,e);if(t?l>=0:l>0){let a=s(o.left);return a!=null?a:o}else return s(o.right)};return s(this.root)}else{let s=this.root,o;for(;this.isRealNode(s);){let l=this.comparator(s.key,e);(t?l>=0:l>0)?(o=s,s=(i=s.left)!=null?i:void 0):s=(r=s.right)!=null?r:void 0}return o}}_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,o)=>{let l=s==null?void 0:s.parent;l?l.left===s?l.left=o:l.right=o:this._setRoot(o),o&&(o.parent=l)},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}};return J(te);})();
5
+ `;i+=l})(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,o=_=>!!_,l=_=>!!_,u=_=>s?this.isRealNodeOrNull(_):this.isRealNode(_),a=_=>this.isRealNodeOrNull(_)){if(i=this.ensureNode(i),!i)return[];let _=[];if(r==="RECURSIVE"){let c=h=>{if(!u(h))return;let T=()=>{o(h)&&(h==null?void 0:h.left)!==void 0&&c(h==null?void 0:h.left)},N=()=>{l(h)&&(h==null?void 0:h.right)!==void 0&&c(h==null?void 0:h.right)};switch(t){case"IN":if(T(),a(h)&&(_.push(e(h)),n))return;N();break;case"PRE":if(a(h)&&(_.push(e(h)),n))return;T(),N();break;case"POST":if(T(),N(),a(h)&&(_.push(e(h)),n))return;break}};c(i)}else{let c=[{opt:0,node:i}],h=y=>{var b;o(y.node)&&c.push({opt:0,node:(b=y.node)==null?void 0:b.left})},T=y=>{var b;l(y.node)&&c.push({opt:0,node:(b=y.node)==null?void 0:b.right})},N=y=>{u(y.node)&&c.push({opt:1,node:y.node})};for(;c.length>0;){let y=c.pop();if(y!==void 0&&u(y.node))if(y.opt===1){if(a(y.node)&&y.node!==void 0&&(_.push(e(y.node)),n))return _}else switch(t){case"IN":T(y),N(y),h(y);break;case"PRE":T(y),h(y),N(y);break;case"POST":N(y),T(y),h(y);break}}}return _}*_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.set(null):this._isMapMode?e.set([t.key,this._store.get(t.key)]):e.set([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 l=e.key,u=this.isNIL(e)?"S":String(l),a=u.length;return o(u,a,this._displayAux(e.left,t),this._displayAux(e.right,t))}else{let l=e===void 0?"U":"N",u=l.length;return o(l,u,[[""],1,0,0],[[""],1,0,0])}function o(l,u,a,_){let[c,h,T,N]=a,[y,b,g,m]=_,I=" ".repeat(Math.max(0,N+1))+"_".repeat(Math.max(0,h-N-1))+l+"_".repeat(Math.max(0,m))+" ".repeat(Math.max(0,b-m)),M=(T>0?" ".repeat(N)+"/"+" ".repeat(h-N-1):" ".repeat(h))+" ".repeat(u)+(g>0?" ".repeat(m)+"\\"+" ".repeat(b-m-1):" ".repeat(b)),P=[I,M];for(let V=0;V<Math.max(T,g);V++){let z=V<T?c[V]:" ".repeat(h),q=V<g?y[V]:" ".repeat(b);P.push(z+" ".repeat(u)+q)}return[P,h+u+b,Math.max(T,g)+2,h+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(d,e){p(this,"key");p(this,"value");p(this,"parent");p(this,"_left");p(this,"_right");p(this,"_height",0);p(this,"_color","BLACK");p(this,"_count",1);this.key=d,this.value=e}get left(){return this._left}set left(d){d&&(d.parent=this),this._left=d}get right(){return this._right}set right(d){d&&(d.parent=this),this._right=d}get height(){return this._height}set height(d){this._height=d}get color(){return this._color}set color(d){this._color=d}get count(){return this._count}set count(d){this._count=d}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"}},F=class extends L{constructor(e=[],t){super([],t);p(this,"_root");p(this,"_comparator");t?"comparator"in t&&t.comparator!==void 0?this._comparator=t.comparator:this._comparator=this._createDefaultComparator():this._comparator=this._createDefaultComparator(),e&&this.setMany(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 k(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,o=this.isRange(e);o?s=a=>a?e.isInRange(a.key,this._comparator):!1:s=this._ensurePredicate(e);let l=a=>{if(!a||!this.isRealNode(a.left))return!1;if(o){let _=e,c=_.low,h=_.includeLow;return h&&this._compare(a.key,c)>=0||!h&&this._compare(a.key,c)>0}if(!o&&!this._isPredicate(e)){let _=this._extractKey(e);return _!=null&&this._compare(a.key,_)>0}return!0},u=a=>{if(!a||!this.isRealNode(a.right))return!1;if(o){let _=e,c=_.high,h=_.includeHigh;return h&&this._compare(a.key,c)<=0||!h&&this._compare(a.key,c)<0}if(!o&&!this._isPredicate(e)){let _=this._extractKey(e);return _!=null&&this._compare(a.key,_)<0}return!0};return super._dfs(n,"IN",t,i,r,!1,l,u,()=>!0,a=>!!a&&s(a))}rangeSearch(e,t=this._DEFAULT_NODE_CALLBACK,n=this._root,i=this.iterationType){let r=e instanceof E?e:new E(e[0],e[1]);return this.search(r,!1,t,n,i)}set(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}setMany(e,t,n=!0,i=this.iterationType){let r=[],s=t==null?void 0:t[Symbol.iterator]();if(!n){for(let c of e){let h=s==null?void 0:s.next().value;this.isRaw(c)&&(c=this._toEntryFn(c)),r.push(this.set(c,h))}return r}let o=[],l=0;for(let c of e)o.push({key:c,value:s==null?void 0:s.next().value,orgIndex:l++});let u=o.sort(({key:c},{key:h})=>{let T,N;return this.isRaw(c)?T=this._toEntryFn(c)[0]:this.isEntry(c)?T=c[0]:this.isRealNode(c)?T=c.key:T=c,this.isRaw(h)?N=this._toEntryFn(h)[0]:this.isEntry(h)?N=h[0]:this.isRealNode(h)?N=h.key:N=h,T!=null&&N!=null?this._compare(T,N):0}),a=c=>{if(c.length===0)return;let h=Math.floor((c.length-1)/2),{key:T,value:N,orgIndex:y}=c[h];if(this.isRaw(T)){let b=this._toEntryFn(T);r[y]=this.set(b)}else r[y]=this.set(T,N);a(c.slice(0,h)),a(c.slice(h+1))};return i==="RECURSIVE"?a(u):(()=>{let h=[[0,u.length-1]];for(;h.length>0;){let T=h.pop();if(!T)continue;let[N,y]=T;if(N>y)continue;let b=N+Math.floor((y-N)/2),{key:g,value:m,orgIndex:I}=u[b];if(this.isRaw(g)){let M=this._toEntryFn(g);r[I]=this.set(M)}else r[I]=this.set(g,m);h.push([b+1,y]),h.push([N,b-1])}})(),r}ceiling(e,t=this._DEFAULT_NODE_CALLBACK,n){let i,r=this.iterationType;typeof t=="string"?r=t:t&&(i=t,n&&(r=n));let s=this._bound(e,!0,r);return i?s?i(s):void 0:s==null?void 0:s.key}higher(e,t=this._DEFAULT_NODE_CALLBACK,n){let i,r=this.iterationType;typeof t=="string"?r=t:t&&(i=t,n&&(r=n));let s=this._bound(e,!1,r);return i?s?i(s):void 0:s==null?void 0:s.key}floor(e,t=this._DEFAULT_NODE_CALLBACK,n){if(e==null)return void 0;let i,r=this.iterationType;if(typeof t=="string"?r=t:t&&(i=t,n&&(r=n)),this._isPredicate(e)){let o=this._floorByPredicate(e,r);return i?o?i(o):void 0:o==null?void 0:o.key}let s;if(this.isNode(e))s=e.key;else if(this.isEntry(e)){let o=e[0];if(o==null)return void 0;s=o}else s=e;if(s!==void 0){let o=this._floorByKey(s,r);return i?o?i(o):void 0:o==null?void 0:o.key}}lower(e,t,n){if(e==null)return void 0;let i,r=this.iterationType;if(typeof t=="string"?r=t:t&&(i=t,n&&(r=n)),this._isPredicate(e)){let o=this._lowerByPredicate(e,r);return i?o?i(o):void 0:o==null?void 0:o.key}let s;if(this.isNode(e))s=e.key;else if(this.isEntry(e)){let o=e[0];if(o==null)return void 0;s=o}else s=e;if(s!==void 0){let o=this._lowerByKey(s,r);return i?o?i(o):void 0:o==null?void 0:o.key}}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 o=r.key;if(i==="RECURSIVE"){let l=u=>{let a=this._compare(u.key,o);Math.sign(a)==t&&s.push(e(u)),this.isRealNode(u.left)&&l(u.left),this.isRealNode(u.right)&&l(u.right)};return l(this._root),s}else{let l=new K([this._root]);for(;l.length>0;){let u=l.shift();if(this.isRealNode(u)){let a=this._compare(u.key,o);Math.sign(a)==t&&s.push(e(u)),this.isRealNode(u.left)&&l.push(u.left),this.isRealNode(u.right)&&l.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,o,l)=>{if(s>o)return;let u=s+(o-s>>1),a=t[u],_=i(s,u-1,a),c=i(u+1,o,a);return a.left=_,a.right=c,a.parent=l,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 o=i.left?s.get(i.left):-1,l=i.right?s.get(i.right):-1;if(Math.abs(o-l)>1)return!1;s.set(i,1+Math.max(o,l)),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,o]of this)i.set(e.call(n,o,s,r++,this));return i}deleteWhere(e,t=!1,n=this._root,i=this.iterationType){let r=this.search(e,t,o=>o,n,i),s=[];for(let o of r){let l=this.delete(o);s=s.concat(l)}return s}_createDefaultComparator(){return(e,t)=>{if(k(e)&&k(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 l=r(s.right);return l!=null?l: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 l=r(s.right);return l!=null?l: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=o=>{if(!this.isRealNode(o))return;let l=this.comparator(o.key,e);if(t?l>=0:l>0){let a=s(o.left);return a!=null?a:o}else return s(o.right)};return s(this.root)}else{let s=this.root,o;for(;this.isRealNode(s);){let l=this.comparator(s.key,e);(t?l>=0:l>0)?(o=s,s=(i=s.left)!=null?i:void 0):s=(r=s.right)!=null?r:void 0}return o}}_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,o)=>{let l=s==null?void 0:s.parent;l?l.left===s?l.left=o:l.right=o:this._setRoot(o),o&&(o.parent=l)},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}};return J(te);})();
6
6
  /**
7
7
  * data-structure-typed
8
8
  *