avl-tree-typed 2.2.5 → 2.2.6
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 +101 -83
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +101 -83
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +101 -83
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +101 -83
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +6 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +14 -57
- package/dist/types/data-structures/binary-tree/bst.d.ts +46 -126
- package/dist/umd/avl-tree-typed.js +101 -83
- 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/index.ts +2 -4
- package/src/data-structures/base/iterable-entry-base.ts +9 -0
- package/src/data-structures/binary-tree/binary-tree.ts +67 -0
- package/src/data-structures/binary-tree/bst.ts +295 -89
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
"use strict";var avlTreeTyped=(()=>{var k=Object.defineProperty;var G=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var q=Object.prototype.hasOwnProperty;var W=(h,o,e)=>o in h?k(h,o,{enumerable:!0,configurable:!0,writable:!0,value:e}):h[o]=e;var X=(h,o)=>{for(var e in o)k(h,e,{get:o[e],enumerable:!0})},J=(h,o,e,t)=>{if(o&&typeof o=="object"||typeof o=="function")for(let n of j(o))!q.call(h,n)&&n!==e&&k(h,n,{get:()=>o[n],enumerable:!(t=G(o,n))||t.enumerable});return h};var Y=h=>J(k({},"__esModule",{value:!0}),h);var c=(h,o,e)=>W(h,typeof o!="symbol"?o+"":o,e);var ee={};X(ee,{AVLTree:()=>U,AVLTreeNode:()=>L,BST:()=>I,BSTNode:()=>S,BinaryTree:()=>C,BinaryTreeNode:()=>E});function O(h){let o=typeof h;return o==="number"?!0:o==="bigint"||o==="string"||o==="boolean"}function H(h){if(typeof h.valueOf=="function"){let o=h.valueOf();if(o!==h){if(O(o))return o;if(typeof o=="object"&&o!==null)return H(o)}}if(typeof h.toString=="function"){let o=h.toString();if(o!=="[object Object]")return o}return null}function g(h,o=!1){if(h==null)return!1;if(O(h))return!0;if(typeof h!="object")return!1;if(h instanceof Date||o)return!0;let e=H(h);return e==null?!1:O(e)}var D=h=>({isThunk:!0,fn:h}),Z=h=>typeof h=="object"&&h!==null&&"isThunk"in h&&h.isThunk;function $(h){let o=h;for(;Z(o);)o=o.fn();return o}function F(h){return(...o)=>$(h(...o))}var x=class{constructor(o){c(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 w=class h extends x{constructor(e){super(e);c(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 h?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 m=class h extends w{constructor(e=[],t){super(t);c(this,"_elements",[]);c(this,"_offset",0);c(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 h(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 v=class{*[Symbol.iterator](...o){yield*this._getIterator(...o)}*entries(){for(let o of this)yield o}*keys(){for(let o of this)yield o[0]}*values(){for(let o of this)yield o[1]}every(o,e){let t=0;for(let n of this)if(!o.call(e,n[1],n[0],t++,this))return!1;return!0}some(o,e){let t=0;for(let n of this)if(o.call(e,n[1],n[0],t++,this))return!0;return!1}forEach(o,e){let t=0;for(let n of this){let[i,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}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var _=class{constructor(o,e,t=!0,n=!0){this.low=o;this.high=e;this.includeLow=t;this.includeHigh=n;if(!(g(o)&&g(e)))throw new RangeError("low or high is not comparable");if(o>e)throw new RangeError("low must be less than or equal to high")}isInRange(o,e){let t=this.includeLow?e(o,this.low)>=0:e(o,this.low)>0,n=this.includeHigh?e(o,this.high)<=0:e(o,this.high)<0;return t&&n}};var E=class{constructor(o,e){c(this,"key");c(this,"value");c(this,"parent");c(this,"_left");c(this,"_right");c(this,"_height",0);c(this,"_color","BLACK");c(this,"_count",1);this.key=o,this.value=e}get left(){return this._left}set left(o){o&&(o.parent=this),this._left=o}get right(){return this._right}set right(o){o&&(o.parent=this),this._right=o}get height(){return this._height}set height(o){this._height=o}get color(){return this._color}set color(o){this._color=o}get count(){return this._count}set count(o){this._count=o}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},C=class extends v{constructor(e=[],t){super();c(this,"iterationType","ITERATIVE");c(this,"_isMapMode",!0);c(this,"_isDuplicate",!1);c(this,"_store",new Map);c(this,"_root");c(this,"_size",0);c(this,"_NIL",new E(NaN));c(this,"_toEntryFn");c(this,"_DEFAULT_NODE_CALLBACK",e=>e?e.key:void 0);if(t){let{iterationType:n,toEntryFn:i,isMapMode:r,isDuplicate:s}=t;if(n&&(this.iterationType=n),r!==void 0&&(this._isMapMode=r),s!==void 0&&(this._isDuplicate=s),typeof i=="function")this._toEntryFn=i;else if(i)throw TypeError("toEntryFn must be a function type")}e&&this.addMany(e)}get isMapMode(){return this._isMapMode}get isDuplicate(){return this._isDuplicate}get store(){return this._store}get root(){return this._root}get size(){return this._size}get NIL(){return this._NIL}get toEntryFn(){return this._toEntryFn}createNode(e,t){return new E(e,this._isMapMode?void 0:t)}createTree(e){return this._createInstance(e)}ensureNode(e,t=this.iterationType){if(e===null)return null;if(e!==void 0&&e!==this._NIL){if(this.isNode(e))return e;if(this.isEntry(e)){let n=e[0];return n===null?null:n===void 0?void 0:this.getNode(n,this._root,t)}return this.getNode(e,this._root,t)}}isNode(e){return e instanceof E}isRaw(e){return this._toEntryFn!==void 0&&typeof e=="object"}isRealNode(e){return e===this._NIL||e===null||e===void 0?!1:this.isNode(e)}isRealNodeOrNull(e){return e===null||this.isRealNode(e)}isNIL(e){return e===this._NIL}isRange(e){return e instanceof _}isLeaf(e){return e=this.ensureNode(e),e===void 0?!1:e===null?!0:!this.isRealNode(e.left)&&!this.isRealNode(e.right)}isEntry(e){return Array.isArray(e)&&e.length===2}isValidKey(e){return e===null?!0:g(e)}add(e,t){let[n,i]=this._keyValueNodeOrEntryToNodeAndValue(e,t);if(n===void 0)return!1;if(!this._root)return this._setRoot(n),this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size=1,!0;let r=new m([this._root]),s;for(;r.length>0;){let l=r.shift();if(l){if(!this._isDuplicate&&n!==null&&l.key===n.key)return this._replaceNode(l,n),this._isMapMode&&this._setValue(l.key,i),!0;s===void 0&&(l.left===void 0||l.right===void 0)&&(s=l),l.left!==null&&l.left&&r.push(l.left),l.right!==null&&l.right&&r.push(l.right)}}return s?(s.left===void 0?s.left=n:s.right===void 0&&(s.right=n),this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0):!1}set(e,t){return this.add(e,t)}addMany(e,t){let n=[],i;t&&(i=t[Symbol.iterator]());for(let r of e){let s;if(i){let l=i.next();l.done||(s=l.value)}this.isRaw(r)&&(r=this._toEntryFn(r)),n.push(this.add(r,s))}return n}setMany(e,t){return this.addMany(e,t)}merge(e){this.addMany(e,[])}refill(e,t){this.clear(),this.addMany(e,t)}delete(e){let t=[];if(!this._root)return t;let n=this.getNode(e);if(!n)return t;let i=n==null?void 0:n.parent,r,s=n;if(!n.left&&!n.right&&!i)this._setRoot(void 0);else if(n.left){let l=this.getRightMost(d=>d,n.left);if(l){let d=l.parent;s=this._swapProperties(n,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){return this.search(e,!0,i=>i,t,n)[0]}get(e,t=this._root,n=this.iterationType){var i;if(this._isMapMode){let r=this._extractKey(e);return r==null?void 0:this._store.get(r)}return(i=this.getNode(e,t,n))==null?void 0:i.value}has(e,t=this._root,n=this.iterationType){return this.search(e,!0,i=>i,t,n).length>0}clear(){this._clearNodes(),this._isMapMode&&this._clearValues()}isEmpty(){return this._size===0}isPerfectlyBalanced(e=this._root){return this.getMinHeight(e)+1>=this.getHeight(e)}isBST(e=this._root,t=this.iterationType){let n=this.ensureNode(e);if(!n)return!0;if(t==="RECURSIVE"){let i=(l,d,u)=>{if(!this.isRealNode(l))return!0;let a=Number(l.key);return a<=d||a>=u?!1:i(l.left,d,a)&&i(l.right,a,u)},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,a=n;for(;this.isRealNode(a)||d.length>0;){for(;this.isRealNode(a);)d.push(a),a=a.left;a=d.pop();let p=Number(a.key);if(!this.isRealNode(a)||!l&&u>=p||l&&u<=p)return!1;u=p,a=a.right}return!0},r=i(!1),s=i(!0);return r||s}}getDepth(e,t=this._root){let n=this.ensureNode(e),i=this.ensureNode(t),r=0;for(;n!=null&&n.parent;){if(n===i)return r;r++,n=n.parent}return r}getHeight(e=this._root,t=this.iterationType){if(e=this.ensureNode(e),!this.isRealNode(e))return-1;if(t==="RECURSIVE"){let n=i=>{if(!this.isRealNode(i))return-1;let r=n(i.left),s=n(i.right);return Math.max(r,s)+1};return n(e)}else{let n=[{node:e,depth:0}],i=0;for(;n.length>0;){let{node:r,depth:s}=n.pop();this.isRealNode(r.left)&&n.push({node:r.left,depth:s+1}),this.isRealNode(r.right)&&n.push({node:r.right,depth:s+1}),i=Math.max(i,s)}return i}}getMinHeight(e=this._root,t=this.iterationType){if(e=this.ensureNode(e),!e)return-1;if(t==="RECURSIVE"){let n=i=>{if(!this.isRealNode(i)||!this.isRealNode(i.left)&&!this.isRealNode(i.right))return 0;let r=n(i.left),s=n(i.right);return Math.min(r,s)+1};return n(e)}else{let n=[],i=e,r=null,s=new Map;for(;n.length>0||i;)if(this.isRealNode(i))n.push(i),i=i.left;else if(i=n[n.length-1],!this.isRealNode(i.right)||r===i.right){if(i=n.pop(),this.isRealNode(i)){let 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=F(s=>{let{left:l}=s;return this.isRealNode(l)?D(()=>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=F(r=>{let{right:s}=r;return this.isRealNode(s)?D(()=>i(s)):r});return e(i(t))}}getPredecessor(e){if(this.isRealNode(e.left)){let t=e.left;for(;!this.isRealNode(t)||this.isRealNode(t.right)&&t.right!==e;)this.isRealNode(t)&&(t=t.right);return t}else return e}getSuccessor(e){if(e=this.ensureNode(e),!this.isRealNode(e))return;if(this.isRealNode(e.right))return this.getLeftMost(n=>n,e.right);let t=e.parent;for(;this.isRealNode(t)&&e===t.right;)e=t,t=t.parent;return t}dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,r=this.iterationType,s=!1){return i=this.ensureNode(i),i?this._dfs(e,t,n,i,r,s):[]}bfs(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType,i=!1){if(t=this.ensureNode(t),!t)return[];let r=[];if(n==="RECURSIVE"){let s=new m([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 m([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=new m([t]);for(;r.length>0;){let s=r.shift();this.isRealNode(s)&&(this.isLeaf(s)&&i.push(e(s)),this.isRealNode(s.left)&&r.push(s.left),this.isRealNode(s.right)&&r.push(s.right))}}return i}listLevels(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType,i=!1){t=this.ensureNode(t);let r=[];if(!t)return r;if(n==="RECURSIVE"){let s=(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,a=null;for(;d;)a=d.right,d.right=u,u=d,d=a;return u},l=d=>{let u=s(d),a=u;for(;a;)i.push(e(a)),a=a.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.add([r,s]);return n}map(e,t,n){let i=this._createLike([],t),r=0;for(let[s,l]of this)i.add(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 M=Object.defineProperty;var Q=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var q=Object.prototype.hasOwnProperty;var W=(h,s,e)=>s in h?M(h,s,{enumerable:!0,configurable:!0,writable:!0,value:e}):h[s]=e;var X=(h,s)=>{for(var e in s)M(h,e,{get:s[e],enumerable:!0})},J=(h,s,e,t)=>{if(s&&typeof s=="object"||typeof s=="function")for(let n of j(s))!q.call(h,n)&&n!==e&&M(h,n,{get:()=>s[n],enumerable:!(t=Q(s,n))||t.enumerable});return h};var Y=h=>J(M({},"__esModule",{value:!0}),h);var c=(h,s,e)=>W(h,typeof s!="symbol"?s+"":s,e);var ee={};X(ee,{AVLTree:()=>F,AVLTreeNode:()=>L,BST:()=>I,BSTNode:()=>S,BinaryTree:()=>E,BinaryTreeNode:()=>C});function A(h){let s=typeof h;return s==="number"?!0:s==="bigint"||s==="string"||s==="boolean"}function H(h){if(typeof h.valueOf=="function"){let s=h.valueOf();if(s!==h){if(A(s))return s;if(typeof s=="object"&&s!==null)return H(s)}}if(typeof h.toString=="function"){let s=h.toString();if(s!=="[object Object]")return s}return null}function b(h,s=!1){if(h==null)return!1;if(A(h))return!0;if(typeof h!="object")return!1;if(h instanceof Date||s)return!0;let e=H(h);return e==null?!1:A(e)}var D=h=>({isThunk:!0,fn:h}),Z=h=>typeof h=="object"&&h!==null&&"isThunk"in h&&h.isThunk;function $(h){let s=h;for(;Z(s);)s=s.fn();return s}function P(h){return(...s)=>$(h(...s))}var x=class{constructor(s){c(this,"_toElementFn");if(s){let{toElementFn:e}=s;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](...s){yield*this._getIterator(...s)}*values(){for(let s of this)yield s}every(s,e){let t=0;for(let n of this)if(e===void 0){if(!s(n,t++,this))return!1}else if(!s.call(e,n,t++,this))return!1;return!0}some(s,e){let t=0;for(let n of this)if(e===void 0){if(s(n,t++,this))return!0}else if(s.call(e,n,t++,this))return!0;return!1}forEach(s,e){let t=0;for(let n of this)e===void 0?s(n,t++,this):s.call(e,n,t++,this)}find(s,e){let t=0;for(let n of this)if(e===void 0){if(s(n,t++,this))return n}else if(s.call(e,n,t++,this))return n}has(s){for(let e of this)if(e===s)return!0;return!1}reduce(s,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=s(i,r,t++,this);return i}toArray(){return[...this]}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var w=class h extends x{constructor(e){super(e);c(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 h?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 d=this.deleteAt(e);d!==void 0&&i.push(d)}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 g=class h extends w{constructor(e=[],t){super(t);c(this,"_elements",[]);c(this,"_offset",0);c(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 h(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 d=this._createInstance({toElementFn:this.toElementFn,maxLen:this._maxLen});return d._setAutoCompactRatio(this._autoCompactRatio),d.pushMany(r),d}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 d,o;let i=new this.constructor([],{toElementFn:t==null?void 0:t.toElementFn,maxLen:(d=t==null?void 0:t.maxLen)!=null?d:this._maxLen,autoCompactRatio:(o=t==null?void 0:t.autoCompactRatio)!=null?o:this._autoCompactRatio}),r=0;for(let l of this)i.push(n===void 0?e(l,r++,this):e.call(n,l,r++,this));return i}mapSame(e,t){var d;let n=this.constructor,i=new n([],{toElementFn:this.toElementFn,maxLen:this._maxLen,autoCompactRatio:this._autoCompactRatio});(d=i._setAutoCompactRatio)==null||d.call(i,this._autoCompactRatio);let r=0;for(let o of this){let l=t===void 0?e(o,r++,this):e.call(t,o,r++,this);i.push(l)}return i}_setAutoCompactRatio(e){this._autoCompactRatio=e}*_getIterator(){for(let e=this._offset;e<this.elements.length;e++)yield this.elements[e]}*_getReverseIterator(){for(let e=this.length-1;e>=0;e--){let t=this.at(e);t!==void 0&&(yield t)}}_createInstance(e){let t=this.constructor;return new t([],e)}_createLike(e=[],t){let n=this.constructor;return new n(e,t)}};var v=class{*[Symbol.iterator](...s){yield*this._getIterator(...s)}*entries(){for(let s of this)yield s}*keys(){for(let s of this)yield s[0]}*values(){for(let s of this)yield s[1]}every(s,e){let t=0;for(let n of this)if(!s.call(e,n[1],n[0],t++,this))return!1;return!0}some(s,e){let t=0;for(let n of this)if(s.call(e,n[1],n[0],t++,this))return!0;return!1}forEach(s,e){let t=0;for(let n of this){let[i,r]=n;s.call(e,r,i,t++,this)}}find(s,e){let t=0;for(let n of this){let[i,r]=n;if(s.call(e,r,i,t++,this))return n}}has(s){for(let e of this){let[t]=e;if(t===s)return!0}return!1}hasValue(s){for(let[,e]of this)if(e===s)return!0;return!1}get(s){for(let e of this){let[t,n]=e;if(t===s)return n}}reduce(s,e){let t=e,n=0;for(let i of this){let[r,d]=i;t=s(t,d,r,n++,this)}return t}toArray(){return[...this]}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var m=class{constructor(s,e,t=!0,n=!0){this.low=s;this.high=e;this.includeLow=t;this.includeHigh=n}isInRange(s,e){let t=this.includeLow?e(s,this.low)>=0:e(s,this.low)>0,n=this.includeHigh?e(s,this.high)<=0:e(s,this.high)<0;return t&&n}};var C=class{constructor(s,e){c(this,"key");c(this,"value");c(this,"parent");c(this,"_left");c(this,"_right");c(this,"_height",0);c(this,"_color","BLACK");c(this,"_count",1);this.key=s,this.value=e}get left(){return this._left}set left(s){s&&(s.parent=this),this._left=s}get right(){return this._right}set right(s){s&&(s.parent=this),this._right=s}get height(){return this._height}set height(s){this._height=s}get color(){return this._color}set color(s){this._color=s}get count(){return this._count}set count(s){this._count=s}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"}},E=class extends v{constructor(e=[],t){super();c(this,"iterationType","ITERATIVE");c(this,"_isMapMode",!0);c(this,"_isDuplicate",!1);c(this,"_store",new Map);c(this,"_root");c(this,"_size",0);c(this,"_NIL",new C(NaN));c(this,"_toEntryFn");c(this,"_DEFAULT_NODE_CALLBACK",e=>e?e.key:void 0);if(t){let{iterationType:n,toEntryFn:i,isMapMode:r,isDuplicate:d}=t;if(n&&(this.iterationType=n),r!==void 0&&(this._isMapMode=r),d!==void 0&&(this._isDuplicate=d),typeof i=="function")this._toEntryFn=i;else if(i)throw TypeError("toEntryFn must be a function type")}e&&this.addMany(e)}get isMapMode(){return this._isMapMode}get isDuplicate(){return this._isDuplicate}get store(){return this._store}get root(){return this._root}get size(){return this._size}get NIL(){return this._NIL}get toEntryFn(){return this._toEntryFn}createNode(e,t){return new C(e,this._isMapMode?void 0:t)}createTree(e){return this._createInstance(e)}ensureNode(e,t=this.iterationType){if(e===null)return null;if(e!==void 0&&e!==this._NIL){if(this.isNode(e))return e;if(this.isEntry(e)){let n=e[0];return n===null?null:n===void 0?void 0:this.getNode(n,this._root,t)}return this.getNode(e,this._root,t)}}isNode(e){return e instanceof C}isRaw(e){return this._toEntryFn!==void 0&&typeof e=="object"}isRealNode(e){return e===this._NIL||e===null||e===void 0?!1:this.isNode(e)}isRealNodeOrNull(e){return e===null||this.isRealNode(e)}isNIL(e){return e===this._NIL}isRange(e){return e instanceof m}isLeaf(e){return e=this.ensureNode(e),e===void 0?!1:e===null?!0:!this.isRealNode(e.left)&&!this.isRealNode(e.right)}isEntry(e){return Array.isArray(e)&&e.length===2}isValidKey(e){return e===null?!0:b(e)}add(e,t){let[n,i]=this._keyValueNodeOrEntryToNodeAndValue(e,t);if(n===void 0)return!1;if(!this._root)return this._setRoot(n),this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size=1,!0;let r=new g([this._root]),d;for(;r.length>0;){let o=r.shift();if(o){if(!this._isDuplicate&&n!==null&&o.key===n.key)return this._replaceNode(o,n),this._isMapMode&&this._setValue(o.key,i),!0;d===void 0&&(o.left===void 0||o.right===void 0)&&(d=o),o.left!==null&&o.left&&r.push(o.left),o.right!==null&&o.right&&r.push(o.right)}}return d?(d.left===void 0?d.left=n:d.right===void 0&&(d.right=n),this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0):!1}set(e,t){return this.add(e,t)}addMany(e,t){let n=[],i;t&&(i=t[Symbol.iterator]());for(let r of e){let d;if(i){let o=i.next();o.done||(d=o.value)}this.isRaw(r)&&(r=this._toEntryFn(r)),n.push(this.add(r,d))}return n}setMany(e,t){return this.addMany(e,t)}merge(e){this.addMany(e,[])}refill(e,t){this.clear(),this.addMany(e,t)}delete(e){let t=[];if(!this._root)return t;let n=this.getNode(e);if(!n)return t;let i=n==null?void 0:n.parent,r,d=n;if(!n.left&&!n.right&&!i)this._setRoot(void 0);else if(n.left){let o=this.getRightMost(l=>l,n.left);if(o){let l=o.parent;d=this._swapProperties(n,o),l&&(l.right===o?l.right=o.left:l.left=o.left,r=l)}}else if(i){let{familyPosition:o}=n;o==="LEFT"||o==="ROOT_LEFT"?i.left=n.right:(o==="RIGHT"||o==="ROOT_RIGHT")&&(i.right=n.right),r=i}else this._setRoot(n.right),n.right=void 0;return this._size=this._size-1,t.push({deleted:d,needBalanced:r}),this._isMapMode&&d&&this._store.delete(d.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 d=this._ensurePredicate(e),o=[];if(r==="RECURSIVE"){let l=u=>{d(u)&&(o.push(n(u)),t)||!this.isRealNode(u.left)&&!this.isRealNode(u.right)||(this.isRealNode(u.left)&&l(u.left),this.isRealNode(u.right)&&l(u.right))};l(i)}else{let l=[i];for(;l.length>0;){let u=l.pop();if(this.isRealNode(u)){if(d(u)&&(o.push(n(u)),t))return o;this.isRealNode(u.left)&&l.push(u.left),this.isRealNode(u.right)&&l.push(u.right)}}}return o}getNodes(e,t=!1,n=this._root,i=this.iterationType){return this.search(e,t,r=>r,n,i)}getNode(e,t=this._root,n=this.iterationType){return this.search(e,!0,i=>i,t,n)[0]}get(e,t=this._root,n=this.iterationType){var i;if(this._isMapMode){let r=this._extractKey(e);return r==null?void 0:this._store.get(r)}return(i=this.getNode(e,t,n))==null?void 0:i.value}has(e,t=this._root,n=this.iterationType){return this.search(e,!0,i=>i,t,n).length>0}clear(){this._clearNodes(),this._isMapMode&&this._clearValues()}isEmpty(){return this._size===0}isPerfectlyBalanced(e=this._root){return this.getMinHeight(e)+1>=this.getHeight(e)}isBST(e=this._root,t=this.iterationType){let n=this.ensureNode(e);if(!n)return!0;if(t==="RECURSIVE"){let i=(o,l,u)=>{if(!this.isRealNode(o))return!0;let a=Number(o.key);return a<=l||a>=u?!1:i(o.left,l,a)&&i(o.right,a,u)},r=i(n,Number.MIN_SAFE_INTEGER,Number.MAX_SAFE_INTEGER),d=i(n,Number.MAX_SAFE_INTEGER,Number.MIN_SAFE_INTEGER);return r||d}else{let i=(o=!1)=>{let l=[],u=o?Number.MAX_SAFE_INTEGER:Number.MIN_SAFE_INTEGER,a=n;for(;this.isRealNode(a)||l.length>0;){for(;this.isRealNode(a);)l.push(a),a=a.left;a=l.pop();let p=Number(a.key);if(!this.isRealNode(a)||!o&&u>=p||o&&u<=p)return!1;u=p,a=a.right}return!0},r=i(!1),d=i(!0);return r||d}}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),d=n(i.right);return Math.max(r,d)+1};return n(e)}else{let n=[{node:e,depth:0}],i=0;for(;n.length>0;){let{node:r,depth:d}=n.pop();this.isRealNode(r.left)&&n.push({node:r.left,depth:d+1}),this.isRealNode(r.right)&&n.push({node:r.right,depth:d+1}),i=Math.max(i,d)}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),d=n(i.right);return Math.min(r,d)+1};return n(e)}else{let n=[],i=e,r=null,d=new Map;for(;n.length>0||i;)if(this.isRealNode(i))n.push(i),i=i.left;else if(i=n[n.length-1],!this.isRealNode(i.right)||r===i.right){if(i=n.pop(),this.isRealNode(i)){let o=this.isRealNode(i.left)?d.get(i.left):-1,l=this.isRealNode(i.right)?d.get(i.right):-1;d.set(i,1+Math.min(o,l)),r=i,i=null}}else i=i.right;return d.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=d=>{let{left:o}=d;return this.isRealNode(o)?r(o):d};return e(r(i))}else{let r=P(d=>{let{left:o}=d;return this.isRealNode(o)?D(()=>r(o)):d});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:d}=r;return this.isRealNode(d)?i(d):r};return e(i(t))}else{let i=P(r=>{let{right:d}=r;return this.isRealNode(d)?D(()=>i(d)):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,d=!1){return i=this.ensureNode(i),i?this._dfs(e,t,n,i,r,d):[]}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 d=new g([t]),o=l=>{if(d.length===0)return;let u=d.shift();r.push(e(u)),i?(u&&this.isRealNodeOrNull(u.left)&&d.push(u.left),u&&this.isRealNodeOrNull(u.right)&&d.push(u.right)):(this.isRealNode(u.left)&&d.push(u.left),this.isRealNode(u.right)&&d.push(u.right)),o(l+1)};o(0)}else{let d=new g([t]);for(;d.length>0;){let o=d.length;for(let l=0;l<o;l++){let u=d.shift();r.push(e(u)),i?(u&&this.isRealNodeOrNull(u.left)&&d.push(u.left),u&&this.isRealNodeOrNull(u.right)&&d.push(u.right)):(this.isRealNode(u.left)&&d.push(u.left),this.isRealNode(u.right)&&d.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=d=>{this.isLeaf(d)&&i.push(e(d)),!(!this.isRealNode(d.left)&&!this.isRealNode(d.right))&&(this.isRealNode(d.left)&&r(d.left),this.isRealNode(d.right)&&r(d.right))};r(t)}else{let r=new g([t]);for(;r.length>0;){let d=r.shift();this.isRealNode(d)&&(this.isLeaf(d)&&i.push(e(d)),this.isRealNode(d.left)&&r.push(d.left),this.isRealNode(d.right)&&r.push(d.right))}}return i}listLevels(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType,i=!1){t=this.ensureNode(t);let r=[];if(!t)return r;if(n==="RECURSIVE"){let d=(o,l)=>{r[l]||(r[l]=[]),r[l].push(e(o)),i?(o&&this.isRealNodeOrNull(o.left)&&d(o.left,l+1),o&&this.isRealNodeOrNull(o.right)&&d(o.right,l+1)):(o&&o.left&&d(o.left,l+1),o&&o.right&&d(o.right,l+1))};d(t,0)}else{let d=[[t,0]];for(;d.length>0;){let o=d.pop(),[l,u]=o;r[u]||(r[u]=[]),r[u].push(e(l)),i?(l&&this.isRealNodeOrNull(l.right)&&d.push([l.right,u+1]),l&&this.isRealNodeOrNull(l.left)&&d.push([l.left,u+1])):(l&&l.right&&d.push([l.right,u+1]),l&&l.left&&d.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,d=l=>{let u=null,a=null;for(;l;)a=l.right,l.right=u,u=l,l=a;return u},o=l=>{let u=d(l),a=u;for(;a;)i.push(e(a)),a=a.right;d(u)};switch(t){case"IN":for(;r;){if(r.left){let l=this.getPredecessor(r);if(l.right)l.right=null;else{l.right=r,r=r.left;continue}}i.push(e(r)),r=r.right}break;case"PRE":for(;r;){if(r.left){let l=this.getPredecessor(r);if(l.right)l.right=null;else{l.right=r,i.push(e(r)),r=r.left;continue}}else i.push(e(r));r=r.right}break;case"POST":for(;r;){if(r.left){let l=this.getPredecessor(r);if(l.right===null){l.right=r,r=r.left;continue}else l.right=null,o(r.left)}r=r.right}o(n);break}return i}clone(){let e=this._createInstance();return this._clone(e),e}filter(e,t){let n=this._createInstance(),i=0;for(let[r,d]of this)e.call(t,d,r,i++,this)&&n.add([r,d]);return n}map(e,t,n){let i=this._createLike([],t),r=0;for(let[d,o]of this)i.add(e.call(n,o,d,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
|
-
`),(
|
|
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=p=>!!p,d=p=>!!p,u=p=>s?this.isRealNodeOrNull(p):this.isRealNode(p),a=p=>this.isRealNodeOrNull(p)){if(i=this.ensureNode(i),!i)return[];let p=[];if(r==="RECURSIVE"){let T=f=>{if(!u(f))return;let V=()=>{l(f)&&(f==null?void 0:f.left)!==void 0&&T(f==null?void 0:f.left)},N=()=>{d(f)&&(f==null?void 0:f.right)!==void 0&&T(f==null?void 0:f.right)};switch(t){case"IN":if(V(),a(f)&&(p.push(e(f)),n))return;N();break;case"PRE":if(a(f)&&(p.push(e(f)),n))return;V(),N();break;case"POST":if(V(),N(),a(f)&&(p.push(e(f)),n))return;break}};T(i)}else{let T=[{opt:0,node:i}],f=K=>{var y;l(K.node)&&T.push({opt:0,node:(y=K.node)==null?void 0:y.left})},V=K=>{var y;d(K.node)&&T.push({opt:0,node:(y=K.node)==null?void 0:y.right})},N=K=>{u(K.node)&&T.push({opt:1,node:K.node})};for(;T.length>0;){let K=T.pop();if(K!==void 0&&u(K.node))if(K.opt===1){if(a(K.node)&&K.node!==void 0&&(p.push(e(K.node)),n))return p}else switch(t){case"IN":V(K),N(K),f(K);break;case"PRE":V(K),f(K),N(K);break;case"POST":N(K),V(K),f(K);break}}}return p}*_getIterator(e=this._root){if(e)if(this.iterationType==="ITERATIVE"){let t=[],n=e;for(;n||t.length>0;){for(;this.isRealNode(n);)t.push(n),n=n.left;n=t.pop(),this.isRealNode(n)&&(this._isMapMode?yield[n.key,this._store.get(n.key)]:yield[n.key,n.value],n=n.right)}}else e.left&&this.isRealNode(e)&&(yield*this[Symbol.iterator](e.left)),this._isMapMode?yield[e.key,this._store.get(e.key)]:yield[e.key,e.value],e.right&&this.isRealNode(e)&&(yield*this[Symbol.iterator](e.right))}_snapshotOptions(){return{iterationType:this.iterationType,toEntryFn:this.toEntryFn,isMapMode:this.isMapMode,isDuplicate:this.isDuplicate}}_createInstance(e){let t=this.constructor;return new t([],{...this._snapshotOptions(),...e!=null?e:{}})}_createLike(e=[],t){let n=this.constructor;return new n(e,{...this._snapshotOptions(),...t!=null?t:{}})}_keyValueNodeOrEntryToNodeAndValue(e,t){if(e===void 0)return[void 0,void 0];if(e===null)return[null,void 0];if(this.isNode(e))return[e,t];if(this.isEntry(e)){let[n,i]=e;if(n===void 0)return[void 0,void 0];if(n===null)return[null,void 0];let r=t!=null?t:i;return[this.createNode(n,r),r]}return[this.createNode(e,t),t]}_clone(e){this.bfs(t=>{t===null?e.add(null):this._isMapMode?e.add([t.key,this._store.get(t.key)]):e.add([t.key,t.value])},this._root,this.iterationType,!0),this._isMapMode&&(e._store=this._store)}_displayAux(e,t){let{isShowNull:n,isShowUndefined:i,isShowRedBlackNIL:r}=t,s=[["\u2500"],1,0,0];if(e===null&&!n)return s;if(e===void 0&&!i)return s;if(this.isNIL(e)&&!r)return s;if(e!=null){let d=e.key,u=this.isNIL(e)?"S":String(d),a=u.length;return l(u,a,this._displayAux(e.left,t),this._displayAux(e.right,t))}else{let d=e===void 0?"U":"N",u=d.length;return l(d,u,[[""],1,0,0],[[""],1,0,0])}function l(d,u,a,p){let[T,f,V,N]=a,[K,y,R,B]=p,M=" ".repeat(Math.max(0,N+1))+"_".repeat(Math.max(0,f-N-1))+d+"_".repeat(Math.max(0,B))+" ".repeat(Math.max(0,y-B)),A=(V>0?" ".repeat(N)+"/"+" ".repeat(f-N-1):" ".repeat(f))+" ".repeat(u)+(R>0?" ".repeat(B)+"\\"+" ".repeat(y-B-1):" ".repeat(y)),P=[M,A];for(let b=0;b<Math.max(V,R);b++){let z=b<V?T[b]:" ".repeat(f),Q=b<R?K[b]:" ".repeat(y);P.push(z+" ".repeat(u)+Q)}return[P,f+u+y,Math.max(V,R)+2,f+Math.floor(u/2)]}}_swapProperties(e,t){if(e=this.ensureNode(e),t=this.ensureNode(t),e&&t){let{key:n,value:i}=t,r=this.createNode(n,i);return r&&(t.key=e.key,this._isMapMode||(t.value=e.value),e.key=r.key,this._isMapMode||(e.value=r.value)),t}}_replaceNode(e,t){return e.parent&&(e.parent.left===e?e.parent.left=t:e.parent.right===e&&(e.parent.right=t)),t.left=e.left,t.right=e.right,t.parent=e.parent,this._root===e&&this._setRoot(t),t}_setRoot(e){e&&(e.parent=void 0),this._root=e}_ensurePredicate(e){if(e==null)return t=>!1;if(this._isPredicate(e))return e;if(this.isRealNode(e))return t=>t===e;if(this.isEntry(e)){let[t]=e;return n=>n?n.key===t:!1}return t=>t?t.key===e:!1}_isPredicate(e){return typeof e=="function"}_extractKey(e){if(e===null)return null;if(e!==void 0&&e!==this._NIL)return this.isNode(e)?e.key:this.isEntry(e)?e[0]:e}_setValue(e,t){return e==null||t===void 0?!1:this._store.set(e,t)}_clearNodes(){this._setRoot(void 0),this._size=0}_clearValues(){this._store.clear()}};var S=class{constructor(o,e){c(this,"key");c(this,"value");c(this,"parent");c(this,"_left");c(this,"_right");c(this,"_height",0);c(this,"_color","BLACK");c(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 C{constructor(e=[],t){super([],t);c(this,"_root");c(this,"_comparator");t?"comparator"in t&&t.comparator!==void 0?this._comparator=t.comparator:this._comparator=this._createDefaultComparator():this._comparator=this._createDefaultComparator(),e&&this.addMany(e)}get root(){return this._root}get comparator(){return this._comparator}createNode(e,t){return new S(e,this._isMapMode?void 0:t)}ensureNode(e,t=this.iterationType){var n;return(n=super.ensureNode(e,t))!=null?n:void 0}isNode(e){return e instanceof S}isValidKey(e){return g(e)}dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,r=this.iterationType){return super.dfs(e,t,n,i,r)}bfs(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){return super.bfs(e,t,n,!1)}listLevels(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){return super.listLevels(e,t,n,!1)}getNode(e,t=this._root,n=this.iterationType){var i;return(i=this.getNodes(e,!0,t,n)[0])!=null?i:void 0}search(e,t=!1,n=this._DEFAULT_NODE_CALLBACK,i=this._root,r=this.iterationType){if(e===void 0)return[];if(e===null)return[];if(i=this.ensureNode(i),!i)return[];let s,l=this.isRange(e);l?s=a=>a?e.isInRange(a.key,this._comparator):!1:s=this._ensurePredicate(e);let d=a=>{if(!a||!this.isRealNode(a.left))return!1;if(l){let p=e,T=p.low,f=p.includeLow;return f&&this._compare(a.key,T)>=0||!f&&this._compare(a.key,T)>0}if(!l&&!this._isPredicate(e)){let p=this._extractKey(e);return p!=null&&this._compare(a.key,p)>0}return!0},u=a=>{if(!a||!this.isRealNode(a.right))return!1;if(l){let p=e,T=p.high,f=p.includeHigh;return f&&this._compare(a.key,T)<=0||!f&&this._compare(a.key,T)<0}if(!l&&!this._isPredicate(e)){let p=this._extractKey(e);return p!=null&&this._compare(a.key,p)<0}return!0};return super._dfs(n,"IN",t,i,r,!1,d,u,()=>!0,a=>!!a&&s(a))}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)}add(e,t){let[n,i]=this._keyValueNodeOrEntryToNodeAndValue(e,t);if(n===void 0)return!1;if(this._root===void 0)return this._setRoot(n),this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0;let r=this._root;for(;r!==void 0;){if(this._compare(r.key,n.key)===0)return this._replaceNode(r,n),this._isMapMode&&this._setValue(r.key,i),!0;if(this._compare(r.key,n.key)>0){if(r.left===void 0)return r.left=n,this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0;r.left!==null&&(r=r.left)}else{if(r.right===void 0)return r.right=n,this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0;r.right!==null&&(r=r.right)}}return!1}addMany(e,t,n=!0,i=this.iterationType){let r=[],s=t==null?void 0:t[Symbol.iterator]();if(!n){for(let T of e){let f=s==null?void 0:s.next().value;this.isRaw(T)&&(T=this._toEntryFn(T)),r.push(this.add(T,f))}return r}let l=[],d=0;for(let T of e)l.push({key:T,value:s==null?void 0:s.next().value,orgIndex:d++});let u=l.sort(({key:T},{key:f})=>{let V,N;return this.isRaw(T)?V=this._toEntryFn(T)[0]:this.isEntry(T)?V=T[0]:this.isRealNode(T)?V=T.key:V=T,this.isRaw(f)?N=this._toEntryFn(f)[0]:this.isEntry(f)?N=f[0]:this.isRealNode(f)?N=f.key:N=f,V!=null&&N!=null?this._compare(V,N):0}),a=T=>{if(T.length===0)return;let f=Math.floor((T.length-1)/2),{key:V,value:N,orgIndex:K}=T[f];if(this.isRaw(V)){let y=this._toEntryFn(V);r[K]=this.add(y)}else r[K]=this.add(V,N);a(T.slice(0,f)),a(T.slice(f+1))};return i==="RECURSIVE"?a(u):(()=>{let f=[[0,u.length-1]];for(;f.length>0;){let V=f.pop();if(!V)continue;let[N,K]=V;if(N>K)continue;let y=N+Math.floor((K-N)/2),{key:R,value:B,orgIndex:M}=u[y];if(this.isRaw(R)){let A=this._toEntryFn(R);r[M]=this.add(A)}else r[M]=this.add(R,B);f.push([y+1,K]),f.push([N,y-1])}})(),r}lowerBound(e,t=this.iterationType){return this._bound(e,!0,t)}upperBound(e,t=this.iterationType){return this._bound(e,!1,t)}ceilingEntry(e,t=this.iterationType){return this.lowerBound(e,t)}higherEntry(e,t=this.iterationType){return this.upperBound(e,t)}floorEntry(e,t=this.iterationType){if(e==null)return;if(this._isPredicate(e))return this._floorByPredicate(e,t);let n;if(this.isNode(e))n=e.key;else if(this.isEntry(e)){let i=e[0];if(i==null)return;n=i}else n=e;if(n!==void 0)return this._floorByKey(n,t)}lowerEntry(e,t=this.iterationType){if(e==null)return;if(this._isPredicate(e))return this._lowerByPredicate(e,t);let n;if(this.isNode(e))n=e.key;else if(this.isEntry(e)){let i=e[0];if(i==null)return;n=i}else n=e;if(n!==void 0)return this._lowerByKey(n,t)}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 a=this._compare(u.key,l);Math.sign(a)==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 m([this._root]);for(;d.length>0;){let u=d.shift();if(this.isRealNode(u)){let a=this._compare(u.key,l);Math.sign(a)==t&&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),a=t[u],p=i(s,u-1,a),T=i(u+1,l,a);return a.left=p,a.right=T,a.parent=d,a},r=i(0,n-1,void 0);return this._setRoot(r),this._size=n,!0}isAVLBalanced(e=this.iterationType){if(!this._root)return!0;let t=!0;if(e==="RECURSIVE"){let n=i=>{if(!i)return 0;let r=n(i.left),s=n(i.right);return Math.abs(r-s)>1&&(t=!1),Math.max(r,s)+1};n(this._root)}else{let n=[],i=this._root,r,s=new Map;for(;n.length>0||i;)if(i)n.push(i),i.left!==null&&(i=i.left);else if(i=n[n.length-1],!i.right||r===i.right){if(i=n.pop(),i){let 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.add(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)=>{debugger;if(g(e)&&g(t))return e>t?1:e<t?-1:0;if(typeof e=="object"||typeof t=="object")throw TypeError("When comparing object type keys, a custom comparator must be provided in the constructor's options!");return 0}}_floorByKey(e,t){var n,i;if(t==="RECURSIVE"){let r=s=>{if(!this.isRealNode(s))return;if(this.comparator(s.key,e)<=0){let 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 a=s(l.left);return a!=null?a: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){var r;let t=this._root;for(;t;){let s=this._compare(t.key,e);if(s===0)break;t=s>0?t.left:t.right}if(!t)return!1;let n=(s,l)=>{let d=s==null?void 0:s.parent;d?d.left===s?d.left=l:d.right=l:this._setRoot(l),l&&(l.parent=d)},i=s=>{if(s){for(;s.left!==void 0&&s.left!==null;)s=s.left;return s}};if(t.left===void 0)n(t,t.right);else if(t.right===void 0)n(t,t.left);else{let s=i(t.right);s.parent!==t&&(n(s,s.right),s.right=t.right,s.right&&(s.right.parent=s)),n(t,s),s.left=t.left,s.left&&(s.left.parent=s)}return this._size=Math.max(0,((r=this._size)!=null?r:0)-1),!0}};var L=class{constructor(o,e){c(this,"key");c(this,"value");c(this,"parent");c(this,"_left");c(this,"_right");c(this,"_height",0);c(this,"_color","BLACK");c(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"}},U=class extends I{constructor(o=[],e){super([],e),o&&super.addMany(o)}createNode(o,e){return new L(o,this._isMapMode?void 0:e)}isNode(o){return o instanceof L}add(o,e){if(o===null)return!1;let t=super.add(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 a=u.left?u.left.height:-1,p=u.right?u.right.height:-1;return u.height=Math.max(a,p)+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.add(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 Y(ee);})();
|
|
4
|
+
`),(d=>{let[o]=this._displayAux(d,n),l="";for(let u of o)l+=u+`
|
|
5
|
+
`;i+=l})(e)),i}print(e,t=this._root){console.log(this.toVisual(t,e))}_dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,r=this.iterationType,d=!1,o=p=>!!p,l=p=>!!p,u=p=>d?this.isRealNodeOrNull(p):this.isRealNode(p),a=p=>this.isRealNodeOrNull(p)){if(i=this.ensureNode(i),!i)return[];let p=[];if(r==="RECURSIVE"){let T=f=>{if(!u(f))return;let V=()=>{o(f)&&(f==null?void 0:f.left)!==void 0&&T(f==null?void 0:f.left)},N=()=>{l(f)&&(f==null?void 0:f.right)!==void 0&&T(f==null?void 0:f.right)};switch(t){case"IN":if(V(),a(f)&&(p.push(e(f)),n))return;N();break;case"PRE":if(a(f)&&(p.push(e(f)),n))return;V(),N();break;case"POST":if(V(),N(),a(f)&&(p.push(e(f)),n))return;break}};T(i)}else{let T=[{opt:0,node:i}],f=K=>{var y;o(K.node)&&T.push({opt:0,node:(y=K.node)==null?void 0:y.left})},V=K=>{var y;l(K.node)&&T.push({opt:0,node:(y=K.node)==null?void 0:y.right})},N=K=>{u(K.node)&&T.push({opt:1,node:K.node})};for(;T.length>0;){let K=T.pop();if(K!==void 0&&u(K.node))if(K.opt===1){if(a(K.node)&&K.node!==void 0&&(p.push(e(K.node)),n))return p}else switch(t){case"IN":V(K),N(K),f(K);break;case"PRE":V(K),f(K),N(K);break;case"POST":N(K),V(K),f(K);break}}}return p}*_getIterator(e=this._root){if(e)if(this.iterationType==="ITERATIVE"){let t=[],n=e;for(;n||t.length>0;){for(;this.isRealNode(n);)t.push(n),n=n.left;n=t.pop(),this.isRealNode(n)&&(this._isMapMode?yield[n.key,this._store.get(n.key)]:yield[n.key,n.value],n=n.right)}}else e.left&&this.isRealNode(e)&&(yield*this[Symbol.iterator](e.left)),this._isMapMode?yield[e.key,this._store.get(e.key)]:yield[e.key,e.value],e.right&&this.isRealNode(e)&&(yield*this[Symbol.iterator](e.right))}_snapshotOptions(){return{iterationType:this.iterationType,toEntryFn:this.toEntryFn,isMapMode:this.isMapMode,isDuplicate:this.isDuplicate}}_createInstance(e){let t=this.constructor;return new t([],{...this._snapshotOptions(),...e!=null?e:{}})}_createLike(e=[],t){let n=this.constructor;return new n(e,{...this._snapshotOptions(),...t!=null?t:{}})}_keyValueNodeOrEntryToNodeAndValue(e,t){if(e===void 0)return[void 0,void 0];if(e===null)return[null,void 0];if(this.isNode(e))return[e,t];if(this.isEntry(e)){let[n,i]=e;if(n===void 0)return[void 0,void 0];if(n===null)return[null,void 0];let r=t!=null?t:i;return[this.createNode(n,r),r]}return[this.createNode(e,t),t]}_clone(e){this.bfs(t=>{t===null?e.add(null):this._isMapMode?e.add([t.key,this._store.get(t.key)]):e.add([t.key,t.value])},this._root,this.iterationType,!0),this._isMapMode&&(e._store=this._store)}_displayAux(e,t){let{isShowNull:n,isShowUndefined:i,isShowRedBlackNIL:r}=t,d=[["\u2500"],1,0,0];if(e===null&&!n)return d;if(e===void 0&&!i)return d;if(this.isNIL(e)&&!r)return d;if(e!=null){let l=e.key,u=this.isNIL(e)?"S":String(l),a=u.length;return o(u,a,this._displayAux(e.left,t),this._displayAux(e.right,t))}else{let l=e===void 0?"U":"N",u=l.length;return o(l,u,[[""],1,0,0],[[""],1,0,0])}function o(l,u,a,p){let[T,f,V,N]=a,[K,y,R,B]=p,k=" ".repeat(Math.max(0,N+1))+"_".repeat(Math.max(0,f-N-1))+l+"_".repeat(Math.max(0,B))+" ".repeat(Math.max(0,y-B)),O=(V>0?" ".repeat(N)+"/"+" ".repeat(f-N-1):" ".repeat(f))+" ".repeat(u)+(R>0?" ".repeat(B)+"\\"+" ".repeat(y-B-1):" ".repeat(y)),U=[k,O];for(let _=0;_<Math.max(V,R);_++){let z=_<V?T[_]:" ".repeat(f),G=_<R?K[_]:" ".repeat(y);U.push(z+" ".repeat(u)+G)}return[U,f+u+y,Math.max(V,R)+2,f+Math.floor(u/2)]}}_swapProperties(e,t){if(e=this.ensureNode(e),t=this.ensureNode(t),e&&t){let{key:n,value:i}=t,r=this.createNode(n,i);return r&&(t.key=e.key,this._isMapMode||(t.value=e.value),e.key=r.key,this._isMapMode||(e.value=r.value)),t}}_replaceNode(e,t){return e.parent&&(e.parent.left===e?e.parent.left=t:e.parent.right===e&&(e.parent.right=t)),t.left=e.left,t.right=e.right,t.parent=e.parent,this._root===e&&this._setRoot(t),t}_setRoot(e){e&&(e.parent=void 0),this._root=e}_ensurePredicate(e){if(e==null)return t=>!1;if(this._isPredicate(e))return e;if(this.isRealNode(e))return t=>t===e;if(this.isEntry(e)){let[t]=e;return n=>n?n.key===t:!1}return t=>t?t.key===e:!1}_isPredicate(e){return typeof e=="function"}_extractKey(e){if(e===null)return null;if(e!==void 0&&e!==this._NIL)return this.isNode(e)?e.key:this.isEntry(e)?e[0]:e}_setValue(e,t){return e==null||t===void 0?!1:this._store.set(e,t)}_clearNodes(){this._setRoot(void 0),this._size=0}_clearValues(){this._store.clear()}};var S=class{constructor(s,e){c(this,"key");c(this,"value");c(this,"parent");c(this,"_left");c(this,"_right");c(this,"_height",0);c(this,"_color","BLACK");c(this,"_count",1);this.key=s,this.value=e}get left(){return this._left}set left(s){s&&(s.parent=this),this._left=s}get right(){return this._right}set right(s){s&&(s.parent=this),this._right=s}get height(){return this._height}set height(s){this._height=s}get color(){return this._color}set color(s){this._color=s}get count(){return this._count}set count(s){this._count=s}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 E{constructor(e=[],t){super([],t);c(this,"_root");c(this,"_comparator");t?"comparator"in t&&t.comparator!==void 0?this._comparator=t.comparator:this._comparator=this._createDefaultComparator():this._comparator=this._createDefaultComparator(),e&&this.addMany(e)}get root(){return this._root}get comparator(){return this._comparator}createNode(e,t){return new S(e,this._isMapMode?void 0:t)}ensureNode(e,t=this.iterationType){var n;return(n=super.ensureNode(e,t))!=null?n:void 0}isNode(e){return e instanceof S}isValidKey(e){return b(e)}dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,r=this.iterationType){return super.dfs(e,t,n,i,r)}bfs(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){return super.bfs(e,t,n,!1)}listLevels(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){return super.listLevels(e,t,n,!1)}getNode(e,t=this._root,n=this.iterationType){var i;return(i=this.getNodes(e,!0,t,n)[0])!=null?i:void 0}search(e,t=!1,n=this._DEFAULT_NODE_CALLBACK,i=this._root,r=this.iterationType){if(e===void 0)return[];if(e===null)return[];if(i=this.ensureNode(i),!i)return[];let d,o=this.isRange(e);o?d=a=>a?e.isInRange(a.key,this._comparator):!1:d=this._ensurePredicate(e);let l=a=>{if(!a||!this.isRealNode(a.left))return!1;if(o){let p=e,T=p.low,f=p.includeLow;return f&&this._compare(a.key,T)>=0||!f&&this._compare(a.key,T)>0}if(!o&&!this._isPredicate(e)){let p=this._extractKey(e);return p!=null&&this._compare(a.key,p)>0}return!0},u=a=>{if(!a||!this.isRealNode(a.right))return!1;if(o){let p=e,T=p.high,f=p.includeHigh;return f&&this._compare(a.key,T)<=0||!f&&this._compare(a.key,T)<0}if(!o&&!this._isPredicate(e)){let p=this._extractKey(e);return p!=null&&this._compare(a.key,p)<0}return!0};return super._dfs(n,"IN",t,i,r,!1,l,u,()=>!0,a=>!!a&&d(a))}rangeSearch(e,t=this._DEFAULT_NODE_CALLBACK,n=this._root,i=this.iterationType){let r=e instanceof m?e:new m(e[0],e[1]);return this.search(r,!1,t,n,i)}add(e,t){let[n,i]=this._keyValueNodeOrEntryToNodeAndValue(e,t);if(n===void 0)return!1;if(this._root===void 0)return this._setRoot(n),this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0;let r=this._root;for(;r!==void 0;){if(this._compare(r.key,n.key)===0)return this._replaceNode(r,n),this._isMapMode&&this._setValue(r.key,i),!0;if(this._compare(r.key,n.key)>0){if(r.left===void 0)return r.left=n,this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0;r.left!==null&&(r=r.left)}else{if(r.right===void 0)return r.right=n,this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0;r.right!==null&&(r=r.right)}}return!1}addMany(e,t,n=!0,i=this.iterationType){let r=[],d=t==null?void 0:t[Symbol.iterator]();if(!n){for(let T of e){let f=d==null?void 0:d.next().value;this.isRaw(T)&&(T=this._toEntryFn(T)),r.push(this.add(T,f))}return r}let o=[],l=0;for(let T of e)o.push({key:T,value:d==null?void 0:d.next().value,orgIndex:l++});let u=o.sort(({key:T},{key:f})=>{let V,N;return this.isRaw(T)?V=this._toEntryFn(T)[0]:this.isEntry(T)?V=T[0]:this.isRealNode(T)?V=T.key:V=T,this.isRaw(f)?N=this._toEntryFn(f)[0]:this.isEntry(f)?N=f[0]:this.isRealNode(f)?N=f.key:N=f,V!=null&&N!=null?this._compare(V,N):0}),a=T=>{if(T.length===0)return;let f=Math.floor((T.length-1)/2),{key:V,value:N,orgIndex:K}=T[f];if(this.isRaw(V)){let y=this._toEntryFn(V);r[K]=this.add(y)}else r[K]=this.add(V,N);a(T.slice(0,f)),a(T.slice(f+1))};return i==="RECURSIVE"?a(u):(()=>{let f=[[0,u.length-1]];for(;f.length>0;){let V=f.pop();if(!V)continue;let[N,K]=V;if(N>K)continue;let y=N+Math.floor((K-N)/2),{key:R,value:B,orgIndex:k}=u[y];if(this.isRaw(R)){let O=this._toEntryFn(R);r[k]=this.add(O)}else r[k]=this.add(R,B);f.push([y+1,K]),f.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 d=this._bound(e,!0,r);return i?d?i(d):void 0:d==null?void 0:d.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 d=this._bound(e,!1,r);return i?d?i(d):void 0:d==null?void 0:d.key}floor(e,t=this._DEFAULT_NODE_CALLBACK,n){if(e==null)return void 0;let i,r=this.iterationType;if(typeof t=="string"?r=t:t&&(i=t,n&&(r=n)),this._isPredicate(e)){let o=this._floorByPredicate(e,r);return i?o?i(o):void 0:o==null?void 0:o.key}let d;if(this.isNode(e))d=e.key;else if(this.isEntry(e)){let o=e[0];if(o==null)return void 0;d=o}else d=e;if(d!==void 0){let o=this._floorByKey(d,r);return i?o?i(o):void 0:o==null?void 0:o.key}}lower(e,t,n){if(e==null)return void 0;let i,r=this.iterationType;if(typeof t=="string"?r=t:t&&(i=t,n&&(r=n)),this._isPredicate(e)){let o=this._lowerByPredicate(e,r);return i?o?i(o):void 0:o==null?void 0:o.key}let d;if(this.isNode(e))d=e.key;else if(this.isEntry(e)){let o=e[0];if(o==null)return void 0;d=o}else d=e;if(d!==void 0){let o=this._lowerByKey(d,r);return i?o?i(o):void 0:o==null?void 0:o.key}}lesserOrGreaterTraverse(e=this._DEFAULT_NODE_CALLBACK,t=-1,n=this._root,i=this.iterationType){let r=this.ensureNode(n),d=[];if(!this._root||!r)return d;let o=r.key;if(i==="RECURSIVE"){let l=u=>{let a=this._compare(u.key,o);Math.sign(a)==t&&d.push(e(u)),this.isRealNode(u.left)&&l(u.left),this.isRealNode(u.right)&&l(u.right)};return l(this._root),d}else{let l=new g([this._root]);for(;l.length>0;){let u=l.shift();if(this.isRealNode(u)){let a=this._compare(u.key,o);Math.sign(a)==t&&d.push(e(u)),this.isRealNode(u.left)&&l.push(u.left),this.isRealNode(u.right)&&l.push(u.right)}}return d}}perfectlyBalance(e=this.iterationType){let t=this.dfs(d=>d,"IN",!1,this._root,e),n=t.length;if(this._clearNodes(),n===0)return!1;let i=(d,o,l)=>{if(d>o)return;let u=d+(o-d>>1),a=t[u],p=i(d,u-1,a),T=i(u+1,o,a);return a.left=p,a.right=T,a.parent=l,a},r=i(0,n-1,void 0);return this._setRoot(r),this._size=n,!0}isAVLBalanced(e=this.iterationType){if(!this._root)return!0;let t=!0;if(e==="RECURSIVE"){let n=i=>{if(!i)return 0;let r=n(i.left),d=n(i.right);return Math.abs(r-d)>1&&(t=!1),Math.max(r,d)+1};n(this._root)}else{let n=[],i=this._root,r,d=new Map;for(;n.length>0||i;)if(i)n.push(i),i.left!==null&&(i=i.left);else if(i=n[n.length-1],!i.right||r===i.right){if(i=n.pop(),i){let o=i.left?d.get(i.left):-1,l=i.right?d.get(i.right):-1;if(Math.abs(o-l)>1)return!1;d.set(i,1+Math.max(o,l)),r=i,i=void 0}}else i=i.right}return t}map(e,t,n){let i=this._createLike([],t),r=0;for(let[d,o]of this)i.add(e.call(n,o,d,r++,this));return i}deleteWhere(e,t=!1,n=this._root,i=this.iterationType){let r=this.search(e,t,o=>o,n,i),d=[];for(let o of r){let l=this.delete(o);d=d.concat(l)}return d}_createDefaultComparator(){return(e,t)=>{debugger;if(b(e)&&b(t))return e>t?1:e<t?-1:0;if(typeof e=="object"||typeof t=="object")throw TypeError("When comparing object type keys, a custom comparator must be provided in the constructor's options!");return 0}}_floorByKey(e,t){var n,i;if(t==="RECURSIVE"){let r=d=>{if(!this.isRealNode(d))return;if(this.comparator(d.key,e)<=0){let l=r(d.right);return l!=null?l:d}else return r(d.left)};return r(this.root)}else{let r=this.root,d;for(;this.isRealNode(r);)this.comparator(r.key,e)<=0?(d=r,r=(n=r.right)!=null?n:void 0):r=(i=r.left)!=null?i:void 0;return d}}_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 d=n.pop();if(!this.isRealNode(d))break;e(d)&&(r=d),i=d.right}return r}}_lowerByKey(e,t){var n,i;if(t==="RECURSIVE"){let r=d=>{if(!this.isRealNode(d))return;if(this.comparator(d.key,e)<0){let l=r(d.right);return l!=null?l:d}else return r(d.left)};return r(this.root)}else{let r=this.root,d;for(;this.isRealNode(r);)this.comparator(r.key,e)<0?(d=r,r=(n=r.right)!=null?n:void 0):r=(i=r.left)!=null?i:void 0;return d}}_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 d=n.pop();if(!this.isRealNode(d))break;e(d)&&(r=d),i=d.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 d=o=>{if(!this.isRealNode(o))return;let l=this.comparator(o.key,e);if(t?l>=0:l>0){let a=d(o.left);return a!=null?a:o}else return d(o.right)};return d(this.root)}else{let d=this.root,o;for(;this.isRealNode(d);){let l=this.comparator(d.key,e);(t?l>=0:l>0)?(o=d,d=(i=d.left)!=null?i:void 0):d=(r=d.right)!=null?r:void 0}return o}}_boundByPredicate(e,t){if(t==="RECURSIVE"){let n,i=r=>{n||!this.isRealNode(r)||(this.isRealNode(r.left)&&i(r.left),!n&&e(r)&&(n=r),!n&&this.isRealNode(r.right)&&i(r.right))};return i(this.root),n}else{let n=[],i=this.root;for(;n.length>0||this.isRealNode(i);)if(this.isRealNode(i))n.push(i),i=i.left;else{let r=n.pop();if(!this.isRealNode(r))break;if(e(r))return r;i=r.right}return}}_createInstance(e){let t=this.constructor;return new t([],{...this._snapshotOptions(),...e!=null?e:{}})}_createLike(e=[],t){let n=this.constructor;return new n(e,{...this._snapshotOptions(),...t!=null?t:{}})}_snapshotOptions(){return{...super._snapshotOptions(),comparator:this._comparator}}_keyValueNodeOrEntryToNodeAndValue(e,t){let[n,i]=super._keyValueNodeOrEntryToNodeAndValue(e,t);return n===null?[void 0,void 0]:[n,t!=null?t:i]}_setRoot(e){e&&(e.parent=void 0),this._root=e}_compare(e,t){return this._comparator(e,t)}_deleteByKey(e){var r;let t=this._root;for(;t;){let d=this._compare(t.key,e);if(d===0)break;t=d>0?t.left:t.right}if(!t)return!1;let n=(d,o)=>{let l=d==null?void 0:d.parent;l?l.left===d?l.left=o:l.right=o:this._setRoot(o),o&&(o.parent=l)},i=d=>{if(d){for(;d.left!==void 0&&d.left!==null;)d=d.left;return d}};if(t.left===void 0)n(t,t.right);else if(t.right===void 0)n(t,t.left);else{let d=i(t.right);d.parent!==t&&(n(d,d.right),d.right=t.right,d.right&&(d.right.parent=d)),n(t,d),d.left=t.left,d.left&&(d.left.parent=d)}return this._size=Math.max(0,((r=this._size)!=null?r:0)-1),!0}};var L=class{constructor(s,e){c(this,"key");c(this,"value");c(this,"parent");c(this,"_left");c(this,"_right");c(this,"_height",0);c(this,"_color","BLACK");c(this,"_count",1);this.key=s,this.value=e}get left(){return this._left}set left(s){s&&(s.parent=this),this._left=s}get right(){return this._right}set right(s){s&&(s.parent=this),this._right=s}get height(){return this._height}set height(s){this._height=s}get color(){return this._color}set color(s){this._color=s}get count(){return this._count}set count(s){this._count=s}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},F=class extends I{constructor(s=[],e){super([],e),s&&super.addMany(s)}createNode(s,e){return new L(s,this._isMapMode?void 0:e)}isNode(s){return s instanceof L}add(s,e){if(s===null)return!1;let t=super.add(s,e);return t&&this._balancePath(s),t}delete(s){let e=super.delete(s);for(let{needBalanced:t}of e)t&&this._balancePath(t);return e}perfectlyBalance(s=this.iterationType){let e=this.dfs(r=>r,"IN",!1,this._root,s),t=e.length;if(t===0)return!1;this._clearNodes();let n=(r,d,o)=>{if(r>d)return;let l=r+(d-r>>1),u=e[l];u.left=n(r,l-1,u),u.right=n(l+1,d,u),u.parent=o;let a=u.left?u.left.height:-1,p=u.right?u.right.height:-1;return u.height=Math.max(a,p)+1,u},i=n(0,t-1,void 0);return this._setRoot(i),this._size=t,!0}map(s,e,t){let n=this._createLike([],e),i=0;for(let[r,d]of this)n.add(s.call(t,d,r,i++,this));return n}_createInstance(s){let e=this.constructor;return new e([],{...this._snapshotOptions(),...s!=null?s:{}})}_createLike(s=[],e){let t=this.constructor;return new t(s,{...this._snapshotOptions(),...e!=null?e:{}})}_swapProperties(s,e){let t=this.ensureNode(s),n=this.ensureNode(e);if(t&&n){let{key:i,value:r,height:d}=n,o=this.createNode(i,r);return o&&(o.height=d,n.key=t.key,this._isMapMode||(n.value=t.value),n.height=t.height,t.key=o.key,this._isMapMode||(t.value=o.value),t.height=o.height),n}}_balanceFactor(s){let e=s.left?s.left.height:-1;return(s.right?s.right.height:-1)-e}_updateHeight(s){let e=s.left?s.left.height:-1,t=s.right?s.right.height:-1;s.height=1+Math.max(e,t)}_balanceLL(s){let e=s.parent,t=s.left;t!==null&&(s.parent=t),t&&t.right&&(t.right.parent=s),t&&(t.parent=e),s===this.root?t&&this._setRoot(t):(e==null?void 0:e.left)===s?e.left=t:e&&(e.right=t),t&&(s.left=t.right,t.right=s),this._updateHeight(s),t&&this._updateHeight(t)}_balanceLR(s){let e=s.parent,t=s.left,n;t&&(n=t.right),s&&n!==null&&(s.parent=n),t&&n!==null&&(t.parent=n),n&&(n.left&&t!==null&&(n.left.parent=t),n.right&&(n.right.parent=s),n.parent=e),s===this.root?n&&this._setRoot(n):e&&(e.left===s?e.left=n:e.right=n),n&&(s.left=n.right,t&&(t.right=n.left),n.left=t,n.right=s),this._updateHeight(s),t&&this._updateHeight(t),n&&this._updateHeight(n)}_balanceRR(s){let e=s.parent,t=s.right;t!==null&&(s.parent=t),t&&(t.left&&(t.left.parent=s),t.parent=e),s===this.root?t&&this._setRoot(t):e&&(e.left===s?e.left=t:e.right=t),t&&(s.right=t.left,t.left=s),this._updateHeight(s),t&&this._updateHeight(t)}_balanceRL(s){let e=s.parent,t=s.right,n;t&&(n=t.left),n!==null&&(s.parent=n),t&&n!==null&&(t.parent=n),n&&(n.left&&(n.left.parent=s),n.right&&t!==null&&(n.right.parent=t),n.parent=e),s===this.root?n&&this._setRoot(n):e&&(e.left===s?e.left=n:e.right=n),n&&(s.right=n.left),t&&n&&(t.left=n.right),n&&(n.left=s),n&&(n.right=t),this._updateHeight(s),t&&this._updateHeight(t),n&&this._updateHeight(n)}_balancePath(s){s=this.ensureNode(s);let e=this.getPathToRoot(s,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(s,e){return e.height=s.height,super._replaceNode(s,e)}};return Y(ee);})();
|
|
6
6
|
/**
|
|
7
7
|
* data-structure-typed
|
|
8
8
|
*
|