tree-multimap-typed 2.2.2 → 2.2.4
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 +245 -72
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +246 -72
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +245 -72
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +246 -72
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +98 -5
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +103 -7
- package/dist/types/data-structures/binary-tree/bst.d.ts +202 -39
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +86 -37
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +7 -7
- package/dist/types/data-structures/graph/directed-graph.d.ts +126 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +160 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +110 -27
- package/dist/types/data-structures/heap/heap.d.ts +107 -58
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +72 -404
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -5
- package/dist/types/data-structures/queue/deque.d.ts +95 -67
- package/dist/types/data-structures/queue/queue.d.ts +90 -34
- package/dist/types/data-structures/stack/stack.d.ts +58 -40
- package/dist/types/data-structures/trie/trie.d.ts +109 -47
- package/dist/types/interfaces/binary-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
- package/dist/umd/tree-multimap-typed.js +246 -72
- package/dist/umd/tree-multimap-typed.js.map +1 -1
- package/dist/umd/tree-multimap-typed.min.js +3 -3
- package/dist/umd/tree-multimap-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -8
- package/src/data-structures/binary-tree/avl-tree.ts +100 -7
- package/src/data-structures/binary-tree/binary-tree.ts +117 -7
- package/src/data-structures/binary-tree/bst.ts +431 -93
- package/src/data-structures/binary-tree/red-black-tree.ts +85 -37
- package/src/data-structures/binary-tree/tree-counter.ts +5 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +9 -10
- package/src/data-structures/graph/directed-graph.ts +126 -1
- package/src/data-structures/graph/undirected-graph.ts +160 -1
- package/src/data-structures/hash/hash-map.ts +110 -27
- package/src/data-structures/heap/heap.ts +107 -58
- package/src/data-structures/linked-list/doubly-linked-list.ts +72 -404
- package/src/data-structures/linked-list/singly-linked-list.ts +121 -5
- package/src/data-structures/queue/deque.ts +95 -67
- package/src/data-structures/queue/queue.ts +90 -34
- package/src/data-structures/stack/stack.ts +58 -40
- package/src/data-structures/trie/trie.ts +109 -47
- package/src/interfaces/binary-tree.ts +2 -0
- package/src/types/data-structures/binary-tree/bst.ts +5 -5
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
"use strict";var treeMultimapTyped=(()=>{var w=Object.defineProperty;var X=Object.getOwnPropertyDescriptor;var J=Object.getOwnPropertyNames;var Y=Object.prototype.hasOwnProperty;var Z=(c,l,e)=>l in c?w(c,l,{enumerable:!0,configurable:!0,writable:!0,value:e}):c[l]=e;var $=(c,l)=>{for(var e in l)w(c,e,{get:l[e],enumerable:!0})},z=(c,l,e,t)=>{if(l&&typeof l=="object"||typeof l=="function")for(let n of J(l))!Y.call(c,n)&&n!==e&&w(c,n,{get:()=>l[n],enumerable:!(t=X(l,n))||t.enumerable});return c};var ee=c=>z(w({},"__esModule",{value:!0}),c);var f=(c,l,e)=>Z(c,typeof l!="symbol"?l+"":l,e);var ie={};$(ie,{BST:()=>M,BSTNode:()=>k,BinaryTree:()=>C,BinaryTreeNode:()=>E,DFSOperation:()=>H,Range:()=>_,RedBlackTree:()=>L,RedBlackTreeNode:()=>I,TreeMultiMap:()=>G,TreeMultiMapNode:()=>x});function F(c){let l=typeof c;return l==="number"?!0:l==="bigint"||l==="string"||l==="boolean"}function j(c){if(typeof c.valueOf=="function"){let l=c.valueOf();if(l!==c){if(F(l))return l;if(typeof l=="object"&&l!==null)return j(l)}}if(typeof c.toString=="function"){let l=c.toString();if(l!=="[object Object]")return l}return null}function y(c,l=!1){if(c==null)return!1;if(F(c))return!0;if(typeof c!="object")return!1;if(c instanceof Date||l)return!0;let e=j(c);return e==null?!1:F(e)}var P=c=>({isThunk:!0,fn:c}),te=c=>typeof c=="object"&&c!==null&&"isThunk"in c&&c.isThunk;function ne(c){let l=c;for(;te(l);)l=l.fn();return l}function U(c){return(...l)=>ne(c(...l))}var O=class{constructor(l){f(this,"_toElementFn");if(l){let{toElementFn:e}=l;if(typeof e=="function")this._toElementFn=e;else if(e)throw new TypeError("toElementFn must be a function type")}}get toElementFn(){return this._toElementFn}*[Symbol.iterator](...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}reduce(l,e){let t=0,n=this[Symbol.iterator](),i;if(arguments.length>=2)i=e;else{let s=n.next();if(s.done)throw new TypeError("Reduce of empty structure with no initial value");i=s.value,t=1}for(let s of n)i=l(i,s,t++,this);return i}toArray(){return[...this]}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var A=class c extends O{constructor(e){super(e);f(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 c?t.pushMany(n):t.push(n);return t}sort(e){let t=this.toArray();t.sort(e),this.clear();for(let n of t)this.push(n);return this}splice(e,t=0,...n){let i=this._createInstance();e=e<0?this.length+e:e,e=Math.max(0,Math.min(e,this.length)),t=Math.max(0,Math.min(t,this.length-e));for(let s=0;s<t;s++){let r=this.deleteAt(e);r!==void 0&&i.push(r)}for(let s=0;s<n.length;s++)this.addAt(e+s,n[s]);return i}join(e=","){return this.toArray().join(e)}toReversedArray(){let e=[];for(let t=this.length-1;t>=0;t--)e.push(this.at(t));return e}reduceRight(e,t){let n=t!=null?t:0;for(let i=this.length-1;i>=0;i--)n=e(n,this.at(i),i,this);return n}slice(e=0,t=this.length){e=e<0?this.length+e:e,t=t<0?this.length+t:t;let n=this._createInstance();for(let i=e;i<t;i++)n.push(this.at(i));return n}fill(e,t=0,n=this.length){if(t=t<0?this.length+t:t,n=n<0?this.length+n:n,t<0&&(t=0),n>this.length&&(n=this.length),t>=n)return this;for(let i=t;i<n;i++)this.setAt(i,e);return this}};var m=class c extends A{constructor(e=[],t){super(t);f(this,"_elements",[]);f(this,"_offset",0);f(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 c(e)}isEmpty(){return this.length===0}push(e){return this.elements.push(e),this._maxLen>0&&this.length>this._maxLen&&this.shift(),!0}pushMany(e){let t=[];for(let n of e)this.toElementFn?t.push(this.push(this.toElementFn(n))):t.push(this.push(n));return t}shift(){if(this.length===0)return;let e=this.first;return this._offset+=1,this.elements.length>0&&this.offset/this.elements.length>this.autoCompactRatio&&this.compact(),e}delete(e){for(let t=this._offset;t<this.elements.length;t++)if(Object.is(this.elements[t],e))return this.elements.splice(t,1),!0;return!1}at(e){if(!(e<0||e>=this.length))return this._elements[this._offset+e]}deleteAt(e){if(e<0||e>=this.length)return;let t=this._offset+e,[n]=this.elements.splice(t,1);return n}addAt(e,t){return e<0||e>this.length?!1:(this._elements.splice(this._offset+e,0,t),!0)}setAt(e,t){return e<0||e>=this.length?!1:(this._elements[this._offset+e]=t,!0)}reverse(){return this._elements=this.elements.slice(this._offset).reverse(),this._offset=0,this}clear(){this._elements=[],this._offset=0}compact(){return this._elements=this.elements.slice(this._offset),this._offset=0,!0}splice(e,t=0,...n){e=Math.max(0,Math.min(e,this.length)),t=Math.max(0,Math.min(t,this.length-e));let i=this._offset+e,s=this._elements.splice(i,t,...n);this.elements.length>0&&this.offset/this.elements.length>this.autoCompactRatio&&this.compact();let r=this._createInstance({toElementFn:this.toElementFn,maxLen:this._maxLen});return r._setAutoCompactRatio(this._autoCompactRatio),r.pushMany(s),r}clone(){let e=this._createInstance({toElementFn:this.toElementFn,maxLen:this._maxLen});e._setAutoCompactRatio(this._autoCompactRatio);for(let t=this._offset;t<this.elements.length;t++)e.push(this.elements[t]);return e}filter(e,t){let n=this._createInstance({toElementFn:this.toElementFn,maxLen:this._maxLen});n._setAutoCompactRatio(this._autoCompactRatio);let i=0;for(let s of this)e.call(t,s,i,this)&&n.push(s),i++;return n}map(e,t,n){var r,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 v=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}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var H=(e=>(e[e.VISIT=0]="VISIT",e[e.PROCESS=1]="PROCESS",e))(H||{}),_=class{constructor(l,e,t=!0,n=!0){this.low=l;this.high=e;this.includeLow=t;this.includeHigh=n;if(!(y(l)&&y(e)))throw new RangeError("low or high is not comparable");if(l>e)throw new RangeError("low must be less than or equal to high")}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 E=class{constructor(l,e){f(this,"key");f(this,"value");f(this,"parent");f(this,"_left");f(this,"_right");f(this,"_height",0);f(this,"_color","BLACK");f(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"}},C=class extends v{constructor(e=[],t){super();f(this,"iterationType","ITERATIVE");f(this,"_isMapMode",!0);f(this,"_isDuplicate",!1);f(this,"_store",new Map);f(this,"_root");f(this,"_size",0);f(this,"_NIL",new E(NaN));f(this,"_toEntryFn");f(this,"_DEFAULT_NODE_CALLBACK",e=>e?e.key:void 0);if(t){let{iterationType:n,toEntryFn:i,isMapMode:s,isDuplicate:r}=t;if(n&&(this.iterationType=n),s!==void 0&&(this._isMapMode=s),r!==void 0&&(this._isDuplicate=r),typeof i=="function")this._toEntryFn=i;else if(i)throw TypeError("toEntryFn must be a function type")}e&&this.addMany(e)}get isMapMode(){return this._isMapMode}get isDuplicate(){return this._isDuplicate}get store(){return this._store}get root(){return this._root}get size(){return this._size}get NIL(){return this._NIL}get toEntryFn(){return this._toEntryFn}createNode(e,t){return new E(e,this._isMapMode?void 0:t)}createTree(e){return this._createInstance(e)}ensureNode(e,t=this.iterationType){if(e===null)return null;if(e!==void 0&&e!==this._NIL){if(this.isNode(e))return e;if(this.isEntry(e)){let n=e[0];return n===null?null:n===void 0?void 0:this.getNode(n,this._root,t)}return this.getNode(e,this._root,t)}}isNode(e){return e instanceof E}isRaw(e){return this._toEntryFn!==void 0&&typeof e=="object"}isRealNode(e){return e===this._NIL||e===null||e===void 0?!1:this.isNode(e)}isRealNodeOrNull(e){return e===null||this.isRealNode(e)}isNIL(e){return e===this._NIL}isRange(e){return e instanceof _}isLeaf(e){return e=this.ensureNode(e),e===void 0?!1:e===null?!0:!this.isRealNode(e.left)&&!this.isRealNode(e.right)}isEntry(e){return Array.isArray(e)&&e.length===2}isValidKey(e){return e===null?!0:y(e)}add(e,t){let[n,i]=this._keyValueNodeOrEntryToNodeAndValue(e,t);if(n===void 0)return!1;if(!this._root)return this._setRoot(n),this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size=1,!0;let s=new m([this._root]),r;for(;s.length>0;){let o=s.shift();if(o){if(!this._isDuplicate&&n!==null&&o.key===n.key)return this._replaceNode(o,n),this._isMapMode&&this._setValue(o.key,i),!0;r===void 0&&(o.left===void 0||o.right===void 0)&&(r=o),o.left!==null&&o.left&&s.push(o.left),o.right!==null&&o.right&&s.push(o.right)}}return r?(r.left===void 0?r.left=n:r.right===void 0&&(r.right=n),this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0):!1}addMany(e,t){let n=[],i;t&&(i=t[Symbol.iterator]());for(let s of e){let r;if(i){let o=i.next();o.done||(r=o.value)}this.isRaw(s)&&(s=this._toEntryFn(s)),n.push(this.add(s,r))}return n}merge(e){this.addMany(e,[])}refill(e,t){this.clear(),this.addMany(e,t)}delete(e){let t=[];if(!this._root)return t;let n=this.getNode(e);if(!n)return t;let i=n==null?void 0:n.parent,s,r=n;if(!n.left&&!n.right&&!i)this._setRoot(void 0);else if(n.left){let o=this.getRightMost(d=>d,n.left);if(o){let d=o.parent;r=this._swapProperties(n,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}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){return this.search(e,!0,i=>i,t,n)[0]}get(e,t=this._root,n=this.iterationType){var i;if(this._isMapMode){let s=this._extractKey(e);return s==null?void 0:this._store.get(s)}return(i=this.getNode(e,t,n))==null?void 0:i.value}has(e,t=this._root,n=this.iterationType){return this.search(e,!0,i=>i,t,n).length>0}clear(){this._clearNodes(),this._isMapMode&&this._clearValues()}isEmpty(){return this._size===0}isPerfectlyBalanced(e=this._root){return this.getMinHeight(e)+1>=this.getHeight(e)}isBST(e=this._root,t=this.iterationType){let n=this.ensureNode(e);if(!n)return!0;if(t==="RECURSIVE"){let i=(o,d,u)=>{if(!this.isRealNode(o))return!0;let a=Number(o.key);return a<=d||a>=u?!1:i(o.left,d,a)&&i(o.right,a,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,a=n;for(;this.isRealNode(a)||d.length>0;){for(;this.isRealNode(a);)d.push(a),a=a.left;a=d.pop();let T=Number(a.key);if(!this.isRealNode(a)||!o&&u>=T||o&&u<=T)return!1;u=T,a=a.right}return!0},s=i(!1),r=i(!0);return s||r}}getDepth(e,t=this._root){let n=this.ensureNode(e),i=this.ensureNode(t),s=0;for(;n!=null&&n.parent;){if(n===i)return s;s++,n=n.parent}return s}getHeight(e=this._root,t=this.iterationType){if(e=this.ensureNode(e),!this.isRealNode(e))return-1;if(t==="RECURSIVE"){let n=i=>{if(!this.isRealNode(i))return-1;let s=n(i.left),r=n(i.right);return Math.max(s,r)+1};return n(e)}else{let n=[{node:e,depth:0}],i=0;for(;n.length>0;){let{node:s,depth:r}=n.pop();this.isRealNode(s.left)&&n.push({node:s.left,depth:r+1}),this.isRealNode(s.right)&&n.push({node:s.right,depth:r+1}),i=Math.max(i,r)}return i}}getMinHeight(e=this._root,t=this.iterationType){if(e=this.ensureNode(e),!e)return-1;if(t==="RECURSIVE"){let n=i=>{if(!this.isRealNode(i)||!this.isRealNode(i.left)&&!this.isRealNode(i.right))return 0;let s=n(i.left),r=n(i.right);return Math.min(s,r)+1};return n(e)}else{let n=[],i=e,s=null,r=new Map;for(;n.length>0||i;)if(this.isRealNode(i))n.push(i),i=i.left;else if(i=n[n.length-1],!this.isRealNode(i.right)||s===i.right){if(i=n.pop(),this.isRealNode(i)){let 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=U(r=>{let{left:o}=r;return this.isRealNode(o)?P(()=>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=U(s=>{let{right:r}=s;return this.isRealNode(r)?P(()=>i(r)):s});return e(i(t))}}getPredecessor(e){if(this.isRealNode(e.left)){let t=e.left;for(;!this.isRealNode(t)||this.isRealNode(t.right)&&t.right!==e;)this.isRealNode(t)&&(t=t.right);return t}else return e}getSuccessor(e){if(e=this.ensureNode(e),!this.isRealNode(e))return;if(this.isRealNode(e.right))return this.getLeftMost(n=>n,e.right);let t=e.parent;for(;this.isRealNode(t)&&e===t.right;)e=t,t=t.parent;return t}dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,s=this.iterationType,r=!1){return i=this.ensureNode(i),i?this._dfs(e,t,n,i,s,r):[]}bfs(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType,i=!1){if(t=this.ensureNode(t),!t)return[];let s=[];if(n==="RECURSIVE"){let r=new m([t]),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 m([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=new m([t]);for(;s.length>0;){let r=s.shift();this.isRealNode(r)&&(this.isLeaf(r)&&i.push(e(r)),this.isRealNode(r.left)&&s.push(r.left),this.isRealNode(r.right)&&s.push(r.right))}}return i}listLevels(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType,i=!1){t=this.ensureNode(t);let s=[];if(!t)return s;if(n==="RECURSIVE"){let r=(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,a=null;for(;d;)a=d.right,d.right=u,u=d,d=a;return u},o=d=>{let u=r(d),a=u;for(;a;)i.push(e(a)),a=a.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.add([s,r]);return n}map(e,t,n){let i=this._createLike([],t),s=0;for(let[r,o]of this)i.add(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
|
|
1
|
+
"use strict";var treeMultimapTyped=(()=>{var w=Object.defineProperty;var X=Object.getOwnPropertyDescriptor;var J=Object.getOwnPropertyNames;var Y=Object.prototype.hasOwnProperty;var Z=(c,o,e)=>o in c?w(c,o,{enumerable:!0,configurable:!0,writable:!0,value:e}):c[o]=e;var $=(c,o)=>{for(var e in o)w(c,e,{get:o[e],enumerable:!0})},z=(c,o,e,t)=>{if(o&&typeof o=="object"||typeof o=="function")for(let n of J(o))!Y.call(c,n)&&n!==e&&w(c,n,{get:()=>o[n],enumerable:!(t=X(o,n))||t.enumerable});return c};var ee=c=>z(w({},"__esModule",{value:!0}),c);var h=(c,o,e)=>Z(c,typeof o!="symbol"?o+"":o,e);var ie={};$(ie,{BST:()=>k,BSTNode:()=>M,BinaryTree:()=>C,BinaryTreeNode:()=>E,DFSOperation:()=>H,Range:()=>_,RedBlackTree:()=>L,RedBlackTreeNode:()=>I,TreeMultiMap:()=>G,TreeMultiMapNode:()=>S});function F(c){let o=typeof c;return o==="number"?!0:o==="bigint"||o==="string"||o==="boolean"}function j(c){if(typeof c.valueOf=="function"){let o=c.valueOf();if(o!==c){if(F(o))return o;if(typeof o=="object"&&o!==null)return j(o)}}if(typeof c.toString=="function"){let o=c.toString();if(o!=="[object Object]")return o}return null}function y(c,o=!1){if(c==null)return!1;if(F(c))return!0;if(typeof c!="object")return!1;if(c instanceof Date||o)return!0;let e=j(c);return e==null?!1:F(e)}var P=c=>({isThunk:!0,fn:c}),te=c=>typeof c=="object"&&c!==null&&"isThunk"in c&&c.isThunk;function ne(c){let o=c;for(;te(o);)o=o.fn();return o}function U(c){return(...o)=>ne(c(...o))}var A=class{constructor(o){h(this,"_toElementFn");if(o){let{toElementFn:e}=o;if(typeof e=="function")this._toElementFn=e;else if(e)throw new TypeError("toElementFn must be a function type")}}get toElementFn(){return this._toElementFn}*[Symbol.iterator](...o){yield*this._getIterator(...o)}*values(){for(let o of this)yield o}every(o,e){let t=0;for(let n of this)if(e===void 0){if(!o(n,t++,this))return!1}else if(!o.call(e,n,t++,this))return!1;return!0}some(o,e){let t=0;for(let n of this)if(e===void 0){if(o(n,t++,this))return!0}else if(o.call(e,n,t++,this))return!0;return!1}forEach(o,e){let t=0;for(let n of this)e===void 0?o(n,t++,this):o.call(e,n,t++,this)}find(o,e){let t=0;for(let n of this)if(e===void 0){if(o(n,t++,this))return n}else if(o.call(e,n,t++,this))return n}has(o){for(let e of this)if(e===o)return!0;return!1}reduce(o,e){let t=0,n=this[Symbol.iterator](),i;if(arguments.length>=2)i=e;else{let s=n.next();if(s.done)throw new TypeError("Reduce of empty structure with no initial value");i=s.value,t=1}for(let s of n)i=o(i,s,t++,this);return i}toArray(){return[...this]}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var O=class c extends A{constructor(e){super(e);h(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 c?t.pushMany(n):t.push(n);return t}sort(e){let t=this.toArray();t.sort(e),this.clear();for(let n of t)this.push(n);return this}splice(e,t=0,...n){let i=this._createInstance();e=e<0?this.length+e:e,e=Math.max(0,Math.min(e,this.length)),t=Math.max(0,Math.min(t,this.length-e));for(let s=0;s<t;s++){let r=this.deleteAt(e);r!==void 0&&i.push(r)}for(let s=0;s<n.length;s++)this.addAt(e+s,n[s]);return i}join(e=","){return this.toArray().join(e)}toReversedArray(){let e=[];for(let t=this.length-1;t>=0;t--)e.push(this.at(t));return e}reduceRight(e,t){let n=t!=null?t:0;for(let i=this.length-1;i>=0;i--)n=e(n,this.at(i),i,this);return n}slice(e=0,t=this.length){e=e<0?this.length+e:e,t=t<0?this.length+t:t;let n=this._createInstance();for(let i=e;i<t;i++)n.push(this.at(i));return n}fill(e,t=0,n=this.length){if(t=t<0?this.length+t:t,n=n<0?this.length+n:n,t<0&&(t=0),n>this.length&&(n=this.length),t>=n)return this;for(let i=t;i<n;i++)this.setAt(i,e);return this}};var m=class c extends O{constructor(e=[],t){super(t);h(this,"_elements",[]);h(this,"_offset",0);h(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 c(e)}isEmpty(){return this.length===0}push(e){return this.elements.push(e),this._maxLen>0&&this.length>this._maxLen&&this.shift(),!0}pushMany(e){let t=[];for(let n of e)this.toElementFn?t.push(this.push(this.toElementFn(n))):t.push(this.push(n));return t}shift(){if(this.length===0)return;let e=this.first;return this._offset+=1,this.elements.length>0&&this.offset/this.elements.length>this.autoCompactRatio&&this.compact(),e}delete(e){for(let t=this._offset;t<this.elements.length;t++)if(Object.is(this.elements[t],e))return this.elements.splice(t,1),!0;return!1}at(e){if(!(e<0||e>=this.length))return this._elements[this._offset+e]}deleteAt(e){if(e<0||e>=this.length)return;let t=this._offset+e,[n]=this.elements.splice(t,1);return n}addAt(e,t){return e<0||e>this.length?!1:(this._elements.splice(this._offset+e,0,t),!0)}setAt(e,t){return e<0||e>=this.length?!1:(this._elements[this._offset+e]=t,!0)}reverse(){return this._elements=this.elements.slice(this._offset).reverse(),this._offset=0,this}clear(){this._elements=[],this._offset=0}compact(){return this._elements=this.elements.slice(this._offset),this._offset=0,!0}splice(e,t=0,...n){e=Math.max(0,Math.min(e,this.length)),t=Math.max(0,Math.min(t,this.length-e));let i=this._offset+e,s=this._elements.splice(i,t,...n);this.elements.length>0&&this.offset/this.elements.length>this.autoCompactRatio&&this.compact();let r=this._createInstance({toElementFn:this.toElementFn,maxLen:this._maxLen});return r._setAutoCompactRatio(this._autoCompactRatio),r.pushMany(s),r}clone(){let e=this._createInstance({toElementFn:this.toElementFn,maxLen:this._maxLen});e._setAutoCompactRatio(this._autoCompactRatio);for(let t=this._offset;t<this.elements.length;t++)e.push(this.elements[t]);return e}filter(e,t){let n=this._createInstance({toElementFn:this.toElementFn,maxLen:this._maxLen});n._setAutoCompactRatio(this._autoCompactRatio);let i=0;for(let s of this)e.call(t,s,i,this)&&n.push(s),i++;return n}map(e,t,n){var r,l;let i=new this.constructor([],{toElementFn:t==null?void 0:t.toElementFn,maxLen:(r=t==null?void 0:t.maxLen)!=null?r:this._maxLen,autoCompactRatio:(l=t==null?void 0:t.autoCompactRatio)!=null?l:this._autoCompactRatio}),s=0;for(let 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 l of this){let d=t===void 0?e(l,s++,this):e.call(t,l,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 v=class{*[Symbol.iterator](...o){yield*this._getIterator(...o)}*entries(){for(let o of this)yield o}*keys(){for(let o of this)yield o[0]}*values(){for(let o of this)yield o[1]}every(o,e){let t=0;for(let n of this)if(!o.call(e,n[1],n[0],t++,this))return!1;return!0}some(o,e){let t=0;for(let n of this)if(o.call(e,n[1],n[0],t++,this))return!0;return!1}forEach(o,e){let t=0;for(let n of this){let[i,s]=n;o.call(e,s,i,t++,this)}}find(o,e){let t=0;for(let n of this){let[i,s]=n;if(o.call(e,s,i,t++,this))return n}}has(o){for(let e of this){let[t]=e;if(t===o)return!0}return!1}hasValue(o){for(let[,e]of this)if(e===o)return!0;return!1}get(o){for(let e of this){let[t,n]=e;if(t===o)return n}}reduce(o,e){let t=e,n=0;for(let i of this){let[s,r]=i;t=o(t,r,s,n++,this)}return t}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var H=(e=>(e[e.VISIT=0]="VISIT",e[e.PROCESS=1]="PROCESS",e))(H||{}),_=class{constructor(o,e,t=!0,n=!0){this.low=o;this.high=e;this.includeLow=t;this.includeHigh=n;if(!(y(o)&&y(e)))throw new RangeError("low or high is not comparable");if(o>e)throw new RangeError("low must be less than or equal to high")}isInRange(o,e){let t=this.includeLow?e(o,this.low)>=0:e(o,this.low)>0,n=this.includeHigh?e(o,this.high)<=0:e(o,this.high)<0;return t&&n}};var E=class{constructor(o,e){h(this,"key");h(this,"value");h(this,"parent");h(this,"_left");h(this,"_right");h(this,"_height",0);h(this,"_color","BLACK");h(this,"_count",1);this.key=o,this.value=e}get left(){return this._left}set left(o){o&&(o.parent=this),this._left=o}get right(){return this._right}set right(o){o&&(o.parent=this),this._right=o}get height(){return this._height}set height(o){this._height=o}get color(){return this._color}set color(o){this._color=o}get count(){return this._count}set count(o){this._count=o}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},C=class extends v{constructor(e=[],t){super();h(this,"iterationType","ITERATIVE");h(this,"_isMapMode",!0);h(this,"_isDuplicate",!1);h(this,"_store",new Map);h(this,"_root");h(this,"_size",0);h(this,"_NIL",new E(NaN));h(this,"_toEntryFn");h(this,"_DEFAULT_NODE_CALLBACK",e=>e?e.key:void 0);if(t){let{iterationType:n,toEntryFn:i,isMapMode:s,isDuplicate:r}=t;if(n&&(this.iterationType=n),s!==void 0&&(this._isMapMode=s),r!==void 0&&(this._isDuplicate=r),typeof i=="function")this._toEntryFn=i;else if(i)throw TypeError("toEntryFn must be a function type")}e&&this.addMany(e)}get isMapMode(){return this._isMapMode}get isDuplicate(){return this._isDuplicate}get store(){return this._store}get root(){return this._root}get size(){return this._size}get NIL(){return this._NIL}get toEntryFn(){return this._toEntryFn}createNode(e,t){return new E(e,this._isMapMode?void 0:t)}createTree(e){return this._createInstance(e)}ensureNode(e,t=this.iterationType){if(e===null)return null;if(e!==void 0&&e!==this._NIL){if(this.isNode(e))return e;if(this.isEntry(e)){let n=e[0];return n===null?null:n===void 0?void 0:this.getNode(n,this._root,t)}return this.getNode(e,this._root,t)}}isNode(e){return e instanceof E}isRaw(e){return this._toEntryFn!==void 0&&typeof e=="object"}isRealNode(e){return e===this._NIL||e===null||e===void 0?!1:this.isNode(e)}isRealNodeOrNull(e){return e===null||this.isRealNode(e)}isNIL(e){return e===this._NIL}isRange(e){return e instanceof _}isLeaf(e){return e=this.ensureNode(e),e===void 0?!1:e===null?!0:!this.isRealNode(e.left)&&!this.isRealNode(e.right)}isEntry(e){return Array.isArray(e)&&e.length===2}isValidKey(e){return e===null?!0:y(e)}add(e,t){let[n,i]=this._keyValueNodeOrEntryToNodeAndValue(e,t);if(n===void 0)return!1;if(!this._root)return this._setRoot(n),this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size=1,!0;let s=new m([this._root]),r;for(;s.length>0;){let l=s.shift();if(l){if(!this._isDuplicate&&n!==null&&l.key===n.key)return this._replaceNode(l,n),this._isMapMode&&this._setValue(l.key,i),!0;r===void 0&&(l.left===void 0||l.right===void 0)&&(r=l),l.left!==null&&l.left&&s.push(l.left),l.right!==null&&l.right&&s.push(l.right)}}return r?(r.left===void 0?r.left=n:r.right===void 0&&(r.right=n),this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0):!1}set(e,t){return this.add(e,t)}addMany(e,t){let n=[],i;t&&(i=t[Symbol.iterator]());for(let s of e){let r;if(i){let l=i.next();l.done||(r=l.value)}this.isRaw(s)&&(s=this._toEntryFn(s)),n.push(this.add(s,r))}return n}setMany(e,t){return this.addMany(e,t)}merge(e){this.addMany(e,[])}refill(e,t){this.clear(),this.addMany(e,t)}delete(e){let t=[];if(!this._root)return t;let n=this.getNode(e);if(!n)return t;let i=n==null?void 0:n.parent,s,r=n;if(!n.left&&!n.right&&!i)this._setRoot(void 0);else if(n.left){let l=this.getRightMost(d=>d,n.left);if(l){let d=l.parent;r=this._swapProperties(n,l),d&&(d.right===l?d.right=l.left:d.left=l.left,s=d)}}else if(i){let{familyPosition:l}=n;l==="LEFT"||l==="ROOT_LEFT"?i.left=n.right:(l==="RIGHT"||l==="ROOT_RIGHT")&&(i.right=n.right),s=i}else this._setRoot(n.right),n.right=void 0;return this._size=this._size-1,t.push({deleted:r,needBalanced:s}),this._isMapMode&&r&&this._store.delete(r.key),t}search(e,t=!1,n=this._DEFAULT_NODE_CALLBACK,i=this._root,s=this.iterationType){if(e===void 0)return[];if(e===null)return[];if(i=this.ensureNode(i),!i)return[];let r=this._ensurePredicate(e),l=[];if(s==="RECURSIVE"){let d=u=>{r(u)&&(l.push(n(u)),t)||!this.isRealNode(u.left)&&!this.isRealNode(u.right)||(this.isRealNode(u.left)&&d(u.left),this.isRealNode(u.right)&&d(u.right))};d(i)}else{let d=[i];for(;d.length>0;){let u=d.pop();if(this.isRealNode(u)){if(r(u)&&(l.push(n(u)),t))return l;this.isRealNode(u.left)&&d.push(u.left),this.isRealNode(u.right)&&d.push(u.right)}}}return l}getNodes(e,t=!1,n=this._root,i=this.iterationType){return this.search(e,t,s=>s,n,i)}getNode(e,t=this._root,n=this.iterationType){return this.search(e,!0,i=>i,t,n)[0]}get(e,t=this._root,n=this.iterationType){var i;if(this._isMapMode){let s=this._extractKey(e);return s==null?void 0:this._store.get(s)}return(i=this.getNode(e,t,n))==null?void 0:i.value}has(e,t=this._root,n=this.iterationType){return this.search(e,!0,i=>i,t,n).length>0}clear(){this._clearNodes(),this._isMapMode&&this._clearValues()}isEmpty(){return this._size===0}isPerfectlyBalanced(e=this._root){return this.getMinHeight(e)+1>=this.getHeight(e)}isBST(e=this._root,t=this.iterationType){let n=this.ensureNode(e);if(!n)return!0;if(t==="RECURSIVE"){let i=(l,d,u)=>{if(!this.isRealNode(l))return!0;let a=Number(l.key);return a<=d||a>=u?!1:i(l.left,d,a)&&i(l.right,a,u)},s=i(n,Number.MIN_SAFE_INTEGER,Number.MAX_SAFE_INTEGER),r=i(n,Number.MAX_SAFE_INTEGER,Number.MIN_SAFE_INTEGER);return s||r}else{let i=(l=!1)=>{let d=[],u=l?Number.MAX_SAFE_INTEGER:Number.MIN_SAFE_INTEGER,a=n;for(;this.isRealNode(a)||d.length>0;){for(;this.isRealNode(a);)d.push(a),a=a.left;a=d.pop();let p=Number(a.key);if(!this.isRealNode(a)||!l&&u>=p||l&&u<=p)return!1;u=p,a=a.right}return!0},s=i(!1),r=i(!0);return s||r}}getDepth(e,t=this._root){let n=this.ensureNode(e),i=this.ensureNode(t),s=0;for(;n!=null&&n.parent;){if(n===i)return s;s++,n=n.parent}return s}getHeight(e=this._root,t=this.iterationType){if(e=this.ensureNode(e),!this.isRealNode(e))return-1;if(t==="RECURSIVE"){let n=i=>{if(!this.isRealNode(i))return-1;let s=n(i.left),r=n(i.right);return Math.max(s,r)+1};return n(e)}else{let n=[{node:e,depth:0}],i=0;for(;n.length>0;){let{node:s,depth:r}=n.pop();this.isRealNode(s.left)&&n.push({node:s.left,depth:r+1}),this.isRealNode(s.right)&&n.push({node:s.right,depth:r+1}),i=Math.max(i,r)}return i}}getMinHeight(e=this._root,t=this.iterationType){if(e=this.ensureNode(e),!e)return-1;if(t==="RECURSIVE"){let n=i=>{if(!this.isRealNode(i)||!this.isRealNode(i.left)&&!this.isRealNode(i.right))return 0;let s=n(i.left),r=n(i.right);return Math.min(s,r)+1};return n(e)}else{let n=[],i=e,s=null,r=new Map;for(;n.length>0||i;)if(this.isRealNode(i))n.push(i),i=i.left;else if(i=n[n.length-1],!this.isRealNode(i.right)||s===i.right){if(i=n.pop(),this.isRealNode(i)){let l=this.isRealNode(i.left)?r.get(i.left):-1,d=this.isRealNode(i.right)?r.get(i.right):-1;r.set(i,1+Math.min(l,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:l}=r;return this.isRealNode(l)?s(l):r};return e(s(i))}else{let s=U(r=>{let{left:l}=r;return this.isRealNode(l)?P(()=>s(l)):r});return e(s(i))}}getRightMost(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){if(this.isNIL(t)||(t=this.ensureNode(t),!t))return e(void 0);if(n==="RECURSIVE"){let i=s=>{let{right:r}=s;return this.isRealNode(r)?i(r):s};return e(i(t))}else{let i=U(s=>{let{right:r}=s;return this.isRealNode(r)?P(()=>i(r)):s});return e(i(t))}}getPredecessor(e){if(this.isRealNode(e.left)){let t=e.left;for(;!this.isRealNode(t)||this.isRealNode(t.right)&&t.right!==e;)this.isRealNode(t)&&(t=t.right);return t}else return e}getSuccessor(e){if(e=this.ensureNode(e),!this.isRealNode(e))return;if(this.isRealNode(e.right))return this.getLeftMost(n=>n,e.right);let t=e.parent;for(;this.isRealNode(t)&&e===t.right;)e=t,t=t.parent;return t}dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,s=this.iterationType,r=!1){return i=this.ensureNode(i),i?this._dfs(e,t,n,i,s,r):[]}bfs(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType,i=!1){if(t=this.ensureNode(t),!t)return[];let s=[];if(n==="RECURSIVE"){let r=new m([t]),l=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)),l(d+1)};l(0)}else{let r=new m([t]);for(;r.length>0;){let l=r.length;for(let d=0;d<l;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=new m([t]);for(;s.length>0;){let r=s.shift();this.isRealNode(r)&&(this.isLeaf(r)&&i.push(e(r)),this.isRealNode(r.left)&&s.push(r.left),this.isRealNode(r.right)&&s.push(r.right))}}return i}listLevels(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType,i=!1){t=this.ensureNode(t);let s=[];if(!t)return s;if(n==="RECURSIVE"){let r=(l,d)=>{s[d]||(s[d]=[]),s[d].push(e(l)),i?(l&&this.isRealNodeOrNull(l.left)&&r(l.left,d+1),l&&this.isRealNodeOrNull(l.right)&&r(l.right,d+1)):(l&&l.left&&r(l.left,d+1),l&&l.right&&r(l.right,d+1))};r(t,0)}else{let r=[[t,0]];for(;r.length>0;){let l=r.pop(),[d,u]=l;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,a=null;for(;d;)a=d.right,d.right=u,u=d,d=a;return u},l=d=>{let u=r(d),a=u;for(;a;)i.push(e(a)),a=a.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,l(s.left)}s=s.right}l(n);break}return i}clone(){let e=this._createInstance();return this._clone(e),e}filter(e,t){let n=this._createInstance(),i=0;for(let[s,r]of this)e.call(t,r,s,i++,this)&&n.add([s,r]);return n}map(e,t,n){let i=this._createLike([],t),s=0;for(let[r,l]of this)i.add(e.call(n,l,r,s++,this));return i}toVisual(e=this._root,t){let n={isShowUndefined:!1,isShowNull:!0,isShowRedBlackNIL:!1,...t};e=this.ensureNode(e);let i="";return e&&(n.isShowUndefined&&(i+=`U for undefined
|
|
2
2
|
`),n.isShowNull&&(i+=`N for null
|
|
3
3
|
`),n.isShowRedBlackNIL&&(i+=`S for Sentinel Node(NIL)
|
|
4
|
-
`),(r=>{let[
|
|
5
|
-
`;i+=d})(e)),i}print(e,t=this._root){console.log(this.toVisual(t,e))}_dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,s=this.iterationType,r=!1,o=T=>!!T,d=T=>!!T,u=T=>r?this.isRealNodeOrNull(T):this.isRealNode(T),a=T=>this.isRealNodeOrNull(T)){if(i=this.ensureNode(i),!i)return[];let T=[];if(s==="RECURSIVE"){let p=h=>{if(!u(h))return;let V=()=>{o(h)&&(h==null?void 0:h.left)!==void 0&&p(h==null?void 0:h.left)},R=()=>{d(h)&&(h==null?void 0:h.right)!==void 0&&p(h==null?void 0:h.right)};switch(t){case"IN":if(V(),a(h)&&(T.push(e(h)),n))return;R();break;case"PRE":if(a(h)&&(T.push(e(h)),n))return;V(),R();break;case"POST":if(V(),R(),a(h)&&(T.push(e(h)),n))return;break}};p(i)}else{let p=[{opt:0,node:i}],h=K=>{var N;o(K.node)&&p.push({opt:0,node:(N=K.node)==null?void 0:N.left})},V=K=>{var N;d(K.node)&&p.push({opt:0,node:(N=K.node)==null?void 0:N.right})},R=K=>{u(K.node)&&p.push({opt:1,node:K.node})};for(;p.length>0;){let K=p.pop();if(K!==void 0&&u(K.node))if(K.opt===1){if(a(K.node)&&K.node!==void 0&&(T.push(e(K.node)),n))return T}else switch(t){case"IN":V(K),R(K),h(K);break;case"PRE":V(K),h(K),R(K);break;case"POST":R(K),V(K),h(K);break}}}return T}*_getIterator(e=this._root){if(e)if(this.iterationType==="ITERATIVE"){let t=[],n=e;for(;n||t.length>0;){for(;this.isRealNode(n);)t.push(n),n=n.left;n=t.pop(),this.isRealNode(n)&&(this._isMapMode?yield[n.key,this._store.get(n.key)]:yield[n.key,n.value],n=n.right)}}else e.left&&this.isRealNode(e)&&(yield*this[Symbol.iterator](e.left)),this._isMapMode?yield[e.key,this._store.get(e.key)]:yield[e.key,e.value],e.right&&this.isRealNode(e)&&(yield*this[Symbol.iterator](e.right))}_snapshotOptions(){return{iterationType:this.iterationType,toEntryFn:this.toEntryFn,isMapMode:this.isMapMode,isDuplicate:this.isDuplicate}}_createInstance(e){let t=this.constructor;return new t([],{...this._snapshotOptions(),...e!=null?e:{}})}_createLike(e=[],t){let n=this.constructor;return new n(e,{...this._snapshotOptions(),...t!=null?t:{}})}_keyValueNodeOrEntryToNodeAndValue(e,t){if(e===void 0)return[void 0,void 0];if(e===null)return[null,void 0];if(this.isNode(e))return[e,t];if(this.isEntry(e)){let[n,i]=e;if(n===void 0)return[void 0,void 0];if(n===null)return[null,void 0];let s=t!=null?t:i;return[this.createNode(n,s),s]}return[this.createNode(e,t),t]}_clone(e){this.bfs(t=>{t===null?e.add(null):this._isMapMode?e.add([t.key,this._store.get(t.key)]):e.add([t.key,t.value])},this._root,this.iterationType,!0),this._isMapMode&&(e._store=this._store)}_displayAux(e,t){let{isShowNull:n,isShowUndefined:i,isShowRedBlackNIL:s}=t,r=[["\u2500"],1,0,0];if(e===null&&!n)return r;if(e===void 0&&!i)return r;if(this.isNIL(e)&&!s)return r;if(e!=null){let d=e.key,u=this.isNIL(e)?"S":String(d),a=u.length;return o(u,a,this._displayAux(e.left,t),this._displayAux(e.right,t))}else{let d=e===void 0?"U":"N",u=d.length;return o(d,u,[[""],1,0,0],[[""],1,0,0])}function o(d,u,a,T){let[p,h,V,R]=a,[K,N,g,B]=T,S=" ".repeat(Math.max(0,R+1))+"_".repeat(Math.max(0,h-R-1))+d+"_".repeat(Math.max(0,B))+" ".repeat(Math.max(0,N-B)),D=(V>0?" ".repeat(R)+"/"+" ".repeat(h-R-1):" ".repeat(h))+" ".repeat(u)+(g>0?" ".repeat(B)+"\\"+" ".repeat(N-B-1):" ".repeat(N)),Q=[S,D];for(let b=0;b<Math.max(V,g);b++){let q=b<V?p[b]:" ".repeat(h),W=b<g?K[b]:" ".repeat(N);Q.push(q+" ".repeat(u)+W)}return[Q,h+u+N,Math.max(V,g)+2,h+Math.floor(u/2)]}}_swapProperties(e,t){if(e=this.ensureNode(e),t=this.ensureNode(t),e&&t){let{key:n,value:i}=t,s=this.createNode(n,i);return s&&(t.key=e.key,this._isMapMode||(t.value=e.value),e.key=s.key,this._isMapMode||(e.value=s.value)),t}}_replaceNode(e,t){return e.parent&&(e.parent.left===e?e.parent.left=t:e.parent.right===e&&(e.parent.right=t)),t.left=e.left,t.right=e.right,t.parent=e.parent,this._root===e&&this._setRoot(t),t}_setRoot(e){e&&(e.parent=void 0),this._root=e}_ensurePredicate(e){if(e==null)return t=>!1;if(this._isPredicate(e))return e;if(this.isRealNode(e))return t=>t===e;if(this.isEntry(e)){let[t]=e;return n=>n?n.key===t:!1}return t=>t?t.key===e:!1}_isPredicate(e){return typeof e=="function"}_extractKey(e){if(e===null)return null;if(e!==void 0&&e!==this._NIL)return this.isNode(e)?e.key:this.isEntry(e)?e[0]:e}_setValue(e,t){return e==null||t===void 0?!1:this._store.set(e,t)}_clearNodes(){this._setRoot(void 0),this._size=0}_clearValues(){this._store.clear()}};var k=class{constructor(l,e){f(this,"key");f(this,"value");f(this,"parent");f(this,"_left");f(this,"_right");f(this,"_height",0);f(this,"_color","BLACK");f(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"}},M=class extends C{constructor(e=[],t){super([],t);f(this,"_root");f(this,"_isReverse",!1);f(this,"_comparator",(e,t)=>{if(y(e)&&y(t))return e>t?1:e<t?-1:0;if(this._specifyComparable){let n=this._specifyComparable(e),i=this._specifyComparable(t);return n>i?1:n<i?-1:0}if(typeof e=="object"||typeof t=="object")throw TypeError("When comparing object types, a custom specifyComparable must be defined in the constructor's options.");return 0});f(this,"_specifyComparable");if(t){let{specifyComparable:n,isReverse:i}=t;typeof n=="function"&&(this._specifyComparable=n),i!==void 0&&(this._isReverse=i)}e&&this.addMany(e)}get root(){return this._root}get isReverse(){return this._isReverse}get comparator(){return this._comparator}get specifyComparable(){return this._specifyComparable}createNode(e,t){return new k(e,this._isMapMode?void 0:t)}ensureNode(e,t=this.iterationType){var n;return(n=super.ensureNode(e,t))!=null?n:void 0}isNode(e){return e instanceof k}isValidKey(e){return y(e,this._specifyComparable!==void 0)}dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,s=this.iterationType){return super.dfs(e,t,n,i,s)}bfs(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){return super.bfs(e,t,n,!1)}listLevels(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){return super.listLevels(e,t,n,!1)}getNode(e,t=this._root,n=this.iterationType){var i;return(i=this.getNodes(e,!0,t,n)[0])!=null?i:void 0}search(e,t=!1,n=this._DEFAULT_NODE_CALLBACK,i=this._root,s=this.iterationType){if(e===void 0)return[];if(e===null)return[];if(i=this.ensureNode(i),!i)return[];let r,o=this.isRange(e);o?r=a=>a?e.isInRange(a.key,this._comparator):!1:r=this._ensurePredicate(e);let d=a=>{if(!a||!this.isRealNode(a.left))return!1;if(o){let T=e,p=this.isReverse?T.high:T.low,h=this.isReverse?T.includeHigh:T.includeLow;return h&&this._compare(a.key,p)>=0||!h&&this._compare(a.key,p)>0}if(!o&&!this._isPredicate(e)){let T=this._extractKey(e);return T!=null&&this._compare(a.key,T)>0}return!0},u=a=>{if(!a||!this.isRealNode(a.right))return!1;if(o){let T=e,p=this.isReverse?T.low:T.high,h=this.isReverse?T.includeLow:T.includeHigh;return h&&this._compare(a.key,p)<=0||!h&&this._compare(a.key,p)<0}if(!o&&!this._isPredicate(e)){let T=this._extractKey(e);return T!=null&&this._compare(a.key,T)<0}return!0};return super._dfs(n,"IN",t,i,s,!1,d,u,()=>!0,a=>!!a&&r(a))}rangeSearch(e,t=this._DEFAULT_NODE_CALLBACK,n=this._root,i=this.iterationType){let s=e instanceof _?e:new _(e[0],e[1]);return this.search(s,!1,t,n,i)}add(e,t){let[n,i]=this._keyValueNodeOrEntryToNodeAndValue(e,t);if(n===void 0)return!1;if(this._root===void 0)return this._setRoot(n),this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0;let s=this._root;for(;s!==void 0;){if(this._compare(s.key,n.key)===0)return this._replaceNode(s,n),this._isMapMode&&this._setValue(s.key,i),!0;if(this._compare(s.key,n.key)>0){if(s.left===void 0)return s.left=n,this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0;s.left!==null&&(s=s.left)}else{if(s.right===void 0)return s.right=n,this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0;s.right!==null&&(s=s.right)}}return!1}addMany(e,t,n=!0,i=this.iterationType){let s=[],r=t==null?void 0:t[Symbol.iterator]();if(!n){for(let p of e){let h=r==null?void 0:r.next().value;this.isRaw(p)&&(p=this._toEntryFn(p)),s.push(this.add(p,h))}return s}let o=[],d=0;for(let p of e)o.push({key:p,value:r==null?void 0:r.next().value,orgIndex:d++});let u=o.sort(({key:p},{key:h})=>{let V,R;return this.isRaw(p)?V=this._toEntryFn(p)[0]:this.isEntry(p)?V=p[0]:this.isRealNode(p)?V=p.key:V=p,this.isRaw(h)?R=this._toEntryFn(h)[0]:this.isEntry(h)?R=h[0]:this.isRealNode(h)?R=h.key:R=h,V!=null&&R!=null?this._compare(V,R):0}),a=p=>{if(p.length===0)return;let h=Math.floor((p.length-1)/2),{key:V,value:R,orgIndex:K}=p[h];if(this.isRaw(V)){let N=this._toEntryFn(V);s[K]=this.add(N)}else s[K]=this.add(V,R);a(p.slice(0,h)),a(p.slice(h+1))};return i==="RECURSIVE"?a(u):(()=>{let h=[[0,u.length-1]];for(;h.length>0;){let V=h.pop();if(!V)continue;let[R,K]=V;if(R>K)continue;let N=R+Math.floor((K-R)/2),{key:g,value:B,orgIndex:S}=u[N];if(this.isRaw(g)){let D=this._toEntryFn(g);s[S]=this.add(D)}else s[S]=this.add(g,B);h.push([N+1,K]),h.push([R,N-1])}})(),s}lesserOrGreaterTraverse(e=this._DEFAULT_NODE_CALLBACK,t=-1,n=this._root,i=this.iterationType){let s=this.ensureNode(n),r=[];if(!this._root||!s)return r;let o=s.key;if(i==="RECURSIVE"){let d=u=>{let a=this._compare(u.key,o);Math.sign(a)==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 m([this._root]);for(;d.length>0;){let u=d.shift();if(this.isRealNode(u)){let a=this._compare(u.key,o);Math.sign(a)==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),a=t[u],T=i(r,u-1,a),p=i(u+1,o,a);return a.left=T,a.right=p,a.parent=d,a},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.add(e.call(n,o,r,s++,this));return i}deleteWhere(e){let t=[],n=this._root,i=0;for(;t.length>0||n!==void 0;){for(;n!=null;)t.push(n),n=n.left;let s=t.pop();if(!s)break;let r=s.key,o=s.value;if(e(r,o,i++,this))return this._deleteByKey(r);n=s.right}return!1}_createInstance(e){let t=this.constructor;return new t([],{...this._snapshotOptions(),...e!=null?e:{}})}_createLike(e=[],t){let n=this.constructor;return new n(e,{...this._snapshotOptions(),...t!=null?t:{}})}_snapshotOptions(){return{...super._snapshotOptions(),specifyComparable:this.specifyComparable,isReverse:this.isReverse}}_keyValueNodeOrEntryToNodeAndValue(e,t){let[n,i]=super._keyValueNodeOrEntryToNodeAndValue(e,t);return n===null?[void 0,void 0]:[n,t!=null?t:i]}_setRoot(e){e&&(e.parent=void 0),this._root=e}_compare(e,t){return this._isReverse?-this._comparator(e,t):this._comparator(e,t)}_deleteByKey(e){var s;let t=this._root;for(;t;){let r=this._compare(t.key,e);if(r===0)break;t=r>0?t.left:t.right}if(!t)return!1;let n=(r,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}};if(t.left===void 0)n(t,t.right);else if(t.right===void 0)n(t,t.left);else{let r=i(t.right);r.parent!==t&&(n(r,r.right),r.right=t.right,r.right&&(r.right.parent=r)),n(t,r),r.left=t.left,r.left&&(r.left.parent=r)}return this._size=Math.max(0,((s=this._size)!=null?s:0)-1),!0}};var I=class{constructor(l,e,t="BLACK"){f(this,"key");f(this,"value");f(this,"parent");f(this,"_left");f(this,"_right");f(this,"_height",0);f(this,"_color","BLACK");f(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}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"}},L=class extends M{constructor(e=[],t){super([],t);f(this,"_root");this._root=this.NIL,e&&this.addMany(e)}get root(){return this._root}createNode(e,t,n="BLACK"){return new I(e,this._isMapMode?void 0:t,n)}isNode(e){return e instanceof I}clear(){super.clear(),this._root=this.NIL}add(e,t){let[n,i]=this._keyValueNodeOrEntryToNodeAndValue(e,t);if(!this.isRealNode(n))return!1;let s=this._insert(n);if(s==="CREATED"){if(this.isRealNode(this._root))this._root.color="BLACK";else return!1;return this._isMapMode&&this._setValue(n.key,i),this._size++,!0}return s==="UPDATED"?(this._isMapMode&&this._setValue(n.key,i),!0):!1}delete(e){if(e===null)return[];let t=[],n;if(this._isPredicate(e)?n=this.getNode(e):n=this.isRealNode(e)?e:this.getNode(e),!n)return t;let i=n.color,s;if(!this.isRealNode(n.left))n.right!==null&&(s=n.right,this._transplant(n,n.right));else if(!this.isRealNode(n.right))s=n.left,this._transplant(n,n.left);else{let r=this.getLeftMost(o=>o,n.right);r&&(i=r.color,r.right!==null&&(s=r.right),r.parent===n?this.isRealNode(s)&&(s.parent=r):(r.right!==null&&(this._transplant(r,r.right),r.right=n.right),this.isRealNode(r.right)&&(r.right.parent=r)),this._transplant(n,r),r.left=n.left,this.isRealNode(r.left)&&(r.left.parent=r),r.color=n.color)}return this._isMapMode&&this._store.delete(n.key),this._size--,i==="BLACK"&&this._deleteFixup(s),t.push({deleted:n,needBalanced:void 0}),t}map(e,t,n){let i=this._createLike([],t),s=0;for(let[r,o]of this)i.add(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){e&&(e.parent=void 0),this._root=e}_replaceNode(e,t){return t.color=e.color,super._replaceNode(e,t)}_insert(e){var i,s,r;let t=(i=this.root)!=null?i:this.NIL,n;for(;t!==this.NIL;){n=t;let o=this._compare(e.key,t.key);if(o<0)t=(s=t.left)!=null?s:this.NIL;else if(o>0)t=(r=t.right)!=null?r:this.NIL;else return this._replaceNode(t,e),"UPDATED"}return e.parent=n,n?this._compare(e.key,n.key)<0?n.left=e:n.right=e:this._setRoot(e),e.left=this.NIL,e.right=this.NIL,e.color="RED",this._insertFixup(e),"CREATED"}_transplant(e,t){e.parent?e===e.parent.left?e.parent.left=t:e.parent.right=t:this._setRoot(t),t&&(t.parent=e.parent)}_insertFixup(e){var t,n,i,s,r;for(;((t=e==null?void 0:e.parent)==null?void 0:t.color)==="RED";)if(e.parent===((n=e.parent.parent)==null?void 0:n.left)){let o=e.parent.parent.right;(o==null?void 0:o.color)==="RED"?(e.parent.color="BLACK",o.color="BLACK",e.parent.parent.color="RED",e=e.parent.parent):(e===e.parent.right&&(e=e.parent,this._leftRotate(e)),e&&e.parent&&e.parent.parent&&(e.parent.color="BLACK",e.parent.parent.color="RED",this._rightRotate(e.parent.parent)))}else{let o=(r=(s=(i=e==null?void 0:e.parent)==null?void 0:i.parent)==null?void 0:s.left)!=null?r:void 0;(o==null?void 0:o.color)==="RED"?(e.parent.color="BLACK",o.color="BLACK",e.parent.parent.color="RED",e=e.parent.parent):(e===e.parent.left&&(e=e.parent,this._rightRotate(e)),e&&e.parent&&e.parent.parent&&(e.parent.color="BLACK",e.parent.parent.color="RED",this._leftRotate(e.parent.parent)))}this.isRealNode(this._root)&&(this._root.color="BLACK")}_deleteFixup(e){var t,n,i,s;if(!e||e===this.root||e.color==="BLACK"){e&&(e.color="BLACK");return}for(;e&&e!==this.root&&e.color==="BLACK";){let r=e.parent;if(!r)break;if(e===r.left){let o=r.right;(o==null?void 0:o.color)==="RED"&&(o.color="BLACK",r.color="RED",this._leftRotate(r),o=r.right),((n=(t=o==null?void 0:o.left)==null?void 0:t.color)!=null?n:"BLACK")==="BLACK"?(o&&(o.color="RED"),e=r):(o!=null&&o.left&&(o.left.color="BLACK"),o&&(o.color=r.color),r.color="BLACK",this._rightRotate(r),e=this.root)}else{let o=r.left;(o==null?void 0:o.color)==="RED"&&(o.color="BLACK",r&&(r.color="RED"),this._rightRotate(r),r&&(o=r.left)),((s=(i=o==null?void 0:o.right)==null?void 0:i.color)!=null?s:"BLACK")==="BLACK"?(o&&(o.color="RED"),e=r):(o!=null&&o.right&&(o.right.color="BLACK"),o&&(o.color=r.color),r&&(r.color="BLACK"),this._leftRotate(r),e=this.root)}}e&&(e.color="BLACK")}_leftRotate(e){if(!e||!e.right)return;let t=e.right;e.right=t.left,t.left&&t.left!==this.NIL&&(t.left.parent=e),t.parent=e.parent,e.parent?e===e.parent.left?e.parent.left=t:e.parent.right=t:this._setRoot(t),t.left=e,e.parent=t}_rightRotate(e){if(!e||!e.left)return;let t=e.left;e.left=t.right,t.right&&t.right!==this.NIL&&(t.right.parent=e),t.parent=e.parent,e.parent?e===e.parent.left?e.parent.left=t:e.parent.right=t:this._setRoot(t),t.right=e,e.parent=t}};var x=class{constructor(l,e=[],t="BLACK"){f(this,"key");f(this,"value");f(this,"parent");f(this,"_left");f(this,"_right");f(this,"_height",0);f(this,"_color","BLACK");f(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}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"}},G=class extends L{constructor(l=[],e){super([],{...e}),l&&this.addMany(l)}createNode(l,e=[]){return new x(l,this._isMapMode?[]:e)}isNode(l){return l instanceof x}add(l,e){if(this.isRealNode(l))return super.add(l);let t=(n,i)=>{if(n==null)return!1;let s=()=>{let o=this.get(n);if(o!==void 0&&i!==void 0){for(let d of i)o.push(d);return!0}return!1},r=()=>{let o=this.getNode(n);if(this.isRealNode(o)){let d=this.get(o);if(d===void 0)return super.add(n,i),!0;if(i!==void 0){for(let u of i)d.push(u);return!0}else return!1}else return super.add(n,i)};return this._isMapMode?r()||s():s()||r()};if(this.isEntry(l)){let[n,i]=l;return t(n,e!==void 0?[e]:i)}return t(l,e!==void 0?[e]:void 0)}deleteValue(l,e){let t=this.get(l);if(Array.isArray(t)){let n=t.indexOf(e);return n===-1?!1:(t.splice(n,1),t.length===0&&this.delete(l),!0)}return!1}map(l,e,t){let n=this._createLike([],e),i=0;for(let[s,r]of this)n.add(l.call(t,r,s,i++,this));return n}_createInstance(l){var t,n;let e=this.constructor;return new e([],{...(n=(t=this._snapshotOptions)==null?void 0:t.call(this))!=null?n:{},...l!=null?l:{}})}_createLike(l=[],e){var n,i;let t=this.constructor;return new t(l,{...(i=(n=this._snapshotOptions)==null?void 0:n.call(this))!=null?i:{},...e!=null?e:{}})}};return ee(ie);})();
|
|
4
|
+
`),(r=>{let[l]=this._displayAux(r,n),d="";for(let u of l)d+=u+`
|
|
5
|
+
`;i+=d})(e)),i}print(e,t=this._root){console.log(this.toVisual(t,e))}_dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,s=this.iterationType,r=!1,l=p=>!!p,d=p=>!!p,u=p=>r?this.isRealNodeOrNull(p):this.isRealNode(p),a=p=>this.isRealNodeOrNull(p)){if(i=this.ensureNode(i),!i)return[];let p=[];if(s==="RECURSIVE"){let T=f=>{if(!u(f))return;let V=()=>{l(f)&&(f==null?void 0:f.left)!==void 0&&T(f==null?void 0:f.left)},N=()=>{d(f)&&(f==null?void 0:f.right)!==void 0&&T(f==null?void 0:f.right)};switch(t){case"IN":if(V(),a(f)&&(p.push(e(f)),n))return;N();break;case"PRE":if(a(f)&&(p.push(e(f)),n))return;V(),N();break;case"POST":if(V(),N(),a(f)&&(p.push(e(f)),n))return;break}};T(i)}else{let T=[{opt:0,node:i}],f=K=>{var R;l(K.node)&&T.push({opt:0,node:(R=K.node)==null?void 0:R.left})},V=K=>{var R;d(K.node)&&T.push({opt:0,node:(R=K.node)==null?void 0:R.right})},N=K=>{u(K.node)&&T.push({opt:1,node:K.node})};for(;T.length>0;){let K=T.pop();if(K!==void 0&&u(K.node))if(K.opt===1){if(a(K.node)&&K.node!==void 0&&(p.push(e(K.node)),n))return p}else switch(t){case"IN":V(K),N(K),f(K);break;case"PRE":V(K),f(K),N(K);break;case"POST":N(K),V(K),f(K);break}}}return p}*_getIterator(e=this._root){if(e)if(this.iterationType==="ITERATIVE"){let t=[],n=e;for(;n||t.length>0;){for(;this.isRealNode(n);)t.push(n),n=n.left;n=t.pop(),this.isRealNode(n)&&(this._isMapMode?yield[n.key,this._store.get(n.key)]:yield[n.key,n.value],n=n.right)}}else e.left&&this.isRealNode(e)&&(yield*this[Symbol.iterator](e.left)),this._isMapMode?yield[e.key,this._store.get(e.key)]:yield[e.key,e.value],e.right&&this.isRealNode(e)&&(yield*this[Symbol.iterator](e.right))}_snapshotOptions(){return{iterationType:this.iterationType,toEntryFn:this.toEntryFn,isMapMode:this.isMapMode,isDuplicate:this.isDuplicate}}_createInstance(e){let t=this.constructor;return new t([],{...this._snapshotOptions(),...e!=null?e:{}})}_createLike(e=[],t){let n=this.constructor;return new n(e,{...this._snapshotOptions(),...t!=null?t:{}})}_keyValueNodeOrEntryToNodeAndValue(e,t){if(e===void 0)return[void 0,void 0];if(e===null)return[null,void 0];if(this.isNode(e))return[e,t];if(this.isEntry(e)){let[n,i]=e;if(n===void 0)return[void 0,void 0];if(n===null)return[null,void 0];let s=t!=null?t:i;return[this.createNode(n,s),s]}return[this.createNode(e,t),t]}_clone(e){this.bfs(t=>{t===null?e.add(null):this._isMapMode?e.add([t.key,this._store.get(t.key)]):e.add([t.key,t.value])},this._root,this.iterationType,!0),this._isMapMode&&(e._store=this._store)}_displayAux(e,t){let{isShowNull:n,isShowUndefined:i,isShowRedBlackNIL:s}=t,r=[["\u2500"],1,0,0];if(e===null&&!n)return r;if(e===void 0&&!i)return r;if(this.isNIL(e)&&!s)return r;if(e!=null){let d=e.key,u=this.isNIL(e)?"S":String(d),a=u.length;return l(u,a,this._displayAux(e.left,t),this._displayAux(e.right,t))}else{let d=e===void 0?"U":"N",u=d.length;return l(d,u,[[""],1,0,0],[[""],1,0,0])}function l(d,u,a,p){let[T,f,V,N]=a,[K,R,g,B]=p,x=" ".repeat(Math.max(0,N+1))+"_".repeat(Math.max(0,f-N-1))+d+"_".repeat(Math.max(0,B))+" ".repeat(Math.max(0,R-B)),D=(V>0?" ".repeat(N)+"/"+" ".repeat(f-N-1):" ".repeat(f))+" ".repeat(u)+(g>0?" ".repeat(B)+"\\"+" ".repeat(R-B-1):" ".repeat(R)),Q=[x,D];for(let b=0;b<Math.max(V,g);b++){let q=b<V?T[b]:" ".repeat(f),W=b<g?K[b]:" ".repeat(R);Q.push(q+" ".repeat(u)+W)}return[Q,f+u+R,Math.max(V,g)+2,f+Math.floor(u/2)]}}_swapProperties(e,t){if(e=this.ensureNode(e),t=this.ensureNode(t),e&&t){let{key:n,value:i}=t,s=this.createNode(n,i);return s&&(t.key=e.key,this._isMapMode||(t.value=e.value),e.key=s.key,this._isMapMode||(e.value=s.value)),t}}_replaceNode(e,t){return e.parent&&(e.parent.left===e?e.parent.left=t:e.parent.right===e&&(e.parent.right=t)),t.left=e.left,t.right=e.right,t.parent=e.parent,this._root===e&&this._setRoot(t),t}_setRoot(e){e&&(e.parent=void 0),this._root=e}_ensurePredicate(e){if(e==null)return t=>!1;if(this._isPredicate(e))return e;if(this.isRealNode(e))return t=>t===e;if(this.isEntry(e)){let[t]=e;return n=>n?n.key===t:!1}return t=>t?t.key===e:!1}_isPredicate(e){return typeof e=="function"}_extractKey(e){if(e===null)return null;if(e!==void 0&&e!==this._NIL)return this.isNode(e)?e.key:this.isEntry(e)?e[0]:e}_setValue(e,t){return e==null||t===void 0?!1:this._store.set(e,t)}_clearNodes(){this._setRoot(void 0),this._size=0}_clearValues(){this._store.clear()}};var M=class{constructor(o,e){h(this,"key");h(this,"value");h(this,"parent");h(this,"_left");h(this,"_right");h(this,"_height",0);h(this,"_color","BLACK");h(this,"_count",1);this.key=o,this.value=e}get left(){return this._left}set left(o){o&&(o.parent=this),this._left=o}get right(){return this._right}set right(o){o&&(o.parent=this),this._right=o}get height(){return this._height}set height(o){this._height=o}get color(){return this._color}set color(o){this._color=o}get count(){return this._count}set count(o){this._count=o}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},k=class extends C{constructor(e=[],t){super([],t);h(this,"_root");h(this,"_comparator");t?"comparator"in t&&t.comparator!==void 0?this._comparator=t.comparator:this._comparator=this._createDefaultComparator():this._comparator=this._createDefaultComparator(),e&&this.addMany(e)}get root(){return this._root}_createDefaultComparator(){return(e,t)=>{debugger;if(y(e)&&y(t))return e>t?1:e<t?-1:0;if(typeof e=="object"||typeof t=="object")throw TypeError("When comparing object type keys, a custom comparator must be provided in the constructor's options!");return 0}}get comparator(){return this._comparator}createNode(e,t){return new M(e,this._isMapMode?void 0:t)}ensureNode(e,t=this.iterationType){var n;return(n=super.ensureNode(e,t))!=null?n:void 0}isNode(e){return e instanceof M}isValidKey(e){return y(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 i;return(i=this.getNodes(e,!0,t,n)[0])!=null?i:void 0}search(e,t=!1,n=this._DEFAULT_NODE_CALLBACK,i=this._root,s=this.iterationType){if(e===void 0)return[];if(e===null)return[];if(i=this.ensureNode(i),!i)return[];let r,l=this.isRange(e);l?r=a=>a?e.isInRange(a.key,this._comparator):!1:r=this._ensurePredicate(e);let d=a=>{if(!a||!this.isRealNode(a.left))return!1;if(l){let p=e,T=p.low,f=p.includeLow;return f&&this._compare(a.key,T)>=0||!f&&this._compare(a.key,T)>0}if(!l&&!this._isPredicate(e)){let p=this._extractKey(e);return p!=null&&this._compare(a.key,p)>0}return!0},u=a=>{if(!a||!this.isRealNode(a.right))return!1;if(l){let p=e,T=p.high,f=p.includeHigh;return f&&this._compare(a.key,T)<=0||!f&&this._compare(a.key,T)<0}if(!l&&!this._isPredicate(e)){let p=this._extractKey(e);return p!=null&&this._compare(a.key,p)<0}return!0};return super._dfs(n,"IN",t,i,s,!1,d,u,()=>!0,a=>!!a&&r(a))}rangeSearch(e,t=this._DEFAULT_NODE_CALLBACK,n=this._root,i=this.iterationType){let s=e instanceof _?e:new _(e[0],e[1]);return this.search(s,!1,t,n,i)}add(e,t){let[n,i]=this._keyValueNodeOrEntryToNodeAndValue(e,t);if(n===void 0)return!1;if(this._root===void 0)return this._setRoot(n),this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0;let s=this._root;for(;s!==void 0;){if(this._compare(s.key,n.key)===0)return this._replaceNode(s,n),this._isMapMode&&this._setValue(s.key,i),!0;if(this._compare(s.key,n.key)>0){if(s.left===void 0)return s.left=n,this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0;s.left!==null&&(s=s.left)}else{if(s.right===void 0)return s.right=n,this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0;s.right!==null&&(s=s.right)}}return!1}addMany(e,t,n=!0,i=this.iterationType){let s=[],r=t==null?void 0:t[Symbol.iterator]();if(!n){for(let T of e){let f=r==null?void 0:r.next().value;this.isRaw(T)&&(T=this._toEntryFn(T)),s.push(this.add(T,f))}return s}let l=[],d=0;for(let T of e)l.push({key:T,value:r==null?void 0:r.next().value,orgIndex:d++});let u=l.sort(({key:T},{key:f})=>{let V,N;return this.isRaw(T)?V=this._toEntryFn(T)[0]:this.isEntry(T)?V=T[0]:this.isRealNode(T)?V=T.key:V=T,this.isRaw(f)?N=this._toEntryFn(f)[0]:this.isEntry(f)?N=f[0]:this.isRealNode(f)?N=f.key:N=f,V!=null&&N!=null?this._compare(V,N):0}),a=T=>{if(T.length===0)return;let f=Math.floor((T.length-1)/2),{key:V,value:N,orgIndex:K}=T[f];if(this.isRaw(V)){let R=this._toEntryFn(V);s[K]=this.add(R)}else s[K]=this.add(V,N);a(T.slice(0,f)),a(T.slice(f+1))};return i==="RECURSIVE"?a(u):(()=>{let f=[[0,u.length-1]];for(;f.length>0;){let V=f.pop();if(!V)continue;let[N,K]=V;if(N>K)continue;let R=N+Math.floor((K-N)/2),{key:g,value:B,orgIndex:x}=u[R];if(this.isRaw(g)){let D=this._toEntryFn(g);s[x]=this.add(D)}else s[x]=this.add(g,B);f.push([R+1,K]),f.push([N,R-1])}})(),s}lowerBound(e,t=this.iterationType){return this._bound(e,!0,t)}upperBound(e,t=this.iterationType){return this._bound(e,!1,t)}lesserOrGreaterTraverse(e=this._DEFAULT_NODE_CALLBACK,t=-1,n=this._root,i=this.iterationType){let s=this.ensureNode(n),r=[];if(!this._root||!s)return r;let l=s.key;if(i==="RECURSIVE"){let d=u=>{let a=this._compare(u.key,l);Math.sign(a)==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 m([this._root]);for(;d.length>0;){let u=d.shift();if(this.isRealNode(u)){let a=this._compare(u.key,l);Math.sign(a)==t&&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,l,d)=>{if(r>l)return;let u=r+(l-r>>1),a=t[u],p=i(r,u-1,a),T=i(u+1,l,a);return a.left=p,a.right=T,a.parent=d,a},s=i(0,n-1,void 0);return this._setRoot(s),this._size=n,!0}isAVLBalanced(e=this.iterationType){if(!this._root)return!0;let t=!0;if(e==="RECURSIVE"){let n=i=>{if(!i)return 0;let s=n(i.left),r=n(i.right);return Math.abs(s-r)>1&&(t=!1),Math.max(s,r)+1};n(this._root)}else{let n=[],i=this._root,s,r=new Map;for(;n.length>0||i;)if(i)n.push(i),i.left!==null&&(i=i.left);else if(i=n[n.length-1],!i.right||s===i.right){if(i=n.pop(),i){let l=i.left?r.get(i.left):-1,d=i.right?r.get(i.right):-1;if(Math.abs(l-d)>1)return!1;r.set(i,1+Math.max(l,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,l]of this)i.add(e.call(n,l,r,s++,this));return i}deleteWhere(e,t=!1,n=this._root,i=this.iterationType){let s=this.search(e,t,l=>l,n,i),r=[];for(let l of s){let d=this.delete(l);r=r.concat(d)}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 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=l=>{if(!this.isRealNode(l))return;let d=this.comparator(l.key,e);if(t?d>=0:d>0){let a=r(l.left);return a!=null?a:l}else return r(l.right)};return r(this.root)}else{let r=this.root,l;for(;this.isRealNode(r);){let d=this.comparator(r.key,e);(t?d>=0:d>0)?(l=r,r=(i=r.left)!=null?i:void 0):r=(s=r.right)!=null?s:void 0}return l}}_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}}_keyValueNodeOrEntryToNodeAndValue(e,t){let[n,i]=super._keyValueNodeOrEntryToNodeAndValue(e,t);return n===null?[void 0,void 0]:[n,t!=null?t:i]}_setRoot(e){e&&(e.parent=void 0),this._root=e}_compare(e,t){return this._comparator(e,t)}_deleteByKey(e){var s;let t=this._root;for(;t;){let r=this._compare(t.key,e);if(r===0)break;t=r>0?t.left:t.right}if(!t)return!1;let n=(r,l)=>{let d=r==null?void 0:r.parent;d?d.left===r?d.left=l:d.right=l:this._setRoot(l),l&&(l.parent=d)},i=r=>{if(r){for(;r.left!==void 0&&r.left!==null;)r=r.left;return r}};if(t.left===void 0)n(t,t.right);else if(t.right===void 0)n(t,t.left);else{let r=i(t.right);r.parent!==t&&(n(r,r.right),r.right=t.right,r.right&&(r.right.parent=r)),n(t,r),r.left=t.left,r.left&&(r.left.parent=r)}return this._size=Math.max(0,((s=this._size)!=null?s:0)-1),!0}};var I=class{constructor(o,e,t="BLACK"){h(this,"key");h(this,"value");h(this,"parent");h(this,"_left");h(this,"_right");h(this,"_height",0);h(this,"_color","BLACK");h(this,"_count",1);this.key=o,this.value=e,this.color=t}get left(){return this._left}set left(o){o&&(o.parent=this),this._left=o}get right(){return this._right}set right(o){o&&(o.parent=this),this._right=o}get height(){return this._height}set height(o){this._height=o}get color(){return this._color}set color(o){this._color=o}get count(){return this._count}set count(o){this._count=o}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},L=class extends k{constructor(e=[],t){super([],t);h(this,"_root");this._root=this.NIL,e&&this.addMany(e)}get root(){return this._root}createNode(e,t,n="BLACK"){return new I(e,this._isMapMode?void 0:t,n)}isNode(e){return e instanceof I}clear(){super.clear(),this._root=this.NIL}add(e,t){let[n,i]=this._keyValueNodeOrEntryToNodeAndValue(e,t);if(!this.isRealNode(n))return!1;let s=this._insert(n);if(s==="CREATED"){if(this.isRealNode(this._root))this._root.color="BLACK";else return!1;return this._isMapMode&&this._setValue(n.key,i),this._size++,!0}return s==="UPDATED"?(this._isMapMode&&this._setValue(n.key,i),!0):!1}delete(e){if(e===null)return[];let t=[],n;if(this._isPredicate(e)?n=this.getNode(e):n=this.isRealNode(e)?e:this.getNode(e),!n)return t;let i=n.color,s;if(!this.isRealNode(n.left))n.right!==null&&(s=n.right,this._transplant(n,n.right));else if(!this.isRealNode(n.right))s=n.left,this._transplant(n,n.left);else{let r=this.getLeftMost(l=>l,n.right);r&&(i=r.color,r.right!==null&&(s=r.right),r.parent===n?this.isRealNode(s)&&(s.parent=r):(r.right!==null&&(this._transplant(r,r.right),r.right=n.right),this.isRealNode(r.right)&&(r.right.parent=r)),this._transplant(n,r),r.left=n.left,this.isRealNode(r.left)&&(r.left.parent=r),r.color=n.color)}return this._isMapMode&&this._store.delete(n.key),this._size--,i==="BLACK"&&this._deleteFixup(s),t.push({deleted:n,needBalanced:void 0}),t}map(e,t,n){let i=this._createLike([],t),s=0;for(let[r,l]of this)i.add(e.call(n,l,r,s++,this));return i}_createInstance(e){let t=this.constructor;return new t([],{...this._snapshotOptions(),...e!=null?e:{}})}_createLike(e=[],t){let n=this.constructor;return new n(e,{...this._snapshotOptions(),...t!=null?t:{}})}_setRoot(e){e&&(e.parent=void 0),this._root=e}_replaceNode(e,t){return t.color=e.color,super._replaceNode(e,t)}_insert(e){var i,s,r;let t=(i=this.root)!=null?i:this.NIL,n;for(;t!==this.NIL;){n=t;let l=this._compare(e.key,t.key);if(l<0)t=(s=t.left)!=null?s:this.NIL;else if(l>0)t=(r=t.right)!=null?r:this.NIL;else return this._replaceNode(t,e),"UPDATED"}return e.parent=n,n?this._compare(e.key,n.key)<0?n.left=e:n.right=e:this._setRoot(e),e.left=this.NIL,e.right=this.NIL,e.color="RED",this._insertFixup(e),"CREATED"}_transplant(e,t){e.parent?e===e.parent.left?e.parent.left=t:e.parent.right=t:this._setRoot(t),t&&(t.parent=e.parent)}_insertFixup(e){var t,n,i,s,r;for(;((t=e==null?void 0:e.parent)==null?void 0:t.color)==="RED";)if(e.parent===((n=e.parent.parent)==null?void 0:n.left)){let l=e.parent.parent.right;(l==null?void 0:l.color)==="RED"?(e.parent.color="BLACK",l.color="BLACK",e.parent.parent.color="RED",e=e.parent.parent):(e===e.parent.right&&(e=e.parent,this._leftRotate(e)),e&&e.parent&&e.parent.parent&&(e.parent.color="BLACK",e.parent.parent.color="RED",this._rightRotate(e.parent.parent)))}else{let l=(r=(s=(i=e==null?void 0:e.parent)==null?void 0:i.parent)==null?void 0:s.left)!=null?r:void 0;(l==null?void 0:l.color)==="RED"?(e.parent.color="BLACK",l.color="BLACK",e.parent.parent.color="RED",e=e.parent.parent):(e===e.parent.left&&(e=e.parent,this._rightRotate(e)),e&&e.parent&&e.parent.parent&&(e.parent.color="BLACK",e.parent.parent.color="RED",this._leftRotate(e.parent.parent)))}this.isRealNode(this._root)&&(this._root.color="BLACK")}_deleteFixup(e){var t,n,i,s;if(!e||e===this.root||e.color==="BLACK"){e&&(e.color="BLACK");return}for(;e&&e!==this.root&&e.color==="BLACK";){let r=e.parent;if(!r)break;if(e===r.left){let l=r.right;(l==null?void 0:l.color)==="RED"&&(l.color="BLACK",r.color="RED",this._leftRotate(r),l=r.right),((n=(t=l==null?void 0:l.left)==null?void 0:t.color)!=null?n:"BLACK")==="BLACK"?(l&&(l.color="RED"),e=r):(l!=null&&l.left&&(l.left.color="BLACK"),l&&(l.color=r.color),r.color="BLACK",this._rightRotate(r),e=this.root)}else{let l=r.left;(l==null?void 0:l.color)==="RED"&&(l.color="BLACK",r&&(r.color="RED"),this._rightRotate(r),r&&(l=r.left)),((s=(i=l==null?void 0:l.right)==null?void 0:i.color)!=null?s:"BLACK")==="BLACK"?(l&&(l.color="RED"),e=r):(l!=null&&l.right&&(l.right.color="BLACK"),l&&(l.color=r.color),r&&(r.color="BLACK"),this._leftRotate(r),e=this.root)}}e&&(e.color="BLACK")}_leftRotate(e){if(!e||!e.right)return;let t=e.right;e.right=t.left,t.left&&t.left!==this.NIL&&(t.left.parent=e),t.parent=e.parent,e.parent?e===e.parent.left?e.parent.left=t:e.parent.right=t:this._setRoot(t),t.left=e,e.parent=t}_rightRotate(e){if(!e||!e.left)return;let t=e.left;e.left=t.right,t.right&&t.right!==this.NIL&&(t.right.parent=e),t.parent=e.parent,e.parent?e===e.parent.left?e.parent.left=t:e.parent.right=t:this._setRoot(t),t.right=e,e.parent=t}};var S=class{constructor(o,e=[],t="BLACK"){h(this,"key");h(this,"value");h(this,"parent");h(this,"_left");h(this,"_right");h(this,"_height",0);h(this,"_color","BLACK");h(this,"_count",1);this.key=o,this.value=e,this.color=t}get left(){return this._left}set left(o){o&&(o.parent=this),this._left=o}get right(){return this._right}set right(o){o&&(o.parent=this),this._right=o}get height(){return this._height}set height(o){this._height=o}get color(){return this._color}set color(o){this._color=o}get count(){return this._count}set count(o){this._count=o}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},G=class extends L{constructor(o=[],e){super([],{...e}),o&&this.addMany(o)}createNode(o,e=[]){return new S(o,this._isMapMode?[]:e)}isNode(o){return o instanceof S}add(o,e){if(this.isRealNode(o))return super.add(o);let t=(n,i)=>{if(n==null)return!1;let s=()=>{let l=this.get(n);if(l!==void 0&&i!==void 0){for(let d of i)l.push(d);return!0}return!1},r=()=>{let l=this.getNode(n);if(this.isRealNode(l)){let d=this.get(l);if(d===void 0)return super.add(n,i),!0;if(i!==void 0){for(let u of i)d.push(u);return!0}else return!1}else return super.add(n,i)};return this._isMapMode?r()||s():s()||r()};if(this.isEntry(o)){let[n,i]=o;return t(n,e!==void 0?[e]:i)}return t(o,e!==void 0?[e]:void 0)}deleteValue(o,e){let t=this.get(o);if(Array.isArray(t)){let n=t.indexOf(e);return n===-1?!1:(t.splice(n,1),t.length===0&&this.delete(o),!0)}return!1}map(o,e,t){let n=this._createLike([],e),i=0;for(let[s,r]of this)n.add(o.call(t,r,s,i++,this));return n}_createInstance(o){var t,n;let e=this.constructor;return new e([],{...(n=(t=this._snapshotOptions)==null?void 0:t.call(this))!=null?n:{},...o!=null?o:{}})}_createLike(o=[],e){var n,i;let t=this.constructor;return new t(o,{...(i=(n=this._snapshotOptions)==null?void 0:n.call(this))!=null?i:{},...e!=null?e:{}})}};return ee(ie);})();
|
|
6
6
|
/**
|
|
7
7
|
* data-structure-typed
|
|
8
8
|
*
|