UNPKG

red-black-tree-typed

Version:
13 lines 53.7 kB
"use strict";var redBlackTreeTyped=(()=>{var F=Object.defineProperty;var J=Object.getOwnPropertyDescriptor;var Y=Object.getOwnPropertyNames;var Z=Object.prototype.hasOwnProperty;var ee=(f,l,e)=>l in f?F(f,l,{enumerable:!0,configurable:!0,writable:!0,value:e}):f[l]=e;var te=(f,l)=>{for(var e in l)F(f,e,{get:l[e],enumerable:!0})},ne=(f,l,e,t)=>{if(l&&typeof l=="object"||typeof l=="function")for(let n of Y(l))!Z.call(f,n)&&n!==e&&F(f,n,{get:()=>l[n],enumerable:!(t=J(l,n))||t.enumerable});return f};var ie=f=>ne(F({},"__esModule",{value:!0}),f);var K=(f,l,e)=>ee(f,typeof l!="symbol"?l+"":l,e);var oe={};te(oe,{BST:()=>O,BSTNode:()=>D,BinaryTree:()=>v,BinaryTreeNode:()=>w,DFSOperation:()=>z,ERR:()=>E,Range:()=>I,RedBlackTree:()=>j,RedBlackTreeNode:()=>A,raise:()=>b});function G(f){let l=typeof f;return l==="number"?!0:l==="bigint"||l==="string"||l==="boolean"}function X(f){if(typeof f.valueOf=="function"){let l=f.valueOf();if(l!==f){if(G(l))return l;if(typeof l=="object"&&l!==null)return X(l)}}if(typeof f.toString=="function"){let l=f.toString();if(l!=="[object Object]")return l}return null}function x(f,l=!1){if(f==null)return!1;if(G(f))return!0;if(typeof f!="object")return!1;if(f instanceof Date||l)return!0;let e=X(f);return e==null?!1:G(e)}var Q=f=>({isThunk:!0,fn:f}),re=f=>typeof f=="object"&&f!==null&&"isThunk"in f&&f.isThunk;function se(f){let l=f;for(;re(l);)l=l.fn();return l}function q(f){return(...l)=>se(f(...l))}function b(f,l){throw new f(l)}var E={indexOutOfRange:(f,l,e,t)=>`${t?t+": ":""}Index ${f} is out of range [${l}, ${e}].`,invalidIndex:f=>`${f?f+": ":""}Index must be an integer.`,invalidArgument:(f,l)=>`${l?l+": ":""}${f}`,comparatorRequired:f=>`${f?f+": ":""}Comparator is required for non-number/non-string/non-Date keys.`,invalidKey:(f,l)=>`${l?l+": ":""}${f}`,notAFunction:(f,l)=>`${l?l+": ":""}${f} must be a function.`,invalidEntry:f=>`${f?f+": ":""}Each entry must be a [key, value] tuple.`,invalidNaN:f=>`${f?f+": ":""}NaN is not a valid key.`,invalidDate:f=>`${f?f+": ":""}Invalid Date key.`,reduceEmpty:f=>`${f?f+": ":""}Reduce of empty structure with no initial value.`,callbackReturnType:(f,l,e)=>`${e?e+": ":""}Callback must return ${f}; got ${l}.`,invalidOperation:(f,l)=>`${l?l+": ":""}${f}`,matrixDimensionMismatch:f=>`Matrix: Dimensions must be compatible for ${f}.`,matrixSingular:()=>"Matrix: Singular matrix, inverse does not exist.",matrixNotSquare:()=>"Matrix: Must be square for inversion.",matrixNotRectangular:()=>"Matrix: Must be rectangular for transposition.",matrixRowMismatch:(f,l)=>`Matrix: Expected row length ${f}, but got ${l}.`,orderStatisticNotEnabled:(f,l)=>`${l?l+": ":""}${f}() requires enableOrderStatistic: true.`};var z=(e=>(e[e.VISIT=0]="VISIT",e[e.PROCESS=1]="PROCESS",e))(z||{}),I=class{constructor(l,e,t=!0,n=!0){this.low=l;this.high=e;this.includeLow=t;this.includeHigh=n}isInRange(l,e){let t=this.includeLow?e(l,this.low)>=0:e(l,this.low)>0,n=this.includeHigh?e(l,this.high)<=0:e(l,this.high)<0;return t&&n}};var U=class{constructor(l){K(this,"_toElementFn");if(l){let{toElementFn:e}=l;typeof e=="function"?this._toElementFn=e:e&&b(TypeError,"toElementFn must be a function type")}}get toElementFn(){return this._toElementFn}*[Symbol.iterator](...l){yield*this._getIterator(...l)}*values(){for(let l of this)yield l}every(l,e){let t=0;for(let n of this)if(e===void 0){if(!l(n,t++,this))return!1}else if(!l.call(e,n,t++,this))return!1;return!0}some(l,e){let t=0;for(let n of this)if(e===void 0){if(l(n,t++,this))return!0}else if(l.call(e,n,t++,this))return!0;return!1}forEach(l,e){let t=0;for(let n of this)e===void 0?l(n,t++,this):l.call(e,n,t++,this)}find(l,e){let t=0;for(let n of this)if(e===void 0){if(l(n,t++,this))return n}else if(l.call(e,n,t++,this))return n}has(l){for(let e of this)if(e===l)return!0;return!1}includes(l){return this.has(l)}*entries(){let l=0;for(let e of this)yield[l++,e]}*keys(){let l=0;for(let e of this)yield l++}reduce(l,e){let t=0,n=this[Symbol.iterator](),i;if(arguments.length>=2)i=e;else{let s=n.next();s.done&&b(TypeError,"Reduce of empty structure with no initial value"),i=s.value,t=1}for(let s of n)i=l(i,s,t++,this);return i}toArray(){return[...this]}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var P=class f extends U{constructor(e){super(e);K(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 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}toReversed(){let e=this.clone();return e.reverse(),e}};var H=class{*[Symbol.iterator](...l){yield*this._getIterator(...l)}*entries(){for(let l of this)yield l}*keys(){for(let l of this)yield l[0]}*values(){for(let l of this)yield l[1]}every(l,e){let t=0;for(let n of this)if(!l.call(e,n[1],n[0],t++,this))return!1;return!0}some(l,e){let t=0;for(let n of this)if(l.call(e,n[1],n[0],t++,this))return!0;return!1}forEach(l,e){let t=0;for(let n of this){let[i,s]=n;l.call(e,s,i,t++,this)}}find(l,e){let t=0;for(let n of this){let[i,s]=n;if(l.call(e,s,i,t++,this))return n}}has(l){for(let e of this){let[t]=e;if(t===l)return!0}return!1}hasValue(l){for(let[,e]of this)if(e===l)return!0;return!1}get(l){for(let e of this){let[t,n]=e;if(t===l)return n}}reduce(l,e){let t=e,n=0;for(let i of this){let[s,r]=i;t=l(t,r,s,n++,this)}return t}toArray(){return[...this]}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var k=class f extends P{constructor(e=[],t){super(t);K(this,"_elements",[]);K(this,"_offset",0);K(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}peek(){return this.first}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)}deleteWhere(e){for(let t=0;t<this.length;t++)if(e(this._elements[this._offset+t],t,this))return this.deleteAt(t),!0;return!1}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,o;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:(o=t==null?void 0:t.autoCompactRatio)!=null?o:this._autoCompactRatio}),s=0;for(let d of this)i.push(n===void 0?e(d,s++,this):e.call(n,d,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 o of this){let d=t===void 0?e(o,s++,this):e.call(t,o,s++,this);i.push(d)}return i}_setAutoCompactRatio(e){this._autoCompactRatio=e}*_getIterator(){for(let e=this._offset;e<this.elements.length;e++)yield this.elements[e]}*_getReverseIterator(){for(let e=this.length-1;e>=0;e--){let t=this.at(e);t!==void 0&&(yield t)}}_createInstance(e){let t=this.constructor;return new t([],e)}_createLike(e=[],t){let n=this.constructor;return new n(e,t)}};var w=class{constructor(l,e){K(this,"key");K(this,"value");K(this,"parent");K(this,"_left");K(this,"_right");K(this,"_height",0);K(this,"_color","BLACK");K(this,"_count",1);this.key=l,this.value=e}get left(){return this._left}set left(l){l&&(l.parent=this),this._left=l}get right(){return this._right}set right(l){l&&(l.parent=this),this._right=l}get height(){return this._height}set height(l){this._height=l}get color(){return this._color}set color(l){this._color=l}get count(){return this._count}set count(l){this._count=l}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"}},v=class f extends H{constructor(e=[],t){super();K(this,"iterationType","ITERATIVE");K(this,"_isMapMode",!0);K(this,"_isDuplicate",!1);K(this,"_store",new Map);K(this,"_root");K(this,"_size",0);K(this,"_NIL",new w(NaN));K(this,"_toEntryFn");K(this,"_DEFAULT_NODE_CALLBACK",e=>e==null?void 0:e.key);if(t){let{iterationType:n,toEntryFn:i,isMapMode:s,isDuplicate:r}=t;n&&(this.iterationType=n),s!==void 0&&(this._isMapMode=s),r!==void 0&&(this._isDuplicate=r),typeof i=="function"?this._toEntryFn=i:i&&b(TypeError,E.notAFunction("toEntryFn","BinaryTree"))}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 w(e,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 w}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 I}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:x(e)}add(e){return this.set(e)}set(e,t){let[n]=this._keyValueNodeOrEntryToNodeAndValue(e,t);if(n===void 0)return!1;if(!this._root)return this._setRoot(n),this._isMapMode&&n!==null&&n!==void 0&&this._store.set(n.key,n),n!==null&&(this._size=1),!0;let i=new k([this._root]),s;for(;i.length>0;){let r=i.shift();if(r){if(!this._isDuplicate&&n!==null&&r.key===n.key)return this._replaceNode(r,n),this._isMapMode&&n!==null&&this._store.set(r.key,n),!0;s===void 0&&(r.left===void 0||r.right===void 0)&&(s=r),r.left!==null&&r.left&&i.push(r.left),r.right!==null&&r.right&&i.push(r.right)}}return s?(s.left===void 0?s.left=n:s.right===void 0&&(s.right=n),this._isMapMode&&n!==null&&n!==void 0&&this._store.set(n.key,n),n!==null&&this._size++,!0):!1}addMany(e){return this.setMany(e)}setMany(e,t){let n=[],i;t&&(i=t[Symbol.iterator]());for(let s of e){let r;if(i){let o=i.next();o.done||(r=o.value)}this.isRaw(s)&&(s=this._toEntryFn(s)),n.push(this.set(s,r))}return n}merge(e){this.setMany(e,[])}_deleteInternal(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 o=this.getRightMost(d=>d,n.left);if(o){let d=o.parent;r=this._swapProperties(n,o),this._isMapMode&&(this._store.set(n.key,n),this._store.set(o.key,o)),d&&(d.right===o?d.right=o.left:d.left=o.left,s=d)}}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),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}delete(e){return this._deleteInternal(e).length>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=this._ensurePredicate(e),o=[];if(s==="RECURSIVE"){let d=u=>{r(u)&&(o.push(n(u)),t)||!this.isRealNode(u.left)&&!this.isRealNode(u.right)||(this.isRealNode(u.left)&&d(u.left),this.isRealNode(u.right)&&d(u.right))};d(i)}else{let d=[i];for(;d.length>0;){let u=d.pop();if(this.isRealNode(u)){if(r(u)&&(o.push(n(u)),t))return o;this.isRealNode(u.left)&&d.push(u.left),this.isRealNode(u.right)&&d.push(u.right)}}}return o}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){if(this._isMapMode&&e!==null&&e!==void 0&&!this._isPredicate(e)){let i=this._extractKey(e);return i==null?void 0:this._store.get(i)}return this.search(e,!0,i=>i,t,n)[0]}get(e,t=this._root,n=this.iterationType){var i,s;if(this._isMapMode){let r=this._extractKey(e);return r==null||(i=this._store.get(r))==null?void 0:i.value}return(s=this.getNode(e,t,n))==null?void 0:s.value}has(e,t=this._root,n=this.iterationType){if(this._isMapMode&&e!==void 0&&e!==null&&!this._isPredicate(e)){let i=this._extractKey(e);return i==null?!1:this._store.has(i)}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,d,u)=>{if(!this.isRealNode(o))return!0;let h=Number(o.key);return h<=d||h>=u?!1:i(o.left,d,h)&&i(o.right,h,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=(o=!1)=>{let d=[],u=o?Number.MAX_SAFE_INTEGER:Number.MIN_SAFE_INTEGER,h=n;for(;this.isRealNode(h)||d.length>0;){for(;this.isRealNode(h);)d.push(h),h=h.left;h=d.pop();let c=Number(h.key);if(!this.isRealNode(h)||!o&&u>=c||o&&u<=c)return!1;u=c,h=h.right}return!0},s=i(),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 o=this.isRealNode(i.left)?r.get(i.left):-1,d=this.isRealNode(i.right)?r.get(i.right):-1;r.set(i,1+Math.min(o,d)),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:o}=r;return this.isRealNode(o)?s(o):r};return e(s(i))}else{let s=q(r=>{let{left:o}=r;return this.isRealNode(o)?Q(()=>s(o)):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=q(s=>{let{right:r}=s;return this.isRealNode(r)?Q(()=>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 k([t]),o=d=>{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)),o(d+1)};o(0)}else{let r=new k([t]);for(;r.length>0;){let o=r.length;for(let d=0;d<o;d++){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=[t];for(;s.length>0;){let r=s.pop();this.isRealNode(r)&&(this.isLeaf(r)&&i.push(e(r)),this.isRealNode(r.right)&&s.push(r.right),this.isRealNode(r.left)&&s.push(r.left))}}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=(o,d)=>{s[d]||(s[d]=[]),s[d].push(e(o)),i?(o&&this.isRealNodeOrNull(o.left)&&r(o.left,d+1),o&&this.isRealNodeOrNull(o.right)&&r(o.right,d+1)):(o&&o.left&&r(o.left,d+1),o&&o.right&&r(o.right,d+1))};r(t,0)}else{let r=[[t,0]];for(;r.length>0;){let o=r.pop(),[d,u]=o;s[u]||(s[u]=[]),s[u].push(e(d)),i?(d&&this.isRealNodeOrNull(d.right)&&r.push([d.right,u+1]),d&&this.isRealNodeOrNull(d.left)&&r.push([d.left,u+1])):(d&&d.right&&r.push([d.right,u+1]),d&&d.left&&r.push([d.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=d=>{let u=null,h=null;for(;d;)h=d.right,d.right=u,u=d,d=h;return u},o=d=>{let u=r(d),h=u;for(;h;)i.push(e(h)),h=h.right;r(u)};switch(t){case"IN":for(;s;){if(s.left){let d=this.getPredecessor(s);if(d.right)d.right=null;else{d.right=s,s=s.left;continue}}i.push(e(s)),s=s.right}break;case"PRE":for(;s;){if(s.left){let d=this.getPredecessor(s);if(d.right)d.right=null;else{d.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 d=this.getPredecessor(s);if(d.right===null){d.right=s,s=s.left;continue}else d.right=null,o(s.left)}s=s.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[s,r]of this)e.call(t,r,s,i++,this)&&n.set([s,r]);return n}map(e,t,n){let i=this._createLike([],t),s=0;for(let[r,o]of this)i.set(e.call(n,o,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
`),n.isShowNull&&(i+=`N for null
`),n.isShowRedBlackNIL&&(i+=`S for Sentinel Node(NIL)
`),(r=>{let[o]=this._displayAux(r,n),d="";for(let u of o)d+=u+`
`;i+=d})(e)),i}print(e,t=this._root){console.log(this.toVisual(t,e))}_dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,s=this.iterationType,r=!1,o=c=>!!c,d=c=>!!c,u=c=>r?this.isRealNodeOrNull(c):this.isRealNode(c),h=c=>this.isRealNodeOrNull(c)){if(i=this.ensureNode(i),!i)return[];let c=[];if(s==="RECURSIVE"){let T=a=>{if(!u(a))return;let p=()=>{o(a)&&(a==null?void 0:a.left)!==void 0&&T(a==null?void 0:a.left)},V=()=>{d(a)&&(a==null?void 0:a.right)!==void 0&&T(a==null?void 0:a.right)};switch(t){case"IN":if(p(),h(a)&&(c.push(e(a)),n))return;V();break;case"PRE":if(h(a)&&(c.push(e(a)),n))return;p(),V();break;case"POST":if(p(),V(),h(a)&&(c.push(e(a)),n))return;break}};T(i)}else{let T=[{opt:0,node:i}],a=N=>{var y;o(N.node)&&T.push({opt:0,node:(y=N.node)==null?void 0:y.left})},p=N=>{var y;d(N.node)&&T.push({opt:0,node:(y=N.node)==null?void 0:y.right})},V=N=>{u(N.node)&&T.push({opt:1,node:N.node})};for(;T.length>0;){let N=T.pop();if(N!==void 0&&u(N.node))if(N.opt===1){if(h(N.node)&&N.node!==void 0&&(c.push(e(N.node)),n))return c}else switch(t){case"IN":p(N),V(N),a(N);break;case"PRE":p(N),a(N),V(N);break;case"POST":V(N),p(N),a(N);break}}}return c}*_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)&&(yield[n.key,n.value],n=n.right)}}else e.left&&this.isRealNode(e)&&(yield*this[Symbol.iterator](e.left)),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.set(null):e.set([t.key,t.value])},this._root,this.iterationType,!0)}_displayAux(e,t){let n=[["\u2500"],1,0,0],i=d=>({node:d,stage:0,leftLayout:n,rightLayout:n}),s=[i(e)],r=n,o=d=>{if(s.length===0){r=d;return}let u=s[s.length-1];u.stage===1?u.leftLayout=d:u.rightLayout=d};for(;s.length>0;){let d=s[s.length-1],u=d.node;if(d.stage===0){if(this._isDisplayLeaf(u,t)){s.pop();let h=this._resolveDisplayLeaf(u,t,n);o(h);continue}d.stage=1,s.push(i(u.left))}else if(d.stage===1)d.stage=2,s.push(i(u.right));else{s.pop();let h=this.isNIL(u)?"S":String(u.key),c=f._buildNodeDisplay(h,h.length,d.leftLayout,d.rightLayout);o(c)}}return r}static _buildNodeDisplay(e,t,n,i){let[s,r,o,d]=n,[u,h,c,T]=i,a=" ".repeat(Math.max(0,d+1))+"_".repeat(Math.max(0,r-d-1))+e+"_".repeat(Math.max(0,T))+" ".repeat(Math.max(0,h-T)),p=(o>0?" ".repeat(d)+"/"+" ".repeat(r-d-1):" ".repeat(r))+" ".repeat(t)+(c>0?" ".repeat(T)+"\\"+" ".repeat(h-T-1):" ".repeat(h)),V=[a,p];for(let N=0;N<Math.max(o,c);N++){let y=N<o?s[N]:" ".repeat(r),C=N<c?u[N]:" ".repeat(h);V.push(y+" ".repeat(t)+C)}return[V,r+t+h,Math.max(o,c)+2,r+Math.floor(t/2)]}_isDisplayLeaf(e,t){let{isShowNull:n,isShowUndefined:i,isShowRedBlackNIL:s}=t;if(e===null&&!n||e===void 0&&!i||this.isNIL(e)&&!s||e==null)return!0;let r=this._hasDisplayableChild(e.left,t),o=this._hasDisplayableChild(e.right,t);return!r&&!o}_hasDisplayableChild(e,t){return e===null?!!t.isShowNull:e===void 0?!!t.isShowUndefined:this.isNIL(e)?!!t.isShowRedBlackNIL:!0}_resolveDisplayLeaf(e,t,n){let{isShowNull:i,isShowUndefined:s,isShowRedBlackNIL:r}=t;if(e===null&&!i||e===void 0&&!s||this.isNIL(e)&&!r)return n;if(e!=null){let d=this.isNIL(e)?"S":String(e.key);return f._buildNodeDisplay(d,d.length,n,n)}let o=e===void 0?"U":"N";return f._buildNodeDisplay(o,o.length,[[""],1,0,0],[[""],1,0,0])}_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){if(e==null)return!1;let n=this._store.get(e);return n?(n.value=t,!0):!1}_clearNodes(){this._setRoot(void 0),this._size=0}_clearValues(){this._store.clear()}};var D=class{constructor(l,e){K(this,"key");K(this,"value");K(this,"parent");K(this,"_left");K(this,"_right");K(this,"_height",0);K(this,"_color","BLACK");K(this,"_count",1);this.key=l,this.value=e}get left(){return this._left}set left(l){l&&(l.parent=this),this._left=l}get right(){return this._right}set right(l){l&&(l.parent=this),this._right=l}get height(){return this._height}set height(l){this._height=l}get color(){return this._color}set color(l){this._color=l}get count(){return this._count}set count(l){this._count=l}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"}},O=class extends v{constructor(e=[],t){super([],t);K(this,"_root");K(this,"_enableOrderStatistic",!1);K(this,"_comparator");t?("comparator"in t&&t.comparator!==void 0?this._comparator=t.comparator:this._comparator=this._createDefaultComparator(),t.enableOrderStatistic&&(this._enableOrderStatistic=!0)):this._comparator=this._createDefaultComparator(),e&&this.setMany(e)}get root(){return this._root}get comparator(){return this._comparator}createNode(e,t){return new D(e,t)}ensureNode(e,t=this.iterationType){var n;return(n=super.ensureNode(e,t))!=null?n:void 0}isNode(e){return e instanceof D}isValidKey(e){return x(e)}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 u,h;if(e==null)return;if(this._isPredicate(e))return(u=this.getNodes(e,!0,t,n)[0])!=null?u:void 0;if(e instanceof I)return(h=this.getNodes(e,!0,t,n)[0])!=null?h:void 0;let i;if(this.isNode(e))i=e.key;else if(this.isEntry(e)){let c=e[0];if(c==null)return;i=c}else i=e;let s=this.ensureNode(t);if(!s)return;let r=this._NIL,o=s,d=this._comparator;for(;o&&o!==r;){let c=d(i,o.key);if(c===0)return o;o=c<0?o._left:o._right}}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.isRange(e),o=!r&&this._isPredicate(e);if(!r&&!o){let c;if(this.isNode(e))c=e.key;else if(this.isEntry(e)){let V=e[0];V!=null&&(c=V)}else c=e;if(c===void 0)return[];let T=this._NIL,a=this._comparator,p=i;for(;p&&p!==T;){let V=a(c,p.key);if(V===0)return[n(p)];p=V<0?p._left:p._right}return[]}let d;r?d=c=>c?e.isInRange(c.key,this._comparator):!1:d=this._ensurePredicate(e);let u=c=>{if(!c||!this.isRealNode(c.left))return!1;if(r){let T=e,a=T.low,p=T.includeLow;return p&&this._compare(c.key,a)>=0||!p&&this._compare(c.key,a)>0}if(!r&&!this._isPredicate(e)){let T=this._extractKey(e);return T!=null&&this._compare(c.key,T)>0}return!0},h=c=>{if(!c||!this.isRealNode(c.right))return!1;if(r){let T=e,a=T.high,p=T.includeHigh;return p&&this._compare(c.key,a)<=0||!p&&this._compare(c.key,a)<0}if(!r&&!this._isPredicate(e)){let T=this._extractKey(e);return T!=null&&this._compare(c.key,T)<0}return!0};return super._dfs(n,"IN",t,i,s,!1,u,h,()=>!0,c=>!!c&&d(c))}rangeSearch(e,t=this._DEFAULT_NODE_CALLBACK,n=this._root,i=this.iterationType){let s=e instanceof I?e:new I(e[0],e[1]);return this.search(s,!1,t,n,i)}getByRank(e,t=this._DEFAULT_NODE_CALLBACK,n=this.iterationType){if(this._enableOrderStatistic||b(Error,E.orderStatisticNotEnabled("getByRank")),e<0||e>=this._size)return;let i,s=this.iterationType;typeof t=="string"?s=t:t&&(i=t,n&&(s=n));let r=s==="RECURSIVE"?this._getByRankRecursive(this._root,e):this._getByRankIterative(this._root,e);if(r)return i?i(r):r.key}getRank(e,t=this.iterationType){var s;if(this._enableOrderStatistic||b(Error,E.orderStatisticNotEnabled("getRank")),!this._root||this._size===0)return-1;let n=this.iterationType;t&&(n=t);let i;if(typeof e=="function"){let r=this.search(e,!0);if(r.length===0||r[0]===void 0)return-1;i=r[0]}else{if(e==null)return-1;if(this.isNode(e))i=e.key;else if(Array.isArray(e)){if(i=(s=e[0])!=null?s:void 0,i==null)return-1}else i=e}return i===void 0?-1:n==="RECURSIVE"?this._getRankRecursive(this._root,i):this._getRankIterative(this._root,i)}rangeByRank(e,t,n=this._DEFAULT_NODE_CALLBACK,i=this.iterationType){if(this._enableOrderStatistic||b(Error,E.orderStatisticNotEnabled("rangeByRank")),this._size===0)return[];let s=Math.max(0,e),r=Math.min(this._size-1,t);if(s>r)return[];let o,d=this.iterationType;typeof n=="string"?d=n:n&&(o=n,i&&(d=i));let u=[],h=r-s+1,c=d==="RECURSIVE"?this._getByRankRecursive(this._root,s):this._getByRankIterative(this._root,s);if(!c)return[];let T=0,a=o!=null?o:this._DEFAULT_NODE_CALLBACK,p=c;for(;p&&T<h;)u.push(a(p)),T++,T<h&&(p=this._next(p));return u}set(e,t){let[n]=this._keyValueNodeOrEntryToNodeAndValue(e,t);if(n===void 0)return!1;if(this._root===void 0)return this._setRoot(n),this._isMapMode&&this.isRealNode(n)&&this._store.set(n.key,n),this._size++,this._updateCount(n),!0;let i=this._root;for(;i!==void 0;){if(this._compare(i.key,n.key)===0)return this._replaceNode(i,n),this._isMapMode&&this.isRealNode(n)&&this._store.set(i.key,n),!0;if(this._compare(i.key,n.key)>0){if(i.left===void 0)return i.left=n,this._isMapMode&&this.isRealNode(n)&&this._store.set(n.key,n),this._size++,this._updateCountAlongPath(n),!0;i.left!==null&&(i=i.left)}else{if(i.right===void 0)return i.right=n,this._isMapMode&&this.isRealNode(n)&&this._store.set(n.key,n),this._size++,this._updateCountAlongPath(n),!0;i.right!==null&&(i=i.right)}}return!1}setMany(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 a=r==null?void 0:r.next().value;this.isRaw(T)&&(T=this._toEntryFn(T)),s.push(this.set(T,a))}return s}let o=[],d=0;for(let T of e)o.push({key:T,value:r==null?void 0:r.next().value,orgIndex:d++});let u=o.sort(({key:T},{key:a})=>{let p,V;return this.isRaw(T)?p=this._toEntryFn(T)[0]:this.isEntry(T)?p=T[0]:this.isRealNode(T)?p=T.key:p=T,this.isRaw(a)?V=this._toEntryFn(a)[0]:this.isEntry(a)?V=a[0]:this.isRealNode(a)?V=a.key:V=a,p!=null&&V!=null?this._compare(p,V):0}),h=T=>{if(T.length===0)return;let a=Math.floor((T.length-1)/2),{key:p,value:V,orgIndex:N}=T[a];if(this.isRaw(p)){let y=this._toEntryFn(p);s[N]=this.set(y)}else s[N]=this.set(p,V);h(T.slice(0,a)),h(T.slice(a+1))};return i==="RECURSIVE"?h(u):(()=>{let a=[[0,u.length-1]];for(;a.length>0;){let p=a.pop();if(!p)continue;let[V,N]=p;if(V>N)continue;let y=V+Math.floor((N-V)/2),{key:C,value:M,orgIndex:R}=u[y];if(this.isRaw(C)){let _=this._toEntryFn(C);s[R]=this.set(_)}else s[R]=this.set(C,M);a.push([y+1,N]),a.push([V,y-1])}})(),s}ceiling(e,t=this._DEFAULT_NODE_CALLBACK,n){let i,s=this.iterationType;typeof t=="string"?s=t:t&&(i=t,n&&(s=n));let r=this._bound(e,!0,s);return i?r?i(r):void 0:r==null?void 0:r.key}higher(e,t=this._DEFAULT_NODE_CALLBACK,n){let i,s=this.iterationType;typeof t=="string"?s=t:t&&(i=t,n&&(s=n));let r=this._bound(e,!1,s);return i?r?i(r):void 0:r==null?void 0:r.key}floor(e,t=this._DEFAULT_NODE_CALLBACK,n){if(e==null)return void 0;let i,s=this.iterationType;if(typeof t=="string"?s=t:t&&(i=t,n&&(s=n)),this._isPredicate(e)){let o=this._floorByPredicate(e,s);return i?o?i(o):void 0:o==null?void 0:o.key}let r;if(this.isNode(e))r=e.key;else if(this.isEntry(e)){let o=e[0];if(o==null)return void 0;r=o}else r=e;if(r!==void 0){let o=this._floorByKey(r,s);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,s=this.iterationType;if(typeof t=="string"?s=t:t&&(i=t,n&&(s=n)),this._isPredicate(e)){let o=this._lowerByPredicate(e,s);return i?o?i(o):void 0:o==null?void 0:o.key}let r;if(this.isNode(e))r=e.key;else if(this.isEntry(e)){let o=e[0];if(o==null)return void 0;r=o}else r=e;if(r!==void 0){let o=this._lowerByKey(r,s);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 s=this.ensureNode(n),r=[];if(!this._root||!s)return r;let o=s.key;if(i==="RECURSIVE"){let d=u=>{let h=this._compare(u.key,o);Math.sign(h)==t&&r.push(e(u)),this.isRealNode(u.left)&&d(u.left),this.isRealNode(u.right)&&d(u.right)};return d(this._root),r}else{let d=new k([this._root]);for(;d.length>0;){let u=d.shift();if(this.isRealNode(u)){let h=this._compare(u.key,o);Math.sign(h)==t&&r.push(e(u)),this.isRealNode(u.left)&&d.push(u.left),this.isRealNode(u.right)&&d.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,o,d)=>{if(r>o)return;let u=r+(o-r>>1),h=t[u],c=i(r,u-1,h),T=i(u+1,o,h);return h.left=c,h.right=T,h.parent=d,h},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 o=i.left?r.get(i.left):-1,d=i.right?r.get(i.right):-1;if(Math.abs(o-d)>1)return!1;r.set(i,1+Math.max(o,d)),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,o]of this)i.set(e.call(n,o,r,s++,this));return i}deleteWhere(e,t=!1,n=this._root,i=this.iterationType){let s=this.search(e,t,o=>o,n,i),r=!1;for(let o of s)this.delete(o)&&(r=!0);return r}_createDefaultComparator(){return(e,t)=>{if(x(e)&&x(t))return e>t?1:e<t?-1:0;if(e instanceof Date&&t instanceof Date){let n=e.getTime(),i=t.getTime();return(Number.isNaN(n)||Number.isNaN(i))&&b(TypeError,E.invalidDate("BST")),n>i?1:n<i?-1:0}return(typeof e=="object"||typeof t=="object")&&b(TypeError,E.comparatorRequired("BST")),0}}_floorByKey(e,t){var n,i;if(t==="RECURSIVE"){let s=r=>{if(!this.isRealNode(r))return;if(this.comparator(r.key,e)<=0){let d=s(r.right);return d!=null?d:r}else return s(r.left)};return s(this.root)}else{let s=this.root,r;for(;this.isRealNode(s);)this.comparator(s.key,e)<=0?(r=s,s=(n=s.right)!=null?n:void 0):s=(i=s.left)!=null?i:void 0;return r}}_floorByPredicate(e,t){if(t==="RECURSIVE"){let n,i=s=>{this.isRealNode(s)&&(this.isRealNode(s.left)&&i(s.left),e(s)&&(n=s),this.isRealNode(s.right)&&i(s.right))};return i(this.root),n}else{let n=[],i=this.root,s;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;e(r)&&(s=r),i=r.right}return s}}_lowerByKey(e,t){var n,i;if(t==="RECURSIVE"){let s=r=>{if(!this.isRealNode(r))return;if(this.comparator(r.key,e)<0){let d=s(r.right);return d!=null?d:r}else return s(r.left)};return s(this.root)}else{let s=this.root,r;for(;this.isRealNode(s);)this.comparator(s.key,e)<0?(r=s,s=(n=s.right)!=null?n:void 0):s=(i=s.left)!=null?i:void 0;return r}}_lowerByPredicate(e,t){if(t==="RECURSIVE"){let n,i=s=>{this.isRealNode(s)&&(this.isRealNode(s.left)&&i(s.left),e(s)&&(n=s),this.isRealNode(s.right)&&i(s.right))};return i(this.root),n}else{let n=[],i=this.root,s;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;e(r)&&(s=r),i=r.right}return s}}_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 s=e[0];if(s==null)return;i=s}else i=e;if(i!==void 0)return this._boundByKey(i,t,n)}_boundByKey(e,t,n){var i,s;if(n==="RECURSIVE"){let r=o=>{if(!this.isRealNode(o))return;let d=this.comparator(o.key,e);if(t?d>=0:d>0){let h=r(o.left);return h!=null?h:o}else return r(o.right)};return r(this.root)}else{let r=this.root,o;for(;this.isRealNode(r);){let d=this.comparator(r.key,e);(t?d>=0:d>0)?(o=r,r=(i=r.left)!=null?i:void 0):r=(s=r.right)!=null?s:void 0}return o}}_boundByPredicate(e,t){if(t==="RECURSIVE"){let n,i=s=>{n||!this.isRealNode(s)||(this.isRealNode(s.left)&&i(s.left),!n&&e(s)&&(n=s),!n&&this.isRealNode(s.right)&&i(s.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 s=n.pop();if(!this.isRealNode(s))break;if(e(s))return s;i=s.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,enableOrderStatistic:this._enableOrderStatistic}}_keyValueNodeOrEntryToNodeAndValue(e,t){let[n,i]=super._keyValueNodeOrEntryToNodeAndValue(e,t);return n===null?[void 0,void 0]:[n,t!=null?t:i]}_updateCount(e){this._enableOrderStatistic&&(e._count=1+(this.isRealNode(e.left)?e.left._count:0)+(this.isRealNode(e.right)?e.right._count:0))}_updateCountAlongPath(e){if(!this._enableOrderStatistic)return;let t=e;for(;t;)this._updateCount(t),t=t.parent}_getByRankIterative(e,t){let n=e,i=t;for(;n;){let s=this.isRealNode(n.left)?n.left._count:0;if(i<s)n=n.left;else{if(i===s)return n;i=i-s-1,n=n.right}}}_getByRankRecursive(e,t){if(!e)return;let n=this.isRealNode(e.left)?e.left._count:0;return t<n?this._getByRankRecursive(e.left,t):t===n?e:this._getByRankRecursive(e.right,t-n-1)}_getRankIterative(e,t){let n=0,i=e;for(;this.isRealNode(i);){let s=this._compare(i.key,t);if(s>0)i=i.left;else if(s<0)n+=(this.isRealNode(i.left)?i.left._count:0)+1,i=i.right;else return n+=this.isRealNode(i.left)?i.left._count:0,n}return n}_getRankRecursive(e,t){if(!e)return 0;let n=this._compare(e.key,t);return n>0?this._getRankRecursive(e.left,t):n<0?(this.isRealNode(e.left)?e.left._count:0)+1+this._getRankRecursive(e.right,t):this.isRealNode(e.left)?e.left._count:0}_next(e){if(this.isRealNode(e.right)){let i=e.right;for(;this.isRealNode(i.left);)i=i.left;return i}let t=e,n=t.parent;for(;n&&t===n.right;)t=n,n=n.parent;return n}_setRoot(e){e&&(e.parent=void 0),this._root=e}_compare(e,t){return this._comparator(e,t)}_deleteByKey(e){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,o)=>{let d=r==null?void 0:r.parent;d?d.left===r?d.left=o:d.right=o:this._setRoot(o),o&&(o.parent=d)},i=r=>{if(r){for(;r.left!==void 0&&r.left!==null;)r=r.left;return r}},s;if(t.left===void 0)s=t.parent,n(t,t.right);else if(t.right===void 0)s=t.parent,n(t,t.left);else{let r=i(t.right);r.parent!==t?(s=r.parent,n(r,r.right),r.right=t.right,r.right&&(r.right.parent=r)):s=r,n(t,r),r.left=t.left,r.left&&(r.left.parent=r)}return this._updateCountAlongPath(s),this._size=Math.max(0,this._size-1),!0}};var A=class{constructor(l,e,t="BLACK"){K(this,"key");K(this,"value");K(this,"parent");K(this,"_left");K(this,"_right");K(this,"_height",0);K(this,"_color","BLACK");K(this,"_count",1);this.key=l,this.value=e,this.color=t}get left(){return this._left}set left(l){l&&(l.parent=this),this._left=l}get right(){return this._right}set right(l){l&&(l.parent=this),this._right=l}get height(){return this._height}set height(l){this._height=l}get color(){return this._color}set color(l){this._color=l}get count(){return this._count}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"}},j=class extends O{constructor(e=[],t){super([],t);K(this,"_root");K(this,"_header");K(this,"_minNode");K(this,"_maxNode");this._root=this.NIL,this._header=new A(void 0,void 0,"BLACK"),this._header.parent=this.NIL,this._header._left=this.NIL,this._header._right=this.NIL,e&&this.setMany(e)}get root(){return this._root}createNode(e,t,n="BLACK"){return new A(e,t,n)}isNode(e){return e instanceof A}clear(){super.clear(),this._root=this.NIL,this._header.parent=this.NIL,this._setMinCache(void 0),this._setMaxCache(void 0)}_findNodeByKey(e){var s,r,o;let t=this.NIL,n=this._compare.bind(this),i=(s=this._header.parent)!=null?s:t;for(;i!==t;){let d=n(e,i.key);if(d<0)i=(r=i.left)!=null?r:t;else if(d>0)i=(o=i.right)!=null?o:t;else return i}}_predecessorOf(e){let t=this.NIL;if(e.left&&e.left!==t){let s=e.left;for(;s.right&&s.right!==t;)s=s.right;return s}let n=e,i=e.parent;for(;i&&n===i.left;)n=i,i=i.parent;return i}_successorOf(e){let t=this.NIL;if(e.right&&e.right!==t){let s=e.right;for(;s.left&&s.left!==t;)s=s.left;return s}let n=e,i=e.parent;for(;i&&n===i.right;)n=i,i=i.parent;return i}_attachNewNode(e,t,n){let i=this.NIL;n.parent=e,t==="left"?e.left=n:e.right=n,n.left=i,n.right=i,n.color="RED",this._updateCountAlongPath(n),this._insertFixup(n),this.isRealNode(this._root)&&(this._root.color="BLACK")}_setMinCache(e){this._minNode=e,this._header._left=e!=null?e:this.NIL}_setMaxCache(e){this._maxNode=e,this._header._right=e!=null?e:this.NIL}_setKVNode(e,t){var N,y,C,M,R,_,g;let n=this.NIL,i=this._comparator,s=this._header,r=(N=s._left)!=null?N:n;if(r!==n){let B=i(e,r.key);if(B===0)return r.value=t,this._isMapMode&&this._store.set(e,r),{node:r,created:!1};let S=r.left;if(B<0&&(S===n||S===null||S===void 0)){let m=this.createNode(e,t);return this._attachNewNode(r,"left",m),this._isMapMode&&this._store.set(m.key,m),this._size++,this._setMinCache(m),s._right===n&&this._setMaxCache(m),{node:m,created:!0}}if(B>0){let m=(y=s._right)!=null?y:n,W=i(e,m.key);if(W===0)return m.value=t,this._isMapMode&&this._store.set(e,m),{node:m,created:!1};let $=m.right;if(W>0&&($===n||$===null||$===void 0)){let L=this.createNode(e,t);return this._attachNewNode(m,"right",L),this._isMapMode&&this._store.set(L.key,L),this._size++,this._setMaxCache(L),s._left===n&&this._setMinCache(L),{node:L,created:!0}}}}let o=i,d=this._isMapMode,u=this._store,h=(C=this._header.parent)!=null?C:n,c,T=0;for(;h!==n;)if(c=h,T=o(e,h.key),T<0)h=(M=h.left)!=null?M:n;else if(T>0)h=(R=h.right)!=null?R:n;else return h.value=t,d&&u.set(e,h),{node:h,created:!1};let a=this.createNode(e,t);if(a.parent=c,c?T<0?c.left=a:c.right=a:this._setRoot(a),a.left=n,a.right=n,a.color="RED",this._updateCountAlongPath(a),this._insertFixup(a),this.isRealNode(this._root))this._root.color="BLACK";else return;d&&u.set(a.key,a),this._size++;let p=(_=this._header._left)!=null?_:n,V=(g=this._header._right)!=null?g:n;return p===n||V===n?(this._setMinCache(a),this._setMaxCache(a)):c===V&&T>0?this._setMaxCache(a):c===p&&T<0?this._setMinCache(a):(o(a.key,p.key)<0&&this._setMinCache(a),o(a.key,V.key)>0&&this._setMaxCache(a)),{node:a,created:!0}}_setKV(e,t){if(this._isMapMode){let i=this._store.get(e);if(i)return i.value=t,!0}return this._setKVNode(e,t)!==void 0}setWithHintNode(e,t,n){var o,d,u,h,c,T,a,p,V,N,y,C,M;if(!n||!this.isRealNode(n))return(o=this._setKVNode(e,t))==null?void 0:o.node;let i=this._compare.bind(this),s=i(e,n.key);if(s===0)return n.value=t,this._isMapMode&&this._store.set(e,n),n;if(s<0){if(!this.isRealNode(n.left)){let _=this.createNode(e,t);if(!this.isRealNode(_))return;this._attachNewNode(n,"left",_),this._isMapMode&&this._store.set(e,_),this._size++;let g=this.NIL,B=(d=this._header._left)!=null?d:g;(B===g||this._compare(_.key,B.key)<0)&&this._setMinCache(_);let S=(u=this._header._right)!=null?u:g;return(S===g||this._compare(_.key,S.key)>0)&&this._setMaxCache(_),_}let R=this._predecessorOf(n);if(R&&i(R.key,e)>=0)return(h=this._setKVNode(e,t))==null?void 0:h.node;if(R&&!this.isRealNode(R.right)){let _=this.createNode(e,t);if(!this.isRealNode(_))return;this._attachNewNode(R,"right",_),this._isMapMode&&this._store.set(e,_),this._size++;let g=this.NIL,B=(c=this._header._left)!=null?c:g;(B===g||this._compare(_.key,B.key)<0)&&this._setMinCache(_);let S=(T=this._header._right)!=null?T:g;return(S===g||this._compare(_.key,S.key)>0)&&this._setMaxCache(_),_}return(a=this._setKVNode(e,t))==null?void 0:a.node}if(!this.isRealNode(n.right)){let R=this.createNode(e,t);if(!this.isRealNode(R))return;this._attachNewNode(n,"right",R),this._isMapMode&&this._store.set(e,R),this._size++;let _=this.NIL,g=(p=this._header._left)!=null?p:_;(g===_||this._compare(R.key,g.key)<0)&&this._setMinCache(R);let B=(V=this._header._right)!=null?V:_;return(B===_||this._compare(R.key,B.key)>0)&&this._setMaxCache(R),R}let r=this._successorOf(n);if(r&&i(r.key,e)<=0)return(N=this._setKVNode(e,t))==null?void 0:N.node;if(r&&!this.isRealNode(r.left)){let R=this.createNode(e,t);if(!this.isRealNode(R))return;this._attachNewNode(r,"left",R),this._isMapMode&&this._store.set(e,R),this._size++;let _=this.NIL,g=(y=this._header._left)!=null?y:_;(g===_||this._compare(R.key,g.key)<0)&&this._setMinCache(R);let B=(C=this._header._right)!=null?C:_;return(B===_||this._compare(R.key,B.key)>0)&&this._setMaxCache(R),R}return(M=this._setKVNode(e,t))==null?void 0:M.node}setWithHint(e,t,n){return this.setWithHintNode(e,t,n)!==void 0}set(e,t){if(!this.isNode(e)){if(e==null)return!1;if(this.isEntry(e)){let r=e[0];if(r==null)return!1;let o=t!=null?t:e[1];return this._setKV(r,o)}return this._setKV(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;if(this._isMapMode){let r=this.getNode(n.key);this.isRealNode(r)&&(r.value=i,this._store.set(r.key,r))}return this._size++,!0}if(s==="UPDATED"){if(this._isMapMode){let r=this.getNode(n.key);this.isRealNode(r)&&(r.value=i,this._store.set(r.key,r))}return!0}return!1}delete(e){var h,c,T;if(e===null)return!1;let t;if(this._isPredicate(e)?t=this.getNode(e):t=this.isRealNode(e)?e:this.getNode(e),!t)return!1;let n=t===this._minNode,i=t===this._maxNode,s=n?this._successorOf(t):void 0,r=i?this._predecessorOf(t):void 0,o=t.color,d=this.NIL,u=d;if(!this.isRealNode(t.left))u=(h=t.right)!=null?h:d,this._transplant(t,u);else if(!this.isRealNode(t.right))u=t.left,this._transplant(t,u);else{let a=this.getLeftMost(p=>p,t.right);a&&(o=a.color,u=(c=a.right)!=null?c:d,a.parent===t?u.parent=a:(this._transplant(a,u),a.right=t.right,a.right&&(a.right.parent=a)),this._transplant(t,a),a.left=t.left,a.left&&(a.left.parent=a),a.color=t.color)}return this._isMapMode&&this._store.delete(t.key),this._size--,this._updateCountAlongPath((T=u==null?void 0:u.parent)!=null?T:u),this._size<=0?(this._setMinCache(void 0),this._setMaxCache(void 0)):(n&&this._setMinCache(s),i&&this._setMaxCache(r),(!this._minNode||!this.isRealNode(this._minNode))&&this._setMinCache(this.isRealNode(this._root)?this.getLeftMost(a=>a,this._root):void 0),(!this._maxNode||!this.isRealNode(this._maxNode))&&this._setMaxCache(this.isRealNode(this._root)?this.getRightMost(a=>a,this._root):void 0)),o==="BLACK"&&this._deleteFixup(u),!0}perfectlyBalance(e){let t=[];for(let[n,i]of this)t.push([n,i]);return t.length<=1||(this.clear(),this.setMany(t.map(([n])=>n),t.map(([,n])=>n),!0)),!0}map(e,t,n){let i=this._createLike([],t),s=0;for(let[r,o]of this)i.set(e.call(n,o,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){let t=this.NIL;e&&(e.parent=void 0),this._root=e,this._header.parent=e!=null?e:t}_replaceNode(e,t){return t.color=e.color,super._replaceNode(e,t)}_insert(e){var o,d,u;let t=this.NIL,n=this._compare.bind(this),i=(o=this._header.parent)!=null?o:t,s,r=0;for(;i!==t;)if(s=i,r=n(e.key,i.key),r<0)i=(d=i.left)!=null?d:t;else if(r>0)i=(u=i.right)!=null?u:t;else return this._replaceNode(i,e),"UPDATED";return e.parent=s,s?r<0?s.left=e:s.right=e:this._setRoot(e),e.left=t,e.right=t,e.color="RED",this._updateCountAlongPath(e),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){let t=this._leftRotate.bind(this),n=this._rightRotate.bind(this);for(;e;){let i=e.parent;if(!i||i.color!=="RED")break;let s=i.parent;if(!s)break;if(i===s.left){let r=s.right;if((r==null?void 0:r.color)==="RED"){i.color="BLACK",r.color="BLACK",s.color="RED",e=s;continue}e===i.right&&(e=i,t(e));let o=e==null?void 0:e.parent,d=o==null?void 0:o.parent;o&&d&&(o.color="BLACK",d.color="RED",n(d))}else{let r=s.left;if((r==null?void 0:r.color)==="RED"){i.color="BLACK",r.color="BLACK",s.color="RED",e=s;continue}e===i.left&&(e=i,n(e));let o=e==null?void 0:e.parent,d=o==null?void 0:o.parent;o&&d&&(o.color="BLACK",d.color="RED",t(d))}break}this.isRealNode(this._root)&&(this._root.color="BLACK")}_deleteFixup(e){if(!e)return;let t=this.NIL,n=e;for(;n!==this.root&&n.color==="BLACK";){let i=n.parent;if(!i)break;let s=n===i.left,r=s?i.right:i.left;r&&r.color==="RED"&&(r.color="BLACK",i.color="RED",s?(this._leftRotate(i),r=i.right):(this._rightRotate(i),r=i.left));let o=r==null?void 0:r.left,d=r==null?void 0:r.right,u=!o||o===t||o.color==="BLACK",h=!d||d===t||d.color==="BLACK";u&&h?(r&&(r.color="RED"),n=i):(s?(h&&(o&&(o.color="BLACK"),r&&(r.color="RED"),r&&this._rightRotate(r),r=i.right),r&&(r.color=i.color),i.color="BLACK",r!=null&&r.right&&(r.right.color="BLACK"),this._leftRotate(i)):(u&&(d&&(d.color="BLACK"),r&&(r.color="RED"),r&&this._leftRotate(r),r=i.left),r&&(r.color=i.color),i.color="BLACK",r!=null&&r.left&&(r.left.color="BLACK"),this._rightRotate(i)),n=this.root)}n.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,this._updateCount(e),this._updateCount(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,this._updateCount(e),this._updateCount(t)}};return ie(oe);})();
/**
 * data-structure-typed
 *
 * @author Pablo Zeng
 * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
 * @license MIT License
 */
//# sourceMappingURL=red-black-tree-typed.min.js.map