bst-typed 2.5.2 → 2.5.3
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.
- package/dist/cjs/index.cjs +241 -59
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +241 -59
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +241 -59
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +241 -59
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +50 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +56 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +116 -15
- package/dist/types/data-structures/binary-tree/bst.d.ts +99 -3
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +79 -8
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +24 -0
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +520 -1
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +489 -1
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +393 -1
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +500 -1
- package/dist/types/data-structures/graph/directed-graph.d.ts +40 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +36 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +51 -6
- package/dist/types/data-structures/heap/heap.d.ts +98 -12
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +75 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +61 -1
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +72 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +32 -0
- package/dist/types/data-structures/queue/deque.d.ts +82 -0
- package/dist/types/data-structures/queue/queue.d.ts +61 -0
- package/dist/types/data-structures/stack/stack.d.ts +42 -2
- package/dist/types/data-structures/trie/trie.d.ts +48 -0
- package/dist/types/interfaces/binary-tree.d.ts +2 -3
- package/dist/umd/bst-typed.js +241 -59
- package/dist/umd/bst-typed.js.map +1 -1
- package/dist/umd/bst-typed.min.js +2 -2
- package/dist/umd/bst-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +52 -5
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +56 -0
- package/src/data-structures/binary-tree/binary-tree.ts +167 -81
- package/src/data-structures/binary-tree/bst.ts +101 -7
- package/src/data-structures/binary-tree/red-black-tree.ts +82 -15
- package/src/data-structures/binary-tree/segment-tree.ts +24 -0
- package/src/data-structures/binary-tree/tree-map.ts +540 -3
- package/src/data-structures/binary-tree/tree-multi-map.ts +490 -2
- package/src/data-structures/binary-tree/tree-multi-set.ts +393 -1
- package/src/data-structures/binary-tree/tree-set.ts +520 -3
- package/src/data-structures/graph/directed-graph.ts +41 -1
- package/src/data-structures/graph/undirected-graph.ts +37 -1
- package/src/data-structures/hash/hash-map.ts +67 -12
- package/src/data-structures/heap/heap.ts +107 -19
- package/src/data-structures/linked-list/doubly-linked-list.ts +88 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +61 -1
- package/src/data-structures/linked-list/skip-linked-list.ts +72 -0
- package/src/data-structures/matrix/matrix.ts +32 -0
- package/src/data-structures/queue/deque.ts +85 -0
- package/src/data-structures/queue/queue.ts +73 -0
- package/src/data-structures/stack/stack.ts +45 -5
- package/src/data-structures/trie/trie.ts +48 -0
- package/src/interfaces/binary-tree.ts +1 -9
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
"use strict";var bstTyped=(()=>{var I=Object.defineProperty;var P=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames;var $=Object.prototype.hasOwnProperty;var H=(a,d,e)=>d in a?I(a,d,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[d]=e;var W=(a,d)=>{for(var e in d)I(a,e,{get:d[e],enumerable:!0})},Q=(a,d,e,t)=>{if(d&&typeof d=="object"||typeof d=="function")for(let n of q(d))!$.call(a,n)&&n!==e&&I(a,n,{get:()=>d[n],enumerable:!(t=P(d,n))||t.enumerable});return a};var G=a=>Q(I({},"__esModule",{value:!0}),a);var T=(a,d,e)=>H(a,typeof d!="symbol"?d+"":d,e);var Z={};W(Z,{BST:()=>A,BSTNode:()=>C,BinaryTree:()=>S,BinaryTreeNode:()=>R,DFSOperation:()=>D,ERR:()=>g,Range:()=>m,raise:()=>b});function v(a){let d=typeof a;return d==="number"?!0:d==="bigint"||d==="string"||d==="boolean"}function O(a){if(typeof a.valueOf=="function"){let d=a.valueOf();if(d!==a){if(v(d))return d;if(typeof d=="object"&&d!==null)return O(d)}}if(typeof a.toString=="function"){let d=a.toString();if(d!=="[object Object]")return d}return null}function E(a,d=!1){if(a==null)return!1;if(v(a))return!0;if(typeof a!="object")return!1;if(a instanceof Date||d)return!0;let e=O(a);return e==null?!1:v(e)}var w=a=>({isThunk:!0,fn:a}),j=a=>typeof a=="object"&&a!==null&&"isThunk"in a&&a.isThunk;function X(a){let d=a;for(;j(d);)d=d.fn();return d}function M(a){return(...d)=>X(a(...d))}function b(a,d){throw new a(d)}var g={indexOutOfRange:(a,d,e,t)=>`${t?t+": ":""}Index ${a} is out of range [${d}, ${e}].`,invalidIndex:a=>`${a?a+": ":""}Index must be an integer.`,invalidArgument:(a,d)=>`${d?d+": ":""}${a}`,comparatorRequired:a=>`${a?a+": ":""}Comparator is required for non-number/non-string/non-Date keys.`,invalidKey:(a,d)=>`${d?d+": ":""}${a}`,notAFunction:(a,d)=>`${d?d+": ":""}${a} must be a function.`,invalidEntry:a=>`${a?a+": ":""}Each entry must be a [key, value] tuple.`,invalidNaN:a=>`${a?a+": ":""}NaN is not a valid key.`,invalidDate:a=>`${a?a+": ":""}Invalid Date key.`,reduceEmpty:a=>`${a?a+": ":""}Reduce of empty structure with no initial value.`,callbackReturnType:(a,d,e)=>`${e?e+": ":""}Callback must return ${a}; got ${d}.`,invalidOperation:(a,d)=>`${d?d+": ":""}${a}`,matrixDimensionMismatch:a=>`Matrix: Dimensions must be compatible for ${a}.`,matrixSingular:()=>"Matrix: Singular matrix, inverse does not exist.",matrixNotSquare:()=>"Matrix: Must be square for inversion.",matrixNotRectangular:()=>"Matrix: Must be rectangular for transposition.",matrixRowMismatch:(a,d)=>`Matrix: Expected row length ${a}, but got ${d}.`,orderStatisticNotEnabled:(a,d)=>`${d?d+": ":""}${a}() requires enableOrderStatistic: true.`};var D=(e=>(e[e.VISIT=0]="VISIT",e[e.PROCESS=1]="PROCESS",e))(D||{}),m=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 x=class{constructor(d){T(this,"_toElementFn");if(d){let{toElementFn:e}=d;typeof e=="function"?this._toElementFn=e:e&&b(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();r.done&&b(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 a extends x{constructor(e){super(e);T(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 a?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 L=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 V=class a extends B{constructor(e=[],t){super(t);T(this,"_elements",[]);T(this,"_offset",0);T(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 a(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 u of this)i.push(n===void 0?e(u,r++,this):e.call(n,u,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 u=t===void 0?e(o,r++,this):e.call(t,o,r++,this);i.push(u)}return i}_setAutoCompactRatio(e){this._autoCompactRatio=e}*_getIterator(){for(let e=this._offset;e<this.elements.length;e++)yield this.elements[e]}*_getReverseIterator(){for(let e=this.length-1;e>=0;e--){let t=this.at(e);t!==void 0&&(yield t)}}_createInstance(e){let t=this.constructor;return new t([],e)}_createLike(e=[],t){let n=this.constructor;return new n(e,t)}};var R=class{constructor(d,e){T(this,"key");T(this,"value");T(this,"parent");T(this,"_left");T(this,"_right");T(this,"_height",0);T(this,"_color","BLACK");T(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"}},S=class a extends L{constructor(e=[],t){super();T(this,"iterationType","ITERATIVE");T(this,"_isMapMode",!0);T(this,"_isDuplicate",!1);T(this,"_store",new Map);T(this,"_root");T(this,"_size",0);T(this,"_NIL",new R(NaN));T(this,"_toEntryFn");T(this,"_DEFAULT_NODE_CALLBACK",e=>e==null?void 0:e.key);if(t){let{iterationType:n,toEntryFn:i,isMapMode:r,isDuplicate:s}=t;n&&(this.iterationType=n),r!==void 0&&(this._isMapMode=r),s!==void 0&&(this._isDuplicate=s),typeof i=="function"?this._toEntryFn=i:i&&b(TypeError,g.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 R(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 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 m}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:E(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 V([this._root]),r;for(;i.length>0;){let s=i.shift();if(s){if(!this._isDuplicate&&n!==null&&s.key===n.key)return this._replaceNode(s,n),this._isMapMode&&n!==null&&this._store.set(s.key,n),!0;r===void 0&&(s.left===void 0||s.right===void 0)&&(r=s),s.left!==null&&s.left&&i.push(s.left),s.right!==null&&s.right&&i.push(s.right)}}return r?(r.left===void 0?r.left=n:r.right===void 0&&(r.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 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(u=>u,n.left);if(o){let u=o.parent;s=this._swapProperties(n,o),this._isMapMode&&(this._store.set(n.key,n),this._store.set(o.key,o)),u&&(u.right===o?u.right=o.left:u.left=o.left,r=u)}}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 u=l=>{s(l)&&(o.push(n(l)),t)||!this.isRealNode(l.left)&&!this.isRealNode(l.right)||(this.isRealNode(l.left)&&u(l.left),this.isRealNode(l.right)&&u(l.right))};u(i)}else{let u=[i];for(;u.length>0;){let l=u.pop();if(this.isRealNode(l)){if(s(l)&&(o.push(n(l)),t))return o;this.isRealNode(l.left)&&u.push(l.left),this.isRealNode(l.right)&&u.push(l.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){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,r;if(this._isMapMode){let s=this._extractKey(e);return s==null||(i=this._store.get(s))==null?void 0:i.value}return(r=this.getNode(e,t,n))==null?void 0:r.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,u,l)=>{if(!this.isRealNode(o))return!0;let h=Number(o.key);return h<=u||h>=l?!1:i(o.left,u,h)&&i(o.right,h,l)},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 u=[],l=o?Number.MAX_SAFE_INTEGER:Number.MIN_SAFE_INTEGER,h=n;for(;this.isRealNode(h)||u.length>0;){for(;this.isRealNode(h);)u.push(h),h=h.left;h=u.pop();let f=Number(h.key);if(!this.isRealNode(h)||!o&&l>=f||o&&l<=f)return!1;l=f,h=h.right}return!0},r=i(),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,u=this.isRealNode(i.right)?s.get(i.right):-1;s.set(i,1+Math.min(o,u)),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=M(s=>{let{left:o}=s;return this.isRealNode(o)?w(()=>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=M(r=>{let{right:s}=r;return this.isRealNode(s)?w(()=>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 V([t]),o=u=>{if(s.length===0)return;let l=s.shift();r.push(e(l)),i?(l&&this.isRealNodeOrNull(l.left)&&s.push(l.left),l&&this.isRealNodeOrNull(l.right)&&s.push(l.right)):(this.isRealNode(l.left)&&s.push(l.left),this.isRealNode(l.right)&&s.push(l.right)),o(u+1)};o(0)}else{let s=new V([t]);for(;s.length>0;){let o=s.length;for(let u=0;u<o;u++){let l=s.shift();r.push(e(l)),i?(l&&this.isRealNodeOrNull(l.left)&&s.push(l.left),l&&this.isRealNodeOrNull(l.right)&&s.push(l.right)):(this.isRealNode(l.left)&&s.push(l.left),this.isRealNode(l.right)&&s.push(l.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=[t];for(;r.length>0;){let s=r.pop();this.isRealNode(s)&&(this.isLeaf(s)&&i.push(e(s)),this.isRealNode(s.right)&&r.push(s.right),this.isRealNode(s.left)&&r.push(s.left))}}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,u)=>{r[u]||(r[u]=[]),r[u].push(e(o)),i?(o&&this.isRealNodeOrNull(o.left)&&s(o.left,u+1),o&&this.isRealNodeOrNull(o.right)&&s(o.right,u+1)):(o&&o.left&&s(o.left,u+1),o&&o.right&&s(o.right,u+1))};s(t,0)}else{let s=[[t,0]];for(;s.length>0;){let o=s.pop(),[u,l]=o;r[l]||(r[l]=[]),r[l].push(e(u)),i?(u&&this.isRealNodeOrNull(u.right)&&s.push([u.right,l+1]),u&&this.isRealNodeOrNull(u.left)&&s.push([u.left,l+1])):(u&&u.right&&s.push([u.right,l+1]),u&&u.left&&s.push([u.left,l+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=u=>{let l=null,h=null;for(;u;)h=u.right,u.right=l,l=u,u=h;return l},o=u=>{let l=s(u),h=l;for(;h;)i.push(e(h)),h=h.right;s(l)};switch(t){case"IN":for(;r;){if(r.left){let u=this.getPredecessor(r);if(u.right)u.right=null;else{u.right=r,r=r.left;continue}}i.push(e(r)),r=r.right}break;case"PRE":for(;r;){if(r.left){let u=this.getPredecessor(r);if(u.right)u.right=null;else{u.right=r,i.push(e(r)),r=r.left;continue}}else i.push(e(r));r=r.right}break;case"POST":for(;r;){if(r.left){let u=this.getPredecessor(r);if(u.right===null){u.right=r,r=r.left;continue}else u.right=null,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
|
|
1
|
+
"use strict";var bstTyped=(()=>{var I=Object.defineProperty;var P=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames;var $=Object.prototype.hasOwnProperty;var H=(a,d,e)=>d in a?I(a,d,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[d]=e;var W=(a,d)=>{for(var e in d)I(a,e,{get:d[e],enumerable:!0})},Q=(a,d,e,t)=>{if(d&&typeof d=="object"||typeof d=="function")for(let n of q(d))!$.call(a,n)&&n!==e&&I(a,n,{get:()=>d[n],enumerable:!(t=P(d,n))||t.enumerable});return a};var G=a=>Q(I({},"__esModule",{value:!0}),a);var T=(a,d,e)=>H(a,typeof d!="symbol"?d+"":d,e);var Z={};W(Z,{BST:()=>A,BSTNode:()=>C,BinaryTree:()=>S,BinaryTreeNode:()=>R,DFSOperation:()=>D,ERR:()=>g,Range:()=>m,raise:()=>K});function v(a){let d=typeof a;return d==="number"?!0:d==="bigint"||d==="string"||d==="boolean"}function O(a){if(typeof a.valueOf=="function"){let d=a.valueOf();if(d!==a){if(v(d))return d;if(typeof d=="object"&&d!==null)return O(d)}}if(typeof a.toString=="function"){let d=a.toString();if(d!=="[object Object]")return d}return null}function E(a,d=!1){if(a==null)return!1;if(v(a))return!0;if(typeof a!="object")return!1;if(a instanceof Date||d)return!0;let e=O(a);return e==null?!1:v(e)}var w=a=>({isThunk:!0,fn:a}),j=a=>typeof a=="object"&&a!==null&&"isThunk"in a&&a.isThunk;function X(a){let d=a;for(;j(d);)d=d.fn();return d}function M(a){return(...d)=>X(a(...d))}function K(a,d){throw new a(d)}var g={indexOutOfRange:(a,d,e,t)=>`${t?t+": ":""}Index ${a} is out of range [${d}, ${e}].`,invalidIndex:a=>`${a?a+": ":""}Index must be an integer.`,invalidArgument:(a,d)=>`${d?d+": ":""}${a}`,comparatorRequired:a=>`${a?a+": ":""}Comparator is required for non-number/non-string/non-Date keys.`,invalidKey:(a,d)=>`${d?d+": ":""}${a}`,notAFunction:(a,d)=>`${d?d+": ":""}${a} must be a function.`,invalidEntry:a=>`${a?a+": ":""}Each entry must be a [key, value] tuple.`,invalidNaN:a=>`${a?a+": ":""}NaN is not a valid key.`,invalidDate:a=>`${a?a+": ":""}Invalid Date key.`,reduceEmpty:a=>`${a?a+": ":""}Reduce of empty structure with no initial value.`,callbackReturnType:(a,d,e)=>`${e?e+": ":""}Callback must return ${a}; got ${d}.`,invalidOperation:(a,d)=>`${d?d+": ":""}${a}`,matrixDimensionMismatch:a=>`Matrix: Dimensions must be compatible for ${a}.`,matrixSingular:()=>"Matrix: Singular matrix, inverse does not exist.",matrixNotSquare:()=>"Matrix: Must be square for inversion.",matrixNotRectangular:()=>"Matrix: Must be rectangular for transposition.",matrixRowMismatch:(a,d)=>`Matrix: Expected row length ${a}, but got ${d}.`,orderStatisticNotEnabled:(a,d)=>`${d?d+": ":""}${a}() requires enableOrderStatistic: true.`};var D=(e=>(e[e.VISIT=0]="VISIT",e[e.PROCESS=1]="PROCESS",e))(D||{}),m=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 x=class{constructor(d){T(this,"_toElementFn");if(d){let{toElementFn:e}=d;typeof e=="function"?this._toElementFn=e:e&&K(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();r.done&&K(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 a extends x{constructor(e){super(e);T(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 a?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 L=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 V=class a extends B{constructor(e=[],t){super(t);T(this,"_elements",[]);T(this,"_offset",0);T(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 a(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,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 u of this)i.push(n===void 0?e(u,r++,this):e.call(n,u,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 u=t===void 0?e(o,r++,this):e.call(t,o,r++,this);i.push(u)}return i}_setAutoCompactRatio(e){this._autoCompactRatio=e}*_getIterator(){for(let e=this._offset;e<this.elements.length;e++)yield this.elements[e]}*_getReverseIterator(){for(let e=this.length-1;e>=0;e--){let t=this.at(e);t!==void 0&&(yield t)}}_createInstance(e){let t=this.constructor;return new t([],e)}_createLike(e=[],t){let n=this.constructor;return new n(e,t)}};var R=class{constructor(d,e){T(this,"key");T(this,"value");T(this,"parent");T(this,"_left");T(this,"_right");T(this,"_height",0);T(this,"_color","BLACK");T(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"}},S=class a extends L{constructor(e=[],t){super();T(this,"iterationType","ITERATIVE");T(this,"_isMapMode",!0);T(this,"_isDuplicate",!1);T(this,"_store",new Map);T(this,"_root");T(this,"_size",0);T(this,"_NIL",new R(NaN));T(this,"_toEntryFn");T(this,"_DEFAULT_NODE_CALLBACK",e=>e==null?void 0:e.key);if(t){let{iterationType:n,toEntryFn:i,isMapMode:r,isDuplicate:s}=t;n&&(this.iterationType=n),r!==void 0&&(this._isMapMode=r),s!==void 0&&(this._isDuplicate=s),typeof i=="function"?this._toEntryFn=i:i&&K(TypeError,g.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 R(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 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 m}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:E(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 V([this._root]),r;for(;i.length>0;){let s=i.shift();if(s){if(!this._isDuplicate&&n!==null&&s.key===n.key)return this._replaceNode(s,n),this._isMapMode&&n!==null&&this._store.set(s.key,n),!0;r===void 0&&(s.left===void 0||s.right===void 0)&&(r=s),s.left!==null&&s.left&&i.push(s.left),s.right!==null&&s.right&&i.push(s.right)}}return r?(r.left===void 0?r.left=n:r.right===void 0&&(r.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 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,[])}_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,r,s=n;if(!n.left&&!n.right&&!i)this._setRoot(void 0);else if(n.left){let o=this.getRightMost(u=>u,n.left);if(o){let u=o.parent;s=this._swapProperties(n,o),this._isMapMode&&(this._store.set(n.key,n),this._store.set(o.key,o)),u&&(u.right===o?u.right=o.left:u.left=o.left,r=u)}}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}delete(e){return this._deleteInternal(e).length>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=this._ensurePredicate(e),o=[];if(r==="RECURSIVE"){let u=l=>{s(l)&&(o.push(n(l)),t)||!this.isRealNode(l.left)&&!this.isRealNode(l.right)||(this.isRealNode(l.left)&&u(l.left),this.isRealNode(l.right)&&u(l.right))};u(i)}else{let u=[i];for(;u.length>0;){let l=u.pop();if(this.isRealNode(l)){if(s(l)&&(o.push(n(l)),t))return o;this.isRealNode(l.left)&&u.push(l.left),this.isRealNode(l.right)&&u.push(l.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){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,r;if(this._isMapMode){let s=this._extractKey(e);return s==null||(i=this._store.get(s))==null?void 0:i.value}return(r=this.getNode(e,t,n))==null?void 0:r.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,u,l)=>{if(!this.isRealNode(o))return!0;let h=Number(o.key);return h<=u||h>=l?!1:i(o.left,u,h)&&i(o.right,h,l)},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 u=[],l=o?Number.MAX_SAFE_INTEGER:Number.MIN_SAFE_INTEGER,h=n;for(;this.isRealNode(h)||u.length>0;){for(;this.isRealNode(h);)u.push(h),h=h.left;h=u.pop();let f=Number(h.key);if(!this.isRealNode(h)||!o&&l>=f||o&&l<=f)return!1;l=f,h=h.right}return!0},r=i(),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,u=this.isRealNode(i.right)?s.get(i.right):-1;s.set(i,1+Math.min(o,u)),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=M(s=>{let{left:o}=s;return this.isRealNode(o)?w(()=>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=M(r=>{let{right:s}=r;return this.isRealNode(s)?w(()=>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 V([t]),o=u=>{if(s.length===0)return;let l=s.shift();r.push(e(l)),i?(l&&this.isRealNodeOrNull(l.left)&&s.push(l.left),l&&this.isRealNodeOrNull(l.right)&&s.push(l.right)):(this.isRealNode(l.left)&&s.push(l.left),this.isRealNode(l.right)&&s.push(l.right)),o(u+1)};o(0)}else{let s=new V([t]);for(;s.length>0;){let o=s.length;for(let u=0;u<o;u++){let l=s.shift();r.push(e(l)),i?(l&&this.isRealNodeOrNull(l.left)&&s.push(l.left),l&&this.isRealNodeOrNull(l.right)&&s.push(l.right)):(this.isRealNode(l.left)&&s.push(l.left),this.isRealNode(l.right)&&s.push(l.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=[t];for(;r.length>0;){let s=r.pop();this.isRealNode(s)&&(this.isLeaf(s)&&i.push(e(s)),this.isRealNode(s.right)&&r.push(s.right),this.isRealNode(s.left)&&r.push(s.left))}}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,u)=>{r[u]||(r[u]=[]),r[u].push(e(o)),i?(o&&this.isRealNodeOrNull(o.left)&&s(o.left,u+1),o&&this.isRealNodeOrNull(o.right)&&s(o.right,u+1)):(o&&o.left&&s(o.left,u+1),o&&o.right&&s(o.right,u+1))};s(t,0)}else{let s=[[t,0]];for(;s.length>0;){let o=s.pop(),[u,l]=o;r[l]||(r[l]=[]),r[l].push(e(u)),i?(u&&this.isRealNodeOrNull(u.right)&&s.push([u.right,l+1]),u&&this.isRealNodeOrNull(u.left)&&s.push([u.left,l+1])):(u&&u.right&&s.push([u.right,l+1]),u&&u.left&&s.push([u.left,l+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=u=>{let l=null,h=null;for(;u;)h=u.right,u.right=l,l=u,u=h;return l},o=u=>{let l=s(u),h=l;for(;h;)i.push(e(h)),h=h.right;s(l)};switch(t){case"IN":for(;r;){if(r.left){let u=this.getPredecessor(r);if(u.right)u.right=null;else{u.right=r,r=r.left;continue}}i.push(e(r)),r=r.right}break;case"PRE":for(;r;){if(r.left){let u=this.getPredecessor(r);if(u.right)u.right=null;else{u.right=r,i.push(e(r)),r=r.left;continue}}else i.push(e(r));r=r.right}break;case"POST":for(;r;){if(r.left){let u=this.getPredecessor(r);if(u.right===null){u.right=r,r=r.left;continue}else u.right=null,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),u="";for(let l of o)u+=l+`
|
|
5
|
-
`;i+=u})(e)),i}print(e,t=this._root){console.log(this.toVisual(t,e))}_dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,r=this.iterationType,s=!1,o=f=>!!f,u=f=>!!f,l=f=>s?this.isRealNodeOrNull(f):this.isRealNode(f),h=f=>this.isRealNodeOrNull(f)){if(i=this.ensureNode(i),!i)return[];let f=[];if(r==="RECURSIVE"){let c=p=>{if(!l(p))return;let _=()=>{o(p)&&(p==null?void 0:p.left)!==void 0&&c(p==null?void 0:p.left)},K=()=>{u(p)&&(p==null?void 0:p.right)!==void 0&&c(p==null?void 0:p.right)};switch(t){case"IN":if(_(),h(p)&&(f.push(e(p)),n))return;K();break;case"PRE":if(h(p)&&(f.push(e(p)),n))return;_(),K();break;case"POST":if(_(),K(),h(p)&&(f.push(e(p)),n))return;break}};c(i)}else{let c=[{opt:0,node:i}],p=N=>{var y;o(N.node)&&c.push({opt:0,node:(y=N.node)==null?void 0:y.left})},_=N=>{var y;u(N.node)&&c.push({opt:0,node:(y=N.node)==null?void 0:y.right})},K=N=>{l(N.node)&&c.push({opt:1,node:N.node})};for(;c.length>0;){let N=c.pop();if(N!==void 0&&l(N.node))if(N.opt===1){if(h(N.node)&&N.node!==void 0&&(f.push(e(N.node)),n))return f}else switch(t){case"IN":_(N),K(N),p(N);break;case"PRE":_(N),p(N),K(N);break;case"POST":K(N),_(N),p(N);break}}}return f}*_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 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):e.set([t.key,t.value])},this._root,this.iterationType,!0)}_displayAux(e,t){let n=[["\u2500"],1,0,0],i=u=>({node:u,stage:0,leftLayout:n,rightLayout:n}),r=[i(e)],s=n,o=u=>{if(r.length===0){s=u;return}let l=r[r.length-1];l.stage===1?l.leftLayout=u:l.rightLayout=u};for(;r.length>0;){let u=r[r.length-1],l=u.node;if(u.stage===0){if(this._isDisplayLeaf(l,t)){r.pop();let h=this._resolveDisplayLeaf(l,t,n);o(h);continue}u.stage=1,r.push(i(l.left))}else if(u.stage===1)u.stage=2,r.push(i(l.right));else{r.pop();let h=this.isNIL(l)?"S":String(l.key),f=a._buildNodeDisplay(h,h.length,u.leftLayout,u.rightLayout);o(f)}}return s}static _buildNodeDisplay(e,t,n,i){let[r,s,o,u]=n,[l,h,f,c]=i,p=" ".repeat(Math.max(0,u+1))+"_".repeat(Math.max(0,s-u-1))+e+"_".repeat(Math.max(0,c))+" ".repeat(Math.max(0,h-c)),_=(o>0?" ".repeat(u)+"/"+" ".repeat(s-u-1):" ".repeat(s))+" ".repeat(t)+(f>0?" ".repeat(c)+"\\"+" ".repeat(h-c-1):" ".repeat(h)),K=[p,_];for(let N=0;N<Math.max(o,f);N++){let y=N<o?r[N]:" ".repeat(s),k=N<f?l[N]:" ".repeat(h);K.push(y+" ".repeat(t)+k)}return[K,s+t+h,Math.max(o,f)+2,s+Math.floor(t/2)]}_isDisplayLeaf(e,t){let{isShowNull:n,isShowUndefined:i,isShowRedBlackNIL:r}=t;if(e===null&&!n||e===void 0&&!i||this.isNIL(e)&&!r||e==null)return!0;let s=this._hasDisplayableChild(e.left,t),o=this._hasDisplayableChild(e.right,t);return!s&&!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:r,isShowRedBlackNIL:s}=t;if(e===null&&!i||e===void 0&&!r||this.isNIL(e)&&!s)return n;if(e!=null){let u=this.isNIL(e)?"S":String(e.key);return a._buildNodeDisplay(u,u.length,n,n)}let o=e===void 0?"U":"N";return a._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,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){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 C=class{constructor(d,e){T(this,"key");T(this,"value");T(this,"parent");T(this,"_left");T(this,"_right");T(this,"_height",0);T(this,"_color","BLACK");T(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"}},A=class extends S{constructor(e=[],t){super([],t);T(this,"_root");T(this,"_enableOrderStatistic",!1);T(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 C(e,t)}ensureNode(e,t=this.iterationType){var n;return(n=super.ensureNode(e,t))!=null?n:void 0}isNode(e){return e instanceof C}isValidKey(e){return E(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 l,h;if(e==null)return;if(this._isPredicate(e))return(l=this.getNodes(e,!0,t,n)[0])!=null?l:void 0;if(e instanceof m)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 f=e[0];if(f==null)return;i=f}else i=e;let r=this.ensureNode(t);if(!r)return;let s=this._NIL,o=r,u=this._comparator;for(;o&&o!==s;){let f=u(i,o.key);if(f===0)return o;o=f<0?o._left:o._right}}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.isRange(e),o=!s&&this._isPredicate(e);if(!s&&!o){let f;if(this.isNode(e))f=e.key;else if(this.isEntry(e)){let K=e[0];K!=null&&(f=K)}else f=e;if(f===void 0)return[];let c=this._NIL,p=this._comparator,_=i;for(;_&&_!==c;){let K=p(f,_.key);if(K===0)return[n(_)];_=K<0?_._left:_._right}return[]}let u;s?u=f=>f?e.isInRange(f.key,this._comparator):!1:u=this._ensurePredicate(e);let l=f=>{if(!f||!this.isRealNode(f.left))return!1;if(s){let c=e,p=c.low,_=c.includeLow;return _&&this._compare(f.key,p)>=0||!_&&this._compare(f.key,p)>0}if(!s&&!this._isPredicate(e)){let c=this._extractKey(e);return c!=null&&this._compare(f.key,c)>0}return!0},h=f=>{if(!f||!this.isRealNode(f.right))return!1;if(s){let c=e,p=c.high,_=c.includeHigh;return _&&this._compare(f.key,p)<=0||!_&&this._compare(f.key,p)<0}if(!s&&!this._isPredicate(e)){let c=this._extractKey(e);return c!=null&&this._compare(f.key,c)<0}return!0};return super._dfs(n,"IN",t,i,r,!1,l,h,()=>!0,f=>!!f&&u(f))}rangeSearch(e,t=this._DEFAULT_NODE_CALLBACK,n=this._root,i=this.iterationType){let r=e instanceof m?e:new m(e[0],e[1]);return this.search(r,!1,t,n,i)}getByRank(e,t=this._DEFAULT_NODE_CALLBACK,n=this.iterationType){if(this._enableOrderStatistic||b(Error,g.orderStatisticNotEnabled("getByRank")),e<0||e>=this._size)return;let i,r=this.iterationType;typeof t=="string"?r=t:t&&(i=t,n&&(r=n));let s=r==="RECURSIVE"?this._getByRankRecursive(this._root,e):this._getByRankIterative(this._root,e);if(s)return i?i(s):s.key}getRank(e,t=this.iterationType){var r;if(this._enableOrderStatistic||b(Error,g.orderStatisticNotEnabled("getRank")),!this._root||this._size===0)return-1;let n=this.iterationType;t&&(n=t);let i;if(typeof e=="function"){let s=this.search(e,!0);if(s.length===0||s[0]===void 0)return-1;i=s[0]}else{if(e==null)return-1;if(this.isNode(e))i=e.key;else if(Array.isArray(e)){if(i=(r=e[0])!=null?r: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,g.orderStatisticNotEnabled("rangeByRank")),this._size===0)return[];let r=Math.max(0,e),s=Math.min(this._size-1,t);if(r>s)return[];let o,u=this.iterationType;typeof n=="string"?u=n:n&&(o=n,i&&(u=i));let l=[],h=s-r+1,f=u==="RECURSIVE"?this._getByRankRecursive(this._root,r):this._getByRankIterative(this._root,r);if(!f)return[];let c=0,p=o!=null?o:this._DEFAULT_NODE_CALLBACK,_=f;for(;_&&c<h;)l.push(p(_)),c++,c<h&&(_=this._next(_));return l}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 r=[],s=t==null?void 0:t[Symbol.iterator]();if(!n){for(let c of e){let p=s==null?void 0:s.next().value;this.isRaw(c)&&(c=this._toEntryFn(c)),r.push(this.set(c,p))}return r}let o=[],u=0;for(let c of e)o.push({key:c,value:s==null?void 0:s.next().value,orgIndex:u++});let l=o.sort(({key:c},{key:p})=>{let _,K;return this.isRaw(c)?_=this._toEntryFn(c)[0]:this.isEntry(c)?_=c[0]:this.isRealNode(c)?_=c.key:_=c,this.isRaw(p)?K=this._toEntryFn(p)[0]:this.isEntry(p)?K=p[0]:this.isRealNode(p)?K=p.key:K=p,_!=null&&K!=null?this._compare(_,K):0}),h=c=>{if(c.length===0)return;let p=Math.floor((c.length-1)/2),{key:_,value:K,orgIndex:N}=c[p];if(this.isRaw(_)){let y=this._toEntryFn(_);r[N]=this.set(y)}else r[N]=this.set(_,K);h(c.slice(0,p)),h(c.slice(p+1))};return i==="RECURSIVE"?h(l):(()=>{let p=[[0,l.length-1]];for(;p.length>0;){let _=p.pop();if(!_)continue;let[K,N]=_;if(K>N)continue;let y=K+Math.floor((N-K)/2),{key:k,value:z,orgIndex:F}=l[y];if(this.isRaw(k)){let U=this._toEntryFn(k);r[F]=this.set(U)}else r[F]=this.set(k,z);p.push([y+1,N]),p.push([K,y-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 u=l=>{let h=this._compare(l.key,o);Math.sign(h)==t&&s.push(e(l)),this.isRealNode(l.left)&&u(l.left),this.isRealNode(l.right)&&u(l.right)};return u(this._root),s}else{let u=new V([this._root]);for(;u.length>0;){let l=u.shift();if(this.isRealNode(l)){let h=this._compare(l.key,o);Math.sign(h)==t&&s.push(e(l)),this.isRealNode(l.left)&&u.push(l.left),this.isRealNode(l.right)&&u.push(l.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,u)=>{if(s>o)return;let l=s+(o-s>>1),h=t[l],f=i(s,l-1,h),c=i(l+1,o,h);return h.left=f,h.right=c,h.parent=u,h},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,u=i.right?s.get(i.right):-1;if(Math.abs(o-u)>1)return!1;s.set(i,1+Math.max(o,u)),r=i,i=void 0}}else i=i.right}return t}map(e,t,n){let i=this._createLike([],t),r=0;for(let[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 u=this.delete(o);s=s.concat(u)}return s}_createDefaultComparator(){return(e,t)=>{if(E(e)&&E(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,g.invalidDate("BST")),n>i?1:n<i?-1:0}return(typeof e=="object"||typeof t=="object")&&b(TypeError,g.comparatorRequired("BST")),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 u=r(s.right);return u!=null?u: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 u=r(s.right);return u!=null?u: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 u=this.comparator(o.key,e);if(t?u>=0:u>0){let h=s(o.left);return h!=null?h:o}else return s(o.right)};return s(this.root)}else{let s=this.root,o;for(;this.isRealNode(s);){let u=this.comparator(s.key,e);(t?u>=0:u>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,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 r=this.isRealNode(n.left)?n.left._count:0;if(i<r)n=n.left;else{if(i===r)return n;i=i-r-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 r=this._compare(i.key,t);if(r>0)i=i.left;else if(r<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 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 u=s==null?void 0:s.parent;u?u.left===s?u.left=o:u.right=o:this._setRoot(o),o&&(o.parent=u)},i=s=>{if(s){for(;s.left!==void 0&&s.left!==null;)s=s.left;return s}},r;if(t.left===void 0)r=t.parent,n(t,t.right);else if(t.right===void 0)r=t.parent,n(t,t.left);else{let s=i(t.right);s.parent!==t?(r=s.parent,n(s,s.right),s.right=t.right,s.right&&(s.right.parent=s)):r=s,n(t,s),s.left=t.left,s.left&&(s.left.parent=s)}return this._updateCountAlongPath(r),this._size=Math.max(0,this._size-1),!0}};return G(Z);})();
|
|
5
|
+
`;i+=u})(e)),i}print(e,t=this._root){console.log(this.toVisual(t,e))}_dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,r=this.iterationType,s=!1,o=f=>!!f,u=f=>!!f,l=f=>s?this.isRealNodeOrNull(f):this.isRealNode(f),h=f=>this.isRealNodeOrNull(f)){if(i=this.ensureNode(i),!i)return[];let f=[];if(r==="RECURSIVE"){let c=p=>{if(!l(p))return;let _=()=>{o(p)&&(p==null?void 0:p.left)!==void 0&&c(p==null?void 0:p.left)},b=()=>{u(p)&&(p==null?void 0:p.right)!==void 0&&c(p==null?void 0:p.right)};switch(t){case"IN":if(_(),h(p)&&(f.push(e(p)),n))return;b();break;case"PRE":if(h(p)&&(f.push(e(p)),n))return;_(),b();break;case"POST":if(_(),b(),h(p)&&(f.push(e(p)),n))return;break}};c(i)}else{let c=[{opt:0,node:i}],p=N=>{var y;o(N.node)&&c.push({opt:0,node:(y=N.node)==null?void 0:y.left})},_=N=>{var y;u(N.node)&&c.push({opt:0,node:(y=N.node)==null?void 0:y.right})},b=N=>{l(N.node)&&c.push({opt:1,node:N.node})};for(;c.length>0;){let N=c.pop();if(N!==void 0&&l(N.node))if(N.opt===1){if(h(N.node)&&N.node!==void 0&&(f.push(e(N.node)),n))return f}else switch(t){case"IN":_(N),b(N),p(N);break;case"PRE":_(N),p(N),b(N);break;case"POST":b(N),_(N),p(N);break}}}return f}*_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 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):e.set([t.key,t.value])},this._root,this.iterationType,!0)}_displayAux(e,t){let n=[["\u2500"],1,0,0],i=u=>({node:u,stage:0,leftLayout:n,rightLayout:n}),r=[i(e)],s=n,o=u=>{if(r.length===0){s=u;return}let l=r[r.length-1];l.stage===1?l.leftLayout=u:l.rightLayout=u};for(;r.length>0;){let u=r[r.length-1],l=u.node;if(u.stage===0){if(this._isDisplayLeaf(l,t)){r.pop();let h=this._resolveDisplayLeaf(l,t,n);o(h);continue}u.stage=1,r.push(i(l.left))}else if(u.stage===1)u.stage=2,r.push(i(l.right));else{r.pop();let h=this.isNIL(l)?"S":String(l.key),f=a._buildNodeDisplay(h,h.length,u.leftLayout,u.rightLayout);o(f)}}return s}static _buildNodeDisplay(e,t,n,i){let[r,s,o,u]=n,[l,h,f,c]=i,p=" ".repeat(Math.max(0,u+1))+"_".repeat(Math.max(0,s-u-1))+e+"_".repeat(Math.max(0,c))+" ".repeat(Math.max(0,h-c)),_=(o>0?" ".repeat(u)+"/"+" ".repeat(s-u-1):" ".repeat(s))+" ".repeat(t)+(f>0?" ".repeat(c)+"\\"+" ".repeat(h-c-1):" ".repeat(h)),b=[p,_];for(let N=0;N<Math.max(o,f);N++){let y=N<o?r[N]:" ".repeat(s),k=N<f?l[N]:" ".repeat(h);b.push(y+" ".repeat(t)+k)}return[b,s+t+h,Math.max(o,f)+2,s+Math.floor(t/2)]}_isDisplayLeaf(e,t){let{isShowNull:n,isShowUndefined:i,isShowRedBlackNIL:r}=t;if(e===null&&!n||e===void 0&&!i||this.isNIL(e)&&!r||e==null)return!0;let s=this._hasDisplayableChild(e.left,t),o=this._hasDisplayableChild(e.right,t);return!s&&!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:r,isShowRedBlackNIL:s}=t;if(e===null&&!i||e===void 0&&!r||this.isNIL(e)&&!s)return n;if(e!=null){let u=this.isNIL(e)?"S":String(e.key);return a._buildNodeDisplay(u,u.length,n,n)}let o=e===void 0?"U":"N";return a._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,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){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 C=class{constructor(d,e){T(this,"key");T(this,"value");T(this,"parent");T(this,"_left");T(this,"_right");T(this,"_height",0);T(this,"_color","BLACK");T(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"}},A=class extends S{constructor(e=[],t){super([],t);T(this,"_root");T(this,"_enableOrderStatistic",!1);T(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 C(e,t)}ensureNode(e,t=this.iterationType){var n;return(n=super.ensureNode(e,t))!=null?n:void 0}isNode(e){return e instanceof C}isValidKey(e){return E(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 l,h;if(e==null)return;if(this._isPredicate(e))return(l=this.getNodes(e,!0,t,n)[0])!=null?l:void 0;if(e instanceof m)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 f=e[0];if(f==null)return;i=f}else i=e;let r=this.ensureNode(t);if(!r)return;let s=this._NIL,o=r,u=this._comparator;for(;o&&o!==s;){let f=u(i,o.key);if(f===0)return o;o=f<0?o._left:o._right}}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.isRange(e),o=!s&&this._isPredicate(e);if(!s&&!o){let f;if(this.isNode(e))f=e.key;else if(this.isEntry(e)){let b=e[0];b!=null&&(f=b)}else f=e;if(f===void 0)return[];let c=this._NIL,p=this._comparator,_=i;for(;_&&_!==c;){let b=p(f,_.key);if(b===0)return[n(_)];_=b<0?_._left:_._right}return[]}let u;s?u=f=>f?e.isInRange(f.key,this._comparator):!1:u=this._ensurePredicate(e);let l=f=>{if(!f||!this.isRealNode(f.left))return!1;if(s){let c=e,p=c.low,_=c.includeLow;return _&&this._compare(f.key,p)>=0||!_&&this._compare(f.key,p)>0}if(!s&&!this._isPredicate(e)){let c=this._extractKey(e);return c!=null&&this._compare(f.key,c)>0}return!0},h=f=>{if(!f||!this.isRealNode(f.right))return!1;if(s){let c=e,p=c.high,_=c.includeHigh;return _&&this._compare(f.key,p)<=0||!_&&this._compare(f.key,p)<0}if(!s&&!this._isPredicate(e)){let c=this._extractKey(e);return c!=null&&this._compare(f.key,c)<0}return!0};return super._dfs(n,"IN",t,i,r,!1,l,h,()=>!0,f=>!!f&&u(f))}rangeSearch(e,t=this._DEFAULT_NODE_CALLBACK,n=this._root,i=this.iterationType){let r=e instanceof m?e:new m(e[0],e[1]);return this.search(r,!1,t,n,i)}getByRank(e,t=this._DEFAULT_NODE_CALLBACK,n=this.iterationType){if(this._enableOrderStatistic||K(Error,g.orderStatisticNotEnabled("getByRank")),e<0||e>=this._size)return;let i,r=this.iterationType;typeof t=="string"?r=t:t&&(i=t,n&&(r=n));let s=r==="RECURSIVE"?this._getByRankRecursive(this._root,e):this._getByRankIterative(this._root,e);if(s)return i?i(s):s.key}getRank(e,t=this.iterationType){var r;if(this._enableOrderStatistic||K(Error,g.orderStatisticNotEnabled("getRank")),!this._root||this._size===0)return-1;let n=this.iterationType;t&&(n=t);let i;if(typeof e=="function"){let s=this.search(e,!0);if(s.length===0||s[0]===void 0)return-1;i=s[0]}else{if(e==null)return-1;if(this.isNode(e))i=e.key;else if(Array.isArray(e)){if(i=(r=e[0])!=null?r: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||K(Error,g.orderStatisticNotEnabled("rangeByRank")),this._size===0)return[];let r=Math.max(0,e),s=Math.min(this._size-1,t);if(r>s)return[];let o,u=this.iterationType;typeof n=="string"?u=n:n&&(o=n,i&&(u=i));let l=[],h=s-r+1,f=u==="RECURSIVE"?this._getByRankRecursive(this._root,r):this._getByRankIterative(this._root,r);if(!f)return[];let c=0,p=o!=null?o:this._DEFAULT_NODE_CALLBACK,_=f;for(;_&&c<h;)l.push(p(_)),c++,c<h&&(_=this._next(_));return l}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 r=[],s=t==null?void 0:t[Symbol.iterator]();if(!n){for(let c of e){let p=s==null?void 0:s.next().value;this.isRaw(c)&&(c=this._toEntryFn(c)),r.push(this.set(c,p))}return r}let o=[],u=0;for(let c of e)o.push({key:c,value:s==null?void 0:s.next().value,orgIndex:u++});let l=o.sort(({key:c},{key:p})=>{let _,b;return this.isRaw(c)?_=this._toEntryFn(c)[0]:this.isEntry(c)?_=c[0]:this.isRealNode(c)?_=c.key:_=c,this.isRaw(p)?b=this._toEntryFn(p)[0]:this.isEntry(p)?b=p[0]:this.isRealNode(p)?b=p.key:b=p,_!=null&&b!=null?this._compare(_,b):0}),h=c=>{if(c.length===0)return;let p=Math.floor((c.length-1)/2),{key:_,value:b,orgIndex:N}=c[p];if(this.isRaw(_)){let y=this._toEntryFn(_);r[N]=this.set(y)}else r[N]=this.set(_,b);h(c.slice(0,p)),h(c.slice(p+1))};return i==="RECURSIVE"?h(l):(()=>{let p=[[0,l.length-1]];for(;p.length>0;){let _=p.pop();if(!_)continue;let[b,N]=_;if(b>N)continue;let y=b+Math.floor((N-b)/2),{key:k,value:z,orgIndex:F}=l[y];if(this.isRaw(k)){let U=this._toEntryFn(k);r[F]=this.set(U)}else r[F]=this.set(k,z);p.push([y+1,N]),p.push([b,y-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 u=l=>{let h=this._compare(l.key,o);Math.sign(h)==t&&s.push(e(l)),this.isRealNode(l.left)&&u(l.left),this.isRealNode(l.right)&&u(l.right)};return u(this._root),s}else{let u=new V([this._root]);for(;u.length>0;){let l=u.shift();if(this.isRealNode(l)){let h=this._compare(l.key,o);Math.sign(h)==t&&s.push(e(l)),this.isRealNode(l.left)&&u.push(l.left),this.isRealNode(l.right)&&u.push(l.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,u)=>{if(s>o)return;let l=s+(o-s>>1),h=t[l],f=i(s,l-1,h),c=i(l+1,o,h);return h.left=f,h.right=c,h.parent=u,h},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,u=i.right?s.get(i.right):-1;if(Math.abs(o-u)>1)return!1;s.set(i,1+Math.max(o,u)),r=i,i=void 0}}else i=i.right}return t}map(e,t,n){let i=this._createLike([],t),r=0;for(let[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=!1;for(let o of r)this.delete(o)&&(s=!0);return s}_createDefaultComparator(){return(e,t)=>{if(E(e)&&E(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))&&K(TypeError,g.invalidDate("BST")),n>i?1:n<i?-1:0}return(typeof e=="object"||typeof t=="object")&&K(TypeError,g.comparatorRequired("BST")),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 u=r(s.right);return u!=null?u: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 u=r(s.right);return u!=null?u: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 u=this.comparator(o.key,e);if(t?u>=0:u>0){let h=s(o.left);return h!=null?h:o}else return s(o.right)};return s(this.root)}else{let s=this.root,o;for(;this.isRealNode(s);){let u=this.comparator(s.key,e);(t?u>=0:u>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,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 r=this.isRealNode(n.left)?n.left._count:0;if(i<r)n=n.left;else{if(i===r)return n;i=i-r-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 r=this._compare(i.key,t);if(r>0)i=i.left;else if(r<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 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 u=s==null?void 0:s.parent;u?u.left===s?u.left=o:u.right=o:this._setRoot(o),o&&(o.parent=u)},i=s=>{if(s){for(;s.left!==void 0&&s.left!==null;)s=s.left;return s}},r;if(t.left===void 0)r=t.parent,n(t,t.right);else if(t.right===void 0)r=t.parent,n(t,t.left);else{let s=i(t.right);s.parent!==t?(r=s.parent,n(s,s.right),s.right=t.right,s.right&&(s.right.parent=s)):r=s,n(t,s),s.left=t.left,s.left&&(s.left.parent=s)}return this._updateCountAlongPath(r),this._size=Math.max(0,this._size-1),!0}};return G(Z);})();
|
|
6
6
|
/**
|
|
7
7
|
* data-structure-typed
|
|
8
8
|
*
|