data-structure-typed 2.5.0 → 2.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vitepress/cache/deps_temp_51f5f1b0/chunk-7OIKW5WK.js +12984 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/package.json +3 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vue_devtools-api.js +4505 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vueuse_core.js +9731 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/vue.js +347 -0
- package/CHANGELOG.md +5 -1
- package/README.md +124 -29
- package/dist/cjs/binary-tree.cjs +26282 -0
- package/dist/cjs/graph.cjs +5422 -0
- package/dist/cjs/hash.cjs +1310 -0
- package/dist/cjs/heap.cjs +1602 -0
- package/dist/cjs/index.cjs +31257 -14673
- package/dist/cjs/linked-list.cjs +4576 -0
- package/dist/cjs/matrix.cjs +1080 -0
- package/dist/cjs/priority-queue.cjs +1376 -0
- package/dist/cjs/queue.cjs +4264 -0
- package/dist/cjs/stack.cjs +907 -0
- package/dist/cjs/trie.cjs +1223 -0
- package/dist/cjs-legacy/binary-tree.cjs +26319 -0
- package/dist/cjs-legacy/graph.cjs +5420 -0
- package/dist/cjs-legacy/hash.cjs +1310 -0
- package/dist/cjs-legacy/heap.cjs +1599 -0
- package/dist/cjs-legacy/index.cjs +31268 -14679
- package/dist/cjs-legacy/linked-list.cjs +4582 -0
- package/dist/cjs-legacy/matrix.cjs +1083 -0
- package/dist/cjs-legacy/priority-queue.cjs +1374 -0
- package/dist/cjs-legacy/queue.cjs +4262 -0
- package/dist/cjs-legacy/stack.cjs +907 -0
- package/dist/cjs-legacy/trie.cjs +1222 -0
- package/dist/esm/binary-tree.mjs +26267 -0
- package/dist/esm/graph.mjs +5409 -0
- package/dist/esm/hash.mjs +1307 -0
- package/dist/esm/heap.mjs +1596 -0
- package/dist/esm/index.mjs +31254 -14674
- package/dist/esm/linked-list.mjs +4569 -0
- package/dist/esm/matrix.mjs +1076 -0
- package/dist/esm/priority-queue.mjs +1372 -0
- package/dist/esm/queue.mjs +4260 -0
- package/dist/esm/stack.mjs +905 -0
- package/dist/esm/trie.mjs +1220 -0
- package/dist/esm-legacy/binary-tree.mjs +26304 -0
- package/dist/esm-legacy/graph.mjs +5407 -0
- package/dist/esm-legacy/hash.mjs +1307 -0
- package/dist/esm-legacy/heap.mjs +1593 -0
- package/dist/esm-legacy/index.mjs +31265 -14680
- package/dist/esm-legacy/linked-list.mjs +4575 -0
- package/dist/esm-legacy/matrix.mjs +1079 -0
- package/dist/esm-legacy/priority-queue.mjs +1370 -0
- package/dist/esm-legacy/queue.mjs +4258 -0
- package/dist/esm-legacy/stack.mjs +905 -0
- package/dist/esm-legacy/trie.mjs +1219 -0
- package/dist/types/common/error.d.ts +9 -0
- package/dist/types/common/index.d.ts +1 -1
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
- package/dist/types/data-structures/base/linear-base.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +288 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +336 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +618 -18
- package/dist/types/data-structures/binary-tree/bst.d.ts +676 -1
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +456 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +144 -1
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3307 -399
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3285 -360
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2674 -325
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3072 -287
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +240 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +216 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +274 -10
- package/dist/types/data-structures/heap/heap.d.ts +336 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +411 -3
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +363 -3
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +434 -2
- package/dist/types/data-structures/matrix/matrix.d.ts +192 -0
- package/dist/types/data-structures/queue/deque.d.ts +364 -4
- package/dist/types/data-structures/queue/queue.d.ts +288 -0
- package/dist/types/data-structures/stack/stack.d.ts +240 -0
- package/dist/types/data-structures/trie/trie.d.ts +292 -4
- package/dist/types/interfaces/graph.d.ts +1 -1
- package/dist/types/types/common.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
- package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/types/utils/validate-type.d.ts +4 -4
- package/dist/umd/data-structure-typed.js +31196 -14608
- package/dist/umd/data-structure-typed.min.js +11 -5
- package/docs-site-docusaurus/README.md +41 -0
- package/docs-site-docusaurus/docs/api/README.md +52 -0
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +6644 -0
- package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +282 -0
- package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +2266 -0
- package/docs-site-docusaurus/docs/api/classes/BST.md +6293 -0
- package/docs-site-docusaurus/docs/api/classes/BSTNode.md +333 -0
- package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +455 -0
- package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +4647 -0
- package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +331 -0
- package/docs-site-docusaurus/docs/api/classes/Deque.md +2767 -0
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +2999 -0
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +2685 -0
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +221 -0
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +253 -0
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +21 -0
- package/docs-site-docusaurus/docs/api/classes/HashMap.md +1333 -0
- package/docs-site-docusaurus/docs/api/classes/Heap.md +1881 -0
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +800 -0
- package/docs-site-docusaurus/docs/api/classes/IterableEntryBase.md +644 -0
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +1632 -0
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +1853 -0
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +1108 -0
- package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +156 -0
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +2824 -0
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +2929 -0
- package/docs-site-docusaurus/docs/api/classes/Matrix.md +1026 -0
- package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +1866 -0
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +1883 -0
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +1879 -0
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +1882 -0
- package/docs-site-docusaurus/docs/api/classes/Navigator.md +109 -0
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +1839 -0
- package/docs-site-docusaurus/docs/api/classes/Queue.md +2244 -0
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +6888 -0
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +372 -0
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +2897 -0
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +169 -0
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +1229 -0
- package/docs-site-docusaurus/docs/api/classes/Stack.md +1573 -0
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +1389 -0
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +1591 -0
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +1246 -0
- package/docs-site-docusaurus/docs/api/classes/Trie.md +1708 -0
- package/docs-site-docusaurus/docs/api/classes/TrieNode.md +199 -0
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +2979 -0
- package/docs-site-docusaurus/docs/guide/_category_.json +6 -0
- package/docs-site-docusaurus/docs/guide/architecture.md +615 -0
- package/docs-site-docusaurus/docs/guide/concepts.md +451 -0
- package/docs-site-docusaurus/docs/guide/faq.md +180 -0
- package/docs-site-docusaurus/docs/guide/guides.md +597 -0
- package/docs-site-docusaurus/docs/guide/installation.md +62 -0
- package/docs-site-docusaurus/docs/guide/integrations.md +825 -0
- package/docs-site-docusaurus/docs/guide/overview.md +645 -0
- package/docs-site-docusaurus/docs/guide/performance.md +835 -0
- package/docs-site-docusaurus/docs/guide/quick-start.md +104 -0
- package/docs-site-docusaurus/docs/guide/use-cases/_category_.json +6 -0
- package/docs-site-docusaurus/docs/guide/use-cases/array-sort-alternative.md +158 -0
- package/docs-site-docusaurus/docs/guide/use-cases/heap-vs-sorting.md +92 -0
- package/docs-site-docusaurus/docs/guide/use-cases/map-vs-treemap.md +151 -0
- package/docs-site-docusaurus/docs/guide/use-cases/priority-queue-typescript.md +113 -0
- package/docs-site-docusaurus/docs/guide/use-cases/treemap-javascript.md +151 -0
- package/docs-site-docusaurus/docusaurus.config.ts +159 -0
- package/docs-site-docusaurus/fix-mdx-generics.mjs +75 -0
- package/docs-site-docusaurus/package-lock.json +18667 -0
- package/docs-site-docusaurus/package.json +50 -0
- package/docs-site-docusaurus/prefix-class-to-methods.mjs +48 -0
- package/docs-site-docusaurus/sidebars.ts +23 -0
- package/docs-site-docusaurus/sort-protected.mjs +87 -0
- package/docs-site-docusaurus/src/css/custom.css +96 -0
- package/docs-site-docusaurus/src/pages/index.module.css +13 -0
- package/docs-site-docusaurus/src/pages/index.tsx +120 -0
- package/docs-site-docusaurus/src/pages/markdown-page.md +7 -0
- package/docs-site-docusaurus/src/theme/TOCItems/index.tsx +34 -0
- package/docs-site-docusaurus/static/.nojekyll +0 -0
- package/docs-site-docusaurus/static/img/docusaurus-social-card.jpg +0 -0
- package/docs-site-docusaurus/static/img/docusaurus.png +0 -0
- package/docs-site-docusaurus/static/img/favicon.ico +0 -0
- package/docs-site-docusaurus/static/img/favicon.png +0 -0
- package/docs-site-docusaurus/static/img/logo-180.png +0 -0
- package/docs-site-docusaurus/static/img/logo.jpg +0 -0
- package/docs-site-docusaurus/static/img/logo.png +0 -0
- package/docs-site-docusaurus/static/img/logo.svg +1 -0
- package/docs-site-docusaurus/static/img/og-image.png +0 -0
- package/docs-site-docusaurus/static/img/undraw_docusaurus_mountain.svg +171 -0
- package/docs-site-docusaurus/static/img/undraw_docusaurus_react.svg +170 -0
- package/docs-site-docusaurus/static/img/undraw_docusaurus_tree.svg +40 -0
- package/docs-site-docusaurus/static/llms.txt +37 -0
- package/docs-site-docusaurus/static/robots.txt +4 -0
- package/docs-site-docusaurus/typedoc.json +23 -0
- package/llms.txt +37 -0
- package/package.json +159 -55
- package/src/common/error.ts +19 -1
- package/src/common/index.ts +1 -1
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +3 -2
- package/src/data-structures/base/iterable-entry-base.ts +8 -8
- package/src/data-structures/base/linear-base.ts +3 -3
- package/src/data-structures/binary-tree/avl-tree.ts +287 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +327 -5
- package/src/data-structures/binary-tree/binary-tree.ts +581 -6
- package/src/data-structures/binary-tree/bst.ts +922 -7
- package/src/data-structures/binary-tree/red-black-tree.ts +453 -0
- package/src/data-structures/binary-tree/segment-tree.ts +139 -2
- package/src/data-structures/binary-tree/tree-map.ts +3300 -495
- package/src/data-structures/binary-tree/tree-multi-map.ts +3384 -563
- package/src/data-structures/binary-tree/tree-multi-set.ts +2757 -493
- package/src/data-structures/binary-tree/tree-set.ts +3122 -440
- package/src/data-structures/graph/abstract-graph.ts +6 -6
- package/src/data-structures/graph/directed-graph.ts +230 -0
- package/src/data-structures/graph/undirected-graph.ts +207 -0
- package/src/data-structures/hash/hash-map.ts +270 -19
- package/src/data-structures/heap/heap.ts +326 -4
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +394 -3
- package/src/data-structures/linked-list/singly-linked-list.ts +348 -3
- package/src/data-structures/linked-list/skip-linked-list.ts +421 -7
- package/src/data-structures/matrix/matrix.ts +194 -10
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +350 -5
- package/src/data-structures/queue/queue.ts +276 -0
- package/src/data-structures/stack/stack.ts +230 -0
- package/src/data-structures/trie/trie.ts +283 -7
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/binary-tree/bst.ts +1 -0
- package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
- package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
- package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/utils/validate-type.ts +4 -4
- package/vercel.json +6 -0
- package/dist/leetcode/avl-tree-counter.mjs +0 -2957
- package/dist/leetcode/avl-tree-multi-map.mjs +0 -2889
- package/dist/leetcode/avl-tree.mjs +0 -2720
- package/dist/leetcode/binary-tree.mjs +0 -1594
- package/dist/leetcode/bst.mjs +0 -2398
- package/dist/leetcode/deque.mjs +0 -683
- package/dist/leetcode/directed-graph.mjs +0 -1733
- package/dist/leetcode/doubly-linked-list.mjs +0 -709
- package/dist/leetcode/hash-map.mjs +0 -493
- package/dist/leetcode/heap.mjs +0 -542
- package/dist/leetcode/max-heap.mjs +0 -375
- package/dist/leetcode/max-priority-queue.mjs +0 -383
- package/dist/leetcode/min-heap.mjs +0 -363
- package/dist/leetcode/min-priority-queue.mjs +0 -371
- package/dist/leetcode/priority-queue.mjs +0 -363
- package/dist/leetcode/queue.mjs +0 -943
- package/dist/leetcode/red-black-tree.mjs +0 -2765
- package/dist/leetcode/singly-linked-list.mjs +0 -754
- package/dist/leetcode/stack.mjs +0 -217
- package/dist/leetcode/tree-counter.mjs +0 -3039
- package/dist/leetcode/tree-multi-map.mjs +0 -2913
- package/dist/leetcode/trie.mjs +0 -413
- package/dist/leetcode/undirected-graph.mjs +0 -1650
|
@@ -1,1594 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
-
function isPrimitiveComparable(value) {
|
|
4
|
-
const valueType = typeof value;
|
|
5
|
-
if (valueType === "number") return true;
|
|
6
|
-
return valueType === "bigint" || valueType === "string" || valueType === "boolean";
|
|
7
|
-
}
|
|
8
|
-
__name(isPrimitiveComparable, "isPrimitiveComparable");
|
|
9
|
-
function tryObjectToPrimitive(obj) {
|
|
10
|
-
if (typeof obj.valueOf === "function") {
|
|
11
|
-
const valueOfResult = obj.valueOf();
|
|
12
|
-
if (valueOfResult !== obj) {
|
|
13
|
-
if (isPrimitiveComparable(valueOfResult)) return valueOfResult;
|
|
14
|
-
if (typeof valueOfResult === "object" && valueOfResult !== null) return tryObjectToPrimitive(valueOfResult);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
if (typeof obj.toString === "function") {
|
|
18
|
-
const stringResult = obj.toString();
|
|
19
|
-
if (stringResult !== "[object Object]") return stringResult;
|
|
20
|
-
}
|
|
21
|
-
return null;
|
|
22
|
-
}
|
|
23
|
-
__name(tryObjectToPrimitive, "tryObjectToPrimitive");
|
|
24
|
-
function isComparable(value, isForceObjectComparable = false) {
|
|
25
|
-
if (value === null || value === void 0) return false;
|
|
26
|
-
if (isPrimitiveComparable(value)) return true;
|
|
27
|
-
if (typeof value !== "object") return false;
|
|
28
|
-
if (value instanceof Date) return true;
|
|
29
|
-
if (isForceObjectComparable) return true;
|
|
30
|
-
const comparableValue = tryObjectToPrimitive(value);
|
|
31
|
-
if (comparableValue === null || comparableValue === void 0) return false;
|
|
32
|
-
return isPrimitiveComparable(comparableValue);
|
|
33
|
-
}
|
|
34
|
-
__name(isComparable, "isComparable");
|
|
35
|
-
var makeTrampolineThunk = __name((computation) => ({
|
|
36
|
-
isThunk: true,
|
|
37
|
-
fn: computation
|
|
38
|
-
}), "makeTrampolineThunk");
|
|
39
|
-
var isTrampolineThunk = __name((value) => typeof value === "object" && // Must be an object
|
|
40
|
-
value !== null && // Must not be null
|
|
41
|
-
"isThunk" in value && // Must have the 'isThunk' property
|
|
42
|
-
value.isThunk, "isTrampolineThunk");
|
|
43
|
-
function trampoline(initial) {
|
|
44
|
-
let current = initial;
|
|
45
|
-
while (isTrampolineThunk(current)) {
|
|
46
|
-
current = current.fn();
|
|
47
|
-
}
|
|
48
|
-
return current;
|
|
49
|
-
}
|
|
50
|
-
__name(trampoline, "trampoline");
|
|
51
|
-
function makeTrampoline(fn) {
|
|
52
|
-
return (...args) => trampoline(fn(...args));
|
|
53
|
-
}
|
|
54
|
-
__name(makeTrampoline, "makeTrampoline");
|
|
55
|
-
var IterableElementBase = class {
|
|
56
|
-
static {
|
|
57
|
-
__name(this, "IterableElementBase");
|
|
58
|
-
}
|
|
59
|
-
constructor(options) {
|
|
60
|
-
if (options) {
|
|
61
|
-
const { toElementFn } = options;
|
|
62
|
-
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
63
|
-
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
_toElementFn;
|
|
67
|
-
get toElementFn() {
|
|
68
|
-
return this._toElementFn;
|
|
69
|
-
}
|
|
70
|
-
*[Symbol.iterator](...args) {
|
|
71
|
-
yield* this._getIterator(...args);
|
|
72
|
-
}
|
|
73
|
-
*values() {
|
|
74
|
-
for (const item of this) yield item;
|
|
75
|
-
}
|
|
76
|
-
every(predicate, thisArg) {
|
|
77
|
-
let index = 0;
|
|
78
|
-
for (const item of this) {
|
|
79
|
-
if (thisArg === void 0) {
|
|
80
|
-
if (!predicate(item, index++, this)) return false;
|
|
81
|
-
} else {
|
|
82
|
-
const fn = predicate;
|
|
83
|
-
if (!fn.call(thisArg, item, index++, this)) return false;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
return true;
|
|
87
|
-
}
|
|
88
|
-
some(predicate, thisArg) {
|
|
89
|
-
let index = 0;
|
|
90
|
-
for (const item of this) {
|
|
91
|
-
if (thisArg === void 0) {
|
|
92
|
-
if (predicate(item, index++, this)) return true;
|
|
93
|
-
} else {
|
|
94
|
-
const fn = predicate;
|
|
95
|
-
if (fn.call(thisArg, item, index++, this)) return true;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return false;
|
|
99
|
-
}
|
|
100
|
-
forEach(callbackfn, thisArg) {
|
|
101
|
-
let index = 0;
|
|
102
|
-
for (const item of this) {
|
|
103
|
-
if (thisArg === void 0) {
|
|
104
|
-
callbackfn(item, index++, this);
|
|
105
|
-
} else {
|
|
106
|
-
const fn = callbackfn;
|
|
107
|
-
fn.call(thisArg, item, index++, this);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
find(predicate, thisArg) {
|
|
112
|
-
let index = 0;
|
|
113
|
-
for (const item of this) {
|
|
114
|
-
if (thisArg === void 0) {
|
|
115
|
-
if (predicate(item, index++, this)) return item;
|
|
116
|
-
} else {
|
|
117
|
-
const fn = predicate;
|
|
118
|
-
if (fn.call(thisArg, item, index++, this)) return item;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
has(element) {
|
|
124
|
-
for (const ele of this) if (ele === element) return true;
|
|
125
|
-
return false;
|
|
126
|
-
}
|
|
127
|
-
reduce(callbackfn, initialValue) {
|
|
128
|
-
let index = 0;
|
|
129
|
-
const iter = this[Symbol.iterator]();
|
|
130
|
-
let acc;
|
|
131
|
-
if (arguments.length >= 2) {
|
|
132
|
-
acc = initialValue;
|
|
133
|
-
} else {
|
|
134
|
-
const first = iter.next();
|
|
135
|
-
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
136
|
-
acc = first.value;
|
|
137
|
-
index = 1;
|
|
138
|
-
}
|
|
139
|
-
for (const value of iter) {
|
|
140
|
-
acc = callbackfn(acc, value, index++, this);
|
|
141
|
-
}
|
|
142
|
-
return acc;
|
|
143
|
-
}
|
|
144
|
-
toArray() {
|
|
145
|
-
return [...this];
|
|
146
|
-
}
|
|
147
|
-
toVisual() {
|
|
148
|
-
return [...this];
|
|
149
|
-
}
|
|
150
|
-
print() {
|
|
151
|
-
console.log(this.toVisual());
|
|
152
|
-
}
|
|
153
|
-
};
|
|
154
|
-
var LinearBase = class _LinearBase extends IterableElementBase {
|
|
155
|
-
static {
|
|
156
|
-
__name(this, "LinearBase");
|
|
157
|
-
}
|
|
158
|
-
constructor(options) {
|
|
159
|
-
super(options);
|
|
160
|
-
if (options) {
|
|
161
|
-
const { maxLen } = options;
|
|
162
|
-
if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
_maxLen = -1;
|
|
166
|
-
get maxLen() {
|
|
167
|
-
return this._maxLen;
|
|
168
|
-
}
|
|
169
|
-
indexOf(searchElement, fromIndex = 0) {
|
|
170
|
-
if (this.length === 0) return -1;
|
|
171
|
-
if (fromIndex < 0) fromIndex = this.length + fromIndex;
|
|
172
|
-
if (fromIndex < 0) fromIndex = 0;
|
|
173
|
-
for (let i = fromIndex; i < this.length; i++) {
|
|
174
|
-
const element = this.at(i);
|
|
175
|
-
if (element === searchElement) return i;
|
|
176
|
-
}
|
|
177
|
-
return -1;
|
|
178
|
-
}
|
|
179
|
-
lastIndexOf(searchElement, fromIndex = this.length - 1) {
|
|
180
|
-
if (this.length === 0) return -1;
|
|
181
|
-
if (fromIndex >= this.length) fromIndex = this.length - 1;
|
|
182
|
-
if (fromIndex < 0) fromIndex = this.length + fromIndex;
|
|
183
|
-
for (let i = fromIndex; i >= 0; i--) {
|
|
184
|
-
const element = this.at(i);
|
|
185
|
-
if (element === searchElement) return i;
|
|
186
|
-
}
|
|
187
|
-
return -1;
|
|
188
|
-
}
|
|
189
|
-
findIndex(predicate, thisArg) {
|
|
190
|
-
for (let i = 0; i < this.length; i++) {
|
|
191
|
-
const item = this.at(i);
|
|
192
|
-
if (item !== void 0 && predicate.call(thisArg, item, i, this)) return i;
|
|
193
|
-
}
|
|
194
|
-
return -1;
|
|
195
|
-
}
|
|
196
|
-
concat(...items) {
|
|
197
|
-
const newList = this.clone();
|
|
198
|
-
for (const item of items) {
|
|
199
|
-
if (item instanceof _LinearBase) {
|
|
200
|
-
newList.pushMany(item);
|
|
201
|
-
} else {
|
|
202
|
-
newList.push(item);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
return newList;
|
|
206
|
-
}
|
|
207
|
-
sort(compareFn) {
|
|
208
|
-
const arr = this.toArray();
|
|
209
|
-
arr.sort(compareFn);
|
|
210
|
-
this.clear();
|
|
211
|
-
for (const item of arr) this.push(item);
|
|
212
|
-
return this;
|
|
213
|
-
}
|
|
214
|
-
splice(start, deleteCount = 0, ...items) {
|
|
215
|
-
const removedList = this._createInstance();
|
|
216
|
-
start = start < 0 ? this.length + start : start;
|
|
217
|
-
start = Math.max(0, Math.min(start, this.length));
|
|
218
|
-
deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
|
|
219
|
-
for (let i = 0; i < deleteCount; i++) {
|
|
220
|
-
const removed = this.deleteAt(start);
|
|
221
|
-
if (removed !== void 0) {
|
|
222
|
-
removedList.push(removed);
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
for (let i = 0; i < items.length; i++) {
|
|
226
|
-
this.addAt(start + i, items[i]);
|
|
227
|
-
}
|
|
228
|
-
return removedList;
|
|
229
|
-
}
|
|
230
|
-
join(separator = ",") {
|
|
231
|
-
return this.toArray().join(separator);
|
|
232
|
-
}
|
|
233
|
-
toReversedArray() {
|
|
234
|
-
const array = [];
|
|
235
|
-
for (let i = this.length - 1; i >= 0; i--) {
|
|
236
|
-
array.push(this.at(i));
|
|
237
|
-
}
|
|
238
|
-
return array;
|
|
239
|
-
}
|
|
240
|
-
reduceRight(callbackfn, initialValue) {
|
|
241
|
-
let accumulator = initialValue ?? 0;
|
|
242
|
-
for (let i = this.length - 1; i >= 0; i--) {
|
|
243
|
-
accumulator = callbackfn(accumulator, this.at(i), i, this);
|
|
244
|
-
}
|
|
245
|
-
return accumulator;
|
|
246
|
-
}
|
|
247
|
-
slice(start = 0, end = this.length) {
|
|
248
|
-
start = start < 0 ? this.length + start : start;
|
|
249
|
-
end = end < 0 ? this.length + end : end;
|
|
250
|
-
const newList = this._createInstance();
|
|
251
|
-
for (let i = start; i < end; i++) {
|
|
252
|
-
newList.push(this.at(i));
|
|
253
|
-
}
|
|
254
|
-
return newList;
|
|
255
|
-
}
|
|
256
|
-
fill(value, start = 0, end = this.length) {
|
|
257
|
-
start = start < 0 ? this.length + start : start;
|
|
258
|
-
end = end < 0 ? this.length + end : end;
|
|
259
|
-
if (start < 0) start = 0;
|
|
260
|
-
if (end > this.length) end = this.length;
|
|
261
|
-
if (start >= end) return this;
|
|
262
|
-
for (let i = start; i < end; i++) {
|
|
263
|
-
this.setAt(i, value);
|
|
264
|
-
}
|
|
265
|
-
return this;
|
|
266
|
-
}
|
|
267
|
-
};
|
|
268
|
-
var Queue = class _Queue extends LinearBase {
|
|
269
|
-
static {
|
|
270
|
-
__name(this, "Queue");
|
|
271
|
-
}
|
|
272
|
-
constructor(elements = [], options) {
|
|
273
|
-
super(options);
|
|
274
|
-
if (options) {
|
|
275
|
-
const { autoCompactRatio = 0.5 } = options;
|
|
276
|
-
this._autoCompactRatio = autoCompactRatio;
|
|
277
|
-
}
|
|
278
|
-
this.pushMany(elements);
|
|
279
|
-
}
|
|
280
|
-
_elements = [];
|
|
281
|
-
get elements() {
|
|
282
|
-
return this._elements;
|
|
283
|
-
}
|
|
284
|
-
_offset = 0;
|
|
285
|
-
get offset() {
|
|
286
|
-
return this._offset;
|
|
287
|
-
}
|
|
288
|
-
_autoCompactRatio = 0.5;
|
|
289
|
-
get autoCompactRatio() {
|
|
290
|
-
return this._autoCompactRatio;
|
|
291
|
-
}
|
|
292
|
-
set autoCompactRatio(value) {
|
|
293
|
-
this._autoCompactRatio = value;
|
|
294
|
-
}
|
|
295
|
-
get length() {
|
|
296
|
-
return this.elements.length - this._offset;
|
|
297
|
-
}
|
|
298
|
-
get first() {
|
|
299
|
-
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
300
|
-
}
|
|
301
|
-
get last() {
|
|
302
|
-
return this.length > 0 ? this.elements[this.elements.length - 1] : void 0;
|
|
303
|
-
}
|
|
304
|
-
static fromArray(elements) {
|
|
305
|
-
return new _Queue(elements);
|
|
306
|
-
}
|
|
307
|
-
isEmpty() {
|
|
308
|
-
return this.length === 0;
|
|
309
|
-
}
|
|
310
|
-
push(element) {
|
|
311
|
-
this.elements.push(element);
|
|
312
|
-
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
313
|
-
return true;
|
|
314
|
-
}
|
|
315
|
-
pushMany(elements) {
|
|
316
|
-
const ans = [];
|
|
317
|
-
for (const el of elements) {
|
|
318
|
-
if (this.toElementFn) ans.push(this.push(this.toElementFn(el)));
|
|
319
|
-
else ans.push(this.push(el));
|
|
320
|
-
}
|
|
321
|
-
return ans;
|
|
322
|
-
}
|
|
323
|
-
shift() {
|
|
324
|
-
if (this.length === 0) return void 0;
|
|
325
|
-
const first = this.first;
|
|
326
|
-
this._offset += 1;
|
|
327
|
-
if (this.elements.length > 0 && this.offset / this.elements.length > this.autoCompactRatio) this.compact();
|
|
328
|
-
return first;
|
|
329
|
-
}
|
|
330
|
-
delete(element) {
|
|
331
|
-
for (let i = this._offset; i < this.elements.length; i++) {
|
|
332
|
-
if (Object.is(this.elements[i], element)) {
|
|
333
|
-
this.elements.splice(i, 1);
|
|
334
|
-
return true;
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
return false;
|
|
338
|
-
}
|
|
339
|
-
at(index) {
|
|
340
|
-
if (index < 0 || index >= this.length) return void 0;
|
|
341
|
-
return this._elements[this._offset + index];
|
|
342
|
-
}
|
|
343
|
-
deleteAt(index) {
|
|
344
|
-
if (index < 0 || index >= this.length) return void 0;
|
|
345
|
-
const gi = this._offset + index;
|
|
346
|
-
const [deleted] = this.elements.splice(gi, 1);
|
|
347
|
-
return deleted;
|
|
348
|
-
}
|
|
349
|
-
addAt(index, newElement) {
|
|
350
|
-
if (index < 0 || index > this.length) return false;
|
|
351
|
-
this._elements.splice(this._offset + index, 0, newElement);
|
|
352
|
-
return true;
|
|
353
|
-
}
|
|
354
|
-
setAt(index, newElement) {
|
|
355
|
-
if (index < 0 || index >= this.length) return false;
|
|
356
|
-
this._elements[this._offset + index] = newElement;
|
|
357
|
-
return true;
|
|
358
|
-
}
|
|
359
|
-
reverse() {
|
|
360
|
-
this._elements = this.elements.slice(this._offset).reverse();
|
|
361
|
-
this._offset = 0;
|
|
362
|
-
return this;
|
|
363
|
-
}
|
|
364
|
-
clear() {
|
|
365
|
-
this._elements = [];
|
|
366
|
-
this._offset = 0;
|
|
367
|
-
}
|
|
368
|
-
compact() {
|
|
369
|
-
this._elements = this.elements.slice(this._offset);
|
|
370
|
-
this._offset = 0;
|
|
371
|
-
return true;
|
|
372
|
-
}
|
|
373
|
-
splice(start, deleteCount = 0, ...items) {
|
|
374
|
-
start = Math.max(0, Math.min(start, this.length));
|
|
375
|
-
deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
|
|
376
|
-
const gi = this._offset + start;
|
|
377
|
-
const removedArray = this._elements.splice(gi, deleteCount, ...items);
|
|
378
|
-
if (this.elements.length > 0 && this.offset / this.elements.length > this.autoCompactRatio) this.compact();
|
|
379
|
-
const removed = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
380
|
-
removed._setAutoCompactRatio(this._autoCompactRatio);
|
|
381
|
-
removed.pushMany(removedArray);
|
|
382
|
-
return removed;
|
|
383
|
-
}
|
|
384
|
-
clone() {
|
|
385
|
-
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
386
|
-
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
387
|
-
for (let i = this._offset; i < this.elements.length; i++) out.push(this.elements[i]);
|
|
388
|
-
return out;
|
|
389
|
-
}
|
|
390
|
-
filter(predicate, thisArg) {
|
|
391
|
-
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
392
|
-
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
393
|
-
let index = 0;
|
|
394
|
-
for (const v of this) {
|
|
395
|
-
if (predicate.call(thisArg, v, index, this)) out.push(v);
|
|
396
|
-
index++;
|
|
397
|
-
}
|
|
398
|
-
return out;
|
|
399
|
-
}
|
|
400
|
-
map(callback, options, thisArg) {
|
|
401
|
-
const out = new this.constructor([], {
|
|
402
|
-
toElementFn: options?.toElementFn,
|
|
403
|
-
maxLen: options?.maxLen ?? this._maxLen,
|
|
404
|
-
autoCompactRatio: options?.autoCompactRatio ?? this._autoCompactRatio
|
|
405
|
-
});
|
|
406
|
-
let index = 0;
|
|
407
|
-
for (const v of this)
|
|
408
|
-
out.push(thisArg === void 0 ? callback(v, index++, this) : callback.call(thisArg, v, index++, this));
|
|
409
|
-
return out;
|
|
410
|
-
}
|
|
411
|
-
mapSame(callback, thisArg) {
|
|
412
|
-
const Ctor = this.constructor;
|
|
413
|
-
const out = new Ctor([], {
|
|
414
|
-
toElementFn: this.toElementFn,
|
|
415
|
-
maxLen: this._maxLen,
|
|
416
|
-
autoCompactRatio: this._autoCompactRatio
|
|
417
|
-
});
|
|
418
|
-
out._setAutoCompactRatio?.(this._autoCompactRatio);
|
|
419
|
-
let index = 0;
|
|
420
|
-
for (const v of this) {
|
|
421
|
-
const mv = thisArg === void 0 ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
|
|
422
|
-
out.push(mv);
|
|
423
|
-
}
|
|
424
|
-
return out;
|
|
425
|
-
}
|
|
426
|
-
_setAutoCompactRatio(value) {
|
|
427
|
-
this._autoCompactRatio = value;
|
|
428
|
-
}
|
|
429
|
-
*_getIterator() {
|
|
430
|
-
for (let i = this._offset; i < this.elements.length; i++) yield this.elements[i];
|
|
431
|
-
}
|
|
432
|
-
*_getReverseIterator() {
|
|
433
|
-
for (let i = this.length - 1; i >= 0; i--) {
|
|
434
|
-
const cur = this.at(i);
|
|
435
|
-
if (cur !== void 0) yield cur;
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
_createInstance(options) {
|
|
439
|
-
const Ctor = this.constructor;
|
|
440
|
-
return new Ctor([], options);
|
|
441
|
-
}
|
|
442
|
-
_createLike(elements = [], options) {
|
|
443
|
-
const Ctor = this.constructor;
|
|
444
|
-
return new Ctor(elements, options);
|
|
445
|
-
}
|
|
446
|
-
};
|
|
447
|
-
var IterableEntryBase = class {
|
|
448
|
-
static {
|
|
449
|
-
__name(this, "IterableEntryBase");
|
|
450
|
-
}
|
|
451
|
-
*[Symbol.iterator](...args) {
|
|
452
|
-
yield* this._getIterator(...args);
|
|
453
|
-
}
|
|
454
|
-
*entries() {
|
|
455
|
-
for (const item of this) {
|
|
456
|
-
yield item;
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
*keys() {
|
|
460
|
-
for (const item of this) {
|
|
461
|
-
yield item[0];
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
*values() {
|
|
465
|
-
for (const item of this) {
|
|
466
|
-
yield item[1];
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
every(predicate, thisArg) {
|
|
470
|
-
let index = 0;
|
|
471
|
-
for (const item of this) {
|
|
472
|
-
if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
473
|
-
return false;
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
return true;
|
|
477
|
-
}
|
|
478
|
-
some(predicate, thisArg) {
|
|
479
|
-
let index = 0;
|
|
480
|
-
for (const item of this) {
|
|
481
|
-
if (predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
482
|
-
return true;
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
return false;
|
|
486
|
-
}
|
|
487
|
-
forEach(callbackfn, thisArg) {
|
|
488
|
-
let index = 0;
|
|
489
|
-
for (const item of this) {
|
|
490
|
-
const [key, value] = item;
|
|
491
|
-
callbackfn.call(thisArg, value, key, index++, this);
|
|
492
|
-
}
|
|
493
|
-
}
|
|
494
|
-
find(callbackfn, thisArg) {
|
|
495
|
-
let index = 0;
|
|
496
|
-
for (const item of this) {
|
|
497
|
-
const [key, value] = item;
|
|
498
|
-
if (callbackfn.call(thisArg, value, key, index++, this)) return item;
|
|
499
|
-
}
|
|
500
|
-
return;
|
|
501
|
-
}
|
|
502
|
-
has(key) {
|
|
503
|
-
for (const item of this) {
|
|
504
|
-
const [itemKey] = item;
|
|
505
|
-
if (itemKey === key) return true;
|
|
506
|
-
}
|
|
507
|
-
return false;
|
|
508
|
-
}
|
|
509
|
-
hasValue(value) {
|
|
510
|
-
for (const [, elementValue] of this) {
|
|
511
|
-
if (elementValue === value) return true;
|
|
512
|
-
}
|
|
513
|
-
return false;
|
|
514
|
-
}
|
|
515
|
-
get(key) {
|
|
516
|
-
for (const item of this) {
|
|
517
|
-
const [itemKey, value] = item;
|
|
518
|
-
if (itemKey === key) return value;
|
|
519
|
-
}
|
|
520
|
-
return;
|
|
521
|
-
}
|
|
522
|
-
reduce(callbackfn, initialValue) {
|
|
523
|
-
let accumulator = initialValue;
|
|
524
|
-
let index = 0;
|
|
525
|
-
for (const item of this) {
|
|
526
|
-
const [key, value] = item;
|
|
527
|
-
accumulator = callbackfn(accumulator, value, key, index++, this);
|
|
528
|
-
}
|
|
529
|
-
return accumulator;
|
|
530
|
-
}
|
|
531
|
-
toArray() {
|
|
532
|
-
return [...this];
|
|
533
|
-
}
|
|
534
|
-
toVisual() {
|
|
535
|
-
return [...this];
|
|
536
|
-
}
|
|
537
|
-
print() {
|
|
538
|
-
console.log(this.toVisual());
|
|
539
|
-
}
|
|
540
|
-
};
|
|
541
|
-
var Range = class {
|
|
542
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
543
|
-
this.low = low;
|
|
544
|
-
this.high = high;
|
|
545
|
-
this.includeLow = includeLow;
|
|
546
|
-
this.includeHigh = includeHigh;
|
|
547
|
-
}
|
|
548
|
-
static {
|
|
549
|
-
__name(this, "Range");
|
|
550
|
-
}
|
|
551
|
-
isInRange(key, comparator) {
|
|
552
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
553
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
554
|
-
return lowCheck && highCheck;
|
|
555
|
-
}
|
|
556
|
-
};
|
|
557
|
-
var BinaryTreeNode = class {
|
|
558
|
-
static {
|
|
559
|
-
__name(this, "BinaryTreeNode");
|
|
560
|
-
}
|
|
561
|
-
key;
|
|
562
|
-
value;
|
|
563
|
-
parent = void 0;
|
|
564
|
-
constructor(key, value) {
|
|
565
|
-
this.key = key;
|
|
566
|
-
this.value = value;
|
|
567
|
-
}
|
|
568
|
-
_left = void 0;
|
|
569
|
-
get left() {
|
|
570
|
-
return this._left;
|
|
571
|
-
}
|
|
572
|
-
set left(v) {
|
|
573
|
-
if (v) {
|
|
574
|
-
v.parent = this;
|
|
575
|
-
}
|
|
576
|
-
this._left = v;
|
|
577
|
-
}
|
|
578
|
-
_right = void 0;
|
|
579
|
-
get right() {
|
|
580
|
-
return this._right;
|
|
581
|
-
}
|
|
582
|
-
set right(v) {
|
|
583
|
-
if (v) {
|
|
584
|
-
v.parent = this;
|
|
585
|
-
}
|
|
586
|
-
this._right = v;
|
|
587
|
-
}
|
|
588
|
-
_height = 0;
|
|
589
|
-
get height() {
|
|
590
|
-
return this._height;
|
|
591
|
-
}
|
|
592
|
-
set height(value) {
|
|
593
|
-
this._height = value;
|
|
594
|
-
}
|
|
595
|
-
_color = "BLACK";
|
|
596
|
-
get color() {
|
|
597
|
-
return this._color;
|
|
598
|
-
}
|
|
599
|
-
set color(value) {
|
|
600
|
-
this._color = value;
|
|
601
|
-
}
|
|
602
|
-
_count = 1;
|
|
603
|
-
get count() {
|
|
604
|
-
return this._count;
|
|
605
|
-
}
|
|
606
|
-
set count(value) {
|
|
607
|
-
this._count = value;
|
|
608
|
-
}
|
|
609
|
-
get familyPosition() {
|
|
610
|
-
if (!this.parent) {
|
|
611
|
-
return this.left || this.right ? "ROOT" : "ISOLATED";
|
|
612
|
-
}
|
|
613
|
-
if (this.parent.left === this) {
|
|
614
|
-
return this.left || this.right ? "ROOT_LEFT" : "LEFT";
|
|
615
|
-
} else if (this.parent.right === this) {
|
|
616
|
-
return this.left || this.right ? "ROOT_RIGHT" : "RIGHT";
|
|
617
|
-
}
|
|
618
|
-
return "MAL_NODE";
|
|
619
|
-
}
|
|
620
|
-
};
|
|
621
|
-
var BinaryTree = class extends IterableEntryBase {
|
|
622
|
-
static {
|
|
623
|
-
__name(this, "BinaryTree");
|
|
624
|
-
}
|
|
625
|
-
iterationType = "ITERATIVE";
|
|
626
|
-
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
627
|
-
super();
|
|
628
|
-
if (options) {
|
|
629
|
-
const { iterationType, toEntryFn, isMapMode, isDuplicate } = options;
|
|
630
|
-
if (iterationType) this.iterationType = iterationType;
|
|
631
|
-
if (isMapMode !== void 0) this._isMapMode = isMapMode;
|
|
632
|
-
if (isDuplicate !== void 0) this._isDuplicate = isDuplicate;
|
|
633
|
-
if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
|
|
634
|
-
else if (toEntryFn) throw TypeError("toEntryFn must be a function type");
|
|
635
|
-
}
|
|
636
|
-
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
637
|
-
}
|
|
638
|
-
_isMapMode = true;
|
|
639
|
-
get isMapMode() {
|
|
640
|
-
return this._isMapMode;
|
|
641
|
-
}
|
|
642
|
-
_isDuplicate = false;
|
|
643
|
-
get isDuplicate() {
|
|
644
|
-
return this._isDuplicate;
|
|
645
|
-
}
|
|
646
|
-
_store = new Map();
|
|
647
|
-
get store() {
|
|
648
|
-
return this._store;
|
|
649
|
-
}
|
|
650
|
-
_root;
|
|
651
|
-
get root() {
|
|
652
|
-
return this._root;
|
|
653
|
-
}
|
|
654
|
-
_size = 0;
|
|
655
|
-
get size() {
|
|
656
|
-
return this._size;
|
|
657
|
-
}
|
|
658
|
-
_NIL = new BinaryTreeNode(NaN);
|
|
659
|
-
get NIL() {
|
|
660
|
-
return this._NIL;
|
|
661
|
-
}
|
|
662
|
-
_toEntryFn;
|
|
663
|
-
get toEntryFn() {
|
|
664
|
-
return this._toEntryFn;
|
|
665
|
-
}
|
|
666
|
-
createNode(key, value) {
|
|
667
|
-
return new BinaryTreeNode(key, this._isMapMode ? void 0 : value);
|
|
668
|
-
}
|
|
669
|
-
createTree(options) {
|
|
670
|
-
return this._createInstance(options);
|
|
671
|
-
}
|
|
672
|
-
ensureNode(keyNodeOrEntry, iterationType = this.iterationType) {
|
|
673
|
-
if (keyNodeOrEntry === null) return null;
|
|
674
|
-
if (keyNodeOrEntry === void 0) return;
|
|
675
|
-
if (keyNodeOrEntry === this._NIL) return;
|
|
676
|
-
if (this.isNode(keyNodeOrEntry)) return keyNodeOrEntry;
|
|
677
|
-
if (this.isEntry(keyNodeOrEntry)) {
|
|
678
|
-
const key = keyNodeOrEntry[0];
|
|
679
|
-
if (key === null) return null;
|
|
680
|
-
if (key === void 0) return;
|
|
681
|
-
return this.getNode(key, this._root, iterationType);
|
|
682
|
-
}
|
|
683
|
-
return this.getNode(keyNodeOrEntry, this._root, iterationType);
|
|
684
|
-
}
|
|
685
|
-
isNode(keyNodeOrEntry) {
|
|
686
|
-
return keyNodeOrEntry instanceof BinaryTreeNode;
|
|
687
|
-
}
|
|
688
|
-
isRaw(keyNodeEntryOrRaw) {
|
|
689
|
-
return this._toEntryFn !== void 0 && typeof keyNodeEntryOrRaw === "object";
|
|
690
|
-
}
|
|
691
|
-
isRealNode(keyNodeOrEntry) {
|
|
692
|
-
if (keyNodeOrEntry === this._NIL || keyNodeOrEntry === null || keyNodeOrEntry === void 0) return false;
|
|
693
|
-
return this.isNode(keyNodeOrEntry);
|
|
694
|
-
}
|
|
695
|
-
isRealNodeOrNull(keyNodeOrEntry) {
|
|
696
|
-
return keyNodeOrEntry === null || this.isRealNode(keyNodeOrEntry);
|
|
697
|
-
}
|
|
698
|
-
isNIL(keyNodeOrEntry) {
|
|
699
|
-
return keyNodeOrEntry === this._NIL;
|
|
700
|
-
}
|
|
701
|
-
isRange(keyNodeEntryOrPredicate) {
|
|
702
|
-
return keyNodeEntryOrPredicate instanceof Range;
|
|
703
|
-
}
|
|
704
|
-
isLeaf(keyNodeOrEntry) {
|
|
705
|
-
keyNodeOrEntry = this.ensureNode(keyNodeOrEntry);
|
|
706
|
-
if (keyNodeOrEntry === void 0) return false;
|
|
707
|
-
if (keyNodeOrEntry === null) return true;
|
|
708
|
-
return !this.isRealNode(keyNodeOrEntry.left) && !this.isRealNode(keyNodeOrEntry.right);
|
|
709
|
-
}
|
|
710
|
-
isEntry(keyNodeOrEntry) {
|
|
711
|
-
return Array.isArray(keyNodeOrEntry) && keyNodeOrEntry.length === 2;
|
|
712
|
-
}
|
|
713
|
-
isValidKey(key) {
|
|
714
|
-
if (key === null) return true;
|
|
715
|
-
return isComparable(key);
|
|
716
|
-
}
|
|
717
|
-
add(keyNodeOrEntry) {
|
|
718
|
-
return this.set(keyNodeOrEntry);
|
|
719
|
-
}
|
|
720
|
-
set(keyNodeOrEntry, value) {
|
|
721
|
-
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
722
|
-
if (newNode === void 0) return false;
|
|
723
|
-
if (!this._root) {
|
|
724
|
-
this._setRoot(newNode);
|
|
725
|
-
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
726
|
-
this._size = 1;
|
|
727
|
-
return true;
|
|
728
|
-
}
|
|
729
|
-
const queue = new Queue([this._root]);
|
|
730
|
-
let potentialParent;
|
|
731
|
-
while (queue.length > 0) {
|
|
732
|
-
const cur = queue.shift();
|
|
733
|
-
if (!cur) continue;
|
|
734
|
-
if (!this._isDuplicate) {
|
|
735
|
-
if (newNode !== null && cur.key === newNode.key) {
|
|
736
|
-
this._replaceNode(cur, newNode);
|
|
737
|
-
if (this._isMapMode) this._setValue(cur.key, newValue);
|
|
738
|
-
return true;
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
if (potentialParent === void 0 && (cur.left === void 0 || cur.right === void 0)) {
|
|
742
|
-
potentialParent = cur;
|
|
743
|
-
}
|
|
744
|
-
if (cur.left !== null) {
|
|
745
|
-
if (cur.left) queue.push(cur.left);
|
|
746
|
-
}
|
|
747
|
-
if (cur.right !== null) {
|
|
748
|
-
if (cur.right) queue.push(cur.right);
|
|
749
|
-
}
|
|
750
|
-
}
|
|
751
|
-
if (potentialParent) {
|
|
752
|
-
if (potentialParent.left === void 0) {
|
|
753
|
-
potentialParent.left = newNode;
|
|
754
|
-
} else if (potentialParent.right === void 0) {
|
|
755
|
-
potentialParent.right = newNode;
|
|
756
|
-
}
|
|
757
|
-
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
758
|
-
this._size++;
|
|
759
|
-
return true;
|
|
760
|
-
}
|
|
761
|
-
return false;
|
|
762
|
-
}
|
|
763
|
-
addMany(keysNodesEntriesOrRaws) {
|
|
764
|
-
return this.setMany(keysNodesEntriesOrRaws);
|
|
765
|
-
}
|
|
766
|
-
setMany(keysNodesEntriesOrRaws, values) {
|
|
767
|
-
const inserted = [];
|
|
768
|
-
let valuesIterator;
|
|
769
|
-
if (values) {
|
|
770
|
-
valuesIterator = values[Symbol.iterator]();
|
|
771
|
-
}
|
|
772
|
-
for (let keyNodeEntryOrRaw of keysNodesEntriesOrRaws) {
|
|
773
|
-
let value = void 0;
|
|
774
|
-
if (valuesIterator) {
|
|
775
|
-
const valueResult = valuesIterator.next();
|
|
776
|
-
if (!valueResult.done) {
|
|
777
|
-
value = valueResult.value;
|
|
778
|
-
}
|
|
779
|
-
}
|
|
780
|
-
if (this.isRaw(keyNodeEntryOrRaw)) keyNodeEntryOrRaw = this._toEntryFn(keyNodeEntryOrRaw);
|
|
781
|
-
inserted.push(this.set(keyNodeEntryOrRaw, value));
|
|
782
|
-
}
|
|
783
|
-
return inserted;
|
|
784
|
-
}
|
|
785
|
-
merge(anotherTree) {
|
|
786
|
-
this.setMany(anotherTree, []);
|
|
787
|
-
}
|
|
788
|
-
refill(keysNodesEntriesOrRaws, values) {
|
|
789
|
-
this.clear();
|
|
790
|
-
this.setMany(keysNodesEntriesOrRaws, values);
|
|
791
|
-
}
|
|
792
|
-
delete(keyNodeOrEntry) {
|
|
793
|
-
const deletedResult = [];
|
|
794
|
-
if (!this._root) return deletedResult;
|
|
795
|
-
const curr = this.getNode(keyNodeOrEntry);
|
|
796
|
-
if (!curr) return deletedResult;
|
|
797
|
-
const parent = curr?.parent;
|
|
798
|
-
let needBalanced;
|
|
799
|
-
let orgCurrent = curr;
|
|
800
|
-
if (!curr.left && !curr.right && !parent) {
|
|
801
|
-
this._setRoot(void 0);
|
|
802
|
-
} else if (curr.left) {
|
|
803
|
-
const leftSubTreeRightMost = this.getRightMost((node) => node, curr.left);
|
|
804
|
-
if (leftSubTreeRightMost) {
|
|
805
|
-
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
806
|
-
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
807
|
-
if (parentOfLeftSubTreeMax) {
|
|
808
|
-
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
809
|
-
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
810
|
-
else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
|
|
811
|
-
needBalanced = parentOfLeftSubTreeMax;
|
|
812
|
-
}
|
|
813
|
-
}
|
|
814
|
-
} else if (parent) {
|
|
815
|
-
const { familyPosition: fp } = curr;
|
|
816
|
-
if (fp === "LEFT" || fp === "ROOT_LEFT") {
|
|
817
|
-
parent.left = curr.right;
|
|
818
|
-
} else if (fp === "RIGHT" || fp === "ROOT_RIGHT") {
|
|
819
|
-
parent.right = curr.right;
|
|
820
|
-
}
|
|
821
|
-
needBalanced = parent;
|
|
822
|
-
} else {
|
|
823
|
-
this._setRoot(curr.right);
|
|
824
|
-
curr.right = void 0;
|
|
825
|
-
}
|
|
826
|
-
this._size = this._size - 1;
|
|
827
|
-
deletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
828
|
-
if (this._isMapMode && orgCurrent) this._store.delete(orgCurrent.key);
|
|
829
|
-
return deletedResult;
|
|
830
|
-
}
|
|
831
|
-
search(keyNodeEntryOrPredicate, onlyOne = false, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
832
|
-
if (keyNodeEntryOrPredicate === void 0) return [];
|
|
833
|
-
if (keyNodeEntryOrPredicate === null) return [];
|
|
834
|
-
startNode = this.ensureNode(startNode);
|
|
835
|
-
if (!startNode) return [];
|
|
836
|
-
const predicate = this._ensurePredicate(keyNodeEntryOrPredicate);
|
|
837
|
-
const ans = [];
|
|
838
|
-
if (iterationType === "RECURSIVE") {
|
|
839
|
-
const dfs = __name((cur) => {
|
|
840
|
-
if (predicate(cur)) {
|
|
841
|
-
ans.push(callback(cur));
|
|
842
|
-
if (onlyOne) return;
|
|
843
|
-
}
|
|
844
|
-
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
|
|
845
|
-
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
846
|
-
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
847
|
-
}, "dfs");
|
|
848
|
-
dfs(startNode);
|
|
849
|
-
} else {
|
|
850
|
-
const stack = [startNode];
|
|
851
|
-
while (stack.length > 0) {
|
|
852
|
-
const cur = stack.pop();
|
|
853
|
-
if (this.isRealNode(cur)) {
|
|
854
|
-
if (predicate(cur)) {
|
|
855
|
-
ans.push(callback(cur));
|
|
856
|
-
if (onlyOne) return ans;
|
|
857
|
-
}
|
|
858
|
-
if (this.isRealNode(cur.left)) stack.push(cur.left);
|
|
859
|
-
if (this.isRealNode(cur.right)) stack.push(cur.right);
|
|
860
|
-
}
|
|
861
|
-
}
|
|
862
|
-
}
|
|
863
|
-
return ans;
|
|
864
|
-
}
|
|
865
|
-
getNodes(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
866
|
-
return this.search(keyNodeEntryOrPredicate, onlyOne, (node) => node, startNode, iterationType);
|
|
867
|
-
}
|
|
868
|
-
getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
869
|
-
return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType)[0];
|
|
870
|
-
}
|
|
871
|
-
get(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
872
|
-
if (this._isMapMode) {
|
|
873
|
-
const key = this._extractKey(keyNodeEntryOrPredicate);
|
|
874
|
-
if (key === null || key === void 0) return;
|
|
875
|
-
return this._store.get(key);
|
|
876
|
-
}
|
|
877
|
-
return this.getNode(keyNodeEntryOrPredicate, startNode, iterationType)?.value;
|
|
878
|
-
}
|
|
879
|
-
has(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
880
|
-
return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType).length > 0;
|
|
881
|
-
}
|
|
882
|
-
clear() {
|
|
883
|
-
this._clearNodes();
|
|
884
|
-
if (this._isMapMode) this._clearValues();
|
|
885
|
-
}
|
|
886
|
-
isEmpty() {
|
|
887
|
-
return this._size === 0;
|
|
888
|
-
}
|
|
889
|
-
isPerfectlyBalanced(startNode = this._root) {
|
|
890
|
-
return this.getMinHeight(startNode) + 1 >= this.getHeight(startNode);
|
|
891
|
-
}
|
|
892
|
-
isBST(startNode = this._root, iterationType = this.iterationType) {
|
|
893
|
-
const startNodeSired = this.ensureNode(startNode);
|
|
894
|
-
if (!startNodeSired) return true;
|
|
895
|
-
if (iterationType === "RECURSIVE") {
|
|
896
|
-
const dfs = __name((cur, min, max) => {
|
|
897
|
-
if (!this.isRealNode(cur)) return true;
|
|
898
|
-
const numKey = Number(cur.key);
|
|
899
|
-
if (numKey <= min || numKey >= max) return false;
|
|
900
|
-
return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
|
|
901
|
-
}, "dfs");
|
|
902
|
-
const isStandardBST = dfs(startNodeSired, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
903
|
-
const isInverseBST = dfs(startNodeSired, Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);
|
|
904
|
-
return isStandardBST || isInverseBST;
|
|
905
|
-
} else {
|
|
906
|
-
const checkBST = __name((checkMax = false) => {
|
|
907
|
-
const stack = [];
|
|
908
|
-
let prev = checkMax ? Number.MAX_SAFE_INTEGER : Number.MIN_SAFE_INTEGER;
|
|
909
|
-
let curr = startNodeSired;
|
|
910
|
-
while (this.isRealNode(curr) || stack.length > 0) {
|
|
911
|
-
while (this.isRealNode(curr)) {
|
|
912
|
-
stack.push(curr);
|
|
913
|
-
curr = curr.left;
|
|
914
|
-
}
|
|
915
|
-
curr = stack.pop();
|
|
916
|
-
const numKey = Number(curr.key);
|
|
917
|
-
if (!this.isRealNode(curr) || !checkMax && prev >= numKey || checkMax && prev <= numKey) return false;
|
|
918
|
-
prev = numKey;
|
|
919
|
-
curr = curr.right;
|
|
920
|
-
}
|
|
921
|
-
return true;
|
|
922
|
-
}, "checkBST");
|
|
923
|
-
const isStandardBST = checkBST(false);
|
|
924
|
-
const isInverseBST = checkBST(true);
|
|
925
|
-
return isStandardBST || isInverseBST;
|
|
926
|
-
}
|
|
927
|
-
}
|
|
928
|
-
getDepth(dist, startNode = this._root) {
|
|
929
|
-
let distEnsured = this.ensureNode(dist);
|
|
930
|
-
const beginRootEnsured = this.ensureNode(startNode);
|
|
931
|
-
let depth = 0;
|
|
932
|
-
while (distEnsured?.parent) {
|
|
933
|
-
if (distEnsured === beginRootEnsured) {
|
|
934
|
-
return depth;
|
|
935
|
-
}
|
|
936
|
-
depth++;
|
|
937
|
-
distEnsured = distEnsured.parent;
|
|
938
|
-
}
|
|
939
|
-
return depth;
|
|
940
|
-
}
|
|
941
|
-
getHeight(startNode = this._root, iterationType = this.iterationType) {
|
|
942
|
-
startNode = this.ensureNode(startNode);
|
|
943
|
-
if (!this.isRealNode(startNode)) return -1;
|
|
944
|
-
if (iterationType === "RECURSIVE") {
|
|
945
|
-
const _getMaxHeight = __name((cur) => {
|
|
946
|
-
if (!this.isRealNode(cur)) return -1;
|
|
947
|
-
const leftHeight = _getMaxHeight(cur.left);
|
|
948
|
-
const rightHeight = _getMaxHeight(cur.right);
|
|
949
|
-
return Math.max(leftHeight, rightHeight) + 1;
|
|
950
|
-
}, "_getMaxHeight");
|
|
951
|
-
return _getMaxHeight(startNode);
|
|
952
|
-
} else {
|
|
953
|
-
const stack = [{ node: startNode, depth: 0 }];
|
|
954
|
-
let maxHeight = 0;
|
|
955
|
-
while (stack.length > 0) {
|
|
956
|
-
const { node, depth } = stack.pop();
|
|
957
|
-
if (this.isRealNode(node.left)) stack.push({ node: node.left, depth: depth + 1 });
|
|
958
|
-
if (this.isRealNode(node.right)) stack.push({ node: node.right, depth: depth + 1 });
|
|
959
|
-
maxHeight = Math.max(maxHeight, depth);
|
|
960
|
-
}
|
|
961
|
-
return maxHeight;
|
|
962
|
-
}
|
|
963
|
-
}
|
|
964
|
-
getMinHeight(startNode = this._root, iterationType = this.iterationType) {
|
|
965
|
-
startNode = this.ensureNode(startNode);
|
|
966
|
-
if (!startNode) return -1;
|
|
967
|
-
if (iterationType === "RECURSIVE") {
|
|
968
|
-
const _getMinHeight = __name((cur) => {
|
|
969
|
-
if (!this.isRealNode(cur)) return 0;
|
|
970
|
-
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return 0;
|
|
971
|
-
const leftMinHeight = _getMinHeight(cur.left);
|
|
972
|
-
const rightMinHeight = _getMinHeight(cur.right);
|
|
973
|
-
return Math.min(leftMinHeight, rightMinHeight) + 1;
|
|
974
|
-
}, "_getMinHeight");
|
|
975
|
-
return _getMinHeight(startNode);
|
|
976
|
-
} else {
|
|
977
|
-
const stack = [];
|
|
978
|
-
let node = startNode, last = null;
|
|
979
|
-
const depths = new Map();
|
|
980
|
-
while (stack.length > 0 || node) {
|
|
981
|
-
if (this.isRealNode(node)) {
|
|
982
|
-
stack.push(node);
|
|
983
|
-
node = node.left;
|
|
984
|
-
} else {
|
|
985
|
-
node = stack[stack.length - 1];
|
|
986
|
-
if (!this.isRealNode(node.right) || last === node.right) {
|
|
987
|
-
node = stack.pop();
|
|
988
|
-
if (this.isRealNode(node)) {
|
|
989
|
-
const leftMinHeight = this.isRealNode(node.left) ? depths.get(node.left) : -1;
|
|
990
|
-
const rightMinHeight = this.isRealNode(node.right) ? depths.get(node.right) : -1;
|
|
991
|
-
depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
|
|
992
|
-
last = node;
|
|
993
|
-
node = null;
|
|
994
|
-
}
|
|
995
|
-
} else node = node.right;
|
|
996
|
-
}
|
|
997
|
-
}
|
|
998
|
-
return depths.get(startNode);
|
|
999
|
-
}
|
|
1000
|
-
}
|
|
1001
|
-
getPathToRoot(beginNode, callback = this._DEFAULT_NODE_CALLBACK, isReverse = false) {
|
|
1002
|
-
const result = [];
|
|
1003
|
-
let beginNodeEnsured = this.ensureNode(beginNode);
|
|
1004
|
-
if (!beginNodeEnsured) return result;
|
|
1005
|
-
while (beginNodeEnsured.parent) {
|
|
1006
|
-
result.push(callback(beginNodeEnsured));
|
|
1007
|
-
beginNodeEnsured = beginNodeEnsured.parent;
|
|
1008
|
-
}
|
|
1009
|
-
result.push(callback(beginNodeEnsured));
|
|
1010
|
-
return isReverse ? result.reverse() : result;
|
|
1011
|
-
}
|
|
1012
|
-
getLeftMost(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
1013
|
-
if (this.isNIL(startNode)) return callback(void 0);
|
|
1014
|
-
const ensuredStartNode = this.ensureNode(startNode);
|
|
1015
|
-
if (!this.isRealNode(ensuredStartNode)) return callback(void 0);
|
|
1016
|
-
if (iterationType === "RECURSIVE") {
|
|
1017
|
-
const dfs = __name((cur) => {
|
|
1018
|
-
const { left } = cur;
|
|
1019
|
-
if (!this.isRealNode(left)) return cur;
|
|
1020
|
-
return dfs(left);
|
|
1021
|
-
}, "dfs");
|
|
1022
|
-
return callback(dfs(ensuredStartNode));
|
|
1023
|
-
} else {
|
|
1024
|
-
const dfs = makeTrampoline((cur) => {
|
|
1025
|
-
const { left } = cur;
|
|
1026
|
-
if (!this.isRealNode(left)) return cur;
|
|
1027
|
-
return makeTrampolineThunk(() => dfs(left));
|
|
1028
|
-
});
|
|
1029
|
-
return callback(dfs(ensuredStartNode));
|
|
1030
|
-
}
|
|
1031
|
-
}
|
|
1032
|
-
getRightMost(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
1033
|
-
if (this.isNIL(startNode)) return callback(void 0);
|
|
1034
|
-
startNode = this.ensureNode(startNode);
|
|
1035
|
-
if (!startNode) return callback(void 0);
|
|
1036
|
-
if (iterationType === "RECURSIVE") {
|
|
1037
|
-
const dfs = __name((cur) => {
|
|
1038
|
-
const { right } = cur;
|
|
1039
|
-
if (!this.isRealNode(right)) return cur;
|
|
1040
|
-
return dfs(right);
|
|
1041
|
-
}, "dfs");
|
|
1042
|
-
return callback(dfs(startNode));
|
|
1043
|
-
} else {
|
|
1044
|
-
const dfs = makeTrampoline((cur) => {
|
|
1045
|
-
const { right } = cur;
|
|
1046
|
-
if (!this.isRealNode(right)) return cur;
|
|
1047
|
-
return makeTrampolineThunk(() => dfs(right));
|
|
1048
|
-
});
|
|
1049
|
-
return callback(dfs(startNode));
|
|
1050
|
-
}
|
|
1051
|
-
}
|
|
1052
|
-
getPredecessor(node) {
|
|
1053
|
-
if (this.isRealNode(node.left)) {
|
|
1054
|
-
let predecessor = node.left;
|
|
1055
|
-
while (!this.isRealNode(predecessor) || this.isRealNode(predecessor.right) && predecessor.right !== node) {
|
|
1056
|
-
if (this.isRealNode(predecessor)) {
|
|
1057
|
-
predecessor = predecessor.right;
|
|
1058
|
-
}
|
|
1059
|
-
}
|
|
1060
|
-
return predecessor;
|
|
1061
|
-
} else {
|
|
1062
|
-
return node;
|
|
1063
|
-
}
|
|
1064
|
-
}
|
|
1065
|
-
getSuccessor(x) {
|
|
1066
|
-
x = this.ensureNode(x);
|
|
1067
|
-
if (!this.isRealNode(x)) return void 0;
|
|
1068
|
-
if (this.isRealNode(x.right)) {
|
|
1069
|
-
return this.getLeftMost((node) => node, x.right);
|
|
1070
|
-
}
|
|
1071
|
-
let y = x.parent;
|
|
1072
|
-
while (this.isRealNode(y) && x === y.right) {
|
|
1073
|
-
x = y;
|
|
1074
|
-
y = y.parent;
|
|
1075
|
-
}
|
|
1076
|
-
return y;
|
|
1077
|
-
}
|
|
1078
|
-
dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = "IN", onlyOne = false, startNode = this._root, iterationType = this.iterationType, includeNull = false) {
|
|
1079
|
-
startNode = this.ensureNode(startNode);
|
|
1080
|
-
if (!startNode) return [];
|
|
1081
|
-
return this._dfs(callback, pattern, onlyOne, startNode, iterationType, includeNull);
|
|
1082
|
-
}
|
|
1083
|
-
bfs(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType, includeNull = false) {
|
|
1084
|
-
startNode = this.ensureNode(startNode);
|
|
1085
|
-
if (!startNode) return [];
|
|
1086
|
-
const ans = [];
|
|
1087
|
-
if (iterationType === "RECURSIVE") {
|
|
1088
|
-
const queue = new Queue([
|
|
1089
|
-
startNode
|
|
1090
|
-
]);
|
|
1091
|
-
const dfs = __name((level) => {
|
|
1092
|
-
if (queue.length === 0) return;
|
|
1093
|
-
const current = queue.shift();
|
|
1094
|
-
ans.push(callback(current));
|
|
1095
|
-
if (includeNull) {
|
|
1096
|
-
if (current && this.isRealNodeOrNull(current.left)) queue.push(current.left);
|
|
1097
|
-
if (current && this.isRealNodeOrNull(current.right)) queue.push(current.right);
|
|
1098
|
-
} else {
|
|
1099
|
-
if (this.isRealNode(current.left)) queue.push(current.left);
|
|
1100
|
-
if (this.isRealNode(current.right)) queue.push(current.right);
|
|
1101
|
-
}
|
|
1102
|
-
dfs(level + 1);
|
|
1103
|
-
}, "dfs");
|
|
1104
|
-
dfs(0);
|
|
1105
|
-
} else {
|
|
1106
|
-
const queue = new Queue([startNode]);
|
|
1107
|
-
while (queue.length > 0) {
|
|
1108
|
-
const levelSize = queue.length;
|
|
1109
|
-
for (let i = 0; i < levelSize; i++) {
|
|
1110
|
-
const current = queue.shift();
|
|
1111
|
-
ans.push(callback(current));
|
|
1112
|
-
if (includeNull) {
|
|
1113
|
-
if (current && this.isRealNodeOrNull(current.left)) queue.push(current.left);
|
|
1114
|
-
if (current && this.isRealNodeOrNull(current.right)) queue.push(current.right);
|
|
1115
|
-
} else {
|
|
1116
|
-
if (this.isRealNode(current.left)) queue.push(current.left);
|
|
1117
|
-
if (this.isRealNode(current.right)) queue.push(current.right);
|
|
1118
|
-
}
|
|
1119
|
-
}
|
|
1120
|
-
}
|
|
1121
|
-
}
|
|
1122
|
-
return ans;
|
|
1123
|
-
}
|
|
1124
|
-
leaves(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
1125
|
-
startNode = this.ensureNode(startNode);
|
|
1126
|
-
const leaves = [];
|
|
1127
|
-
if (!this.isRealNode(startNode)) return [];
|
|
1128
|
-
if (iterationType === "RECURSIVE") {
|
|
1129
|
-
const dfs = __name((cur) => {
|
|
1130
|
-
if (this.isLeaf(cur)) {
|
|
1131
|
-
leaves.push(callback(cur));
|
|
1132
|
-
}
|
|
1133
|
-
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
|
|
1134
|
-
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
1135
|
-
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
1136
|
-
}, "dfs");
|
|
1137
|
-
dfs(startNode);
|
|
1138
|
-
} else {
|
|
1139
|
-
const queue = new Queue([startNode]);
|
|
1140
|
-
while (queue.length > 0) {
|
|
1141
|
-
const cur = queue.shift();
|
|
1142
|
-
if (this.isRealNode(cur)) {
|
|
1143
|
-
if (this.isLeaf(cur)) {
|
|
1144
|
-
leaves.push(callback(cur));
|
|
1145
|
-
}
|
|
1146
|
-
if (this.isRealNode(cur.left)) queue.push(cur.left);
|
|
1147
|
-
if (this.isRealNode(cur.right)) queue.push(cur.right);
|
|
1148
|
-
}
|
|
1149
|
-
}
|
|
1150
|
-
}
|
|
1151
|
-
return leaves;
|
|
1152
|
-
}
|
|
1153
|
-
listLevels(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType, includeNull = false) {
|
|
1154
|
-
startNode = this.ensureNode(startNode);
|
|
1155
|
-
const levelsNodes = [];
|
|
1156
|
-
if (!startNode) return levelsNodes;
|
|
1157
|
-
if (iterationType === "RECURSIVE") {
|
|
1158
|
-
const _recursive = __name((node, level) => {
|
|
1159
|
-
if (!levelsNodes[level]) levelsNodes[level] = [];
|
|
1160
|
-
levelsNodes[level].push(callback(node));
|
|
1161
|
-
if (includeNull) {
|
|
1162
|
-
if (node && this.isRealNodeOrNull(node.left)) _recursive(node.left, level + 1);
|
|
1163
|
-
if (node && this.isRealNodeOrNull(node.right)) _recursive(node.right, level + 1);
|
|
1164
|
-
} else {
|
|
1165
|
-
if (node && node.left) _recursive(node.left, level + 1);
|
|
1166
|
-
if (node && node.right) _recursive(node.right, level + 1);
|
|
1167
|
-
}
|
|
1168
|
-
}, "_recursive");
|
|
1169
|
-
_recursive(startNode, 0);
|
|
1170
|
-
} else {
|
|
1171
|
-
const stack = [[startNode, 0]];
|
|
1172
|
-
while (stack.length > 0) {
|
|
1173
|
-
const head = stack.pop();
|
|
1174
|
-
const [node, level] = head;
|
|
1175
|
-
if (!levelsNodes[level]) levelsNodes[level] = [];
|
|
1176
|
-
levelsNodes[level].push(callback(node));
|
|
1177
|
-
if (includeNull) {
|
|
1178
|
-
if (node && this.isRealNodeOrNull(node.right)) stack.push([node.right, level + 1]);
|
|
1179
|
-
if (node && this.isRealNodeOrNull(node.left)) stack.push([node.left, level + 1]);
|
|
1180
|
-
} else {
|
|
1181
|
-
if (node && node.right) stack.push([node.right, level + 1]);
|
|
1182
|
-
if (node && node.left) stack.push([node.left, level + 1]);
|
|
1183
|
-
}
|
|
1184
|
-
}
|
|
1185
|
-
}
|
|
1186
|
-
return levelsNodes;
|
|
1187
|
-
}
|
|
1188
|
-
morris(callback = this._DEFAULT_NODE_CALLBACK, pattern = "IN", startNode = this._root) {
|
|
1189
|
-
startNode = this.ensureNode(startNode);
|
|
1190
|
-
if (!startNode) return [];
|
|
1191
|
-
const ans = [];
|
|
1192
|
-
let cur = startNode;
|
|
1193
|
-
const _reverseEdge = __name((node) => {
|
|
1194
|
-
let pre = null;
|
|
1195
|
-
let next = null;
|
|
1196
|
-
while (node) {
|
|
1197
|
-
next = node.right;
|
|
1198
|
-
node.right = pre;
|
|
1199
|
-
pre = node;
|
|
1200
|
-
node = next;
|
|
1201
|
-
}
|
|
1202
|
-
return pre;
|
|
1203
|
-
}, "_reverseEdge");
|
|
1204
|
-
const _printEdge = __name((node) => {
|
|
1205
|
-
const tail = _reverseEdge(node);
|
|
1206
|
-
let cur2 = tail;
|
|
1207
|
-
while (cur2) {
|
|
1208
|
-
ans.push(callback(cur2));
|
|
1209
|
-
cur2 = cur2.right;
|
|
1210
|
-
}
|
|
1211
|
-
_reverseEdge(tail);
|
|
1212
|
-
}, "_printEdge");
|
|
1213
|
-
switch (pattern) {
|
|
1214
|
-
case "IN":
|
|
1215
|
-
while (cur) {
|
|
1216
|
-
if (cur.left) {
|
|
1217
|
-
const predecessor = this.getPredecessor(cur);
|
|
1218
|
-
if (!predecessor.right) {
|
|
1219
|
-
predecessor.right = cur;
|
|
1220
|
-
cur = cur.left;
|
|
1221
|
-
continue;
|
|
1222
|
-
} else {
|
|
1223
|
-
predecessor.right = null;
|
|
1224
|
-
}
|
|
1225
|
-
}
|
|
1226
|
-
ans.push(callback(cur));
|
|
1227
|
-
cur = cur.right;
|
|
1228
|
-
}
|
|
1229
|
-
break;
|
|
1230
|
-
case "PRE":
|
|
1231
|
-
while (cur) {
|
|
1232
|
-
if (cur.left) {
|
|
1233
|
-
const predecessor = this.getPredecessor(cur);
|
|
1234
|
-
if (!predecessor.right) {
|
|
1235
|
-
predecessor.right = cur;
|
|
1236
|
-
ans.push(callback(cur));
|
|
1237
|
-
cur = cur.left;
|
|
1238
|
-
continue;
|
|
1239
|
-
} else {
|
|
1240
|
-
predecessor.right = null;
|
|
1241
|
-
}
|
|
1242
|
-
} else {
|
|
1243
|
-
ans.push(callback(cur));
|
|
1244
|
-
}
|
|
1245
|
-
cur = cur.right;
|
|
1246
|
-
}
|
|
1247
|
-
break;
|
|
1248
|
-
case "POST":
|
|
1249
|
-
while (cur) {
|
|
1250
|
-
if (cur.left) {
|
|
1251
|
-
const predecessor = this.getPredecessor(cur);
|
|
1252
|
-
if (predecessor.right === null) {
|
|
1253
|
-
predecessor.right = cur;
|
|
1254
|
-
cur = cur.left;
|
|
1255
|
-
continue;
|
|
1256
|
-
} else {
|
|
1257
|
-
predecessor.right = null;
|
|
1258
|
-
_printEdge(cur.left);
|
|
1259
|
-
}
|
|
1260
|
-
}
|
|
1261
|
-
cur = cur.right;
|
|
1262
|
-
}
|
|
1263
|
-
_printEdge(startNode);
|
|
1264
|
-
break;
|
|
1265
|
-
}
|
|
1266
|
-
return ans;
|
|
1267
|
-
}
|
|
1268
|
-
clone() {
|
|
1269
|
-
const out = this._createInstance();
|
|
1270
|
-
this._clone(out);
|
|
1271
|
-
return out;
|
|
1272
|
-
}
|
|
1273
|
-
filter(predicate, thisArg) {
|
|
1274
|
-
const out = this._createInstance();
|
|
1275
|
-
let i = 0;
|
|
1276
|
-
for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.set([k, v]);
|
|
1277
|
-
return out;
|
|
1278
|
-
}
|
|
1279
|
-
map(cb, options, thisArg) {
|
|
1280
|
-
const out = this._createLike([], options);
|
|
1281
|
-
let i = 0;
|
|
1282
|
-
for (const [k, v] of this) out.set(cb.call(thisArg, v, k, i++, this));
|
|
1283
|
-
return out;
|
|
1284
|
-
}
|
|
1285
|
-
toVisual(startNode = this._root, options) {
|
|
1286
|
-
const opts = { isShowUndefined: false, isShowNull: true, isShowRedBlackNIL: false, ...options };
|
|
1287
|
-
startNode = this.ensureNode(startNode);
|
|
1288
|
-
let output = "";
|
|
1289
|
-
if (!startNode) return output;
|
|
1290
|
-
if (opts.isShowUndefined) output += `U for undefined
|
|
1291
|
-
`;
|
|
1292
|
-
if (opts.isShowNull) output += `N for null
|
|
1293
|
-
`;
|
|
1294
|
-
if (opts.isShowRedBlackNIL) output += `S for Sentinel Node(NIL)
|
|
1295
|
-
`;
|
|
1296
|
-
const display = __name((root) => {
|
|
1297
|
-
const [lines] = this._displayAux(root, opts);
|
|
1298
|
-
let paragraph = "";
|
|
1299
|
-
for (const line of lines) {
|
|
1300
|
-
paragraph += line + "\n";
|
|
1301
|
-
}
|
|
1302
|
-
output += paragraph;
|
|
1303
|
-
}, "display");
|
|
1304
|
-
display(startNode);
|
|
1305
|
-
return output;
|
|
1306
|
-
}
|
|
1307
|
-
print(options, startNode = this._root) {
|
|
1308
|
-
console.log(this.toVisual(startNode, options));
|
|
1309
|
-
}
|
|
1310
|
-
_dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = "IN", onlyOne = false, startNode = this._root, iterationType = this.iterationType, includeNull = false, shouldVisitLeft = (node) => !!node, shouldVisitRight = (node) => !!node, shouldVisitRoot = (node) => {
|
|
1311
|
-
if (includeNull) return this.isRealNodeOrNull(node);
|
|
1312
|
-
return this.isRealNode(node);
|
|
1313
|
-
}, shouldProcessRoot = (node) => this.isRealNodeOrNull(node)) {
|
|
1314
|
-
startNode = this.ensureNode(startNode);
|
|
1315
|
-
if (!startNode) return [];
|
|
1316
|
-
const ans = [];
|
|
1317
|
-
if (iterationType === "RECURSIVE") {
|
|
1318
|
-
const dfs = __name((node) => {
|
|
1319
|
-
if (!shouldVisitRoot(node)) return;
|
|
1320
|
-
const visitLeft = __name(() => {
|
|
1321
|
-
if (shouldVisitLeft(node) && node?.left !== void 0) dfs(node?.left);
|
|
1322
|
-
}, "visitLeft");
|
|
1323
|
-
const visitRight = __name(() => {
|
|
1324
|
-
if (shouldVisitRight(node) && node?.right !== void 0) dfs(node?.right);
|
|
1325
|
-
}, "visitRight");
|
|
1326
|
-
switch (pattern) {
|
|
1327
|
-
case "IN":
|
|
1328
|
-
visitLeft();
|
|
1329
|
-
if (shouldProcessRoot(node)) {
|
|
1330
|
-
ans.push(callback(node));
|
|
1331
|
-
if (onlyOne) return;
|
|
1332
|
-
}
|
|
1333
|
-
visitRight();
|
|
1334
|
-
break;
|
|
1335
|
-
case "PRE":
|
|
1336
|
-
if (shouldProcessRoot(node)) {
|
|
1337
|
-
ans.push(callback(node));
|
|
1338
|
-
if (onlyOne) return;
|
|
1339
|
-
}
|
|
1340
|
-
visitLeft();
|
|
1341
|
-
visitRight();
|
|
1342
|
-
break;
|
|
1343
|
-
case "POST":
|
|
1344
|
-
visitLeft();
|
|
1345
|
-
visitRight();
|
|
1346
|
-
if (shouldProcessRoot(node)) {
|
|
1347
|
-
ans.push(callback(node));
|
|
1348
|
-
if (onlyOne) return;
|
|
1349
|
-
}
|
|
1350
|
-
break;
|
|
1351
|
-
}
|
|
1352
|
-
}, "dfs");
|
|
1353
|
-
dfs(startNode);
|
|
1354
|
-
} else {
|
|
1355
|
-
const stack = [{ opt: 0 , node: startNode }];
|
|
1356
|
-
const pushLeft = __name((cur) => {
|
|
1357
|
-
if (shouldVisitLeft(cur.node)) stack.push({ opt: 0 , node: cur.node?.left });
|
|
1358
|
-
}, "pushLeft");
|
|
1359
|
-
const pushRight = __name((cur) => {
|
|
1360
|
-
if (shouldVisitRight(cur.node)) stack.push({ opt: 0 , node: cur.node?.right });
|
|
1361
|
-
}, "pushRight");
|
|
1362
|
-
const pushRoot = __name((cur) => {
|
|
1363
|
-
if (shouldVisitRoot(cur.node)) stack.push({ opt: 1 , node: cur.node });
|
|
1364
|
-
}, "pushRoot");
|
|
1365
|
-
while (stack.length > 0) {
|
|
1366
|
-
const cur = stack.pop();
|
|
1367
|
-
if (cur === void 0) continue;
|
|
1368
|
-
if (!shouldVisitRoot(cur.node)) continue;
|
|
1369
|
-
if (cur.opt === 1 ) {
|
|
1370
|
-
if (shouldProcessRoot(cur.node) && cur.node !== void 0) {
|
|
1371
|
-
ans.push(callback(cur.node));
|
|
1372
|
-
if (onlyOne) return ans;
|
|
1373
|
-
}
|
|
1374
|
-
} else {
|
|
1375
|
-
switch (pattern) {
|
|
1376
|
-
case "IN":
|
|
1377
|
-
pushRight(cur);
|
|
1378
|
-
pushRoot(cur);
|
|
1379
|
-
pushLeft(cur);
|
|
1380
|
-
break;
|
|
1381
|
-
case "PRE":
|
|
1382
|
-
pushRight(cur);
|
|
1383
|
-
pushLeft(cur);
|
|
1384
|
-
pushRoot(cur);
|
|
1385
|
-
break;
|
|
1386
|
-
case "POST":
|
|
1387
|
-
pushRoot(cur);
|
|
1388
|
-
pushRight(cur);
|
|
1389
|
-
pushLeft(cur);
|
|
1390
|
-
break;
|
|
1391
|
-
}
|
|
1392
|
-
}
|
|
1393
|
-
}
|
|
1394
|
-
}
|
|
1395
|
-
return ans;
|
|
1396
|
-
}
|
|
1397
|
-
*_getIterator(node = this._root) {
|
|
1398
|
-
if (!node) return;
|
|
1399
|
-
if (this.iterationType === "ITERATIVE") {
|
|
1400
|
-
const stack = [];
|
|
1401
|
-
let current = node;
|
|
1402
|
-
while (current || stack.length > 0) {
|
|
1403
|
-
while (this.isRealNode(current)) {
|
|
1404
|
-
stack.push(current);
|
|
1405
|
-
current = current.left;
|
|
1406
|
-
}
|
|
1407
|
-
current = stack.pop();
|
|
1408
|
-
if (this.isRealNode(current)) {
|
|
1409
|
-
if (this._isMapMode) yield [current.key, this._store.get(current.key)];
|
|
1410
|
-
else yield [current.key, current.value];
|
|
1411
|
-
current = current.right;
|
|
1412
|
-
}
|
|
1413
|
-
}
|
|
1414
|
-
} else {
|
|
1415
|
-
if (node.left && this.isRealNode(node)) {
|
|
1416
|
-
yield* this[Symbol.iterator](node.left);
|
|
1417
|
-
}
|
|
1418
|
-
if (this._isMapMode) yield [node.key, this._store.get(node.key)];
|
|
1419
|
-
else yield [node.key, node.value];
|
|
1420
|
-
if (node.right && this.isRealNode(node)) {
|
|
1421
|
-
yield* this[Symbol.iterator](node.right);
|
|
1422
|
-
}
|
|
1423
|
-
}
|
|
1424
|
-
}
|
|
1425
|
-
_DEFAULT_NODE_CALLBACK = __name((node) => node ? node.key : void 0, "_DEFAULT_NODE_CALLBACK");
|
|
1426
|
-
_snapshotOptions() {
|
|
1427
|
-
return {
|
|
1428
|
-
iterationType: this.iterationType,
|
|
1429
|
-
toEntryFn: this.toEntryFn,
|
|
1430
|
-
isMapMode: this.isMapMode,
|
|
1431
|
-
isDuplicate: this.isDuplicate
|
|
1432
|
-
};
|
|
1433
|
-
}
|
|
1434
|
-
_createInstance(options) {
|
|
1435
|
-
const Ctor = this.constructor;
|
|
1436
|
-
return new Ctor([], { ...this._snapshotOptions(), ...options ?? {} });
|
|
1437
|
-
}
|
|
1438
|
-
_createLike(iter = [], options) {
|
|
1439
|
-
const Ctor = this.constructor;
|
|
1440
|
-
return new Ctor(iter, { ...this._snapshotOptions(), ...options ?? {} });
|
|
1441
|
-
}
|
|
1442
|
-
_keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value) {
|
|
1443
|
-
if (keyNodeOrEntry === void 0) return [void 0, void 0];
|
|
1444
|
-
if (keyNodeOrEntry === null) return [null, void 0];
|
|
1445
|
-
if (this.isNode(keyNodeOrEntry)) return [keyNodeOrEntry, value];
|
|
1446
|
-
if (this.isEntry(keyNodeOrEntry)) {
|
|
1447
|
-
const [key, entryValue] = keyNodeOrEntry;
|
|
1448
|
-
if (key === void 0) return [void 0, void 0];
|
|
1449
|
-
else if (key === null) return [null, void 0];
|
|
1450
|
-
const finalValue = value ?? entryValue;
|
|
1451
|
-
return [this.createNode(key, finalValue), finalValue];
|
|
1452
|
-
}
|
|
1453
|
-
return [this.createNode(keyNodeOrEntry, value), value];
|
|
1454
|
-
}
|
|
1455
|
-
_clone(cloned) {
|
|
1456
|
-
this.bfs(
|
|
1457
|
-
(node) => {
|
|
1458
|
-
if (node === null) cloned.set(null);
|
|
1459
|
-
else {
|
|
1460
|
-
if (this._isMapMode) cloned.set([node.key, this._store.get(node.key)]);
|
|
1461
|
-
else cloned.set([node.key, node.value]);
|
|
1462
|
-
}
|
|
1463
|
-
},
|
|
1464
|
-
this._root,
|
|
1465
|
-
this.iterationType,
|
|
1466
|
-
true
|
|
1467
|
-
);
|
|
1468
|
-
if (this._isMapMode) cloned._store = this._store;
|
|
1469
|
-
}
|
|
1470
|
-
_displayAux(node, options) {
|
|
1471
|
-
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
1472
|
-
const emptyDisplayLayout = [["\u2500"], 1, 0, 0];
|
|
1473
|
-
if (node === null && !isShowNull) {
|
|
1474
|
-
return emptyDisplayLayout;
|
|
1475
|
-
} else if (node === void 0 && !isShowUndefined) {
|
|
1476
|
-
return emptyDisplayLayout;
|
|
1477
|
-
} else if (this.isNIL(node) && !isShowRedBlackNIL) {
|
|
1478
|
-
return emptyDisplayLayout;
|
|
1479
|
-
} else if (node !== null && node !== void 0) {
|
|
1480
|
-
const key = node.key, line = this.isNIL(node) ? "S" : String(key), width = line.length;
|
|
1481
|
-
return _buildNodeDisplay(
|
|
1482
|
-
line,
|
|
1483
|
-
width,
|
|
1484
|
-
this._displayAux(node.left, options),
|
|
1485
|
-
this._displayAux(node.right, options)
|
|
1486
|
-
);
|
|
1487
|
-
} else {
|
|
1488
|
-
const line = node === void 0 ? "U" : "N", width = line.length;
|
|
1489
|
-
return _buildNodeDisplay(line, width, [[""], 1, 0, 0], [[""], 1, 0, 0]);
|
|
1490
|
-
}
|
|
1491
|
-
function _buildNodeDisplay(line, width, left, right) {
|
|
1492
|
-
const [leftLines, leftWidth, leftHeight, leftMiddle] = left;
|
|
1493
|
-
const [rightLines, rightWidth, rightHeight, rightMiddle] = right;
|
|
1494
|
-
const firstLine = " ".repeat(Math.max(0, leftMiddle + 1)) + "_".repeat(Math.max(0, leftWidth - leftMiddle - 1)) + line + "_".repeat(Math.max(0, rightMiddle)) + " ".repeat(Math.max(0, rightWidth - rightMiddle));
|
|
1495
|
-
const secondLine = (leftHeight > 0 ? " ".repeat(leftMiddle) + "/" + " ".repeat(leftWidth - leftMiddle - 1) : " ".repeat(leftWidth)) + " ".repeat(width) + (rightHeight > 0 ? " ".repeat(rightMiddle) + "\\" + " ".repeat(rightWidth - rightMiddle - 1) : " ".repeat(rightWidth));
|
|
1496
|
-
const mergedLines = [firstLine, secondLine];
|
|
1497
|
-
for (let i = 0; i < Math.max(leftHeight, rightHeight); i++) {
|
|
1498
|
-
const leftLine = i < leftHeight ? leftLines[i] : " ".repeat(leftWidth);
|
|
1499
|
-
const rightLine = i < rightHeight ? rightLines[i] : " ".repeat(rightWidth);
|
|
1500
|
-
mergedLines.push(leftLine + " ".repeat(width) + rightLine);
|
|
1501
|
-
}
|
|
1502
|
-
return [
|
|
1503
|
-
mergedLines,
|
|
1504
|
-
leftWidth + width + rightWidth,
|
|
1505
|
-
Math.max(leftHeight, rightHeight) + 2,
|
|
1506
|
-
leftWidth + Math.floor(width / 2)
|
|
1507
|
-
];
|
|
1508
|
-
}
|
|
1509
|
-
__name(_buildNodeDisplay, "_buildNodeDisplay");
|
|
1510
|
-
}
|
|
1511
|
-
_swapProperties(srcNode, destNode) {
|
|
1512
|
-
srcNode = this.ensureNode(srcNode);
|
|
1513
|
-
destNode = this.ensureNode(destNode);
|
|
1514
|
-
if (srcNode && destNode) {
|
|
1515
|
-
const { key, value } = destNode;
|
|
1516
|
-
const tempNode = this.createNode(key, value);
|
|
1517
|
-
if (tempNode) {
|
|
1518
|
-
destNode.key = srcNode.key;
|
|
1519
|
-
if (!this._isMapMode) destNode.value = srcNode.value;
|
|
1520
|
-
srcNode.key = tempNode.key;
|
|
1521
|
-
if (!this._isMapMode) srcNode.value = tempNode.value;
|
|
1522
|
-
}
|
|
1523
|
-
return destNode;
|
|
1524
|
-
}
|
|
1525
|
-
return void 0;
|
|
1526
|
-
}
|
|
1527
|
-
_replaceNode(oldNode, newNode) {
|
|
1528
|
-
if (oldNode.parent) {
|
|
1529
|
-
if (oldNode.parent.left === oldNode) {
|
|
1530
|
-
oldNode.parent.left = newNode;
|
|
1531
|
-
} else if (oldNode.parent.right === oldNode) {
|
|
1532
|
-
oldNode.parent.right = newNode;
|
|
1533
|
-
}
|
|
1534
|
-
}
|
|
1535
|
-
newNode.left = oldNode.left;
|
|
1536
|
-
newNode.right = oldNode.right;
|
|
1537
|
-
newNode.parent = oldNode.parent;
|
|
1538
|
-
if (this._root === oldNode) {
|
|
1539
|
-
this._setRoot(newNode);
|
|
1540
|
-
}
|
|
1541
|
-
return newNode;
|
|
1542
|
-
}
|
|
1543
|
-
_setRoot(v) {
|
|
1544
|
-
if (v) {
|
|
1545
|
-
v.parent = void 0;
|
|
1546
|
-
}
|
|
1547
|
-
this._root = v;
|
|
1548
|
-
}
|
|
1549
|
-
_ensurePredicate(keyNodeEntryOrPredicate) {
|
|
1550
|
-
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0)
|
|
1551
|
-
return (node) => node ? false : false;
|
|
1552
|
-
if (this._isPredicate(keyNodeEntryOrPredicate)) return keyNodeEntryOrPredicate;
|
|
1553
|
-
if (this.isRealNode(keyNodeEntryOrPredicate))
|
|
1554
|
-
return (node) => node === keyNodeEntryOrPredicate;
|
|
1555
|
-
if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
1556
|
-
const [key] = keyNodeEntryOrPredicate;
|
|
1557
|
-
return (node) => {
|
|
1558
|
-
if (!node) return false;
|
|
1559
|
-
return node.key === key;
|
|
1560
|
-
};
|
|
1561
|
-
}
|
|
1562
|
-
return (node) => {
|
|
1563
|
-
if (!node) return false;
|
|
1564
|
-
return node.key === keyNodeEntryOrPredicate;
|
|
1565
|
-
};
|
|
1566
|
-
}
|
|
1567
|
-
_isPredicate(p) {
|
|
1568
|
-
return typeof p === "function";
|
|
1569
|
-
}
|
|
1570
|
-
_extractKey(keyNodeOrEntry) {
|
|
1571
|
-
if (keyNodeOrEntry === null) return null;
|
|
1572
|
-
if (keyNodeOrEntry === void 0) return;
|
|
1573
|
-
if (keyNodeOrEntry === this._NIL) return;
|
|
1574
|
-
if (this.isNode(keyNodeOrEntry)) return keyNodeOrEntry.key;
|
|
1575
|
-
if (this.isEntry(keyNodeOrEntry)) return keyNodeOrEntry[0];
|
|
1576
|
-
return keyNodeOrEntry;
|
|
1577
|
-
}
|
|
1578
|
-
_setValue(key, value) {
|
|
1579
|
-
if (key === null || key === void 0) return false;
|
|
1580
|
-
if (value === void 0) return false;
|
|
1581
|
-
return this._store.set(key, value);
|
|
1582
|
-
}
|
|
1583
|
-
_clearNodes() {
|
|
1584
|
-
this._setRoot(void 0);
|
|
1585
|
-
this._size = 0;
|
|
1586
|
-
}
|
|
1587
|
-
_clearValues() {
|
|
1588
|
-
this._store.clear();
|
|
1589
|
-
}
|
|
1590
|
-
};
|
|
1591
|
-
export {
|
|
1592
|
-
BinaryTree,
|
|
1593
|
-
BinaryTreeNode
|
|
1594
|
-
};
|