avl-tree-typed 2.5.1 → 2.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +423 -52
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +423 -51
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +423 -52
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +423 -51
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/common/error.d.ts +9 -0
- package/dist/types/common/index.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +36 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +42 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +77 -2
- package/dist/types/data-structures/binary-tree/bst.d.ts +171 -0
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +57 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +18 -0
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +409 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +411 -6
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +339 -6
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +391 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +30 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +27 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +33 -0
- package/dist/types/data-structures/heap/heap.d.ts +42 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +51 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +45 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +54 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +24 -0
- package/dist/types/data-structures/queue/deque.d.ts +45 -0
- package/dist/types/data-structures/queue/queue.d.ts +36 -0
- package/dist/types/data-structures/stack/stack.d.ts +30 -0
- package/dist/types/data-structures/trie/trie.d.ts +36 -0
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
- package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
- package/dist/umd/avl-tree-typed.js +420 -49
- package/dist/umd/avl-tree-typed.js.map +1 -1
- package/dist/umd/avl-tree-typed.min.js +3 -3
- package/dist/umd/avl-tree-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/common/error.ts +19 -1
- package/src/common/index.ts +1 -1
- package/src/data-structures/base/iterable-element-base.ts +3 -2
- package/src/data-structures/binary-tree/avl-tree.ts +47 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +46 -4
- package/src/data-structures/binary-tree/binary-tree.ts +79 -4
- package/src/data-structures/binary-tree/bst.ts +441 -6
- package/src/data-structures/binary-tree/red-black-tree.ts +73 -0
- package/src/data-structures/binary-tree/segment-tree.ts +18 -0
- package/src/data-structures/binary-tree/tree-map.ts +434 -9
- package/src/data-structures/binary-tree/tree-multi-map.ts +426 -5
- package/src/data-structures/binary-tree/tree-multi-set.ts +350 -6
- package/src/data-structures/binary-tree/tree-set.ts +410 -8
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- package/src/data-structures/graph/directed-graph.ts +30 -0
- package/src/data-structures/graph/undirected-graph.ts +27 -0
- package/src/data-structures/hash/hash-map.ts +35 -4
- package/src/data-structures/heap/heap.ts +46 -4
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +51 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +45 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +59 -5
- package/src/data-structures/matrix/matrix.ts +33 -9
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +45 -0
- package/src/data-structures/queue/queue.ts +36 -0
- package/src/data-structures/stack/stack.ts +30 -0
- package/src/data-structures/trie/trie.ts +38 -2
- package/src/types/data-structures/binary-tree/bst.ts +1 -0
- package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
- package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
- package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
"use strict";var avlTreeTyped=(()=>{var L=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var $=Object.getOwnPropertyNames;var z=Object.prototype.hasOwnProperty;var G=(a,o,e)=>o in a?L(a,o,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[o]=e;var Q=(a,o)=>{for(var e in o)L(a,e,{get:o[e],enumerable:!0})},j=(a,o,e,t)=>{if(o&&typeof o=="object"||typeof o=="function")for(let n of $(o))!z.call(a,n)&&n!==e&&L(a,n,{get:()=>o[n],enumerable:!(t=H(o,n))||t.enumerable});return a};var q=a=>j(L({},"__esModule",{value:!0}),a);var p=(a,o,e)=>G(a,typeof o!="symbol"?o+"":o,e);var J={};Q(J,{AVLTree:()=>O,AVLTreeNode:()=>I,BST:()=>S,BSTNode:()=>E,BinaryTree:()=>C,BinaryTreeNode:()=>_});function w(a){let o=typeof a;return o==="number"?!0:o==="bigint"||o==="string"||o==="boolean"}function F(a){if(typeof a.valueOf=="function"){let o=a.valueOf();if(o!==a){if(w(o))return o;if(typeof o=="object"&&o!==null)return F(o)}}if(typeof a.toString=="function"){let o=a.toString();if(o!=="[object Object]")return o}return null}function m(a,o=!1){if(a==null)return!1;if(w(a))return!0;if(typeof a!="object")return!1;if(a instanceof Date||o)return!0;let e=F(a);return e==null?!1:w(e)}var v=a=>({isThunk:!0,fn:a}),W=a=>typeof a=="object"&&a!==null&&"isThunk"in a&&a.isThunk;function X(a){let o=a;for(;W(o);)o=o.fn();return o}function A(a){return(...o)=>X(a(...o))}var k=class{constructor(o){p(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 r=n.next();if(r.done)throw new TypeError("Reduce of empty structure with no initial value");i=r.value,t=1}for(let r of n)i=o(i,r,t++,this);return i}toArray(){return[...this]}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var M=class a extends k{constructor(e){super(e);p(this,"_maxLen",-1);if(e){let{maxLen:t}=e;typeof t=="number"&&t>0&&t%1===0&&(this._maxLen=t)}}get maxLen(){return this._maxLen}indexOf(e,t=0){if(this.length===0)return-1;t<0&&(t=this.length+t),t<0&&(t=0);for(let n=t;n<this.length;n++)if(this.at(n)===e)return n;return-1}lastIndexOf(e,t=this.length-1){if(this.length===0)return-1;t>=this.length&&(t=this.length-1),t<0&&(t=this.length+t);for(let n=t;n>=0;n--)if(this.at(n)===e)return n;return-1}findIndex(e,t){for(let n=0;n<this.length;n++){let i=this.at(n);if(i!==void 0&&e.call(t,i,n,this))return n}return-1}concat(...e){let t=this.clone();for(let n of e)n instanceof 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 B={indexOutOfRange:(a,o,e,t)=>`${t?t+": ":""}Index ${a} is out of range [${o}, ${e}].`,invalidIndex:a=>`${a?a+": ":""}Index must be an integer.`,invalidArgument:(a,o)=>`${o?o+": ":""}${a}`,comparatorRequired:a=>`${a?a+": ":""}Comparator is required for non-number/non-string/non-Date keys.`,invalidKey:(a,o)=>`${o?o+": ":""}${a}`,notAFunction:(a,o)=>`${o?o+": ":""}${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,o,e)=>`${e?e+": ":""}Callback must return ${a}; got ${o}.`,invalidOperation:(a,o)=>`${o?o+": ":""}${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,o)=>`Matrix: Expected row length ${a}, but got ${o}.`};var g=class{constructor(o,e,t=!0,n=!0){this.low=o;this.high=e;this.includeLow=t;this.includeHigh=n}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 x=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,r]=n;o.call(e,r,i,t++,this)}}find(o,e){let t=0;for(let n of this){let[i,r]=n;if(o.call(e,r,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[r,s]=i;t=o(t,s,r,n++,this)}return t}toArray(){return[...this]}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var R=class a extends M{constructor(e=[],t){super(t);p(this,"_elements",[]);p(this,"_offset",0);p(this,"_autoCompactRatio",.5);if(t){let{autoCompactRatio:n=.5}=t;this._autoCompactRatio=n}this.pushMany(e)}get elements(){return this._elements}get offset(){return this._offset}get autoCompactRatio(){return this._autoCompactRatio}set autoCompactRatio(e){this._autoCompactRatio=e}get length(){return this.elements.length-this._offset}get first(){return this.length>0?this.elements[this._offset]:void 0}get last(){return this.length>0?this.elements[this.elements.length-1]:void 0}static fromArray(e){return new 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,l;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:(l=t==null?void 0:t.autoCompactRatio)!=null?l:this._autoCompactRatio}),r=0;for(let d of this)i.push(n===void 0?e(d,r++,this):e.call(n,d,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 l of this){let d=t===void 0?e(l,r++,this):e.call(t,l,r++,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 _=class{constructor(o,e){p(this,"key");p(this,"value");p(this,"parent");p(this,"_left");p(this,"_right");p(this,"_height",0);p(this,"_color","BLACK");p(this,"_count",1);this.key=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 a extends x{constructor(e=[],t){super();p(this,"iterationType","ITERATIVE");p(this,"_isMapMode",!0);p(this,"_isDuplicate",!1);p(this,"_store",new Map);p(this,"_root");p(this,"_size",0);p(this,"_NIL",new _(NaN));p(this,"_toEntryFn");p(this,"_DEFAULT_NODE_CALLBACK",e=>e==null?void 0:e.key);if(t){let{iterationType:n,toEntryFn:i,isMapMode:r,isDuplicate:s}=t;if(n&&(this.iterationType=n),r!==void 0&&(this._isMapMode=r),s!==void 0&&(this._isDuplicate=s),typeof i=="function")this._toEntryFn=i;else if(i)throw new TypeError(B.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 _(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 _}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 g}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:m(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 R([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 l=i.next();l.done||(s=l.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 l=this.getRightMost(d=>d,n.left);if(l){let d=l.parent;s=this._swapProperties(n,l),this._isMapMode&&(this._store.set(n.key,n),this._store.set(l.key,l)),d&&(d.right===l?d.right=l.left:d.left=l.left,r=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),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),l=[];if(r==="RECURSIVE"){let d=u=>{s(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(s(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,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=(l,d,u)=>{if(!this.isRealNode(l))return!0;let f=Number(l.key);return f<=d||f>=u?!1:i(l.left,d,f)&&i(l.right,f,u)},r=i(n,Number.MIN_SAFE_INTEGER,Number.MAX_SAFE_INTEGER),s=i(n,Number.MAX_SAFE_INTEGER,Number.MIN_SAFE_INTEGER);return r||s}else{let i=(l=!1)=>{let d=[],u=l?Number.MAX_SAFE_INTEGER:Number.MIN_SAFE_INTEGER,f=n;for(;this.isRealNode(f)||d.length>0;){for(;this.isRealNode(f);)d.push(f),f=f.left;f=d.pop();let h=Number(f.key);if(!this.isRealNode(f)||!l&&u>=h||l&&u<=h)return!1;u=h,f=f.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 l=this.isRealNode(i.left)?s.get(i.left):-1,d=this.isRealNode(i.right)?s.get(i.right):-1;s.set(i,1+Math.min(l,d)),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:l}=s;return this.isRealNode(l)?r(l):s};return e(r(i))}else{let r=A(s=>{let{left:l}=s;return this.isRealNode(l)?v(()=>r(l)):s});return e(r(i))}}getRightMost(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){if(this.isNIL(t)||(t=this.ensureNode(t),!t))return e(void 0);if(n==="RECURSIVE"){let i=r=>{let{right:s}=r;return this.isRealNode(s)?i(s):r};return e(i(t))}else{let i=A(r=>{let{right:s}=r;return this.isRealNode(s)?v(()=>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 R([t]),l=d=>{if(s.length===0)return;let u=s.shift();r.push(e(u)),i?(u&&this.isRealNodeOrNull(u.left)&&s.push(u.left),u&&this.isRealNodeOrNull(u.right)&&s.push(u.right)):(this.isRealNode(u.left)&&s.push(u.left),this.isRealNode(u.right)&&s.push(u.right)),l(d+1)};l(0)}else{let s=new R([t]);for(;s.length>0;){let l=s.length;for(let d=0;d<l;d++){let u=s.shift();r.push(e(u)),i?(u&&this.isRealNodeOrNull(u.left)&&s.push(u.left),u&&this.isRealNodeOrNull(u.right)&&s.push(u.right)):(this.isRealNode(u.left)&&s.push(u.left),this.isRealNode(u.right)&&s.push(u.right))}}}return r}leaves(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){t=this.ensureNode(t);let i=[];if(!this.isRealNode(t))return[];if(n==="RECURSIVE"){let r=s=>{this.isLeaf(s)&&i.push(e(s)),!(!this.isRealNode(s.left)&&!this.isRealNode(s.right))&&(this.isRealNode(s.left)&&r(s.left),this.isRealNode(s.right)&&r(s.right))};r(t)}else{let r=[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=(l,d)=>{r[d]||(r[d]=[]),r[d].push(e(l)),i?(l&&this.isRealNodeOrNull(l.left)&&s(l.left,d+1),l&&this.isRealNodeOrNull(l.right)&&s(l.right,d+1)):(l&&l.left&&s(l.left,d+1),l&&l.right&&s(l.right,d+1))};s(t,0)}else{let s=[[t,0]];for(;s.length>0;){let l=s.pop(),[d,u]=l;r[u]||(r[u]=[]),r[u].push(e(d)),i?(d&&this.isRealNodeOrNull(d.right)&&s.push([d.right,u+1]),d&&this.isRealNodeOrNull(d.left)&&s.push([d.left,u+1])):(d&&d.right&&s.push([d.right,u+1]),d&&d.left&&s.push([d.left,u+1]))}}return r}morris(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=this._root){if(n=this.ensureNode(n),!n)return[];let i=[],r=n,s=d=>{let u=null,f=null;for(;d;)f=d.right,d.right=u,u=d,d=f;return u},l=d=>{let u=s(d),f=u;for(;f;)i.push(e(f)),f=f.right;s(u)};switch(t){case"IN":for(;r;){if(r.left){let d=this.getPredecessor(r);if(d.right)d.right=null;else{d.right=r,r=r.left;continue}}i.push(e(r)),r=r.right}break;case"PRE":for(;r;){if(r.left){let d=this.getPredecessor(r);if(d.right)d.right=null;else{d.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 d=this.getPredecessor(r);if(d.right===null){d.right=r,r=r.left;continue}else d.right=null,l(r.left)}r=r.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[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,l]of this)i.set(e.call(n,l,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 avlTreeTyped=(()=>{var k=Object.defineProperty;var $=Object.getOwnPropertyDescriptor;var z=Object.getOwnPropertyNames;var G=Object.prototype.hasOwnProperty;var Q=(a,o,e)=>o in a?k(a,o,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[o]=e;var j=(a,o)=>{for(var e in o)k(a,e,{get:o[e],enumerable:!0})},q=(a,o,e,t)=>{if(o&&typeof o=="object"||typeof o=="function")for(let n of z(o))!G.call(a,n)&&n!==e&&k(a,n,{get:()=>o[n],enumerable:!(t=$(o,n))||t.enumerable});return a};var W=a=>q(k({},"__esModule",{value:!0}),a);var p=(a,o,e)=>Q(a,typeof o!="symbol"?o+"":o,e);var Y={};j(Y,{AVLTree:()=>D,AVLTreeNode:()=>L,BST:()=>I,BSTNode:()=>E,BinaryTree:()=>S,BinaryTreeNode:()=>b});function v(a){let o=typeof a;return o==="number"?!0:o==="bigint"||o==="string"||o==="boolean"}function U(a){if(typeof a.valueOf=="function"){let o=a.valueOf();if(o!==a){if(v(o))return o;if(typeof o=="object"&&o!==null)return U(o)}}if(typeof a.toString=="function"){let o=a.toString();if(o!=="[object Object]")return o}return null}function m(a,o=!1){if(a==null)return!1;if(v(a))return!0;if(typeof a!="object")return!1;if(a instanceof Date||o)return!0;let e=U(a);return e==null?!1:v(e)}var A=a=>({isThunk:!0,fn:a}),X=a=>typeof a=="object"&&a!==null&&"isThunk"in a&&a.isThunk;function J(a){let o=a;for(;X(o);)o=o.fn();return o}function O(a){return(...o)=>J(a(...o))}function g(a,o){throw new a(o)}var R={indexOutOfRange:(a,o,e,t)=>`${t?t+": ":""}Index ${a} is out of range [${o}, ${e}].`,invalidIndex:a=>`${a?a+": ":""}Index must be an integer.`,invalidArgument:(a,o)=>`${o?o+": ":""}${a}`,comparatorRequired:a=>`${a?a+": ":""}Comparator is required for non-number/non-string/non-Date keys.`,invalidKey:(a,o)=>`${o?o+": ":""}${a}`,notAFunction:(a,o)=>`${o?o+": ":""}${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,o,e)=>`${e?e+": ":""}Callback must return ${a}; got ${o}.`,invalidOperation:(a,o)=>`${o?o+": ":""}${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,o)=>`Matrix: Expected row length ${a}, but got ${o}.`,orderStatisticNotEnabled:(a,o)=>`${o?o+": ":""}${a}() requires enableOrderStatistic: true.`};var _=class{constructor(o,e,t=!0,n=!0){this.low=o;this.high=e;this.includeLow=t;this.includeHigh=n}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 x=class{constructor(o){p(this,"_toElementFn");if(o){let{toElementFn:e}=o;typeof e=="function"?this._toElementFn=e:e&&g(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 r=n.next();r.done&&g(TypeError,"Reduce of empty structure with no initial value"),i=r.value,t=1}for(let r of n)i=o(i,r,t++,this);return i}toArray(){return[...this]}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var M=class a extends x{constructor(e){super(e);p(this,"_maxLen",-1);if(e){let{maxLen:t}=e;typeof t=="number"&&t>0&&t%1===0&&(this._maxLen=t)}}get maxLen(){return this._maxLen}indexOf(e,t=0){if(this.length===0)return-1;t<0&&(t=this.length+t),t<0&&(t=0);for(let n=t;n<this.length;n++)if(this.at(n)===e)return n;return-1}lastIndexOf(e,t=this.length-1){if(this.length===0)return-1;t>=this.length&&(t=this.length-1),t<0&&(t=this.length+t);for(let n=t;n>=0;n--)if(this.at(n)===e)return n;return-1}findIndex(e,t){for(let n=0;n<this.length;n++){let i=this.at(n);if(i!==void 0&&e.call(t,i,n,this))return n}return-1}concat(...e){let t=this.clone();for(let n of e)n instanceof 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 w=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,r]=n;o.call(e,r,i,t++,this)}}find(o,e){let t=0;for(let n of this){let[i,r]=n;if(o.call(e,r,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[r,s]=i;t=o(t,s,r,n++,this)}return t}toArray(){return[...this]}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var B=class a extends M{constructor(e=[],t){super(t);p(this,"_elements",[]);p(this,"_offset",0);p(this,"_autoCompactRatio",.5);if(t){let{autoCompactRatio:n=.5}=t;this._autoCompactRatio=n}this.pushMany(e)}get elements(){return this._elements}get offset(){return this._offset}get autoCompactRatio(){return this._autoCompactRatio}set autoCompactRatio(e){this._autoCompactRatio=e}get length(){return this.elements.length-this._offset}get first(){return this.length>0?this.elements[this._offset]:void 0}get last(){return this.length>0?this.elements[this.elements.length-1]:void 0}static fromArray(e){return new 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,d;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:(d=t==null?void 0:t.autoCompactRatio)!=null?d:this._autoCompactRatio}),r=0;for(let l of this)i.push(n===void 0?e(l,r++,this):e.call(n,l,r++,this));return i}mapSame(e,t){var s;let n=this.constructor,i=new n([],{toElementFn:this.toElementFn,maxLen:this._maxLen,autoCompactRatio:this._autoCompactRatio});(s=i._setAutoCompactRatio)==null||s.call(i,this._autoCompactRatio);let r=0;for(let d of this){let l=t===void 0?e(d,r++,this):e.call(t,d,r++,this);i.push(l)}return i}_setAutoCompactRatio(e){this._autoCompactRatio=e}*_getIterator(){for(let e=this._offset;e<this.elements.length;e++)yield this.elements[e]}*_getReverseIterator(){for(let e=this.length-1;e>=0;e--){let t=this.at(e);t!==void 0&&(yield t)}}_createInstance(e){let t=this.constructor;return new t([],e)}_createLike(e=[],t){let n=this.constructor;return new n(e,t)}};var b=class{constructor(o,e){p(this,"key");p(this,"value");p(this,"parent");p(this,"_left");p(this,"_right");p(this,"_height",0);p(this,"_color","BLACK");p(this,"_count",1);this.key=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"}},S=class a extends w{constructor(e=[],t){super();p(this,"iterationType","ITERATIVE");p(this,"_isMapMode",!0);p(this,"_isDuplicate",!1);p(this,"_store",new Map);p(this,"_root");p(this,"_size",0);p(this,"_NIL",new b(NaN));p(this,"_toEntryFn");p(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&&g(TypeError,R.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 b(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 b}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:m(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 B([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 d=i.next();d.done||(s=d.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 d=this.getRightMost(l=>l,n.left);if(d){let l=d.parent;s=this._swapProperties(n,d),this._isMapMode&&(this._store.set(n.key,n),this._store.set(d.key,d)),l&&(l.right===d?l.right=d.left:l.left=d.left,r=l)}}else if(i){let{familyPosition:d}=n;d==="LEFT"||d==="ROOT_LEFT"?i.left=n.right:(d==="RIGHT"||d==="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),d=[];if(r==="RECURSIVE"){let l=u=>{s(u)&&(d.push(n(u)),t)||!this.isRealNode(u.left)&&!this.isRealNode(u.right)||(this.isRealNode(u.left)&&l(u.left),this.isRealNode(u.right)&&l(u.right))};l(i)}else{let l=[i];for(;l.length>0;){let u=l.pop();if(this.isRealNode(u)){if(s(u)&&(d.push(n(u)),t))return d;this.isRealNode(u.left)&&l.push(u.left),this.isRealNode(u.right)&&l.push(u.right)}}}return d}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=(d,l,u)=>{if(!this.isRealNode(d))return!0;let f=Number(d.key);return f<=l||f>=u?!1:i(d.left,l,f)&&i(d.right,f,u)},r=i(n,Number.MIN_SAFE_INTEGER,Number.MAX_SAFE_INTEGER),s=i(n,Number.MAX_SAFE_INTEGER,Number.MIN_SAFE_INTEGER);return r||s}else{let i=(d=!1)=>{let l=[],u=d?Number.MAX_SAFE_INTEGER:Number.MIN_SAFE_INTEGER,f=n;for(;this.isRealNode(f)||l.length>0;){for(;this.isRealNode(f);)l.push(f),f=f.left;f=l.pop();let h=Number(f.key);if(!this.isRealNode(f)||!d&&u>=h||d&&u<=h)return!1;u=h,f=f.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 d=this.isRealNode(i.left)?s.get(i.left):-1,l=this.isRealNode(i.right)?s.get(i.right):-1;s.set(i,1+Math.min(d,l)),r=i,i=null}}else i=i.right;return s.get(e)}}getPathToRoot(e,t=this._DEFAULT_NODE_CALLBACK,n=!1){let i=[],r=this.ensureNode(e);if(!r)return i;for(;r.parent;)i.push(t(r)),r=r.parent;return i.push(t(r)),n?i.reverse():i}getLeftMost(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){if(this.isNIL(t))return e(void 0);let i=this.ensureNode(t);if(!this.isRealNode(i))return e(void 0);if(n==="RECURSIVE"){let r=s=>{let{left:d}=s;return this.isRealNode(d)?r(d):s};return e(r(i))}else{let r=O(s=>{let{left:d}=s;return this.isRealNode(d)?A(()=>r(d)):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=O(r=>{let{right:s}=r;return this.isRealNode(s)?A(()=>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 B([t]),d=l=>{if(s.length===0)return;let u=s.shift();r.push(e(u)),i?(u&&this.isRealNodeOrNull(u.left)&&s.push(u.left),u&&this.isRealNodeOrNull(u.right)&&s.push(u.right)):(this.isRealNode(u.left)&&s.push(u.left),this.isRealNode(u.right)&&s.push(u.right)),d(l+1)};d(0)}else{let s=new B([t]);for(;s.length>0;){let d=s.length;for(let l=0;l<d;l++){let u=s.shift();r.push(e(u)),i?(u&&this.isRealNodeOrNull(u.left)&&s.push(u.left),u&&this.isRealNodeOrNull(u.right)&&s.push(u.right)):(this.isRealNode(u.left)&&s.push(u.left),this.isRealNode(u.right)&&s.push(u.right))}}}return r}leaves(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){t=this.ensureNode(t);let i=[];if(!this.isRealNode(t))return[];if(n==="RECURSIVE"){let r=s=>{this.isLeaf(s)&&i.push(e(s)),!(!this.isRealNode(s.left)&&!this.isRealNode(s.right))&&(this.isRealNode(s.left)&&r(s.left),this.isRealNode(s.right)&&r(s.right))};r(t)}else{let r=[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=(d,l)=>{r[l]||(r[l]=[]),r[l].push(e(d)),i?(d&&this.isRealNodeOrNull(d.left)&&s(d.left,l+1),d&&this.isRealNodeOrNull(d.right)&&s(d.right,l+1)):(d&&d.left&&s(d.left,l+1),d&&d.right&&s(d.right,l+1))};s(t,0)}else{let s=[[t,0]];for(;s.length>0;){let d=s.pop(),[l,u]=d;r[u]||(r[u]=[]),r[u].push(e(l)),i?(l&&this.isRealNodeOrNull(l.right)&&s.push([l.right,u+1]),l&&this.isRealNodeOrNull(l.left)&&s.push([l.left,u+1])):(l&&l.right&&s.push([l.right,u+1]),l&&l.left&&s.push([l.left,u+1]))}}return r}morris(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=this._root){if(n=this.ensureNode(n),!n)return[];let i=[],r=n,s=l=>{let u=null,f=null;for(;l;)f=l.right,l.right=u,u=l,l=f;return u},d=l=>{let u=s(l),f=u;for(;f;)i.push(e(f)),f=f.right;s(u)};switch(t){case"IN":for(;r;){if(r.left){let l=this.getPredecessor(r);if(l.right)l.right=null;else{l.right=r,r=r.left;continue}}i.push(e(r)),r=r.right}break;case"PRE":for(;r;){if(r.left){let l=this.getPredecessor(r);if(l.right)l.right=null;else{l.right=r,i.push(e(r)),r=r.left;continue}}else i.push(e(r));r=r.right}break;case"POST":for(;r;){if(r.left){let l=this.getPredecessor(r);if(l.right===null){l.right=r,r=r.left;continue}else l.right=null,d(r.left)}r=r.right}d(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,d]of this)i.set(e.call(n,d,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
|
-
`),(s=>{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,r=this.iterationType,s=!1,l=h=>!!h,d=h=>!!h,u=h=>s?this.isRealNodeOrNull(h):this.isRealNode(h),f=h=>this.isRealNodeOrNull(h)){if(i=this.ensureNode(i),!i)return[];let h=[];if(r==="RECURSIVE"){let c=T=>{if(!u(T))return;let V=()=>{l(T)&&(T==null?void 0:T.left)!==void 0&&c(T==null?void 0:T.left)},N=()=>{d(T)&&(T==null?void 0:T.right)!==void 0&&c(T==null?void 0:T.right)};switch(t){case"IN":if(V(),f(T)&&(h.push(e(T)),n))return;N();break;case"PRE":if(f(T)&&(h.push(e(T)),n))return;V(),N();break;case"POST":if(V(),N(),f(T)&&(h.push(e(T)),n))return;break}};c(i)}else{let c=[{opt:0,node:i}],T=K=>{var y;l(K.node)&&c.push({opt:0,node:(y=K.node)==null?void 0:y.left})},V=K=>{var y;d(K.node)&&c.push({opt:0,node:(y=K.node)==null?void 0:y.right})},N=K=>{u(K.node)&&c.push({opt:1,node:K.node})};for(;c.length>0;){let K=c.pop();if(K!==void 0&&u(K.node))if(K.opt===1){if(f(K.node)&&K.node!==void 0&&(h.push(e(K.node)),n))return h}else switch(t){case"IN":V(K),N(K),T(K);break;case"PRE":V(K),T(K),N(K);break;case"POST":N(K),V(K),T(K);break}}}return h}*_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=d=>({node:d,stage:0,leftLayout:n,rightLayout:n}),r=[i(e)],s=n,l=d=>{if(r.length===0){s=d;return}let u=r[r.length-1];u.stage===1?u.leftLayout=d:u.rightLayout=d};for(;r.length>0;){let d=r[r.length-1],u=d.node;if(d.stage===0){if(this._isDisplayLeaf(u,t)){r.pop();let f=this._resolveDisplayLeaf(u,t,n);l(f);continue}d.stage=1,r.push(i(u.left))}else if(d.stage===1)d.stage=2,r.push(i(u.right));else{r.pop();let f=this.isNIL(u)?"S":String(u.key),h=a._buildNodeDisplay(f,f.length,d.leftLayout,d.rightLayout);l(h)}}return s}static _buildNodeDisplay(e,t,n,i){let[r,s,l,d]=n,[u,f,h,c]=i,T=" ".repeat(Math.max(0,d+1))+"_".repeat(Math.max(0,s-d-1))+e+"_".repeat(Math.max(0,c))+" ".repeat(Math.max(0,f-c)),V=(l>0?" ".repeat(d)+"/"+" ".repeat(s-d-1):" ".repeat(s))+" ".repeat(t)+(h>0?" ".repeat(c)+"\\"+" ".repeat(f-c-1):" ".repeat(f)),N=[T,V];for(let K=0;K<Math.max(l,h);K++){let y=K<l?r[K]:" ".repeat(s),b=K<h?u[K]:" ".repeat(f);N.push(y+" ".repeat(t)+b)}return[N,s+t+f,Math.max(l,h)+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),l=this._hasDisplayableChild(e.right,t);return!s&&!l}_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 d=this.isNIL(e)?"S":String(e.key);return a._buildNodeDisplay(d,d.length,n,n)}let l=e===void 0?"U":"N";return a._buildNodeDisplay(l,l.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 E=class{constructor(o,e){p(this,"key");p(this,"value");p(this,"parent");p(this,"_left");p(this,"_right");p(this,"_height",0);p(this,"_color","BLACK");p(this,"_count",1);this.key=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"}},S=class extends C{constructor(e=[],t){super([],t);p(this,"_root");p(this,"_comparator");t?"comparator"in t&&t.comparator!==void 0?this._comparator=t.comparator:this._comparator=this._createDefaultComparator():this._comparator=this._createDefaultComparator(),e&&this.setMany(e)}get root(){return this._root}get comparator(){return this._comparator}createNode(e,t){return new E(e,t)}ensureNode(e,t=this.iterationType){var n;return(n=super.ensureNode(e,t))!=null?n:void 0}isNode(e){return e instanceof E}isValidKey(e){return m(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 u,f;if(e==null)return;if(this._isPredicate(e))return(u=this.getNodes(e,!0,t,n)[0])!=null?u:void 0;if(e instanceof g)return(f=this.getNodes(e,!0,t,n)[0])!=null?f:void 0;let i;if(this.isNode(e))i=e.key;else if(this.isEntry(e)){let h=e[0];if(h==null)return;i=h}else i=e;let r=this.ensureNode(t);if(!r)return;let s=this._NIL,l=r,d=this._comparator;for(;l&&l!==s;){let h=d(i,l.key);if(h===0)return l;l=h<0?l._left:l._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),l=!s&&this._isPredicate(e);if(!s&&!l){let h;if(this.isNode(e))h=e.key;else if(this.isEntry(e)){let N=e[0];N!=null&&(h=N)}else h=e;if(h===void 0)return[];let c=this._NIL,T=this._comparator,V=i;for(;V&&V!==c;){let N=T(h,V.key);if(N===0)return[n(V)];V=N<0?V._left:V._right}return[]}let d;s?d=h=>h?e.isInRange(h.key,this._comparator):!1:d=this._ensurePredicate(e);let u=h=>{if(!h||!this.isRealNode(h.left))return!1;if(s){let c=e,T=c.low,V=c.includeLow;return V&&this._compare(h.key,T)>=0||!V&&this._compare(h.key,T)>0}if(!s&&!this._isPredicate(e)){let c=this._extractKey(e);return c!=null&&this._compare(h.key,c)>0}return!0},f=h=>{if(!h||!this.isRealNode(h.right))return!1;if(s){let c=e,T=c.high,V=c.includeHigh;return V&&this._compare(h.key,T)<=0||!V&&this._compare(h.key,T)<0}if(!s&&!this._isPredicate(e)){let c=this._extractKey(e);return c!=null&&this._compare(h.key,c)<0}return!0};return super._dfs(n,"IN",t,i,r,!1,u,f,()=>!0,h=>!!h&&d(h))}rangeSearch(e,t=this._DEFAULT_NODE_CALLBACK,n=this._root,i=this.iterationType){let r=e instanceof g?e:new g(e[0],e[1]);return this.search(r,!1,t,n,i)}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++,!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++,!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++,!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 T=s==null?void 0:s.next().value;this.isRaw(c)&&(c=this._toEntryFn(c)),r.push(this.set(c,T))}return r}let l=[],d=0;for(let c of e)l.push({key:c,value:s==null?void 0:s.next().value,orgIndex:d++});let u=l.sort(({key:c},{key:T})=>{let V,N;return this.isRaw(c)?V=this._toEntryFn(c)[0]:this.isEntry(c)?V=c[0]:this.isRealNode(c)?V=c.key:V=c,this.isRaw(T)?N=this._toEntryFn(T)[0]:this.isEntry(T)?N=T[0]:this.isRealNode(T)?N=T.key:N=T,V!=null&&N!=null?this._compare(V,N):0}),f=c=>{if(c.length===0)return;let T=Math.floor((c.length-1)/2),{key:V,value:N,orgIndex:K}=c[T];if(this.isRaw(V)){let y=this._toEntryFn(V);r[K]=this.set(y)}else r[K]=this.set(V,N);f(c.slice(0,T)),f(c.slice(T+1))};return i==="RECURSIVE"?f(u):(()=>{let T=[[0,u.length-1]];for(;T.length>0;){let V=T.pop();if(!V)continue;let[N,K]=V;if(N>K)continue;let y=N+Math.floor((K-N)/2),{key:b,value:P,orgIndex:D}=u[y];if(this.isRaw(b)){let U=this._toEntryFn(b);r[D]=this.set(U)}else r[D]=this.set(b,P);T.push([y+1,K]),T.push([N,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 l=this._floorByPredicate(e,r);return i?l?i(l):void 0:l==null?void 0:l.key}let s;if(this.isNode(e))s=e.key;else if(this.isEntry(e)){let l=e[0];if(l==null)return void 0;s=l}else s=e;if(s!==void 0){let l=this._floorByKey(s,r);return i?l?i(l):void 0:l==null?void 0:l.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 l=this._lowerByPredicate(e,r);return i?l?i(l):void 0:l==null?void 0:l.key}let s;if(this.isNode(e))s=e.key;else if(this.isEntry(e)){let l=e[0];if(l==null)return void 0;s=l}else s=e;if(s!==void 0){let l=this._lowerByKey(s,r);return i?l?i(l):void 0:l==null?void 0:l.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 l=r.key;if(i==="RECURSIVE"){let d=u=>{let f=this._compare(u.key,l);Math.sign(f)==t&&s.push(e(u)),this.isRealNode(u.left)&&d(u.left),this.isRealNode(u.right)&&d(u.right)};return d(this._root),s}else{let d=new R([this._root]);for(;d.length>0;){let u=d.shift();if(this.isRealNode(u)){let f=this._compare(u.key,l);Math.sign(f)==t&&s.push(e(u)),this.isRealNode(u.left)&&d.push(u.left),this.isRealNode(u.right)&&d.push(u.right)}}return s}}perfectlyBalance(e=this.iterationType){let t=this.dfs(s=>s,"IN",!1,this._root,e),n=t.length;if(this._clearNodes(),n===0)return!1;let i=(s,l,d)=>{if(s>l)return;let u=s+(l-s>>1),f=t[u],h=i(s,u-1,f),c=i(u+1,l,f);return f.left=h,f.right=c,f.parent=d,f},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 l=i.left?s.get(i.left):-1,d=i.right?s.get(i.right):-1;if(Math.abs(l-d)>1)return!1;s.set(i,1+Math.max(l,d)),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,l]of this)i.set(e.call(n,l,s,r++,this));return i}deleteWhere(e,t=!1,n=this._root,i=this.iterationType){let r=this.search(e,t,l=>l,n,i),s=[];for(let l of r){let d=this.delete(l);s=s.concat(d)}return s}_createDefaultComparator(){return(e,t)=>{if(m(e)&&m(t))return e>t?1:e<t?-1:0;if(e instanceof Date&&t instanceof Date){let n=e.getTime(),i=t.getTime();if(Number.isNaN(n)||Number.isNaN(i))throw new TypeError(B.invalidDate("BST"));return n>i?1:n<i?-1:0}if(typeof e=="object"||typeof t=="object")throw new TypeError(B.comparatorRequired("BST"));return 0}}_floorByKey(e,t){var n,i;if(t==="RECURSIVE"){let r=s=>{if(!this.isRealNode(s))return;if(this.comparator(s.key,e)<=0){let d=r(s.right);return d!=null?d: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 d=r(s.right);return d!=null?d: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=l=>{if(!this.isRealNode(l))return;let d=this.comparator(l.key,e);if(t?d>=0:d>0){let f=s(l.left);return f!=null?f:l}else return s(l.right)};return s(this.root)}else{let s=this.root,l;for(;this.isRealNode(s);){let d=this.comparator(s.key,e);(t?d>=0:d>0)?(l=s,s=(i=s.left)!=null?i:void 0):s=(r=s.right)!=null?r:void 0}return l}}_boundByPredicate(e,t){if(t==="RECURSIVE"){let n,i=r=>{n||!this.isRealNode(r)||(this.isRealNode(r.left)&&i(r.left),!n&&e(r)&&(n=r),!n&&this.isRealNode(r.right)&&i(r.right))};return i(this.root),n}else{let n=[],i=this.root;for(;n.length>0||this.isRealNode(i);)if(this.isRealNode(i))n.push(i),i=i.left;else{let r=n.pop();if(!this.isRealNode(r))break;if(e(r))return r;i=r.right}return}}_createInstance(e){let t=this.constructor;return new t([],{...this._snapshotOptions(),...e!=null?e:{}})}_createLike(e=[],t){let n=this.constructor;return new n(e,{...this._snapshotOptions(),...t!=null?t:{}})}_snapshotOptions(){return{...super._snapshotOptions(),comparator:this._comparator}}_keyValueNodeOrEntryToNodeAndValue(e,t){let[n,i]=super._keyValueNodeOrEntryToNodeAndValue(e,t);return n===null?[void 0,void 0]:[n,t!=null?t:i]}_setRoot(e){e&&(e.parent=void 0),this._root=e}_compare(e,t){return this._comparator(e,t)}_deleteByKey(e){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,s)=>{let l=r==null?void 0:r.parent;l?l.left===r?l.left=s:l.right=s:this._setRoot(s),s&&(s.parent=l)},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,this._size-1),!0}};var I=class{constructor(o,e){p(this,"key");p(this,"value");p(this,"parent");p(this,"_left");p(this,"_right");p(this,"_height",0);p(this,"_color","BLACK");p(this,"_count",1);this.key=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"}},O=class extends S{constructor(o=[],e){super([],e),o&&super.setMany(o)}createNode(o,e){return new I(o,e)}isNode(o){return o instanceof I}set(o,e){if(o===null)return!1;let t=super.set(o,e);return t&&this._balancePath(o),t}delete(o){let e=super.delete(o);for(let{needBalanced:t}of e)t&&this._balancePath(t);return e}perfectlyBalance(o=this.iterationType){let e=this.dfs(r=>r,"IN",!1,this._root,o),t=e.length;if(t===0)return!1;this._clearNodes();let n=(r,s,l)=>{if(r>s)return;let d=r+(s-r>>1),u=e[d];u.left=n(r,d-1,u),u.right=n(d+1,s,u),u.parent=l;let f=u.left?u.left.height:-1,h=u.right?u.right.height:-1;return u.height=Math.max(f,h)+1,u},i=n(0,t-1,void 0);return this._setRoot(i),this._size=t,!0}map(o,e,t){let n=this._createLike([],e),i=0;for(let[r,s]of this)n.set(o.call(t,s,r,i++,this));return n}_createInstance(o){let e=this.constructor;return new e([],{...this._snapshotOptions(),...o!=null?o:{}})}_createLike(o=[],e){let t=this.constructor;return new t(o,{...this._snapshotOptions(),...e!=null?e:{}})}_swapProperties(o,e){let t=this.ensureNode(o),n=this.ensureNode(e);if(t&&n){let{key:i,value:r,height:s}=n,l=this.createNode(i,r);return l&&(l.height=s,n.key=t.key,this._isMapMode||(n.value=t.value),n.height=t.height,t.key=l.key,this._isMapMode||(t.value=l.value),t.height=l.height),n}}_balanceFactor(o){let e=o.left?o.left.height:-1;return(o.right?o.right.height:-1)-e}_updateHeight(o){let e=o.left?o.left.height:-1,t=o.right?o.right.height:-1;o.height=1+Math.max(e,t)}_balanceLL(o){let e=o.parent,t=o.left;t!==null&&(o.parent=t),t&&t.right&&(t.right.parent=o),t&&(t.parent=e),o===this.root?t&&this._setRoot(t):(e==null?void 0:e.left)===o?e.left=t:e&&(e.right=t),t&&(o.left=t.right,t.right=o),this._updateHeight(o),t&&this._updateHeight(t)}_balanceLR(o){let e=o.parent,t=o.left,n;t&&(n=t.right),o&&n!==null&&(o.parent=n),t&&n!==null&&(t.parent=n),n&&(n.left&&t!==null&&(n.left.parent=t),n.right&&(n.right.parent=o),n.parent=e),o===this.root?n&&this._setRoot(n):e&&(e.left===o?e.left=n:e.right=n),n&&(o.left=n.right,t&&(t.right=n.left),n.left=t,n.right=o),this._updateHeight(o),t&&this._updateHeight(t),n&&this._updateHeight(n)}_balanceRR(o){let e=o.parent,t=o.right;t!==null&&(o.parent=t),t&&(t.left&&(t.left.parent=o),t.parent=e),o===this.root?t&&this._setRoot(t):e&&(e.left===o?e.left=t:e.right=t),t&&(o.right=t.left,t.left=o),this._updateHeight(o),t&&this._updateHeight(t)}_balanceRL(o){let e=o.parent,t=o.right,n;t&&(n=t.left),n!==null&&(o.parent=n),t&&n!==null&&(t.parent=n),n&&(n.left&&(n.left.parent=o),n.right&&t!==null&&(n.right.parent=t),n.parent=e),o===this.root?n&&this._setRoot(n):e&&(e.left===o?e.left=n:e.right=n),n&&(o.right=n.left),t&&n&&(t.left=n.right),n&&(n.left=o),n&&(n.right=t),this._updateHeight(o),t&&this._updateHeight(t),n&&this._updateHeight(n)}_balancePath(o){o=this.ensureNode(o);let e=this.getPathToRoot(o,t=>t,!1);for(let t=0;t<e.length;t++){let n=e[t];if(n)switch(this._updateHeight(n),this._balanceFactor(n)){case-2:n&&n.left&&(this._balanceFactor(n.left)<=0?this._balanceLL(n):this._balanceLR(n));break;case 2:n&&n.right&&(this._balanceFactor(n.right)>=0?this._balanceRR(n):this._balanceRL(n))}}}_replaceNode(o,e){return e.height=o.height,super._replaceNode(o,e)}};return q(J);})();
|
|
4
|
+
`),(s=>{let[d]=this._displayAux(s,n),l="";for(let u of d)l+=u+`
|
|
5
|
+
`;i+=l})(e)),i}print(e,t=this._root){console.log(this.toVisual(t,e))}_dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,r=this.iterationType,s=!1,d=h=>!!h,l=h=>!!h,u=h=>s?this.isRealNodeOrNull(h):this.isRealNode(h),f=h=>this.isRealNodeOrNull(h)){if(i=this.ensureNode(i),!i)return[];let h=[];if(r==="RECURSIVE"){let c=T=>{if(!u(T))return;let K=()=>{d(T)&&(T==null?void 0:T.left)!==void 0&&c(T==null?void 0:T.left)},V=()=>{l(T)&&(T==null?void 0:T.right)!==void 0&&c(T==null?void 0:T.right)};switch(t){case"IN":if(K(),f(T)&&(h.push(e(T)),n))return;V();break;case"PRE":if(f(T)&&(h.push(e(T)),n))return;K(),V();break;case"POST":if(K(),V(),f(T)&&(h.push(e(T)),n))return;break}};c(i)}else{let c=[{opt:0,node:i}],T=N=>{var y;d(N.node)&&c.push({opt:0,node:(y=N.node)==null?void 0:y.left})},K=N=>{var y;l(N.node)&&c.push({opt:0,node:(y=N.node)==null?void 0:y.right})},V=N=>{u(N.node)&&c.push({opt:1,node:N.node})};for(;c.length>0;){let N=c.pop();if(N!==void 0&&u(N.node))if(N.opt===1){if(f(N.node)&&N.node!==void 0&&(h.push(e(N.node)),n))return h}else switch(t){case"IN":K(N),V(N),T(N);break;case"PRE":K(N),T(N),V(N);break;case"POST":V(N),K(N),T(N);break}}}return h}*_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=l=>({node:l,stage:0,leftLayout:n,rightLayout:n}),r=[i(e)],s=n,d=l=>{if(r.length===0){s=l;return}let u=r[r.length-1];u.stage===1?u.leftLayout=l:u.rightLayout=l};for(;r.length>0;){let l=r[r.length-1],u=l.node;if(l.stage===0){if(this._isDisplayLeaf(u,t)){r.pop();let f=this._resolveDisplayLeaf(u,t,n);d(f);continue}l.stage=1,r.push(i(u.left))}else if(l.stage===1)l.stage=2,r.push(i(u.right));else{r.pop();let f=this.isNIL(u)?"S":String(u.key),h=a._buildNodeDisplay(f,f.length,l.leftLayout,l.rightLayout);d(h)}}return s}static _buildNodeDisplay(e,t,n,i){let[r,s,d,l]=n,[u,f,h,c]=i,T=" ".repeat(Math.max(0,l+1))+"_".repeat(Math.max(0,s-l-1))+e+"_".repeat(Math.max(0,c))+" ".repeat(Math.max(0,f-c)),K=(d>0?" ".repeat(l)+"/"+" ".repeat(s-l-1):" ".repeat(s))+" ".repeat(t)+(h>0?" ".repeat(c)+"\\"+" ".repeat(f-c-1):" ".repeat(f)),V=[T,K];for(let N=0;N<Math.max(d,h);N++){let y=N<d?r[N]:" ".repeat(s),C=N<h?u[N]:" ".repeat(f);V.push(y+" ".repeat(t)+C)}return[V,s+t+f,Math.max(d,h)+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),d=this._hasDisplayableChild(e.right,t);return!s&&!d}_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 l=this.isNIL(e)?"S":String(e.key);return a._buildNodeDisplay(l,l.length,n,n)}let d=e===void 0?"U":"N";return a._buildNodeDisplay(d,d.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 E=class{constructor(o,e){p(this,"key");p(this,"value");p(this,"parent");p(this,"_left");p(this,"_right");p(this,"_height",0);p(this,"_color","BLACK");p(this,"_count",1);this.key=o,this.value=e}get left(){return this._left}set left(o){o&&(o.parent=this),this._left=o}get right(){return this._right}set right(o){o&&(o.parent=this),this._right=o}get height(){return this._height}set height(o){this._height=o}get color(){return this._color}set color(o){this._color=o}get count(){return this._count}set count(o){this._count=o}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},I=class extends S{constructor(e=[],t){super([],t);p(this,"_root");p(this,"_enableOrderStatistic",!1);p(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 E(e,t)}ensureNode(e,t=this.iterationType){var n;return(n=super.ensureNode(e,t))!=null?n:void 0}isNode(e){return e instanceof E}isValidKey(e){return m(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 u,f;if(e==null)return;if(this._isPredicate(e))return(u=this.getNodes(e,!0,t,n)[0])!=null?u:void 0;if(e instanceof _)return(f=this.getNodes(e,!0,t,n)[0])!=null?f:void 0;let i;if(this.isNode(e))i=e.key;else if(this.isEntry(e)){let h=e[0];if(h==null)return;i=h}else i=e;let r=this.ensureNode(t);if(!r)return;let s=this._NIL,d=r,l=this._comparator;for(;d&&d!==s;){let h=l(i,d.key);if(h===0)return d;d=h<0?d._left:d._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),d=!s&&this._isPredicate(e);if(!s&&!d){let h;if(this.isNode(e))h=e.key;else if(this.isEntry(e)){let V=e[0];V!=null&&(h=V)}else h=e;if(h===void 0)return[];let c=this._NIL,T=this._comparator,K=i;for(;K&&K!==c;){let V=T(h,K.key);if(V===0)return[n(K)];K=V<0?K._left:K._right}return[]}let l;s?l=h=>h?e.isInRange(h.key,this._comparator):!1:l=this._ensurePredicate(e);let u=h=>{if(!h||!this.isRealNode(h.left))return!1;if(s){let c=e,T=c.low,K=c.includeLow;return K&&this._compare(h.key,T)>=0||!K&&this._compare(h.key,T)>0}if(!s&&!this._isPredicate(e)){let c=this._extractKey(e);return c!=null&&this._compare(h.key,c)>0}return!0},f=h=>{if(!h||!this.isRealNode(h.right))return!1;if(s){let c=e,T=c.high,K=c.includeHigh;return K&&this._compare(h.key,T)<=0||!K&&this._compare(h.key,T)<0}if(!s&&!this._isPredicate(e)){let c=this._extractKey(e);return c!=null&&this._compare(h.key,c)<0}return!0};return super._dfs(n,"IN",t,i,r,!1,u,f,()=>!0,h=>!!h&&l(h))}rangeSearch(e,t=this._DEFAULT_NODE_CALLBACK,n=this._root,i=this.iterationType){let r=e instanceof _?e:new _(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||g(Error,R.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||g(Error,R.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||g(Error,R.orderStatisticNotEnabled("rangeByRank")),this._size===0)return[];let r=Math.max(0,e),s=Math.min(this._size-1,t);if(r>s)return[];let d,l=this.iterationType;typeof n=="string"?l=n:n&&(d=n,i&&(l=i));let u=[],f=s-r+1,h=l==="RECURSIVE"?this._getByRankRecursive(this._root,r):this._getByRankIterative(this._root,r);if(!h)return[];let c=0,T=d!=null?d:this._DEFAULT_NODE_CALLBACK,K=h;for(;K&&c<f;)u.push(T(K)),c++,c<f&&(K=this._next(K));return u}set(e,t){let[n]=this._keyValueNodeOrEntryToNodeAndValue(e,t);if(n===void 0)return!1;if(this._root===void 0)return this._setRoot(n),this._isMapMode&&this.isRealNode(n)&&this._store.set(n.key,n),this._size++,this._updateCount(n),!0;let i=this._root;for(;i!==void 0;){if(this._compare(i.key,n.key)===0)return this._replaceNode(i,n),this._isMapMode&&this.isRealNode(n)&&this._store.set(i.key,n),!0;if(this._compare(i.key,n.key)>0){if(i.left===void 0)return i.left=n,this._isMapMode&&this.isRealNode(n)&&this._store.set(n.key,n),this._size++,this._updateCountAlongPath(n),!0;i.left!==null&&(i=i.left)}else{if(i.right===void 0)return i.right=n,this._isMapMode&&this.isRealNode(n)&&this._store.set(n.key,n),this._size++,this._updateCountAlongPath(n),!0;i.right!==null&&(i=i.right)}}return!1}setMany(e,t,n=!0,i=this.iterationType){let r=[],s=t==null?void 0:t[Symbol.iterator]();if(!n){for(let c of e){let T=s==null?void 0:s.next().value;this.isRaw(c)&&(c=this._toEntryFn(c)),r.push(this.set(c,T))}return r}let d=[],l=0;for(let c of e)d.push({key:c,value:s==null?void 0:s.next().value,orgIndex:l++});let u=d.sort(({key:c},{key:T})=>{let K,V;return this.isRaw(c)?K=this._toEntryFn(c)[0]:this.isEntry(c)?K=c[0]:this.isRealNode(c)?K=c.key:K=c,this.isRaw(T)?V=this._toEntryFn(T)[0]:this.isEntry(T)?V=T[0]:this.isRealNode(T)?V=T.key:V=T,K!=null&&V!=null?this._compare(K,V):0}),f=c=>{if(c.length===0)return;let T=Math.floor((c.length-1)/2),{key:K,value:V,orgIndex:N}=c[T];if(this.isRaw(K)){let y=this._toEntryFn(K);r[N]=this.set(y)}else r[N]=this.set(K,V);f(c.slice(0,T)),f(c.slice(T+1))};return i==="RECURSIVE"?f(u):(()=>{let T=[[0,u.length-1]];for(;T.length>0;){let K=T.pop();if(!K)continue;let[V,N]=K;if(V>N)continue;let y=V+Math.floor((N-V)/2),{key:C,value:P,orgIndex:F}=u[y];if(this.isRaw(C)){let H=this._toEntryFn(C);r[F]=this.set(H)}else r[F]=this.set(C,P);T.push([y+1,N]),T.push([V,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 d=this._floorByPredicate(e,r);return i?d?i(d):void 0:d==null?void 0:d.key}let s;if(this.isNode(e))s=e.key;else if(this.isEntry(e)){let d=e[0];if(d==null)return void 0;s=d}else s=e;if(s!==void 0){let d=this._floorByKey(s,r);return i?d?i(d):void 0:d==null?void 0:d.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 d=this._lowerByPredicate(e,r);return i?d?i(d):void 0:d==null?void 0:d.key}let s;if(this.isNode(e))s=e.key;else if(this.isEntry(e)){let d=e[0];if(d==null)return void 0;s=d}else s=e;if(s!==void 0){let d=this._lowerByKey(s,r);return i?d?i(d):void 0:d==null?void 0:d.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 d=r.key;if(i==="RECURSIVE"){let l=u=>{let f=this._compare(u.key,d);Math.sign(f)==t&&s.push(e(u)),this.isRealNode(u.left)&&l(u.left),this.isRealNode(u.right)&&l(u.right)};return l(this._root),s}else{let l=new B([this._root]);for(;l.length>0;){let u=l.shift();if(this.isRealNode(u)){let f=this._compare(u.key,d);Math.sign(f)==t&&s.push(e(u)),this.isRealNode(u.left)&&l.push(u.left),this.isRealNode(u.right)&&l.push(u.right)}}return s}}perfectlyBalance(e=this.iterationType){let t=this.dfs(s=>s,"IN",!1,this._root,e),n=t.length;if(this._clearNodes(),n===0)return!1;let i=(s,d,l)=>{if(s>d)return;let u=s+(d-s>>1),f=t[u],h=i(s,u-1,f),c=i(u+1,d,f);return f.left=h,f.right=c,f.parent=l,f},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 d=i.left?s.get(i.left):-1,l=i.right?s.get(i.right):-1;if(Math.abs(d-l)>1)return!1;s.set(i,1+Math.max(d,l)),r=i,i=void 0}}else i=i.right}return t}map(e,t,n){let i=this._createLike([],t),r=0;for(let[s,d]of this)i.set(e.call(n,d,s,r++,this));return i}deleteWhere(e,t=!1,n=this._root,i=this.iterationType){let r=this.search(e,t,d=>d,n,i),s=[];for(let d of r){let l=this.delete(d);s=s.concat(l)}return s}_createDefaultComparator(){return(e,t)=>{if(m(e)&&m(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))&&g(TypeError,R.invalidDate("BST")),n>i?1:n<i?-1:0}return(typeof e=="object"||typeof t=="object")&&g(TypeError,R.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 l=r(s.right);return l!=null?l:s}else return r(s.left)};return r(this.root)}else{let r=this.root,s;for(;this.isRealNode(r);)this.comparator(r.key,e)<=0?(s=r,r=(n=r.right)!=null?n:void 0):r=(i=r.left)!=null?i:void 0;return s}}_floorByPredicate(e,t){if(t==="RECURSIVE"){let n,i=r=>{this.isRealNode(r)&&(this.isRealNode(r.left)&&i(r.left),e(r)&&(n=r),this.isRealNode(r.right)&&i(r.right))};return i(this.root),n}else{let n=[],i=this.root,r;for(;n.length>0||this.isRealNode(i);)if(this.isRealNode(i))n.push(i),i=i.left;else{let s=n.pop();if(!this.isRealNode(s))break;e(s)&&(r=s),i=s.right}return r}}_lowerByKey(e,t){var n,i;if(t==="RECURSIVE"){let r=s=>{if(!this.isRealNode(s))return;if(this.comparator(s.key,e)<0){let l=r(s.right);return l!=null?l:s}else return r(s.left)};return r(this.root)}else{let r=this.root,s;for(;this.isRealNode(r);)this.comparator(r.key,e)<0?(s=r,r=(n=r.right)!=null?n:void 0):r=(i=r.left)!=null?i:void 0;return s}}_lowerByPredicate(e,t){if(t==="RECURSIVE"){let n,i=r=>{this.isRealNode(r)&&(this.isRealNode(r.left)&&i(r.left),e(r)&&(n=r),this.isRealNode(r.right)&&i(r.right))};return i(this.root),n}else{let n=[],i=this.root,r;for(;n.length>0||this.isRealNode(i);)if(this.isRealNode(i))n.push(i),i=i.left;else{let s=n.pop();if(!this.isRealNode(s))break;e(s)&&(r=s),i=s.right}return r}}_bound(e,t,n){if(e==null)return;if(this._isPredicate(e))return this._boundByPredicate(e,n);let i;if(this.isNode(e))i=e.key;else if(this.isEntry(e)){let r=e[0];if(r==null)return;i=r}else i=e;if(i!==void 0)return this._boundByKey(i,t,n)}_boundByKey(e,t,n){var i,r;if(n==="RECURSIVE"){let s=d=>{if(!this.isRealNode(d))return;let l=this.comparator(d.key,e);if(t?l>=0:l>0){let f=s(d.left);return f!=null?f:d}else return s(d.right)};return s(this.root)}else{let s=this.root,d;for(;this.isRealNode(s);){let l=this.comparator(s.key,e);(t?l>=0:l>0)?(d=s,s=(i=s.left)!=null?i:void 0):s=(r=s.right)!=null?r:void 0}return d}}_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,d)=>{let l=s==null?void 0:s.parent;l?l.left===s?l.left=d:l.right=d:this._setRoot(d),d&&(d.parent=l)},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}};var L=class{constructor(o,e){p(this,"key");p(this,"value");p(this,"parent");p(this,"_left");p(this,"_right");p(this,"_height",0);p(this,"_color","BLACK");p(this,"_count",1);this.key=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"}},D=class extends I{constructor(o=[],e){super([],e),o&&super.setMany(o)}createNode(o,e){return new L(o,e)}isNode(o){return o instanceof L}set(o,e){if(o===null)return!1;let t=super.set(o,e);return t&&this._balancePath(o),t}delete(o){let e=super.delete(o);for(let{needBalanced:t}of e)t&&this._balancePath(t);return e}perfectlyBalance(o=this.iterationType){let e=this.dfs(r=>r,"IN",!1,this._root,o),t=e.length;if(t===0)return!1;this._clearNodes();let n=(r,s,d)=>{if(r>s)return;let l=r+(s-r>>1),u=e[l];u.left=n(r,l-1,u),u.right=n(l+1,s,u),u.parent=d;let f=u.left?u.left.height:-1,h=u.right?u.right.height:-1;return u.height=Math.max(f,h)+1,u},i=n(0,t-1,void 0);return this._setRoot(i),this._size=t,!0}map(o,e,t){let n=this._createLike([],e),i=0;for(let[r,s]of this)n.set(o.call(t,s,r,i++,this));return n}_createInstance(o){let e=this.constructor;return new e([],{...this._snapshotOptions(),...o!=null?o:{}})}_createLike(o=[],e){let t=this.constructor;return new t(o,{...this._snapshotOptions(),...e!=null?e:{}})}_swapProperties(o,e){let t=this.ensureNode(o),n=this.ensureNode(e);if(t&&n){let{key:i,value:r,height:s}=n,d=this.createNode(i,r);return d&&(d.height=s,n.key=t.key,this._isMapMode||(n.value=t.value),n.height=t.height,t.key=d.key,this._isMapMode||(t.value=d.value),t.height=d.height),n}}_balanceFactor(o){let e=o.left?o.left.height:-1;return(o.right?o.right.height:-1)-e}_updateHeight(o){let e=o.left?o.left.height:-1,t=o.right?o.right.height:-1;o.height=1+Math.max(e,t)}_balanceLL(o){let e=o.parent,t=o.left;t!==null&&(o.parent=t),t&&t.right&&(t.right.parent=o),t&&(t.parent=e),o===this.root?t&&this._setRoot(t):(e==null?void 0:e.left)===o?e.left=t:e&&(e.right=t),t&&(o.left=t.right,t.right=o),this._updateHeight(o),t&&this._updateHeight(t),this._updateCount(o),t&&this._updateCount(t)}_balanceLR(o){let e=o.parent,t=o.left,n;t&&(n=t.right),o&&n!==null&&(o.parent=n),t&&n!==null&&(t.parent=n),n&&(n.left&&t!==null&&(n.left.parent=t),n.right&&(n.right.parent=o),n.parent=e),o===this.root?n&&this._setRoot(n):e&&(e.left===o?e.left=n:e.right=n),n&&(o.left=n.right,t&&(t.right=n.left),n.left=t,n.right=o),this._updateHeight(o),t&&this._updateHeight(t),n&&this._updateHeight(n),this._updateCount(o),t&&this._updateCount(t),n&&this._updateCount(n)}_balanceRR(o){let e=o.parent,t=o.right;t!==null&&(o.parent=t),t&&(t.left&&(t.left.parent=o),t.parent=e),o===this.root?t&&this._setRoot(t):e&&(e.left===o?e.left=t:e.right=t),t&&(o.right=t.left,t.left=o),this._updateHeight(o),t&&this._updateHeight(t),this._updateCount(o),t&&this._updateCount(t)}_balanceRL(o){let e=o.parent,t=o.right,n;t&&(n=t.left),n!==null&&(o.parent=n),t&&n!==null&&(t.parent=n),n&&(n.left&&(n.left.parent=o),n.right&&t!==null&&(n.right.parent=t),n.parent=e),o===this.root?n&&this._setRoot(n):e&&(e.left===o?e.left=n:e.right=n),n&&(o.right=n.left),t&&n&&(t.left=n.right),n&&(n.left=o),n&&(n.right=t),this._updateHeight(o),t&&this._updateHeight(t),n&&this._updateHeight(n),this._updateCount(o),t&&this._updateCount(t),n&&this._updateCount(n)}_balancePath(o){o=this.ensureNode(o);let e=this.getPathToRoot(o,t=>t,!1);for(let t=0;t<e.length;t++){let n=e[t];if(n)switch(this._updateHeight(n),this._updateCount(n),this._balanceFactor(n)){case-2:n&&n.left&&(this._balanceFactor(n.left)<=0?this._balanceLL(n):this._balanceLR(n));break;case 2:n&&n.right&&(this._balanceFactor(n.right)>=0?this._balanceRR(n):this._balanceRL(n))}}}_replaceNode(o,e){return e.height=o.height,super._replaceNode(o,e)}};return W(Y);})();
|
|
6
6
|
/**
|
|
7
7
|
* data-structure-typed
|
|
8
8
|
*
|