binary-tree-typed 2.2.2 → 2.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +91 -7
- package/dist/cjs/index.cjs +22 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +22 -0
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +22 -0
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +22 -0
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +98 -5
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +103 -7
- package/dist/types/data-structures/binary-tree/bst.d.ts +202 -39
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +86 -37
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +7 -7
- package/dist/types/data-structures/graph/directed-graph.d.ts +126 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +160 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +110 -27
- package/dist/types/data-structures/heap/heap.d.ts +107 -58
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +72 -404
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -5
- package/dist/types/data-structures/queue/deque.d.ts +95 -67
- package/dist/types/data-structures/queue/queue.d.ts +90 -34
- package/dist/types/data-structures/stack/stack.d.ts +58 -40
- package/dist/types/data-structures/trie/trie.d.ts +109 -47
- package/dist/types/interfaces/binary-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
- package/dist/umd/binary-tree-typed.js +22 -0
- package/dist/umd/binary-tree-typed.js.map +1 -1
- package/dist/umd/binary-tree-typed.min.js +2 -2
- package/dist/umd/binary-tree-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -8
- package/src/data-structures/binary-tree/avl-tree.ts +100 -7
- package/src/data-structures/binary-tree/binary-tree.ts +117 -7
- package/src/data-structures/binary-tree/bst.ts +431 -93
- package/src/data-structures/binary-tree/red-black-tree.ts +85 -37
- package/src/data-structures/binary-tree/tree-counter.ts +5 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +9 -10
- package/src/data-structures/graph/directed-graph.ts +126 -1
- package/src/data-structures/graph/undirected-graph.ts +160 -1
- package/src/data-structures/hash/hash-map.ts +110 -27
- package/src/data-structures/heap/heap.ts +107 -58
- package/src/data-structures/linked-list/doubly-linked-list.ts +72 -404
- package/src/data-structures/linked-list/singly-linked-list.ts +121 -5
- package/src/data-structures/queue/deque.ts +95 -67
- package/src/data-structures/queue/queue.ts +90 -34
- package/src/data-structures/stack/stack.ts +58 -40
- package/src/data-structures/trie/trie.ts +109 -47
- package/src/interfaces/binary-tree.ts +2 -0
- package/src/types/data-structures/binary-tree/bst.ts +5 -5
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
"use strict";var binaryTreeTyped=(()=>{var C=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var Q=Object.getOwnPropertyNames;var z=Object.prototype.hasOwnProperty;var G=(d,l,e)=>l in d?C(d,l,{enumerable:!0,configurable:!0,writable:!0,value:e}):d[l]=e;var j=(d,l)=>{for(var e in l)C(d,e,{get:l[e],enumerable:!0})},q=(d,l,e,t)=>{if(l&&typeof l=="object"||typeof l=="function")for(let n of Q(l))!z.call(d,n)&&n!==e&&C(d,n,{get:()=>l[n],enumerable:!(t=H(l,n))||t.enumerable});return d};var W=d=>q(C({},"__esModule",{value:!0}),d);var p=(d,l,e)=>G(d,typeof l!="symbol"?l+"":l,e);var Y={};j(Y,{BinaryTree:()=>O,BinaryTreeNode:()=>b,DFSOperation:()=>v,Range:()=>E});function k(d){let l=typeof d;return l==="number"?!0:l==="bigint"||l==="string"||l==="boolean"}function S(d){if(typeof d.valueOf=="function"){let l=d.valueOf();if(l!==d){if(k(l))return l;if(typeof l=="object"&&l!==null)return S(l)}}if(typeof d.toString=="function"){let l=d.toString();if(l!=="[object Object]")return l}return null}function R(d,l=!1){if(d==null)return!1;if(k(d))return!0;if(typeof d!="object")return!1;if(d instanceof Date||l)return!0;let e=S(d);return e==null?!1:k(e)}var L=d=>({isThunk:!0,fn:d}),X=d=>typeof d=="object"&&d!==null&&"isThunk"in d&&d.isThunk;function J(d){let l=d;for(;X(l);)l=l.fn();return l}function w(d){return(...l)=>J(d(...l))}var I=class{constructor(l){p(this,"_toElementFn");if(l){let{toElementFn:e}=l;if(typeof e=="function")this._toElementFn=e;else if(e)throw new TypeError("toElementFn must be a function type")}}get toElementFn(){return this._toElementFn}*[Symbol.iterator](...l){yield*this._getIterator(...l)}*values(){for(let l of this)yield l}every(l,e){let t=0;for(let n of this)if(e===void 0){if(!l(n,t++,this))return!1}else if(!l.call(e,n,t++,this))return!1;return!0}some(l,e){let t=0;for(let n of this)if(e===void 0){if(l(n,t++,this))return!0}else if(l.call(e,n,t++,this))return!0;return!1}forEach(l,e){let t=0;for(let n of this)e===void 0?l(n,t++,this):l.call(e,n,t++,this)}find(l,e){let t=0;for(let n of this)if(e===void 0){if(l(n,t++,this))return n}else if(l.call(e,n,t++,this))return n}has(l){for(let e of this)if(e===l)return!0;return!1}reduce(l,e){let t=0,n=this[Symbol.iterator](),i;if(arguments.length>=2)i=e;else{let 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=l(i,r,t++,this);return i}toArray(){return[...this]}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var x=class d extends I{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 d?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 V=class d extends x{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 d(e)}isEmpty(){return this.length===0}push(e){return this.elements.push(e),this._maxLen>0&&this.length>this._maxLen&&this.shift(),!0}pushMany(e){let t=[];for(let n of e)this.toElementFn?t.push(this.push(this.toElementFn(n))):t.push(this.push(n));return t}shift(){if(this.length===0)return;let e=this.first;return this._offset+=1,this.elements.length>0&&this.offset/this.elements.length>this.autoCompactRatio&&this.compact(),e}delete(e){for(let t=this._offset;t<this.elements.length;t++)if(Object.is(this.elements[t],e))return this.elements.splice(t,1),!0;return!1}at(e){if(!(e<0||e>=this.length))return this._elements[this._offset+e]}deleteAt(e){if(e<0||e>=this.length)return;let t=this._offset+e,[n]=this.elements.splice(t,1);return n}addAt(e,t){return e<0||e>this.length?!1:(this._elements.splice(this._offset+e,0,t),!0)}setAt(e,t){return e<0||e>=this.length?!1:(this._elements[this._offset+e]=t,!0)}reverse(){return this._elements=this.elements.slice(this._offset).reverse(),this._offset=0,this}clear(){this._elements=[],this._offset=0}compact(){return this._elements=this.elements.slice(this._offset),this._offset=0,!0}splice(e,t=0,...n){e=Math.max(0,Math.min(e,this.length)),t=Math.max(0,Math.min(t,this.length-e));let i=this._offset+e,r=this._elements.splice(i,t,...n);this.elements.length>0&&this.offset/this.elements.length>this.autoCompactRatio&&this.compact();let s=this._createInstance({toElementFn:this.toElementFn,maxLen:this._maxLen});return s._setAutoCompactRatio(this._autoCompactRatio),s.pushMany(r),s}clone(){let e=this._createInstance({toElementFn:this.toElementFn,maxLen:this._maxLen});e._setAutoCompactRatio(this._autoCompactRatio);for(let t=this._offset;t<this.elements.length;t++)e.push(this.elements[t]);return e}filter(e,t){let n=this._createInstance({toElementFn:this.toElementFn,maxLen:this._maxLen});n._setAutoCompactRatio(this._autoCompactRatio);let i=0;for(let r of this)e.call(t,r,i,this)&&n.push(r),i++;return n}map(e,t,n){var s,o;let i=new this.constructor([],{toElementFn:t==null?void 0:t.toElementFn,maxLen:(s=t==null?void 0:t.maxLen)!=null?s:this._maxLen,autoCompactRatio:(o=t==null?void 0:t.autoCompactRatio)!=null?o:this._autoCompactRatio}),r=0;for(let u of this)i.push(n===void 0?e(u,r++,this):e.call(n,u,r++,this));return i}mapSame(e,t){var s;let n=this.constructor,i=new n([],{toElementFn:this.toElementFn,maxLen:this._maxLen,autoCompactRatio:this._autoCompactRatio});(s=i._setAutoCompactRatio)==null||s.call(i,this._autoCompactRatio);let r=0;for(let o of this){let u=t===void 0?e(o,r++,this):e.call(t,o,r++,this);i.push(u)}return i}_setAutoCompactRatio(e){this._autoCompactRatio=e}*_getIterator(){for(let e=this._offset;e<this.elements.length;e++)yield this.elements[e]}*_getReverseIterator(){for(let e=this.length-1;e>=0;e--){let t=this.at(e);t!==void 0&&(yield t)}}_createInstance(e){let t=this.constructor;return new t([],e)}_createLike(e=[],t){let n=this.constructor;return new n(e,t)}};var M=class{*[Symbol.iterator](...l){yield*this._getIterator(...l)}*entries(){for(let l of this)yield l}*keys(){for(let l of this)yield l[0]}*values(){for(let l of this)yield l[1]}every(l,e){let t=0;for(let n of this)if(!l.call(e,n[1],n[0],t++,this))return!1;return!0}some(l,e){let t=0;for(let n of this)if(l.call(e,n[1],n[0],t++,this))return!0;return!1}forEach(l,e){let t=0;for(let n of this){let[i,r]=n;l.call(e,r,i,t++,this)}}find(l,e){let t=0;for(let n of this){let[i,r]=n;if(l.call(e,r,i,t++,this))return n}}has(l){for(let e of this){let[t]=e;if(t===l)return!0}return!1}hasValue(l){for(let[,e]of this)if(e===l)return!0;return!1}get(l){for(let e of this){let[t,n]=e;if(t===l)return n}}reduce(l,e){let t=e,n=0;for(let i of this){let[r,s]=i;t=l(t,s,r,n++,this)}return t}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var v=(e=>(e[e.VISIT=0]="VISIT",e[e.PROCESS=1]="PROCESS",e))(v||{}),E=class{constructor(l,e,t=!0,n=!0){this.low=l;this.high=e;this.includeLow=t;this.includeHigh=n;if(!(R(l)&&R(e)))throw new RangeError("low or high is not comparable");if(l>e)throw new RangeError("low must be less than or equal to high")}isInRange(l,e){let t=this.includeLow?e(l,this.low)>=0:e(l,this.low)>0,n=this.includeHigh?e(l,this.high)<=0:e(l,this.high)<0;return t&&n}};var b=class{constructor(l,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=l,this.value=e}get left(){return this._left}set left(l){l&&(l.parent=this),this._left=l}get right(){return this._right}set right(l){l&&(l.parent=this),this._right=l}get height(){return this._height}set height(l){this._height=l}get color(){return this._color}set color(l){this._color=l}get count(){return this._count}set count(l){this._count=l}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},O=class extends M{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?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 b(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 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 E}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:R(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 V([this._root]),s;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;s===void 0&&(o.left===void 0||o.right===void 0)&&(s=o),o.left!==null&&o.left&&r.push(o.left),o.right!==null&&o.right&&r.push(o.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}addMany(e,t){let n=[],i;t&&(i=t[Symbol.iterator]());for(let r of e){let s;if(i){let o=i.next();o.done||(s=o.value)}this.isRaw(r)&&(r=this._toEntryFn(r)),n.push(this.add(r,s))}return n}merge(e){this.addMany(e,[])}refill(e,t){this.clear(),this.addMany(e,t)}delete(e){let t=[];if(!this._root)return t;let n=this.getNode(e);if(!n)return t;let i=n==null?void 0:n.parent,r,s=n;if(!n.left&&!n.right&&!i)this._setRoot(void 0);else if(n.left){let o=this.getRightMost(u=>u,n.left);if(o){let u=o.parent;s=this._swapProperties(n,o),u&&(u.right===o?u.right=o.left:u.left=o.left,r=u)}}else if(i){let{familyPosition:o}=n;o==="LEFT"||o==="ROOT_LEFT"?i.left=n.right:(o==="RIGHT"||o==="ROOT_RIGHT")&&(i.right=n.right),r=i}else this._setRoot(n.right),n.right=void 0;return this._size=this._size-1,t.push({deleted:s,needBalanced:r}),this._isMapMode&&s&&this._store.delete(s.key),t}search(e,t=!1,n=this._DEFAULT_NODE_CALLBACK,i=this._root,r=this.iterationType){if(e===void 0)return[];if(e===null)return[];if(i=this.ensureNode(i),!i)return[];let s=this._ensurePredicate(e),o=[];if(r==="RECURSIVE"){let u=a=>{s(a)&&(o.push(n(a)),t)||!this.isRealNode(a.left)&&!this.isRealNode(a.right)||(this.isRealNode(a.left)&&u(a.left),this.isRealNode(a.right)&&u(a.right))};u(i)}else{let u=[i];for(;u.length>0;){let a=u.pop();if(this.isRealNode(a)){if(s(a)&&(o.push(n(a)),t))return o;this.isRealNode(a.left)&&u.push(a.left),this.isRealNode(a.right)&&u.push(a.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,u,a)=>{if(!this.isRealNode(o))return!0;let f=Number(o.key);return f<=u||f>=a?!1:i(o.left,u,f)&&i(o.right,f,a)},r=i(n,Number.MIN_SAFE_INTEGER,Number.MAX_SAFE_INTEGER),s=i(n,Number.MAX_SAFE_INTEGER,Number.MIN_SAFE_INTEGER);return r||s}else{let i=(o=!1)=>{let u=[],a=o?Number.MAX_SAFE_INTEGER:Number.MIN_SAFE_INTEGER,f=n;for(;this.isRealNode(f)||u.length>0;){for(;this.isRealNode(f);)u.push(f),f=f.left;f=u.pop();let T=Number(f.key);if(!this.isRealNode(f)||!o&&a>=T||o&&a<=T)return!1;a=T,f=f.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 o=this.isRealNode(i.left)?s.get(i.left):-1,u=this.isRealNode(i.right)?s.get(i.right):-1;s.set(i,1+Math.min(o,u)),r=i,i=null}}else i=i.right;return s.get(e)}}getPathToRoot(e,t=this._DEFAULT_NODE_CALLBACK,n=!1){let i=[],r=this.ensureNode(e);if(!r)return i;for(;r.parent;)i.push(t(r)),r=r.parent;return i.push(t(r)),n?i.reverse():i}getLeftMost(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){if(this.isNIL(t))return e(void 0);let i=this.ensureNode(t);if(!this.isRealNode(i))return e(void 0);if(n==="RECURSIVE"){let r=s=>{let{left:o}=s;return this.isRealNode(o)?r(o):s};return e(r(i))}else{let r=w(s=>{let{left:o}=s;return this.isRealNode(o)?L(()=>r(o)):s});return e(r(i))}}getRightMost(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){if(this.isNIL(t)||(t=this.ensureNode(t),!t))return e(void 0);if(n==="RECURSIVE"){let i=r=>{let{right:s}=r;return this.isRealNode(s)?i(s):r};return e(i(t))}else{let i=w(r=>{let{right:s}=r;return this.isRealNode(s)?L(()=>i(s)):r});return e(i(t))}}getPredecessor(e){if(this.isRealNode(e.left)){let t=e.left;for(;!this.isRealNode(t)||this.isRealNode(t.right)&&t.right!==e;)this.isRealNode(t)&&(t=t.right);return t}else return e}getSuccessor(e){if(e=this.ensureNode(e),!this.isRealNode(e))return;if(this.isRealNode(e.right))return this.getLeftMost(n=>n,e.right);let t=e.parent;for(;this.isRealNode(t)&&e===t.right;)e=t,t=t.parent;return t}dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,r=this.iterationType,s=!1){return i=this.ensureNode(i),i?this._dfs(e,t,n,i,r,s):[]}bfs(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType,i=!1){if(t=this.ensureNode(t),!t)return[];let r=[];if(n==="RECURSIVE"){let s=new V([t]),o=u=>{if(s.length===0)return;let a=s.shift();r.push(e(a)),i?(a&&this.isRealNodeOrNull(a.left)&&s.push(a.left),a&&this.isRealNodeOrNull(a.right)&&s.push(a.right)):(this.isRealNode(a.left)&&s.push(a.left),this.isRealNode(a.right)&&s.push(a.right)),o(u+1)};o(0)}else{let s=new V([t]);for(;s.length>0;){let o=s.length;for(let u=0;u<o;u++){let a=s.shift();r.push(e(a)),i?(a&&this.isRealNodeOrNull(a.left)&&s.push(a.left),a&&this.isRealNodeOrNull(a.right)&&s.push(a.right)):(this.isRealNode(a.left)&&s.push(a.left),this.isRealNode(a.right)&&s.push(a.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 V([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=(o,u)=>{r[u]||(r[u]=[]),r[u].push(e(o)),i?(o&&this.isRealNodeOrNull(o.left)&&s(o.left,u+1),o&&this.isRealNodeOrNull(o.right)&&s(o.right,u+1)):(o&&o.left&&s(o.left,u+1),o&&o.right&&s(o.right,u+1))};s(t,0)}else{let s=[[t,0]];for(;s.length>0;){let o=s.pop(),[u,a]=o;r[a]||(r[a]=[]),r[a].push(e(u)),i?(u&&this.isRealNodeOrNull(u.right)&&s.push([u.right,a+1]),u&&this.isRealNodeOrNull(u.left)&&s.push([u.left,a+1])):(u&&u.right&&s.push([u.right,a+1]),u&&u.left&&s.push([u.left,a+1]))}}return r}morris(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=this._root){if(n=this.ensureNode(n),!n)return[];let i=[],r=n,s=u=>{let a=null,f=null;for(;u;)f=u.right,u.right=a,a=u,u=f;return a},o=u=>{let a=s(u),f=a;for(;f;)i.push(e(f)),f=f.right;s(a)};switch(t){case"IN":for(;r;){if(r.left){let u=this.getPredecessor(r);if(u.right)u.right=null;else{u.right=r,r=r.left;continue}}i.push(e(r)),r=r.right}break;case"PRE":for(;r;){if(r.left){let u=this.getPredecessor(r);if(u.right)u.right=null;else{u.right=r,i.push(e(r)),r=r.left;continue}}else i.push(e(r));r=r.right}break;case"POST":for(;r;){if(r.left){let u=this.getPredecessor(r);if(u.right===null){u.right=r,r=r.left;continue}else u.right=null,o(r.left)}r=r.right}o(n);break}return i}clone(){let e=this._createInstance();return this._clone(e),e}filter(e,t){let n=this._createInstance(),i=0;for(let[r,s]of this)e.call(t,s,r,i++,this)&&n.add([r,s]);return n}map(e,t,n){let i=this._createLike([],t),r=0;for(let[s,o]of this)i.add(e.call(n,o,s,r++,this));return i}toVisual(e=this._root,t){let n={isShowUndefined:!1,isShowNull:!0,isShowRedBlackNIL:!1,...t};e=this.ensureNode(e);let i="";return e&&(n.isShowUndefined&&(i+=`U for undefined
|
|
1
|
+
"use strict";var binaryTreeTyped=(()=>{var C=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var Q=Object.getOwnPropertyNames;var z=Object.prototype.hasOwnProperty;var G=(d,l,e)=>l in d?C(d,l,{enumerable:!0,configurable:!0,writable:!0,value:e}):d[l]=e;var j=(d,l)=>{for(var e in l)C(d,e,{get:l[e],enumerable:!0})},q=(d,l,e,t)=>{if(l&&typeof l=="object"||typeof l=="function")for(let n of Q(l))!z.call(d,n)&&n!==e&&C(d,n,{get:()=>l[n],enumerable:!(t=H(l,n))||t.enumerable});return d};var W=d=>q(C({},"__esModule",{value:!0}),d);var p=(d,l,e)=>G(d,typeof l!="symbol"?l+"":l,e);var Y={};j(Y,{BinaryTree:()=>O,BinaryTreeNode:()=>g,DFSOperation:()=>v,Range:()=>E});function k(d){let l=typeof d;return l==="number"?!0:l==="bigint"||l==="string"||l==="boolean"}function S(d){if(typeof d.valueOf=="function"){let l=d.valueOf();if(l!==d){if(k(l))return l;if(typeof l=="object"&&l!==null)return S(l)}}if(typeof d.toString=="function"){let l=d.toString();if(l!=="[object Object]")return l}return null}function R(d,l=!1){if(d==null)return!1;if(k(d))return!0;if(typeof d!="object")return!1;if(d instanceof Date||l)return!0;let e=S(d);return e==null?!1:k(e)}var L=d=>({isThunk:!0,fn:d}),X=d=>typeof d=="object"&&d!==null&&"isThunk"in d&&d.isThunk;function J(d){let l=d;for(;X(l);)l=l.fn();return l}function w(d){return(...l)=>J(d(...l))}var I=class{constructor(l){p(this,"_toElementFn");if(l){let{toElementFn:e}=l;if(typeof e=="function")this._toElementFn=e;else if(e)throw new TypeError("toElementFn must be a function type")}}get toElementFn(){return this._toElementFn}*[Symbol.iterator](...l){yield*this._getIterator(...l)}*values(){for(let l of this)yield l}every(l,e){let t=0;for(let n of this)if(e===void 0){if(!l(n,t++,this))return!1}else if(!l.call(e,n,t++,this))return!1;return!0}some(l,e){let t=0;for(let n of this)if(e===void 0){if(l(n,t++,this))return!0}else if(l.call(e,n,t++,this))return!0;return!1}forEach(l,e){let t=0;for(let n of this)e===void 0?l(n,t++,this):l.call(e,n,t++,this)}find(l,e){let t=0;for(let n of this)if(e===void 0){if(l(n,t++,this))return n}else if(l.call(e,n,t++,this))return n}has(l){for(let e of this)if(e===l)return!0;return!1}reduce(l,e){let t=0,n=this[Symbol.iterator](),i;if(arguments.length>=2)i=e;else{let 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=l(i,r,t++,this);return i}toArray(){return[...this]}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var x=class d extends I{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 d?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 V=class d extends x{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 d(e)}isEmpty(){return this.length===0}push(e){return this.elements.push(e),this._maxLen>0&&this.length>this._maxLen&&this.shift(),!0}pushMany(e){let t=[];for(let n of e)this.toElementFn?t.push(this.push(this.toElementFn(n))):t.push(this.push(n));return t}shift(){if(this.length===0)return;let e=this.first;return this._offset+=1,this.elements.length>0&&this.offset/this.elements.length>this.autoCompactRatio&&this.compact(),e}delete(e){for(let t=this._offset;t<this.elements.length;t++)if(Object.is(this.elements[t],e))return this.elements.splice(t,1),!0;return!1}at(e){if(!(e<0||e>=this.length))return this._elements[this._offset+e]}deleteAt(e){if(e<0||e>=this.length)return;let t=this._offset+e,[n]=this.elements.splice(t,1);return n}addAt(e,t){return e<0||e>this.length?!1:(this._elements.splice(this._offset+e,0,t),!0)}setAt(e,t){return e<0||e>=this.length?!1:(this._elements[this._offset+e]=t,!0)}reverse(){return this._elements=this.elements.slice(this._offset).reverse(),this._offset=0,this}clear(){this._elements=[],this._offset=0}compact(){return this._elements=this.elements.slice(this._offset),this._offset=0,!0}splice(e,t=0,...n){e=Math.max(0,Math.min(e,this.length)),t=Math.max(0,Math.min(t,this.length-e));let i=this._offset+e,r=this._elements.splice(i,t,...n);this.elements.length>0&&this.offset/this.elements.length>this.autoCompactRatio&&this.compact();let s=this._createInstance({toElementFn:this.toElementFn,maxLen:this._maxLen});return s._setAutoCompactRatio(this._autoCompactRatio),s.pushMany(r),s}clone(){let e=this._createInstance({toElementFn:this.toElementFn,maxLen:this._maxLen});e._setAutoCompactRatio(this._autoCompactRatio);for(let t=this._offset;t<this.elements.length;t++)e.push(this.elements[t]);return e}filter(e,t){let n=this._createInstance({toElementFn:this.toElementFn,maxLen:this._maxLen});n._setAutoCompactRatio(this._autoCompactRatio);let i=0;for(let r of this)e.call(t,r,i,this)&&n.push(r),i++;return n}map(e,t,n){var s,o;let i=new this.constructor([],{toElementFn:t==null?void 0:t.toElementFn,maxLen:(s=t==null?void 0:t.maxLen)!=null?s:this._maxLen,autoCompactRatio:(o=t==null?void 0:t.autoCompactRatio)!=null?o:this._autoCompactRatio}),r=0;for(let u of this)i.push(n===void 0?e(u,r++,this):e.call(n,u,r++,this));return i}mapSame(e,t){var s;let n=this.constructor,i=new n([],{toElementFn:this.toElementFn,maxLen:this._maxLen,autoCompactRatio:this._autoCompactRatio});(s=i._setAutoCompactRatio)==null||s.call(i,this._autoCompactRatio);let r=0;for(let o of this){let u=t===void 0?e(o,r++,this):e.call(t,o,r++,this);i.push(u)}return i}_setAutoCompactRatio(e){this._autoCompactRatio=e}*_getIterator(){for(let e=this._offset;e<this.elements.length;e++)yield this.elements[e]}*_getReverseIterator(){for(let e=this.length-1;e>=0;e--){let t=this.at(e);t!==void 0&&(yield t)}}_createInstance(e){let t=this.constructor;return new t([],e)}_createLike(e=[],t){let n=this.constructor;return new n(e,t)}};var M=class{*[Symbol.iterator](...l){yield*this._getIterator(...l)}*entries(){for(let l of this)yield l}*keys(){for(let l of this)yield l[0]}*values(){for(let l of this)yield l[1]}every(l,e){let t=0;for(let n of this)if(!l.call(e,n[1],n[0],t++,this))return!1;return!0}some(l,e){let t=0;for(let n of this)if(l.call(e,n[1],n[0],t++,this))return!0;return!1}forEach(l,e){let t=0;for(let n of this){let[i,r]=n;l.call(e,r,i,t++,this)}}find(l,e){let t=0;for(let n of this){let[i,r]=n;if(l.call(e,r,i,t++,this))return n}}has(l){for(let e of this){let[t]=e;if(t===l)return!0}return!1}hasValue(l){for(let[,e]of this)if(e===l)return!0;return!1}get(l){for(let e of this){let[t,n]=e;if(t===l)return n}}reduce(l,e){let t=e,n=0;for(let i of this){let[r,s]=i;t=l(t,s,r,n++,this)}return t}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var v=(e=>(e[e.VISIT=0]="VISIT",e[e.PROCESS=1]="PROCESS",e))(v||{}),E=class{constructor(l,e,t=!0,n=!0){this.low=l;this.high=e;this.includeLow=t;this.includeHigh=n;if(!(R(l)&&R(e)))throw new RangeError("low or high is not comparable");if(l>e)throw new RangeError("low must be less than or equal to high")}isInRange(l,e){let t=this.includeLow?e(l,this.low)>=0:e(l,this.low)>0,n=this.includeHigh?e(l,this.high)<=0:e(l,this.high)<0;return t&&n}};var g=class{constructor(l,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=l,this.value=e}get left(){return this._left}set left(l){l&&(l.parent=this),this._left=l}get right(){return this._right}set right(l){l&&(l.parent=this),this._right=l}get height(){return this._height}set height(l){this._height=l}get color(){return this._color}set color(l){this._color=l}get count(){return this._count}set count(l){this._count=l}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},O=class extends M{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 g(NaN));p(this,"_toEntryFn");p(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 g(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 g}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 E}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:R(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 V([this._root]),s;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;s===void 0&&(o.left===void 0||o.right===void 0)&&(s=o),o.left!==null&&o.left&&r.push(o.left),o.right!==null&&o.right&&r.push(o.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 o=i.next();o.done||(s=o.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 o=this.getRightMost(u=>u,n.left);if(o){let u=o.parent;s=this._swapProperties(n,o),u&&(u.right===o?u.right=o.left:u.left=o.left,r=u)}}else if(i){let{familyPosition:o}=n;o==="LEFT"||o==="ROOT_LEFT"?i.left=n.right:(o==="RIGHT"||o==="ROOT_RIGHT")&&(i.right=n.right),r=i}else this._setRoot(n.right),n.right=void 0;return this._size=this._size-1,t.push({deleted:s,needBalanced:r}),this._isMapMode&&s&&this._store.delete(s.key),t}search(e,t=!1,n=this._DEFAULT_NODE_CALLBACK,i=this._root,r=this.iterationType){if(e===void 0)return[];if(e===null)return[];if(i=this.ensureNode(i),!i)return[];let s=this._ensurePredicate(e),o=[];if(r==="RECURSIVE"){let u=a=>{s(a)&&(o.push(n(a)),t)||!this.isRealNode(a.left)&&!this.isRealNode(a.right)||(this.isRealNode(a.left)&&u(a.left),this.isRealNode(a.right)&&u(a.right))};u(i)}else{let u=[i];for(;u.length>0;){let a=u.pop();if(this.isRealNode(a)){if(s(a)&&(o.push(n(a)),t))return o;this.isRealNode(a.left)&&u.push(a.left),this.isRealNode(a.right)&&u.push(a.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,u,a)=>{if(!this.isRealNode(o))return!0;let f=Number(o.key);return f<=u||f>=a?!1:i(o.left,u,f)&&i(o.right,f,a)},r=i(n,Number.MIN_SAFE_INTEGER,Number.MAX_SAFE_INTEGER),s=i(n,Number.MAX_SAFE_INTEGER,Number.MIN_SAFE_INTEGER);return r||s}else{let i=(o=!1)=>{let u=[],a=o?Number.MAX_SAFE_INTEGER:Number.MIN_SAFE_INTEGER,f=n;for(;this.isRealNode(f)||u.length>0;){for(;this.isRealNode(f);)u.push(f),f=f.left;f=u.pop();let T=Number(f.key);if(!this.isRealNode(f)||!o&&a>=T||o&&a<=T)return!1;a=T,f=f.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 o=this.isRealNode(i.left)?s.get(i.left):-1,u=this.isRealNode(i.right)?s.get(i.right):-1;s.set(i,1+Math.min(o,u)),r=i,i=null}}else i=i.right;return s.get(e)}}getPathToRoot(e,t=this._DEFAULT_NODE_CALLBACK,n=!1){let i=[],r=this.ensureNode(e);if(!r)return i;for(;r.parent;)i.push(t(r)),r=r.parent;return i.push(t(r)),n?i.reverse():i}getLeftMost(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){if(this.isNIL(t))return e(void 0);let i=this.ensureNode(t);if(!this.isRealNode(i))return e(void 0);if(n==="RECURSIVE"){let r=s=>{let{left:o}=s;return this.isRealNode(o)?r(o):s};return e(r(i))}else{let r=w(s=>{let{left:o}=s;return this.isRealNode(o)?L(()=>r(o)):s});return e(r(i))}}getRightMost(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){if(this.isNIL(t)||(t=this.ensureNode(t),!t))return e(void 0);if(n==="RECURSIVE"){let i=r=>{let{right:s}=r;return this.isRealNode(s)?i(s):r};return e(i(t))}else{let i=w(r=>{let{right:s}=r;return this.isRealNode(s)?L(()=>i(s)):r});return e(i(t))}}getPredecessor(e){if(this.isRealNode(e.left)){let t=e.left;for(;!this.isRealNode(t)||this.isRealNode(t.right)&&t.right!==e;)this.isRealNode(t)&&(t=t.right);return t}else return e}getSuccessor(e){if(e=this.ensureNode(e),!this.isRealNode(e))return;if(this.isRealNode(e.right))return this.getLeftMost(n=>n,e.right);let t=e.parent;for(;this.isRealNode(t)&&e===t.right;)e=t,t=t.parent;return t}dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,r=this.iterationType,s=!1){return i=this.ensureNode(i),i?this._dfs(e,t,n,i,r,s):[]}bfs(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType,i=!1){if(t=this.ensureNode(t),!t)return[];let r=[];if(n==="RECURSIVE"){let s=new V([t]),o=u=>{if(s.length===0)return;let a=s.shift();r.push(e(a)),i?(a&&this.isRealNodeOrNull(a.left)&&s.push(a.left),a&&this.isRealNodeOrNull(a.right)&&s.push(a.right)):(this.isRealNode(a.left)&&s.push(a.left),this.isRealNode(a.right)&&s.push(a.right)),o(u+1)};o(0)}else{let s=new V([t]);for(;s.length>0;){let o=s.length;for(let u=0;u<o;u++){let a=s.shift();r.push(e(a)),i?(a&&this.isRealNodeOrNull(a.left)&&s.push(a.left),a&&this.isRealNodeOrNull(a.right)&&s.push(a.right)):(this.isRealNode(a.left)&&s.push(a.left),this.isRealNode(a.right)&&s.push(a.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 V([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=(o,u)=>{r[u]||(r[u]=[]),r[u].push(e(o)),i?(o&&this.isRealNodeOrNull(o.left)&&s(o.left,u+1),o&&this.isRealNodeOrNull(o.right)&&s(o.right,u+1)):(o&&o.left&&s(o.left,u+1),o&&o.right&&s(o.right,u+1))};s(t,0)}else{let s=[[t,0]];for(;s.length>0;){let o=s.pop(),[u,a]=o;r[a]||(r[a]=[]),r[a].push(e(u)),i?(u&&this.isRealNodeOrNull(u.right)&&s.push([u.right,a+1]),u&&this.isRealNodeOrNull(u.left)&&s.push([u.left,a+1])):(u&&u.right&&s.push([u.right,a+1]),u&&u.left&&s.push([u.left,a+1]))}}return r}morris(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=this._root){if(n=this.ensureNode(n),!n)return[];let i=[],r=n,s=u=>{let a=null,f=null;for(;u;)f=u.right,u.right=a,a=u,u=f;return a},o=u=>{let a=s(u),f=a;for(;f;)i.push(e(f)),f=f.right;s(a)};switch(t){case"IN":for(;r;){if(r.left){let u=this.getPredecessor(r);if(u.right)u.right=null;else{u.right=r,r=r.left;continue}}i.push(e(r)),r=r.right}break;case"PRE":for(;r;){if(r.left){let u=this.getPredecessor(r);if(u.right)u.right=null;else{u.right=r,i.push(e(r)),r=r.left;continue}}else i.push(e(r));r=r.right}break;case"POST":for(;r;){if(r.left){let u=this.getPredecessor(r);if(u.right===null){u.right=r,r=r.left;continue}else u.right=null,o(r.left)}r=r.right}o(n);break}return i}clone(){let e=this._createInstance();return this._clone(e),e}filter(e,t){let n=this._createInstance(),i=0;for(let[r,s]of this)e.call(t,s,r,i++,this)&&n.add([r,s]);return n}map(e,t,n){let i=this._createLike([],t),r=0;for(let[s,o]of this)i.add(e.call(n,o,s,r++,this));return i}toVisual(e=this._root,t){let n={isShowUndefined:!1,isShowNull:!0,isShowRedBlackNIL:!1,...t};e=this.ensureNode(e);let i="";return e&&(n.isShowUndefined&&(i+=`U for undefined
|
|
2
2
|
`),n.isShowNull&&(i+=`N for null
|
|
3
3
|
`),n.isShowRedBlackNIL&&(i+=`S for Sentinel Node(NIL)
|
|
4
4
|
`),(s=>{let[o]=this._displayAux(s,n),u="";for(let a of o)u+=a+`
|
|
5
|
-
`;i+=u})(e)),i}print(e,t=this._root){console.log(this.toVisual(t,e))}_dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,r=this.iterationType,s=!1,o=T=>!!T,u=T=>!!T,a=T=>s?this.isRealNodeOrNull(T):this.isRealNode(T),f=T=>this.isRealNodeOrNull(T)){if(i=this.ensureNode(i),!i)return[];let T=[];if(r==="RECURSIVE"){let m=h=>{if(!a(h))return;let y=()=>{o(h)&&(h==null?void 0:h.left)!==void 0&&m(h==null?void 0:h.left)},K=()=>{u(h)&&(h==null?void 0:h.right)!==void 0&&m(h==null?void 0:h.right)};switch(t){case"IN":if(y(),f(h)&&(T.push(e(h)),n))return;K();break;case"PRE":if(f(h)&&(T.push(e(h)),n))return;y(),K();break;case"POST":if(y(),K(),f(h)&&(T.push(e(h)),n))return;break}};m(i)}else{let m=[{opt:0,node:i}],h=c=>{var N;o(c.node)&&m.push({opt:0,node:(N=c.node)==null?void 0:N.left})},y=c=>{var N;u(c.node)&&m.push({opt:0,node:(N=c.node)==null?void 0:N.right})},K=c=>{a(c.node)&&m.push({opt:1,node:c.node})};for(;m.length>0;){let c=m.pop();if(c!==void 0&&a(c.node))if(c.opt===1){if(f(c.node)&&c.node!==void 0&&(T.push(e(c.node)),n))return T}else switch(t){case"IN":y(c),K(c),h(c);break;case"PRE":y(c),h(c),K(c);break;case"POST":K(c),y(c),h(c);break}}}return T}*_getIterator(e=this._root){if(e)if(this.iterationType==="ITERATIVE"){let t=[],n=e;for(;n||t.length>0;){for(;this.isRealNode(n);)t.push(n),n=n.left;n=t.pop(),this.isRealNode(n)&&(this._isMapMode?yield[n.key,this._store.get(n.key)]:yield[n.key,n.value],n=n.right)}}else e.left&&this.isRealNode(e)&&(yield*this[Symbol.iterator](e.left)),this._isMapMode?yield[e.key,this._store.get(e.key)]:yield[e.key,e.value],e.right&&this.isRealNode(e)&&(yield*this[Symbol.iterator](e.right))}_snapshotOptions(){return{iterationType:this.iterationType,toEntryFn:this.toEntryFn,isMapMode:this.isMapMode,isDuplicate:this.isDuplicate}}_createInstance(e){let t=this.constructor;return new t([],{...this._snapshotOptions(),...e!=null?e:{}})}_createLike(e=[],t){let n=this.constructor;return new n(e,{...this._snapshotOptions(),...t!=null?t:{}})}_keyValueNodeOrEntryToNodeAndValue(e,t){if(e===void 0)return[void 0,void 0];if(e===null)return[null,void 0];if(this.isNode(e))return[e,t];if(this.isEntry(e)){let[n,i]=e;if(n===void 0)return[void 0,void 0];if(n===null)return[null,void 0];let 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 u=e.key,a=this.isNIL(e)?"S":String(u),f=a.length;return o(a,f,this._displayAux(e.left,t),this._displayAux(e.right,t))}else{let u=e===void 0?"U":"N",a=u.length;return o(u,a,[[""],1,0,0],[[""],1,0,0])}function o(u,a,f,T){let[m,h,y,K]=f,[c,N,_,B]=T,D=" ".repeat(Math.max(0,K+1))+"_".repeat(Math.max(0,h-K-1))+u+"_".repeat(Math.max(0,B))+" ".repeat(Math.max(0,N-B)),F=(y>0?" ".repeat(K)+"/"+" ".repeat(h-K-1):" ".repeat(h))+" ".repeat(a)+(_>0?" ".repeat(B)+"\\"+" ".repeat(N-B-1):" ".repeat(N)),A=[D,F];for(let
|
|
5
|
+
`;i+=u})(e)),i}print(e,t=this._root){console.log(this.toVisual(t,e))}_dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,r=this.iterationType,s=!1,o=T=>!!T,u=T=>!!T,a=T=>s?this.isRealNodeOrNull(T):this.isRealNode(T),f=T=>this.isRealNodeOrNull(T)){if(i=this.ensureNode(i),!i)return[];let T=[];if(r==="RECURSIVE"){let m=h=>{if(!a(h))return;let y=()=>{o(h)&&(h==null?void 0:h.left)!==void 0&&m(h==null?void 0:h.left)},K=()=>{u(h)&&(h==null?void 0:h.right)!==void 0&&m(h==null?void 0:h.right)};switch(t){case"IN":if(y(),f(h)&&(T.push(e(h)),n))return;K();break;case"PRE":if(f(h)&&(T.push(e(h)),n))return;y(),K();break;case"POST":if(y(),K(),f(h)&&(T.push(e(h)),n))return;break}};m(i)}else{let m=[{opt:0,node:i}],h=c=>{var N;o(c.node)&&m.push({opt:0,node:(N=c.node)==null?void 0:N.left})},y=c=>{var N;u(c.node)&&m.push({opt:0,node:(N=c.node)==null?void 0:N.right})},K=c=>{a(c.node)&&m.push({opt:1,node:c.node})};for(;m.length>0;){let c=m.pop();if(c!==void 0&&a(c.node))if(c.opt===1){if(f(c.node)&&c.node!==void 0&&(T.push(e(c.node)),n))return T}else switch(t){case"IN":y(c),K(c),h(c);break;case"PRE":y(c),h(c),K(c);break;case"POST":K(c),y(c),h(c);break}}}return T}*_getIterator(e=this._root){if(e)if(this.iterationType==="ITERATIVE"){let t=[],n=e;for(;n||t.length>0;){for(;this.isRealNode(n);)t.push(n),n=n.left;n=t.pop(),this.isRealNode(n)&&(this._isMapMode?yield[n.key,this._store.get(n.key)]:yield[n.key,n.value],n=n.right)}}else e.left&&this.isRealNode(e)&&(yield*this[Symbol.iterator](e.left)),this._isMapMode?yield[e.key,this._store.get(e.key)]:yield[e.key,e.value],e.right&&this.isRealNode(e)&&(yield*this[Symbol.iterator](e.right))}_snapshotOptions(){return{iterationType:this.iterationType,toEntryFn:this.toEntryFn,isMapMode:this.isMapMode,isDuplicate:this.isDuplicate}}_createInstance(e){let t=this.constructor;return new t([],{...this._snapshotOptions(),...e!=null?e:{}})}_createLike(e=[],t){let n=this.constructor;return new n(e,{...this._snapshotOptions(),...t!=null?t:{}})}_keyValueNodeOrEntryToNodeAndValue(e,t){if(e===void 0)return[void 0,void 0];if(e===null)return[null,void 0];if(this.isNode(e))return[e,t];if(this.isEntry(e)){let[n,i]=e;if(n===void 0)return[void 0,void 0];if(n===null)return[null,void 0];let 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 u=e.key,a=this.isNIL(e)?"S":String(u),f=a.length;return o(a,f,this._displayAux(e.left,t),this._displayAux(e.right,t))}else{let u=e===void 0?"U":"N",a=u.length;return o(u,a,[[""],1,0,0],[[""],1,0,0])}function o(u,a,f,T){let[m,h,y,K]=f,[c,N,_,B]=T,D=" ".repeat(Math.max(0,K+1))+"_".repeat(Math.max(0,h-K-1))+u+"_".repeat(Math.max(0,B))+" ".repeat(Math.max(0,N-B)),F=(y>0?" ".repeat(K)+"/"+" ".repeat(h-K-1):" ".repeat(h))+" ".repeat(a)+(_>0?" ".repeat(B)+"\\"+" ".repeat(N-B-1):" ".repeat(N)),A=[D,F];for(let b=0;b<Math.max(y,_);b++){let U=b<y?m[b]:" ".repeat(h),P=b<_?c[b]:" ".repeat(N);A.push(U+" ".repeat(a)+P)}return[A,h+a+N,Math.max(y,_)+2,h+Math.floor(a/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()}};return W(Y);})();
|
|
6
6
|
/**
|
|
7
7
|
* data-structure-typed
|
|
8
8
|
*
|