data-structure-typed 2.4.5 → 2.5.1
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 +3 -1
- package/README.md +78 -31
- package/dist/cjs/binary-tree.cjs +23698 -0
- package/dist/cjs/graph.cjs +5236 -0
- package/dist/cjs/hash.cjs +1262 -0
- package/dist/cjs/heap.cjs +1540 -0
- package/dist/cjs/index.cjs +24509 -2899
- package/dist/cjs/linked-list.cjs +4370 -0
- package/dist/cjs/matrix.cjs +1042 -0
- package/dist/cjs/priority-queue.cjs +1314 -0
- package/dist/cjs/queue.cjs +4090 -0
- package/dist/cjs/stack.cjs +861 -0
- package/dist/cjs/trie.cjs +1173 -0
- package/dist/cjs-legacy/binary-tree.cjs +23730 -0
- package/dist/cjs-legacy/graph.cjs +5234 -0
- package/dist/cjs-legacy/hash.cjs +1262 -0
- package/dist/cjs-legacy/heap.cjs +1537 -0
- package/dist/cjs-legacy/index.cjs +32555 -10936
- package/dist/cjs-legacy/linked-list.cjs +4376 -0
- package/dist/cjs-legacy/matrix.cjs +1045 -0
- package/dist/cjs-legacy/priority-queue.cjs +1312 -0
- package/dist/cjs-legacy/queue.cjs +4088 -0
- package/dist/cjs-legacy/stack.cjs +861 -0
- package/dist/cjs-legacy/trie.cjs +1172 -0
- package/dist/esm/binary-tree.mjs +23683 -0
- package/dist/esm/graph.mjs +5223 -0
- package/dist/esm/hash.mjs +1259 -0
- package/dist/esm/heap.mjs +1534 -0
- package/dist/esm/index.mjs +24507 -2898
- package/dist/esm/linked-list.mjs +4363 -0
- package/dist/esm/matrix.mjs +1038 -0
- package/dist/esm/priority-queue.mjs +1310 -0
- package/dist/esm/queue.mjs +4086 -0
- package/dist/esm/stack.mjs +859 -0
- package/dist/esm/trie.mjs +1170 -0
- package/dist/esm-legacy/binary-tree.mjs +23715 -0
- package/dist/esm-legacy/graph.mjs +5221 -0
- package/dist/esm-legacy/hash.mjs +1259 -0
- package/dist/esm-legacy/heap.mjs +1531 -0
- package/dist/esm-legacy/index.mjs +32553 -10935
- package/dist/esm-legacy/linked-list.mjs +4369 -0
- package/dist/esm-legacy/matrix.mjs +1041 -0
- package/dist/esm-legacy/priority-queue.mjs +1308 -0
- package/dist/esm-legacy/queue.mjs +4084 -0
- package/dist/esm-legacy/stack.mjs +859 -0
- package/dist/esm-legacy/trie.mjs +1169 -0
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- 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 +368 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +473 -147
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +931 -80
- package/dist/types/data-structures/binary-tree/bst.d.ts +792 -29
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +592 -32
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +320 -135
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3662 -6
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3487 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2778 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3414 -6
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +419 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +384 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +462 -89
- package/dist/types/data-structures/heap/heap.d.ts +567 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +631 -49
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +581 -68
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +775 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +491 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +578 -71
- package/dist/types/data-structures/queue/queue.d.ts +451 -42
- package/dist/types/data-structures/stack/stack.d.ts +374 -32
- package/dist/types/data-structures/trie/trie.d.ts +458 -48
- 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/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- 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 +32432 -10808
- package/dist/umd/data-structure-typed.min.js +10 -4
- 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 +6130 -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 +5831 -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 +6374 -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 +1257 -0
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +1475 -0
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +1117 -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 +613 -0
- package/docs-site-docusaurus/docs/guide/concepts.md +420 -0
- package/docs-site-docusaurus/docs/guide/guides.md +611 -0
- package/docs-site-docusaurus/docs/guide/installation.md +60 -0
- package/docs-site-docusaurus/docs/guide/integrations.md +823 -0
- package/docs-site-docusaurus/docs/guide/overview.md +638 -0
- package/docs-site-docusaurus/docs/guide/performance.md +833 -0
- package/docs-site-docusaurus/docs/guide/quick-start.md +73 -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 +71 -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/robots.txt +4 -0
- package/docs-site-docusaurus/typedoc.json +23 -0
- package/package.json +109 -12
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- 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 +386 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +596 -247
- package/src/data-structures/binary-tree/binary-tree.ts +956 -81
- package/src/data-structures/binary-tree/bst.ts +840 -35
- package/src/data-structures/binary-tree/red-black-tree.ts +689 -97
- package/src/data-structures/binary-tree/segment-tree.ts +498 -249
- package/src/data-structures/binary-tree/tree-map.ts +3784 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +3614 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +2874 -65
- package/src/data-structures/binary-tree/tree-set.ts +3531 -10
- package/src/data-structures/graph/abstract-graph.ts +4 -4
- package/src/data-structures/graph/directed-graph.ts +429 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +393 -59
- package/src/data-structures/hash/hash-map.ts +476 -92
- package/src/data-structures/heap/heap.ts +581 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +646 -47
- package/src/data-structures/linked-list/singly-linked-list.ts +596 -68
- package/src/data-structures/linked-list/skip-linked-list.ts +1067 -90
- package/src/data-structures/matrix/matrix.ts +584 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +592 -70
- package/src/data-structures/queue/queue.ts +463 -42
- package/src/data-structures/stack/stack.ts +384 -32
- package/src/data-structures/trie/trie.ts +470 -48
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
- 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
|
@@ -125,84 +125,6 @@ export declare class BinaryTreeNode<K = any, V = any> {
|
|
|
125
125
|
* 5. Leaf Nodes: Nodes without children are leaves.
|
|
126
126
|
*
|
|
127
127
|
* @example
|
|
128
|
-
* // basic BinaryTree creation and insertion
|
|
129
|
-
* // Create a BinaryTree with entries
|
|
130
|
-
* const entries: [number, string][] = [
|
|
131
|
-
* [6, 'six'],
|
|
132
|
-
* [1, 'one'],
|
|
133
|
-
* [2, 'two'],
|
|
134
|
-
* [7, 'seven'],
|
|
135
|
-
* [5, 'five'],
|
|
136
|
-
* [3, 'three'],
|
|
137
|
-
* [4, 'four'],
|
|
138
|
-
* [9, 'nine'],
|
|
139
|
-
* [8, 'eight']
|
|
140
|
-
* ];
|
|
141
|
-
*
|
|
142
|
-
* const tree = new BinaryTree(entries);
|
|
143
|
-
*
|
|
144
|
-
* // Verify size
|
|
145
|
-
* console.log(tree.size); // 9;
|
|
146
|
-
*
|
|
147
|
-
* // Add new element
|
|
148
|
-
* tree.set(10, 'ten');
|
|
149
|
-
* console.log(tree.size); // 10;
|
|
150
|
-
* @example
|
|
151
|
-
* // BinaryTree get and has operations
|
|
152
|
-
* const tree = new BinaryTree(
|
|
153
|
-
* [
|
|
154
|
-
* [5, 'five'],
|
|
155
|
-
* [3, 'three'],
|
|
156
|
-
* [7, 'seven'],
|
|
157
|
-
* [1, 'one'],
|
|
158
|
-
* [4, 'four'],
|
|
159
|
-
* [6, 'six'],
|
|
160
|
-
* [8, 'eight']
|
|
161
|
-
* ],
|
|
162
|
-
* { isMapMode: false }
|
|
163
|
-
* );
|
|
164
|
-
*
|
|
165
|
-
* // Check if key exists
|
|
166
|
-
* console.log(tree.has(5)); // true;
|
|
167
|
-
* console.log(tree.has(10)); // false;
|
|
168
|
-
*
|
|
169
|
-
* // Get value by key
|
|
170
|
-
* console.log(tree.get(3)); // 'three';
|
|
171
|
-
* console.log(tree.get(7)); // 'seven';
|
|
172
|
-
* console.log(tree.get(100)); // undefined;
|
|
173
|
-
*
|
|
174
|
-
* // Get node structure
|
|
175
|
-
* const node = tree.getNode(5);
|
|
176
|
-
* console.log(node?.key); // 5;
|
|
177
|
-
* console.log(node?.value); // 'five';
|
|
178
|
-
* @example
|
|
179
|
-
* // BinaryTree level-order traversal
|
|
180
|
-
* const tree = new BinaryTree([
|
|
181
|
-
* [1, 'one'],
|
|
182
|
-
* [2, 'two'],
|
|
183
|
-
* [3, 'three'],
|
|
184
|
-
* [4, 'four'],
|
|
185
|
-
* [5, 'five'],
|
|
186
|
-
* [6, 'six'],
|
|
187
|
-
* [7, 'seven']
|
|
188
|
-
* ]);
|
|
189
|
-
*
|
|
190
|
-
* // Binary tree maintains level-order insertion
|
|
191
|
-
* // Complete binary tree structure
|
|
192
|
-
* console.log(tree.size); // 7;
|
|
193
|
-
*
|
|
194
|
-
* // Verify all keys are present
|
|
195
|
-
* console.log(tree.has(1)); // true;
|
|
196
|
-
* console.log(tree.has(4)); // true;
|
|
197
|
-
* console.log(tree.has(7)); // true;
|
|
198
|
-
*
|
|
199
|
-
* // Iterate through tree
|
|
200
|
-
* const keys: number[] = [];
|
|
201
|
-
* for (const [key] of tree) {
|
|
202
|
-
* keys.push(key);
|
|
203
|
-
* }
|
|
204
|
-
* console.log(keys.length); // 7;
|
|
205
|
-
* @example
|
|
206
128
|
* // determine loan approval using a decision tree
|
|
207
129
|
* // Decision tree structure
|
|
208
130
|
* const loanDecisionTree = new BinaryTree<string>(
|
|
@@ -428,13 +350,47 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
428
350
|
* @param key - The key to validate.
|
|
429
351
|
* @returns True if the key is valid, false otherwise.
|
|
430
352
|
*/
|
|
431
|
-
isValidKey(key:
|
|
353
|
+
isValidKey(key: unknown): key is K;
|
|
432
354
|
/**
|
|
433
355
|
* Adds a new node to the tree.
|
|
434
356
|
* @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation adds the node at the first available position in a level-order (BFS) traversal. This is NOT a Binary Search Tree insertion. Time O(N), where N is the number of nodes. It must traverse level-by-level to find an empty slot. Space O(N) in the worst case for the BFS queue (e.g., a full last level).
|
|
435
357
|
*
|
|
436
358
|
* @param keyNodeOrEntry - The key, node, or entry to add.
|
|
437
359
|
* @returns True if the addition was successful, false otherwise.
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
* @example
|
|
387
|
+
* // Add a single node
|
|
388
|
+
* const tree = new BinaryTree<number>();
|
|
389
|
+
* tree.add(1);
|
|
390
|
+
* tree.add(2);
|
|
391
|
+
* tree.add(3);
|
|
392
|
+
* console.log(tree.size); // 3;
|
|
393
|
+
* console.log(tree.has(1)); // true;
|
|
438
394
|
*/
|
|
439
395
|
add(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): boolean;
|
|
440
396
|
/**
|
|
@@ -444,6 +400,60 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
444
400
|
* @param keyNodeOrEntry - The key, node, or entry to set or update.
|
|
445
401
|
* @param [value] - The value, if providing just a key.
|
|
446
402
|
* @returns True if the addition was successful, false otherwise.
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
* @example
|
|
435
|
+
* // basic BinaryTree creation and insertion
|
|
436
|
+
* // Create a BinaryTree with entries
|
|
437
|
+
* const entries: [number, string][] = [
|
|
438
|
+
* [6, 'six'],
|
|
439
|
+
* [1, 'one'],
|
|
440
|
+
* [2, 'two'],
|
|
441
|
+
* [7, 'seven'],
|
|
442
|
+
* [5, 'five'],
|
|
443
|
+
* [3, 'three'],
|
|
444
|
+
* [4, 'four'],
|
|
445
|
+
* [9, 'nine'],
|
|
446
|
+
* [8, 'eight']
|
|
447
|
+
* ];
|
|
448
|
+
*
|
|
449
|
+
* const tree = new BinaryTree(entries);
|
|
450
|
+
*
|
|
451
|
+
* // Verify size
|
|
452
|
+
* console.log(tree.size); // 9;
|
|
453
|
+
*
|
|
454
|
+
* // Add new element
|
|
455
|
+
* tree.set(10, 'ten');
|
|
456
|
+
* console.log(tree.size); // 10;
|
|
447
457
|
*/
|
|
448
458
|
set(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
|
|
449
459
|
/**
|
|
@@ -452,6 +462,40 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
452
462
|
*
|
|
453
463
|
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
454
464
|
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
* @example
|
|
495
|
+
* // Bulk add
|
|
496
|
+
* const tree = new BinaryTree<number>();
|
|
497
|
+
* tree.addMany([1, 2, 3, 4, 5]);
|
|
498
|
+
* console.log(tree.size); // 5;
|
|
455
499
|
*/
|
|
456
500
|
addMany(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>): boolean[];
|
|
457
501
|
/**
|
|
@@ -461,6 +505,33 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
461
505
|
* @param keysNodesEntriesOrRaws - An iterable of items to set or update.
|
|
462
506
|
* @param [values] - An optional parallel iterable of values.
|
|
463
507
|
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
|
|
523
|
+
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
* @example
|
|
531
|
+
* // Set multiple entries
|
|
532
|
+
* const tree = new BinaryTree<number, string>();
|
|
533
|
+
* tree.setMany([[1, 'a'], [2, 'b'], [3, 'c']]);
|
|
534
|
+
* console.log(tree.size); // 3;
|
|
464
535
|
*/
|
|
465
536
|
setMany(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, values?: Iterable<V | undefined>): boolean[];
|
|
466
537
|
/**
|
|
@@ -468,6 +539,41 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
468
539
|
* @remarks Time O(N * M), same as `setMany`, where N is the size of `anotherTree` and M is the size of this tree. Space O(M) (from `set`).
|
|
469
540
|
*
|
|
470
541
|
* @param anotherTree - The tree to merge.
|
|
542
|
+
|
|
543
|
+
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
|
|
571
|
+
* @example
|
|
572
|
+
* // Combine trees
|
|
573
|
+
* const t1 = new BinaryTree<number>([1, 2]);
|
|
574
|
+
* const t2 = new BinaryTree<number>([3, 4]);
|
|
575
|
+
* t1.merge(t2);
|
|
576
|
+
* console.log(t1.size); // 4;
|
|
471
577
|
*/
|
|
472
578
|
merge(anotherTree: BinaryTree<K, V, R>): void;
|
|
473
579
|
/**
|
|
@@ -484,8 +590,75 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
484
590
|
*
|
|
485
591
|
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
486
592
|
* @returns An array containing deletion results (for compatibility with self-balancing trees).
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
|
|
605
|
+
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
|
|
616
|
+
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
|
|
623
|
+
|
|
624
|
+
* @example
|
|
625
|
+
* // Remove a node
|
|
626
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
627
|
+
* tree.delete(3);
|
|
628
|
+
* console.log(tree.has(3)); // false;
|
|
629
|
+
* console.log(tree.size); // 4;
|
|
487
630
|
*/
|
|
488
631
|
delete(keyNodeEntryRawOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V> | null>): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[];
|
|
632
|
+
/**
|
|
633
|
+
* Search by predicate
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
|
|
642
|
+
|
|
643
|
+
|
|
644
|
+
|
|
645
|
+
|
|
646
|
+
|
|
647
|
+
|
|
648
|
+
|
|
649
|
+
|
|
650
|
+
|
|
651
|
+
|
|
652
|
+
|
|
653
|
+
|
|
654
|
+
|
|
655
|
+
|
|
656
|
+
* @example
|
|
657
|
+
* // Search by predicate
|
|
658
|
+
* const tree = new BinaryTree<number>([5, 3, 7, 1, 9]);
|
|
659
|
+
* const found = tree.search(n => n!.key > 5, true);
|
|
660
|
+
* console.log(found.length); // >= 1;
|
|
661
|
+
*/
|
|
489
662
|
search(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V> | null>, onlyOne?: boolean): (K | undefined)[];
|
|
490
663
|
search<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V> | null>, onlyOne: boolean, callback: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
491
664
|
/**
|
|
@@ -497,6 +670,39 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
497
670
|
* @param [startNode=this._root] - The node to start the search from.
|
|
498
671
|
* @param [iterationType=this.iterationType] - The traversal method.
|
|
499
672
|
* @returns An array of matching nodes.
|
|
673
|
+
|
|
674
|
+
|
|
675
|
+
|
|
676
|
+
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
|
|
693
|
+
|
|
694
|
+
|
|
695
|
+
|
|
696
|
+
|
|
697
|
+
|
|
698
|
+
|
|
699
|
+
|
|
700
|
+
|
|
701
|
+
* @example
|
|
702
|
+
* // Get nodes by condition
|
|
703
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
704
|
+
* const nodes = tree.getNodes(node => node.key > 3);
|
|
705
|
+
* console.log(nodes.length); // 2;
|
|
500
706
|
*/
|
|
501
707
|
getNodes(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>, onlyOne?: boolean, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): BinaryTreeNode<K, V>[];
|
|
502
708
|
/**
|
|
@@ -507,6 +713,39 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
507
713
|
* @param [startNode=this._root] - The node to start the search from.
|
|
508
714
|
* @param [iterationType=this.iterationType] - The traversal method.
|
|
509
715
|
* @returns The first matching node, or undefined if not found.
|
|
716
|
+
|
|
717
|
+
|
|
718
|
+
|
|
719
|
+
|
|
720
|
+
|
|
721
|
+
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
|
|
725
|
+
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
|
|
729
|
+
|
|
730
|
+
|
|
731
|
+
|
|
732
|
+
|
|
733
|
+
|
|
734
|
+
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
|
|
740
|
+
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
|
|
744
|
+
|
|
745
|
+
* @example
|
|
746
|
+
* // Get node by key
|
|
747
|
+
* const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'child']]);
|
|
748
|
+
* console.log(tree.getNode(2)?.value); // 'child';
|
|
510
749
|
*/
|
|
511
750
|
getNode(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V> | null>, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): BinaryTreeNode<K, V> | null | undefined;
|
|
512
751
|
/**
|
|
@@ -517,6 +756,42 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
517
756
|
* @param [startNode=this._root] - The node to start searching from (if not in Map mode).
|
|
518
757
|
* @param [iterationType=this.iterationType] - The traversal method (if not in Map mode).
|
|
519
758
|
* @returns The associated value, or undefined.
|
|
759
|
+
|
|
760
|
+
|
|
761
|
+
|
|
762
|
+
|
|
763
|
+
|
|
764
|
+
|
|
765
|
+
|
|
766
|
+
|
|
767
|
+
|
|
768
|
+
|
|
769
|
+
|
|
770
|
+
|
|
771
|
+
|
|
772
|
+
|
|
773
|
+
|
|
774
|
+
|
|
775
|
+
|
|
776
|
+
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
|
|
780
|
+
|
|
781
|
+
|
|
782
|
+
|
|
783
|
+
|
|
784
|
+
|
|
785
|
+
|
|
786
|
+
|
|
787
|
+
|
|
788
|
+
|
|
789
|
+
|
|
790
|
+
* @example
|
|
791
|
+
* // Retrieve value by key
|
|
792
|
+
* const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'left'], [3, 'right']]);
|
|
793
|
+
* console.log(tree.get(2)); // 'left';
|
|
794
|
+
* console.log(tree.get(99)); // undefined;
|
|
520
795
|
*/
|
|
521
796
|
get(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): V | undefined;
|
|
522
797
|
/**
|
|
@@ -527,11 +802,104 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
527
802
|
* @param [startNode] - The node to start the search from.
|
|
528
803
|
* @param [iterationType] - The traversal method.
|
|
529
804
|
* @returns True if a matching node exists, false otherwise.
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
|
|
809
|
+
|
|
810
|
+
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
|
|
814
|
+
|
|
815
|
+
|
|
816
|
+
|
|
817
|
+
|
|
818
|
+
|
|
819
|
+
|
|
820
|
+
|
|
821
|
+
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
|
|
828
|
+
|
|
829
|
+
|
|
830
|
+
|
|
831
|
+
|
|
832
|
+
|
|
833
|
+
|
|
834
|
+
|
|
835
|
+
|
|
836
|
+
* @example
|
|
837
|
+
* // BinaryTree get and has operations
|
|
838
|
+
* const tree = new BinaryTree(
|
|
839
|
+
* [
|
|
840
|
+
* [5, 'five'],
|
|
841
|
+
* [3, 'three'],
|
|
842
|
+
* [7, 'seven'],
|
|
843
|
+
* [1, 'one'],
|
|
844
|
+
* [4, 'four'],
|
|
845
|
+
* [6, 'six'],
|
|
846
|
+
* [8, 'eight']
|
|
847
|
+
* ],
|
|
848
|
+
* { isMapMode: false }
|
|
849
|
+
* );
|
|
850
|
+
*
|
|
851
|
+
* // Check if key exists
|
|
852
|
+
* console.log(tree.has(5)); // true;
|
|
853
|
+
* console.log(tree.has(10)); // false;
|
|
854
|
+
*
|
|
855
|
+
* // Get value by key
|
|
856
|
+
* console.log(tree.get(3)); // 'three';
|
|
857
|
+
* console.log(tree.get(7)); // 'seven';
|
|
858
|
+
* console.log(tree.get(100)); // undefined;
|
|
859
|
+
*
|
|
860
|
+
* // Get node structure
|
|
861
|
+
* const node = tree.getNode(5);
|
|
862
|
+
* console.log(node?.key); // 5;
|
|
863
|
+
* console.log(node?.value); // 'five';
|
|
530
864
|
*/
|
|
531
865
|
has(keyNodeEntryOrPredicate?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): boolean;
|
|
532
866
|
/**
|
|
533
867
|
* Clears the tree of all nodes and values.
|
|
534
868
|
* @remarks Time O(N) if in Map mode (due to `_store.clear()`), O(1) otherwise. Space O(1)
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
|
|
872
|
+
|
|
873
|
+
|
|
874
|
+
|
|
875
|
+
|
|
876
|
+
|
|
877
|
+
|
|
878
|
+
|
|
879
|
+
|
|
880
|
+
|
|
881
|
+
|
|
882
|
+
|
|
883
|
+
|
|
884
|
+
|
|
885
|
+
|
|
886
|
+
|
|
887
|
+
|
|
888
|
+
|
|
889
|
+
|
|
890
|
+
|
|
891
|
+
|
|
892
|
+
|
|
893
|
+
|
|
894
|
+
|
|
895
|
+
|
|
896
|
+
|
|
897
|
+
|
|
898
|
+
* @example
|
|
899
|
+
* // Remove all nodes
|
|
900
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
901
|
+
* tree.clear();
|
|
902
|
+
* console.log(tree.isEmpty()); // true;
|
|
535
903
|
*/
|
|
536
904
|
clear(): void;
|
|
537
905
|
/**
|
|
@@ -539,6 +907,38 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
539
907
|
* @remarks Time O(1), Space O(1)
|
|
540
908
|
*
|
|
541
909
|
* @returns True if the tree has no nodes, false otherwise.
|
|
910
|
+
|
|
911
|
+
|
|
912
|
+
|
|
913
|
+
|
|
914
|
+
|
|
915
|
+
|
|
916
|
+
|
|
917
|
+
|
|
918
|
+
|
|
919
|
+
|
|
920
|
+
|
|
921
|
+
|
|
922
|
+
|
|
923
|
+
|
|
924
|
+
|
|
925
|
+
|
|
926
|
+
|
|
927
|
+
|
|
928
|
+
|
|
929
|
+
|
|
930
|
+
|
|
931
|
+
|
|
932
|
+
|
|
933
|
+
|
|
934
|
+
|
|
935
|
+
|
|
936
|
+
|
|
937
|
+
|
|
938
|
+
|
|
939
|
+
* @example
|
|
940
|
+
* // Check empty
|
|
941
|
+
* console.log(new BinaryTree().isEmpty()); // true;
|
|
542
942
|
*/
|
|
543
943
|
isEmpty(): boolean;
|
|
544
944
|
/**
|
|
@@ -556,6 +956,40 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
556
956
|
* @param [startNode=this._root] - The node to start checking from.
|
|
557
957
|
* @param [iterationType=this.iterationType] - The traversal method.
|
|
558
958
|
* @returns True if it's a valid BST, false otherwise.
|
|
959
|
+
|
|
960
|
+
|
|
961
|
+
|
|
962
|
+
|
|
963
|
+
|
|
964
|
+
|
|
965
|
+
|
|
966
|
+
|
|
967
|
+
|
|
968
|
+
|
|
969
|
+
|
|
970
|
+
|
|
971
|
+
|
|
972
|
+
|
|
973
|
+
|
|
974
|
+
|
|
975
|
+
|
|
976
|
+
|
|
977
|
+
|
|
978
|
+
|
|
979
|
+
|
|
980
|
+
|
|
981
|
+
|
|
982
|
+
|
|
983
|
+
|
|
984
|
+
|
|
985
|
+
|
|
986
|
+
|
|
987
|
+
|
|
988
|
+
* @example
|
|
989
|
+
* // Check BST property
|
|
990
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
991
|
+
* // BinaryTree doesn't guarantee BST order
|
|
992
|
+
* console.log(typeof tree.isBST()); // 'boolean';
|
|
559
993
|
*/
|
|
560
994
|
isBST(startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): boolean;
|
|
561
995
|
/**
|
|
@@ -565,6 +999,42 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
565
999
|
* @param dist - The node to find the depth of.
|
|
566
1000
|
* @param [startNode=this._root] - The node to measure depth from (defaults to root).
|
|
567
1001
|
* @returns The depth (0 if `dist` is `startNode`).
|
|
1002
|
+
|
|
1003
|
+
|
|
1004
|
+
|
|
1005
|
+
|
|
1006
|
+
|
|
1007
|
+
|
|
1008
|
+
|
|
1009
|
+
|
|
1010
|
+
|
|
1011
|
+
|
|
1012
|
+
|
|
1013
|
+
|
|
1014
|
+
|
|
1015
|
+
|
|
1016
|
+
|
|
1017
|
+
|
|
1018
|
+
|
|
1019
|
+
|
|
1020
|
+
|
|
1021
|
+
|
|
1022
|
+
|
|
1023
|
+
|
|
1024
|
+
|
|
1025
|
+
|
|
1026
|
+
|
|
1027
|
+
|
|
1028
|
+
|
|
1029
|
+
|
|
1030
|
+
|
|
1031
|
+
|
|
1032
|
+
|
|
1033
|
+
* @example
|
|
1034
|
+
* // Get depth of a node
|
|
1035
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
1036
|
+
* const node = tree.getNode(4);
|
|
1037
|
+
* console.log(tree.getDepth(node!)); // 2;
|
|
568
1038
|
*/
|
|
569
1039
|
getDepth(dist: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): number;
|
|
570
1040
|
/**
|
|
@@ -574,6 +1044,41 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
574
1044
|
* @param [startNode=this._root] - The node to start measuring from.
|
|
575
1045
|
* @param [iterationType=this.iterationType] - The traversal method.
|
|
576
1046
|
* @returns The height ( -1 for an empty tree, 0 for a single-node tree).
|
|
1047
|
+
|
|
1048
|
+
|
|
1049
|
+
|
|
1050
|
+
|
|
1051
|
+
|
|
1052
|
+
|
|
1053
|
+
|
|
1054
|
+
|
|
1055
|
+
|
|
1056
|
+
|
|
1057
|
+
|
|
1058
|
+
|
|
1059
|
+
|
|
1060
|
+
|
|
1061
|
+
|
|
1062
|
+
|
|
1063
|
+
|
|
1064
|
+
|
|
1065
|
+
|
|
1066
|
+
|
|
1067
|
+
|
|
1068
|
+
|
|
1069
|
+
|
|
1070
|
+
|
|
1071
|
+
|
|
1072
|
+
|
|
1073
|
+
|
|
1074
|
+
|
|
1075
|
+
|
|
1076
|
+
|
|
1077
|
+
|
|
1078
|
+
* @example
|
|
1079
|
+
* // Get tree height
|
|
1080
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
1081
|
+
* console.log(tree.getHeight()); // 2;
|
|
577
1082
|
*/
|
|
578
1083
|
getHeight(startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): number;
|
|
579
1084
|
/**
|
|
@@ -607,17 +1112,227 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
607
1112
|
* @returns The successor node, or null/undefined if none exists.
|
|
608
1113
|
*/
|
|
609
1114
|
getSuccessor(x?: K | BinaryTreeNode<K, V> | null): BinaryTreeNode<K, V> | null | undefined;
|
|
1115
|
+
/**
|
|
1116
|
+
* Depth-first search traversal
|
|
1117
|
+
|
|
1118
|
+
|
|
1119
|
+
|
|
1120
|
+
|
|
1121
|
+
|
|
1122
|
+
|
|
1123
|
+
|
|
1124
|
+
|
|
1125
|
+
|
|
1126
|
+
|
|
1127
|
+
|
|
1128
|
+
|
|
1129
|
+
|
|
1130
|
+
|
|
1131
|
+
|
|
1132
|
+
|
|
1133
|
+
|
|
1134
|
+
|
|
1135
|
+
|
|
1136
|
+
|
|
1137
|
+
|
|
1138
|
+
|
|
1139
|
+
|
|
1140
|
+
|
|
1141
|
+
|
|
1142
|
+
|
|
1143
|
+
|
|
1144
|
+
|
|
1145
|
+
|
|
1146
|
+
|
|
1147
|
+
|
|
1148
|
+
* @example
|
|
1149
|
+
* // Depth-first search traversal
|
|
1150
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
1151
|
+
* const inOrder = tree.dfs(node => node.key, 'IN');
|
|
1152
|
+
* console.log(inOrder); // [4, 2, 5, 1, 3];
|
|
1153
|
+
*/
|
|
610
1154
|
dfs(): (K | undefined)[];
|
|
611
1155
|
dfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, pattern?: DFSOrderPattern, onlyOne?: boolean, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
612
1156
|
dfs<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, pattern?: DFSOrderPattern, onlyOne?: boolean, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: boolean): ReturnType<C>[];
|
|
1157
|
+
/**
|
|
1158
|
+
* BinaryTree level-order traversal
|
|
1159
|
+
|
|
1160
|
+
|
|
1161
|
+
|
|
1162
|
+
|
|
1163
|
+
|
|
1164
|
+
|
|
1165
|
+
|
|
1166
|
+
|
|
1167
|
+
|
|
1168
|
+
|
|
1169
|
+
|
|
1170
|
+
|
|
1171
|
+
|
|
1172
|
+
|
|
1173
|
+
|
|
1174
|
+
|
|
1175
|
+
|
|
1176
|
+
|
|
1177
|
+
|
|
1178
|
+
|
|
1179
|
+
|
|
1180
|
+
|
|
1181
|
+
|
|
1182
|
+
|
|
1183
|
+
|
|
1184
|
+
|
|
1185
|
+
|
|
1186
|
+
|
|
1187
|
+
|
|
1188
|
+
|
|
1189
|
+
|
|
1190
|
+
* @example
|
|
1191
|
+
* // BinaryTree level-order traversal
|
|
1192
|
+
* const tree = new BinaryTree([
|
|
1193
|
+
* [1, 'one'],
|
|
1194
|
+
* [2, 'two'],
|
|
1195
|
+
* [3, 'three'],
|
|
1196
|
+
* [4, 'four'],
|
|
1197
|
+
* [5, 'five'],
|
|
1198
|
+
* [6, 'six'],
|
|
1199
|
+
* [7, 'seven']
|
|
1200
|
+
* ]);
|
|
1201
|
+
*
|
|
1202
|
+
* // Binary tree maintains level-order insertion
|
|
1203
|
+
* // Complete binary tree structure
|
|
1204
|
+
* console.log(tree.size); // 7;
|
|
1205
|
+
*
|
|
1206
|
+
* // Verify all keys are present
|
|
1207
|
+
* console.log(tree.has(1)); // true;
|
|
1208
|
+
* console.log(tree.has(4)); // true;
|
|
1209
|
+
* console.log(tree.has(7)); // true;
|
|
1210
|
+
*
|
|
1211
|
+
* // Iterate through tree
|
|
1212
|
+
* const keys: number[] = [];
|
|
1213
|
+
* for (const [key] of tree) {
|
|
1214
|
+
* keys.push(key);
|
|
1215
|
+
* }
|
|
1216
|
+
* console.log(keys.length); // 7;
|
|
1217
|
+
*/
|
|
613
1218
|
bfs(): (K | undefined)[];
|
|
614
1219
|
bfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: false): ReturnType<C>[];
|
|
615
1220
|
bfs<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: true): ReturnType<C>[];
|
|
1221
|
+
/**
|
|
1222
|
+
* Get leaf nodes
|
|
1223
|
+
|
|
1224
|
+
|
|
1225
|
+
|
|
1226
|
+
|
|
1227
|
+
|
|
1228
|
+
|
|
1229
|
+
|
|
1230
|
+
|
|
1231
|
+
|
|
1232
|
+
|
|
1233
|
+
|
|
1234
|
+
|
|
1235
|
+
|
|
1236
|
+
|
|
1237
|
+
|
|
1238
|
+
|
|
1239
|
+
|
|
1240
|
+
|
|
1241
|
+
|
|
1242
|
+
|
|
1243
|
+
|
|
1244
|
+
|
|
1245
|
+
|
|
1246
|
+
|
|
1247
|
+
|
|
1248
|
+
|
|
1249
|
+
|
|
1250
|
+
|
|
1251
|
+
|
|
1252
|
+
* @example
|
|
1253
|
+
* // Get leaf nodes
|
|
1254
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
1255
|
+
* const leafKeys = tree.leaves(node => node.key);
|
|
1256
|
+
* console.log(leafKeys.length); // > 0;
|
|
1257
|
+
*/
|
|
616
1258
|
leaves(): (K | undefined)[];
|
|
617
1259
|
leaves<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
1260
|
+
/**
|
|
1261
|
+
* Level-order grouping
|
|
1262
|
+
|
|
1263
|
+
|
|
1264
|
+
|
|
1265
|
+
|
|
1266
|
+
|
|
1267
|
+
|
|
1268
|
+
|
|
1269
|
+
|
|
1270
|
+
|
|
1271
|
+
|
|
1272
|
+
|
|
1273
|
+
|
|
1274
|
+
|
|
1275
|
+
|
|
1276
|
+
|
|
1277
|
+
|
|
1278
|
+
|
|
1279
|
+
|
|
1280
|
+
|
|
1281
|
+
|
|
1282
|
+
|
|
1283
|
+
|
|
1284
|
+
|
|
1285
|
+
|
|
1286
|
+
|
|
1287
|
+
|
|
1288
|
+
|
|
1289
|
+
|
|
1290
|
+
* @example
|
|
1291
|
+
* // Level-order grouping
|
|
1292
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
1293
|
+
* const levels = tree.listLevels(node => node.key);
|
|
1294
|
+
* console.log(levels[0]); // [1];
|
|
1295
|
+
* console.log(levels[1].sort()); // [2, 3];
|
|
1296
|
+
*/
|
|
618
1297
|
listLevels(): (K | undefined)[][];
|
|
619
1298
|
listLevels<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: false): ReturnType<C>[][];
|
|
620
1299
|
listLevels<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: true): ReturnType<C>[][];
|
|
1300
|
+
/**
|
|
1301
|
+
* Morris traversal (O(1) space)
|
|
1302
|
+
|
|
1303
|
+
|
|
1304
|
+
|
|
1305
|
+
|
|
1306
|
+
|
|
1307
|
+
|
|
1308
|
+
|
|
1309
|
+
|
|
1310
|
+
|
|
1311
|
+
|
|
1312
|
+
|
|
1313
|
+
|
|
1314
|
+
|
|
1315
|
+
|
|
1316
|
+
|
|
1317
|
+
|
|
1318
|
+
|
|
1319
|
+
|
|
1320
|
+
|
|
1321
|
+
|
|
1322
|
+
|
|
1323
|
+
|
|
1324
|
+
|
|
1325
|
+
|
|
1326
|
+
|
|
1327
|
+
|
|
1328
|
+
|
|
1329
|
+
|
|
1330
|
+
* @example
|
|
1331
|
+
* // Morris traversal (O(1) space)
|
|
1332
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
1333
|
+
* const result = tree.morris(node => node.key, 'IN');
|
|
1334
|
+
* console.log(result.length); // 3;
|
|
1335
|
+
*/
|
|
621
1336
|
morris(): (K | undefined)[];
|
|
622
1337
|
morris<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, pattern?: DFSOrderPattern, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): ReturnType<C>[];
|
|
623
1338
|
/**
|
|
@@ -625,6 +1340,41 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
625
1340
|
* @remarks Time O(N * M), where N is the number of nodes and M is the tree size during insertion (due to `bfs` + `set`, and `set` is O(M)). Space O(N) for the new tree and the BFS queue.
|
|
626
1341
|
*
|
|
627
1342
|
* @returns A new, cloned instance of the tree.
|
|
1343
|
+
|
|
1344
|
+
|
|
1345
|
+
|
|
1346
|
+
|
|
1347
|
+
|
|
1348
|
+
|
|
1349
|
+
|
|
1350
|
+
|
|
1351
|
+
|
|
1352
|
+
|
|
1353
|
+
|
|
1354
|
+
|
|
1355
|
+
|
|
1356
|
+
|
|
1357
|
+
|
|
1358
|
+
|
|
1359
|
+
|
|
1360
|
+
|
|
1361
|
+
|
|
1362
|
+
|
|
1363
|
+
|
|
1364
|
+
|
|
1365
|
+
|
|
1366
|
+
|
|
1367
|
+
|
|
1368
|
+
|
|
1369
|
+
|
|
1370
|
+
|
|
1371
|
+
|
|
1372
|
+
* @example
|
|
1373
|
+
* // Deep copy
|
|
1374
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
1375
|
+
* const copy = tree.clone();
|
|
1376
|
+
* copy.delete(1);
|
|
1377
|
+
* console.log(tree.has(1)); // true;
|
|
628
1378
|
*/
|
|
629
1379
|
clone(): this;
|
|
630
1380
|
/**
|
|
@@ -634,6 +1384,40 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
634
1384
|
* @param predicate - A function to test each [key, value] pair.
|
|
635
1385
|
* @param [thisArg] - `this` context for the predicate.
|
|
636
1386
|
* @returns A new, filtered tree.
|
|
1387
|
+
|
|
1388
|
+
|
|
1389
|
+
|
|
1390
|
+
|
|
1391
|
+
|
|
1392
|
+
|
|
1393
|
+
|
|
1394
|
+
|
|
1395
|
+
|
|
1396
|
+
|
|
1397
|
+
|
|
1398
|
+
|
|
1399
|
+
|
|
1400
|
+
|
|
1401
|
+
|
|
1402
|
+
|
|
1403
|
+
|
|
1404
|
+
|
|
1405
|
+
|
|
1406
|
+
|
|
1407
|
+
|
|
1408
|
+
|
|
1409
|
+
|
|
1410
|
+
|
|
1411
|
+
|
|
1412
|
+
|
|
1413
|
+
|
|
1414
|
+
|
|
1415
|
+
|
|
1416
|
+
* @example
|
|
1417
|
+
* // Filter nodes by condition
|
|
1418
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4]);
|
|
1419
|
+
* const result = tree.filter((_, key) => key > 2);
|
|
1420
|
+
* console.log(result.size); // 2;
|
|
637
1421
|
*/
|
|
638
1422
|
filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: unknown): this;
|
|
639
1423
|
/**
|
|
@@ -647,6 +1431,40 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
647
1431
|
* @param [options] - Options for the new tree.
|
|
648
1432
|
* @param [thisArg] - `this` context for the callback.
|
|
649
1433
|
* @returns A new, mapped tree.
|
|
1434
|
+
|
|
1435
|
+
|
|
1436
|
+
|
|
1437
|
+
|
|
1438
|
+
|
|
1439
|
+
|
|
1440
|
+
|
|
1441
|
+
|
|
1442
|
+
|
|
1443
|
+
|
|
1444
|
+
|
|
1445
|
+
|
|
1446
|
+
|
|
1447
|
+
|
|
1448
|
+
|
|
1449
|
+
|
|
1450
|
+
|
|
1451
|
+
|
|
1452
|
+
|
|
1453
|
+
|
|
1454
|
+
|
|
1455
|
+
|
|
1456
|
+
|
|
1457
|
+
|
|
1458
|
+
|
|
1459
|
+
|
|
1460
|
+
|
|
1461
|
+
|
|
1462
|
+
|
|
1463
|
+
* @example
|
|
1464
|
+
* // Transform to new tree
|
|
1465
|
+
* const tree = new BinaryTree<number, number>([[1, 10], [2, 20]]);
|
|
1466
|
+
* const mapped = tree.map((v, key) => [key, (v ?? 0) + 1] as [number, number]);
|
|
1467
|
+
* console.log([...mapped.values()]); // contains 11;
|
|
650
1468
|
*/
|
|
651
1469
|
map<MK = K, MV = V, MR = any>(cb: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<BinaryTreeOptions<MK, MV, MR>>, thisArg?: unknown): BinaryTree<MK, MV, MR>;
|
|
652
1470
|
/**
|
|
@@ -664,6 +1482,39 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
664
1482
|
*
|
|
665
1483
|
* @param [options] - Options to control the output.
|
|
666
1484
|
* @param [startNode=this._root] - The node to start printing from.
|
|
1485
|
+
|
|
1486
|
+
|
|
1487
|
+
|
|
1488
|
+
|
|
1489
|
+
|
|
1490
|
+
|
|
1491
|
+
|
|
1492
|
+
|
|
1493
|
+
|
|
1494
|
+
|
|
1495
|
+
|
|
1496
|
+
|
|
1497
|
+
|
|
1498
|
+
|
|
1499
|
+
|
|
1500
|
+
|
|
1501
|
+
|
|
1502
|
+
|
|
1503
|
+
|
|
1504
|
+
|
|
1505
|
+
|
|
1506
|
+
|
|
1507
|
+
|
|
1508
|
+
|
|
1509
|
+
|
|
1510
|
+
|
|
1511
|
+
|
|
1512
|
+
|
|
1513
|
+
|
|
1514
|
+
* @example
|
|
1515
|
+
* // Display tree
|
|
1516
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
1517
|
+
* expect(() => tree.print()).not.toThrow();
|
|
667
1518
|
*/
|
|
668
1519
|
print(options?: BinaryTreePrintOptions, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): void;
|
|
669
1520
|
protected _dfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback: C, pattern?: DFSOrderPattern, onlyOne?: boolean, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: boolean, shouldVisitLeft?: (node: BinaryTreeNode<K, V> | null | undefined) => boolean, shouldVisitRight?: (node: BinaryTreeNode<K, V> | null | undefined) => boolean, shouldVisitRoot?: (node: BinaryTreeNode<K, V> | null | undefined) => boolean, shouldProcessRoot?: (node: BinaryTreeNode<K, V> | null | undefined) => boolean): ReturnType<C>[];
|
|
@@ -785,7 +1636,7 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
785
1636
|
* @param p - The item to check.
|
|
786
1637
|
* @returns True if it's a function.
|
|
787
1638
|
*/
|
|
788
|
-
protected _isPredicate(p:
|
|
1639
|
+
protected _isPredicate(p: unknown): p is NodePredicate<BinaryTreeNode<K, V>>;
|
|
789
1640
|
/**
|
|
790
1641
|
* (Protected) Extracts the key from a key, node, or entry.
|
|
791
1642
|
* @remarks Time O(1)
|