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
|
@@ -0,0 +1,4647 @@
|
|
|
1
|
+
[**data-structure-typed**](../README.md)
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
[data-structure-typed](../README.md) / BinaryTree
|
|
6
|
+
|
|
7
|
+
# Class: BinaryTree\<K, V, R\>
|
|
8
|
+
|
|
9
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:270](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L270)
|
|
10
|
+
|
|
11
|
+
A general Binary Tree implementation.
|
|
12
|
+
|
|
13
|
+
## Remarks
|
|
14
|
+
|
|
15
|
+
This class implements a basic Binary Tree, not a Binary Search Tree.
|
|
16
|
+
The `set` operation inserts nodes level-by-level (BFS) into the first available slot.
|
|
17
|
+
|
|
18
|
+
## Examples
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
// determine loan approval using a decision tree
|
|
22
|
+
// Decision tree structure
|
|
23
|
+
const loanDecisionTree = new BinaryTree<string>(
|
|
24
|
+
['stableIncome', 'goodCredit', 'Rejected', 'Approved', 'Rejected'],
|
|
25
|
+
{ isDuplicate: true }
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
function determineLoanApproval(
|
|
29
|
+
node?: BinaryTreeNode<string> | null,
|
|
30
|
+
conditions?: { [key: string]: boolean }
|
|
31
|
+
): string {
|
|
32
|
+
if (!node) raise(Error, 'Invalid node');
|
|
33
|
+
|
|
34
|
+
// If it's a leaf node, return the decision result
|
|
35
|
+
if (!node.left && !node.right) return node.key;
|
|
36
|
+
|
|
37
|
+
// Check if a valid condition exists for the current node's key
|
|
38
|
+
return conditions?.[node.key]
|
|
39
|
+
? determineLoanApproval(node.left, conditions)
|
|
40
|
+
: determineLoanApproval(node.right, conditions);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Test case 1: Stable income and good credit score
|
|
44
|
+
console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: true })); // 'Approved';
|
|
45
|
+
|
|
46
|
+
// Test case 2: Stable income but poor credit score
|
|
47
|
+
console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: false })); // 'Rejected';
|
|
48
|
+
|
|
49
|
+
// Test case 3: No stable income
|
|
50
|
+
console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: true })); // 'Rejected';
|
|
51
|
+
|
|
52
|
+
// Test case 4: No stable income and poor credit score
|
|
53
|
+
console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: false })); // 'Rejected';
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
// evaluate the arithmetic expression represented by the binary tree
|
|
58
|
+
const expressionTree = new BinaryTree<number | string>(['+', 3, '*', null, null, 5, '-', null, null, 2, 8]);
|
|
59
|
+
|
|
60
|
+
function evaluate(node?: BinaryTreeNode<number | string> | null): number {
|
|
61
|
+
if (!node) return 0;
|
|
62
|
+
|
|
63
|
+
if (typeof node.key === 'number') return node.key;
|
|
64
|
+
|
|
65
|
+
const leftValue = evaluate(node.left); // Evaluate the left subtree
|
|
66
|
+
const rightValue = evaluate(node.right); // Evaluate the right subtree
|
|
67
|
+
|
|
68
|
+
// Perform the operation based on the current node's operator
|
|
69
|
+
switch (node.key) {
|
|
70
|
+
case '+':
|
|
71
|
+
return leftValue + rightValue;
|
|
72
|
+
case '-':
|
|
73
|
+
return leftValue - rightValue;
|
|
74
|
+
case '*':
|
|
75
|
+
return leftValue * rightValue;
|
|
76
|
+
case '/':
|
|
77
|
+
return rightValue !== 0 ? leftValue / rightValue : 0; // Handle division by zero
|
|
78
|
+
default:
|
|
79
|
+
raise(Error, `Unsupported operator: ${node.key}`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
console.log(evaluate(expressionTree.root)); // -27;
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Extends
|
|
87
|
+
|
|
88
|
+
- [`IterableEntryBase`](IterableEntryBase.md)\<`K`, `V` \| `undefined`\>
|
|
89
|
+
|
|
90
|
+
## Extended by
|
|
91
|
+
|
|
92
|
+
- [`BST`](BST.md)
|
|
93
|
+
|
|
94
|
+
## Type Parameters
|
|
95
|
+
|
|
96
|
+
### K
|
|
97
|
+
|
|
98
|
+
`K` = `any`
|
|
99
|
+
|
|
100
|
+
The type of the key.
|
|
101
|
+
|
|
102
|
+
### V
|
|
103
|
+
|
|
104
|
+
`V` = `any`
|
|
105
|
+
|
|
106
|
+
The type of the value.
|
|
107
|
+
|
|
108
|
+
### R
|
|
109
|
+
|
|
110
|
+
`R` = `any`
|
|
111
|
+
|
|
112
|
+
The type of the raw data object (if using `toEntryFn`).
|
|
113
|
+
1. Two Children Maximum: Each node has at most two children.
|
|
114
|
+
2. Left and Right Children: Nodes have distinct left and right children.
|
|
115
|
+
3. Depth and Height: Depth is the number of edges from the root to a node; height is the maximum depth in the tree.
|
|
116
|
+
4. Subtrees: Each child of a node forms the root of a subtree.
|
|
117
|
+
5. Leaf Nodes: Nodes without children are leaves.
|
|
118
|
+
|
|
119
|
+
## Implements
|
|
120
|
+
|
|
121
|
+
- `IBinaryTree`\<`K`, `V`, `R`\>
|
|
122
|
+
|
|
123
|
+
## Constructors
|
|
124
|
+
|
|
125
|
+
### Constructor
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
new BinaryTree<K, V, R>(keysNodesEntriesOrRaws?, options?): BinaryTree<K, V, R>;
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:283](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L283)
|
|
132
|
+
|
|
133
|
+
Creates an instance of BinaryTree.
|
|
134
|
+
|
|
135
|
+
#### Parameters
|
|
136
|
+
|
|
137
|
+
##### keysNodesEntriesOrRaws?
|
|
138
|
+
|
|
139
|
+
`Iterable`\<
|
|
140
|
+
\| `K`
|
|
141
|
+
\| `R`
|
|
142
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
143
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
144
|
+
\| `null`
|
|
145
|
+
\| `undefined`\> = `[]`
|
|
146
|
+
|
|
147
|
+
An iterable of items to set.
|
|
148
|
+
|
|
149
|
+
##### options?
|
|
150
|
+
|
|
151
|
+
`BinaryTreeOptions`\<`K`, `V`, `R`\>
|
|
152
|
+
|
|
153
|
+
Configuration options for the tree.
|
|
154
|
+
|
|
155
|
+
#### Returns
|
|
156
|
+
|
|
157
|
+
`BinaryTree`\<`K`, `V`, `R`\>
|
|
158
|
+
|
|
159
|
+
#### Remarks
|
|
160
|
+
|
|
161
|
+
Time O(N * M), where N is the number of items in `keysNodesEntriesOrRaws` and M is the tree size at insertion time (due to O(M) `set` operation). Space O(N) for storing the nodes.
|
|
162
|
+
|
|
163
|
+
#### Overrides
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
IterableEntryBase<K, V | undefined>.constructor
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## Properties
|
|
170
|
+
|
|
171
|
+
### isDuplicate
|
|
172
|
+
|
|
173
|
+
#### Get Signature
|
|
174
|
+
|
|
175
|
+
```ts
|
|
176
|
+
get isDuplicate(): boolean;
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:322](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L322)
|
|
180
|
+
|
|
181
|
+
Gets whether the tree allows duplicate keys.
|
|
182
|
+
|
|
183
|
+
##### Remarks
|
|
184
|
+
|
|
185
|
+
Time O(1)
|
|
186
|
+
|
|
187
|
+
##### Returns
|
|
188
|
+
|
|
189
|
+
`boolean`
|
|
190
|
+
|
|
191
|
+
True if duplicates are allowed, false otherwise.
|
|
192
|
+
|
|
193
|
+
#### Implementation of
|
|
194
|
+
|
|
195
|
+
```ts
|
|
196
|
+
IBinaryTree.isDuplicate
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
***
|
|
200
|
+
|
|
201
|
+
### isMapMode
|
|
202
|
+
|
|
203
|
+
#### Get Signature
|
|
204
|
+
|
|
205
|
+
```ts
|
|
206
|
+
get isMapMode(): boolean;
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:310](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L310)
|
|
210
|
+
|
|
211
|
+
Gets whether the tree is in Map mode.
|
|
212
|
+
|
|
213
|
+
##### Remarks
|
|
214
|
+
|
|
215
|
+
In Map mode (default), values are stored in an external Map, and nodes only hold keys. If false, values are stored directly on the nodes. Time O(1)
|
|
216
|
+
|
|
217
|
+
##### Returns
|
|
218
|
+
|
|
219
|
+
`boolean`
|
|
220
|
+
|
|
221
|
+
True if in Map mode, false otherwise.
|
|
222
|
+
|
|
223
|
+
#### Implementation of
|
|
224
|
+
|
|
225
|
+
```ts
|
|
226
|
+
IBinaryTree.isMapMode
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
***
|
|
230
|
+
|
|
231
|
+
### NIL
|
|
232
|
+
|
|
233
|
+
#### Get Signature
|
|
234
|
+
|
|
235
|
+
```ts
|
|
236
|
+
get NIL(): BinaryTreeNode<K, V>;
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:373](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L373)
|
|
240
|
+
|
|
241
|
+
Gets the sentinel NIL node (used in self-balancing trees like Red-Black Tree).
|
|
242
|
+
|
|
243
|
+
##### Remarks
|
|
244
|
+
|
|
245
|
+
Time O(1)
|
|
246
|
+
|
|
247
|
+
##### Returns
|
|
248
|
+
|
|
249
|
+
[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
250
|
+
|
|
251
|
+
The NIL node.
|
|
252
|
+
|
|
253
|
+
#### Implementation of
|
|
254
|
+
|
|
255
|
+
```ts
|
|
256
|
+
IBinaryTree.NIL
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
***
|
|
260
|
+
|
|
261
|
+
### root
|
|
262
|
+
|
|
263
|
+
#### Get Signature
|
|
264
|
+
|
|
265
|
+
```ts
|
|
266
|
+
get root(): BinaryTreeNode<K, V> | null | undefined;
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:349](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L349)
|
|
270
|
+
|
|
271
|
+
Gets the root node of the tree.
|
|
272
|
+
|
|
273
|
+
##### Remarks
|
|
274
|
+
|
|
275
|
+
Time O(1)
|
|
276
|
+
|
|
277
|
+
##### Returns
|
|
278
|
+
|
|
279
|
+
[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\> \| `null` \| `undefined`
|
|
280
|
+
|
|
281
|
+
The root node.
|
|
282
|
+
|
|
283
|
+
#### Implementation of
|
|
284
|
+
|
|
285
|
+
```ts
|
|
286
|
+
IBinaryTree.root
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
***
|
|
290
|
+
|
|
291
|
+
### size
|
|
292
|
+
|
|
293
|
+
#### Get Signature
|
|
294
|
+
|
|
295
|
+
```ts
|
|
296
|
+
get size(): number;
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:361](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L361)
|
|
300
|
+
|
|
301
|
+
Gets the number of nodes in the tree.
|
|
302
|
+
|
|
303
|
+
##### Remarks
|
|
304
|
+
|
|
305
|
+
Time O(1)
|
|
306
|
+
|
|
307
|
+
##### Returns
|
|
308
|
+
|
|
309
|
+
`number`
|
|
310
|
+
|
|
311
|
+
The size of the tree.
|
|
312
|
+
|
|
313
|
+
#### Implementation of
|
|
314
|
+
|
|
315
|
+
```ts
|
|
316
|
+
IBinaryTree.size
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
#### Overrides
|
|
320
|
+
|
|
321
|
+
[`IterableEntryBase`](IterableEntryBase.md).[`size`](IterableEntryBase.md#size)
|
|
322
|
+
|
|
323
|
+
***
|
|
324
|
+
|
|
325
|
+
### store
|
|
326
|
+
|
|
327
|
+
#### Get Signature
|
|
328
|
+
|
|
329
|
+
```ts
|
|
330
|
+
get store(): Map<K, BinaryTreeNode<K, V>>;
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:337](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L337)
|
|
334
|
+
|
|
335
|
+
Gets the external value store (used in Map mode).
|
|
336
|
+
|
|
337
|
+
##### Remarks
|
|
338
|
+
|
|
339
|
+
Time O(1)
|
|
340
|
+
|
|
341
|
+
##### Returns
|
|
342
|
+
|
|
343
|
+
`Map`\<`K`, [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>\>
|
|
344
|
+
|
|
345
|
+
The map storing key-value pairs.
|
|
346
|
+
|
|
347
|
+
#### Implementation of
|
|
348
|
+
|
|
349
|
+
```ts
|
|
350
|
+
IBinaryTree.store
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
***
|
|
354
|
+
|
|
355
|
+
### toEntryFn
|
|
356
|
+
|
|
357
|
+
#### Get Signature
|
|
358
|
+
|
|
359
|
+
```ts
|
|
360
|
+
get toEntryFn(): ToEntryFn<K, V, R> | undefined;
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:385](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L385)
|
|
364
|
+
|
|
365
|
+
Gets the function used to convert raw data objects (R) into [key, value] entries.
|
|
366
|
+
|
|
367
|
+
##### Remarks
|
|
368
|
+
|
|
369
|
+
Time O(1)
|
|
370
|
+
|
|
371
|
+
##### Returns
|
|
372
|
+
|
|
373
|
+
`ToEntryFn`\<`K`, `V`, `R`\> \| `undefined`
|
|
374
|
+
|
|
375
|
+
The conversion function.
|
|
376
|
+
|
|
377
|
+
#### Implementation of
|
|
378
|
+
|
|
379
|
+
```ts
|
|
380
|
+
IBinaryTree.toEntryFn
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
## Methods
|
|
384
|
+
|
|
385
|
+
### \[iterator\]()
|
|
386
|
+
|
|
387
|
+
```ts
|
|
388
|
+
iterator: IterableIterator<[K, V | undefined]>;
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
Defined in: [data-structures/base/iterable-entry-base.ts:22](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-entry-base.ts#L22)
|
|
392
|
+
|
|
393
|
+
Default iterator yielding `[key, value]` entries.
|
|
394
|
+
|
|
395
|
+
#### Parameters
|
|
396
|
+
|
|
397
|
+
##### args
|
|
398
|
+
|
|
399
|
+
...`unknown`[]
|
|
400
|
+
|
|
401
|
+
#### Returns
|
|
402
|
+
|
|
403
|
+
`IterableIterator`\<\[`K`, `V` \| `undefined`\]\>
|
|
404
|
+
|
|
405
|
+
Iterator of `[K, V]`.
|
|
406
|
+
|
|
407
|
+
#### Remarks
|
|
408
|
+
|
|
409
|
+
Time O(n) to iterate, Space O(1)
|
|
410
|
+
|
|
411
|
+
#### Implementation of
|
|
412
|
+
|
|
413
|
+
```ts
|
|
414
|
+
IBinaryTree.[iterator]
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
#### Inherited from
|
|
418
|
+
|
|
419
|
+
[`IterableEntryBase`](IterableEntryBase.md).[`[iterator]`](IterableEntryBase.md#iterator)
|
|
420
|
+
|
|
421
|
+
***
|
|
422
|
+
|
|
423
|
+
### add()
|
|
424
|
+
|
|
425
|
+
```ts
|
|
426
|
+
add(keyNodeOrEntry): boolean;
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:608](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L608)
|
|
430
|
+
|
|
431
|
+
Adds a new node to the tree.
|
|
432
|
+
|
|
433
|
+
#### Parameters
|
|
434
|
+
|
|
435
|
+
##### keyNodeOrEntry
|
|
436
|
+
|
|
437
|
+
\| `K`
|
|
438
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
439
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
440
|
+
\| `null`
|
|
441
|
+
\| `undefined`
|
|
442
|
+
|
|
443
|
+
The key, node, or entry to add.
|
|
444
|
+
|
|
445
|
+
#### Returns
|
|
446
|
+
|
|
447
|
+
`boolean`
|
|
448
|
+
|
|
449
|
+
True if the addition was successful, false otherwise.
|
|
450
|
+
|
|
451
|
+
*
|
|
452
|
+
|
|
453
|
+
#### Remarks
|
|
454
|
+
|
|
455
|
+
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).
|
|
456
|
+
|
|
457
|
+
#### Example
|
|
458
|
+
|
|
459
|
+
```ts
|
|
460
|
+
// Add a single node
|
|
461
|
+
const tree = new BinaryTree<number>();
|
|
462
|
+
tree.add(1);
|
|
463
|
+
tree.add(2);
|
|
464
|
+
tree.add(3);
|
|
465
|
+
console.log(tree.size); // 3;
|
|
466
|
+
console.log(tree.has(1)); // true;
|
|
467
|
+
```
|
|
468
|
+
|
|
469
|
+
#### Implementation of
|
|
470
|
+
|
|
471
|
+
```ts
|
|
472
|
+
IBinaryTree.add
|
|
473
|
+
```
|
|
474
|
+
|
|
475
|
+
***
|
|
476
|
+
|
|
477
|
+
### addMany()
|
|
478
|
+
|
|
479
|
+
```ts
|
|
480
|
+
addMany(keysNodesEntriesOrRaws): boolean[];
|
|
481
|
+
```
|
|
482
|
+
|
|
483
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:781](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L781)
|
|
484
|
+
|
|
485
|
+
Adds multiple items to the tree.
|
|
486
|
+
|
|
487
|
+
#### Parameters
|
|
488
|
+
|
|
489
|
+
##### keysNodesEntriesOrRaws
|
|
490
|
+
|
|
491
|
+
`Iterable`\<
|
|
492
|
+
\| `K`
|
|
493
|
+
\| `R`
|
|
494
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
495
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
496
|
+
\| `null`
|
|
497
|
+
\| `undefined`\>
|
|
498
|
+
|
|
499
|
+
An iterable of items to set.
|
|
500
|
+
|
|
501
|
+
#### Returns
|
|
502
|
+
|
|
503
|
+
`boolean`[]
|
|
504
|
+
|
|
505
|
+
An array of booleans indicating the success of each individual `set` operation.
|
|
506
|
+
|
|
507
|
+
*
|
|
508
|
+
|
|
509
|
+
#### Remarks
|
|
510
|
+
|
|
511
|
+
Time O(N * M), where N is the number of items to set and M is the size of the tree at insertion (due to O(M) `set` operation). Space O(M) (from `set`) + O(N) (for the `inserted` array).
|
|
512
|
+
|
|
513
|
+
#### Example
|
|
514
|
+
|
|
515
|
+
```ts
|
|
516
|
+
// Bulk add
|
|
517
|
+
const tree = new BinaryTree<number>();
|
|
518
|
+
tree.addMany([1, 2, 3, 4, 5]);
|
|
519
|
+
console.log(tree.size); // 5;
|
|
520
|
+
```
|
|
521
|
+
|
|
522
|
+
#### Implementation of
|
|
523
|
+
|
|
524
|
+
```ts
|
|
525
|
+
IBinaryTree.addMany
|
|
526
|
+
```
|
|
527
|
+
|
|
528
|
+
***
|
|
529
|
+
|
|
530
|
+
### bfs()
|
|
531
|
+
|
|
532
|
+
Performs a Breadth-First Search (BFS) or Level-Order traversal.
|
|
533
|
+
|
|
534
|
+
#### Remarks
|
|
535
|
+
|
|
536
|
+
Time O(N), visits every node. Space O(N) in the worst case for the queue (e.g., a full last level).
|
|
537
|
+
|
|
538
|
+
#### Template
|
|
539
|
+
|
|
540
|
+
The type of the callback function.
|
|
541
|
+
|
|
542
|
+
#### Param
|
|
543
|
+
|
|
544
|
+
Function to call on each node.
|
|
545
|
+
|
|
546
|
+
#### Param
|
|
547
|
+
|
|
548
|
+
The node to start from.
|
|
549
|
+
|
|
550
|
+
#### Param
|
|
551
|
+
|
|
552
|
+
The traversal method ('RECURSIVE' BFS is less common but supported here).
|
|
553
|
+
|
|
554
|
+
#### Param
|
|
555
|
+
|
|
556
|
+
If true, includes null nodes in the traversal.
|
|
557
|
+
|
|
558
|
+
#### Call Signature
|
|
559
|
+
|
|
560
|
+
```ts
|
|
561
|
+
bfs(): (K | undefined)[];
|
|
562
|
+
```
|
|
563
|
+
|
|
564
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:2191](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L2191)
|
|
565
|
+
|
|
566
|
+
BinaryTree level-order traversal
|
|
567
|
+
|
|
568
|
+
*
|
|
569
|
+
|
|
570
|
+
##### Returns
|
|
571
|
+
|
|
572
|
+
(`K` \| `undefined`)[]
|
|
573
|
+
|
|
574
|
+
##### Example
|
|
575
|
+
|
|
576
|
+
```ts
|
|
577
|
+
// BinaryTree level-order traversal
|
|
578
|
+
const tree = new BinaryTree([
|
|
579
|
+
[1, 'one'],
|
|
580
|
+
[2, 'two'],
|
|
581
|
+
[3, 'three'],
|
|
582
|
+
[4, 'four'],
|
|
583
|
+
[5, 'five'],
|
|
584
|
+
[6, 'six'],
|
|
585
|
+
[7, 'seven']
|
|
586
|
+
]);
|
|
587
|
+
|
|
588
|
+
// Binary tree maintains level-order insertion
|
|
589
|
+
// Complete binary tree structure
|
|
590
|
+
console.log(tree.size); // 7;
|
|
591
|
+
|
|
592
|
+
// Verify all keys are present
|
|
593
|
+
console.log(tree.has(1)); // true;
|
|
594
|
+
console.log(tree.has(4)); // true;
|
|
595
|
+
console.log(tree.has(7)); // true;
|
|
596
|
+
|
|
597
|
+
// Iterate through tree
|
|
598
|
+
const keys: number[] = [];
|
|
599
|
+
for (const [key] of tree) {
|
|
600
|
+
keys.push(key);
|
|
601
|
+
}
|
|
602
|
+
console.log(keys.length); // 7;
|
|
603
|
+
```
|
|
604
|
+
|
|
605
|
+
##### Implementation of
|
|
606
|
+
|
|
607
|
+
```ts
|
|
608
|
+
IBinaryTree.bfs
|
|
609
|
+
```
|
|
610
|
+
|
|
611
|
+
#### Call Signature
|
|
612
|
+
|
|
613
|
+
```ts
|
|
614
|
+
bfs<C>(
|
|
615
|
+
callback?,
|
|
616
|
+
startNode?,
|
|
617
|
+
iterationType?,
|
|
618
|
+
includeNull?): ReturnType<C>[];
|
|
619
|
+
```
|
|
620
|
+
|
|
621
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:2193](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L2193)
|
|
622
|
+
|
|
623
|
+
BinaryTree level-order traversal
|
|
624
|
+
|
|
625
|
+
*
|
|
626
|
+
|
|
627
|
+
##### Type Parameters
|
|
628
|
+
|
|
629
|
+
###### C
|
|
630
|
+
|
|
631
|
+
`C` *extends* `NodeCallback`\<[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>\>
|
|
632
|
+
|
|
633
|
+
##### Parameters
|
|
634
|
+
|
|
635
|
+
###### callback?
|
|
636
|
+
|
|
637
|
+
`C`
|
|
638
|
+
|
|
639
|
+
###### startNode?
|
|
640
|
+
|
|
641
|
+
\| `K`
|
|
642
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
643
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
644
|
+
\| `null`
|
|
645
|
+
|
|
646
|
+
###### iterationType?
|
|
647
|
+
|
|
648
|
+
`IterationType`
|
|
649
|
+
|
|
650
|
+
###### includeNull?
|
|
651
|
+
|
|
652
|
+
`false`
|
|
653
|
+
|
|
654
|
+
##### Returns
|
|
655
|
+
|
|
656
|
+
`ReturnType`\<`C`\>[]
|
|
657
|
+
|
|
658
|
+
##### Example
|
|
659
|
+
|
|
660
|
+
```ts
|
|
661
|
+
// BinaryTree level-order traversal
|
|
662
|
+
const tree = new BinaryTree([
|
|
663
|
+
[1, 'one'],
|
|
664
|
+
[2, 'two'],
|
|
665
|
+
[3, 'three'],
|
|
666
|
+
[4, 'four'],
|
|
667
|
+
[5, 'five'],
|
|
668
|
+
[6, 'six'],
|
|
669
|
+
[7, 'seven']
|
|
670
|
+
]);
|
|
671
|
+
|
|
672
|
+
// Binary tree maintains level-order insertion
|
|
673
|
+
// Complete binary tree structure
|
|
674
|
+
console.log(tree.size); // 7;
|
|
675
|
+
|
|
676
|
+
// Verify all keys are present
|
|
677
|
+
console.log(tree.has(1)); // true;
|
|
678
|
+
console.log(tree.has(4)); // true;
|
|
679
|
+
console.log(tree.has(7)); // true;
|
|
680
|
+
|
|
681
|
+
// Iterate through tree
|
|
682
|
+
const keys: number[] = [];
|
|
683
|
+
for (const [key] of tree) {
|
|
684
|
+
keys.push(key);
|
|
685
|
+
}
|
|
686
|
+
console.log(keys.length); // 7;
|
|
687
|
+
```
|
|
688
|
+
|
|
689
|
+
##### Implementation of
|
|
690
|
+
|
|
691
|
+
```ts
|
|
692
|
+
IBinaryTree.bfs
|
|
693
|
+
```
|
|
694
|
+
|
|
695
|
+
#### Call Signature
|
|
696
|
+
|
|
697
|
+
```ts
|
|
698
|
+
bfs<C>(
|
|
699
|
+
callback?,
|
|
700
|
+
startNode?,
|
|
701
|
+
iterationType?,
|
|
702
|
+
includeNull?): ReturnType<C>[];
|
|
703
|
+
```
|
|
704
|
+
|
|
705
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:2200](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L2200)
|
|
706
|
+
|
|
707
|
+
BinaryTree level-order traversal
|
|
708
|
+
|
|
709
|
+
*
|
|
710
|
+
|
|
711
|
+
##### Type Parameters
|
|
712
|
+
|
|
713
|
+
###### C
|
|
714
|
+
|
|
715
|
+
`C` *extends* `NodeCallback`\<[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\> \| `null`\>
|
|
716
|
+
|
|
717
|
+
##### Parameters
|
|
718
|
+
|
|
719
|
+
###### callback?
|
|
720
|
+
|
|
721
|
+
`C`
|
|
722
|
+
|
|
723
|
+
###### startNode?
|
|
724
|
+
|
|
725
|
+
\| `K`
|
|
726
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
727
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
728
|
+
\| `null`
|
|
729
|
+
|
|
730
|
+
###### iterationType?
|
|
731
|
+
|
|
732
|
+
`IterationType`
|
|
733
|
+
|
|
734
|
+
###### includeNull?
|
|
735
|
+
|
|
736
|
+
`true`
|
|
737
|
+
|
|
738
|
+
##### Returns
|
|
739
|
+
|
|
740
|
+
`ReturnType`\<`C`\>[]
|
|
741
|
+
|
|
742
|
+
##### Example
|
|
743
|
+
|
|
744
|
+
```ts
|
|
745
|
+
// BinaryTree level-order traversal
|
|
746
|
+
const tree = new BinaryTree([
|
|
747
|
+
[1, 'one'],
|
|
748
|
+
[2, 'two'],
|
|
749
|
+
[3, 'three'],
|
|
750
|
+
[4, 'four'],
|
|
751
|
+
[5, 'five'],
|
|
752
|
+
[6, 'six'],
|
|
753
|
+
[7, 'seven']
|
|
754
|
+
]);
|
|
755
|
+
|
|
756
|
+
// Binary tree maintains level-order insertion
|
|
757
|
+
// Complete binary tree structure
|
|
758
|
+
console.log(tree.size); // 7;
|
|
759
|
+
|
|
760
|
+
// Verify all keys are present
|
|
761
|
+
console.log(tree.has(1)); // true;
|
|
762
|
+
console.log(tree.has(4)); // true;
|
|
763
|
+
console.log(tree.has(7)); // true;
|
|
764
|
+
|
|
765
|
+
// Iterate through tree
|
|
766
|
+
const keys: number[] = [];
|
|
767
|
+
for (const [key] of tree) {
|
|
768
|
+
keys.push(key);
|
|
769
|
+
}
|
|
770
|
+
console.log(keys.length); // 7;
|
|
771
|
+
```
|
|
772
|
+
|
|
773
|
+
##### Implementation of
|
|
774
|
+
|
|
775
|
+
```ts
|
|
776
|
+
IBinaryTree.bfs
|
|
777
|
+
```
|
|
778
|
+
|
|
779
|
+
***
|
|
780
|
+
|
|
781
|
+
### clear()
|
|
782
|
+
|
|
783
|
+
```ts
|
|
784
|
+
clear(): void;
|
|
785
|
+
```
|
|
786
|
+
|
|
787
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:1509](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L1509)
|
|
788
|
+
|
|
789
|
+
Clears the tree of all nodes and values.
|
|
790
|
+
|
|
791
|
+
#### Returns
|
|
792
|
+
|
|
793
|
+
`void`
|
|
794
|
+
|
|
795
|
+
#### Remarks
|
|
796
|
+
|
|
797
|
+
Time O(N) if in Map mode (due to `_store.clear()`), O(1) otherwise. Space O(1)
|
|
798
|
+
|
|
799
|
+
*
|
|
800
|
+
|
|
801
|
+
#### Example
|
|
802
|
+
|
|
803
|
+
```ts
|
|
804
|
+
// Remove all nodes
|
|
805
|
+
const tree = new BinaryTree<number>([1, 2, 3]);
|
|
806
|
+
tree.clear();
|
|
807
|
+
console.log(tree.isEmpty()); // true;
|
|
808
|
+
```
|
|
809
|
+
|
|
810
|
+
#### Implementation of
|
|
811
|
+
|
|
812
|
+
```ts
|
|
813
|
+
IBinaryTree.clear
|
|
814
|
+
```
|
|
815
|
+
|
|
816
|
+
#### Overrides
|
|
817
|
+
|
|
818
|
+
[`IterableEntryBase`](IterableEntryBase.md).[`clear`](IterableEntryBase.md#clear)
|
|
819
|
+
|
|
820
|
+
***
|
|
821
|
+
|
|
822
|
+
### clone()
|
|
823
|
+
|
|
824
|
+
```ts
|
|
825
|
+
clone(): this;
|
|
826
|
+
```
|
|
827
|
+
|
|
828
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:2697](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L2697)
|
|
829
|
+
|
|
830
|
+
Clones the tree.
|
|
831
|
+
|
|
832
|
+
#### Returns
|
|
833
|
+
|
|
834
|
+
`this`
|
|
835
|
+
|
|
836
|
+
A new, cloned instance of the tree.
|
|
837
|
+
|
|
838
|
+
*
|
|
839
|
+
|
|
840
|
+
#### Remarks
|
|
841
|
+
|
|
842
|
+
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.
|
|
843
|
+
|
|
844
|
+
#### Example
|
|
845
|
+
|
|
846
|
+
```ts
|
|
847
|
+
// Deep copy
|
|
848
|
+
const tree = new BinaryTree<number>([1, 2, 3]);
|
|
849
|
+
const copy = tree.clone();
|
|
850
|
+
copy.delete(1);
|
|
851
|
+
console.log(tree.has(1)); // true;
|
|
852
|
+
```
|
|
853
|
+
|
|
854
|
+
#### Implementation of
|
|
855
|
+
|
|
856
|
+
```ts
|
|
857
|
+
IBinaryTree.clone
|
|
858
|
+
```
|
|
859
|
+
|
|
860
|
+
#### Overrides
|
|
861
|
+
|
|
862
|
+
[`IterableEntryBase`](IterableEntryBase.md).[`clone`](IterableEntryBase.md#clone)
|
|
863
|
+
|
|
864
|
+
***
|
|
865
|
+
|
|
866
|
+
### createNode()
|
|
867
|
+
|
|
868
|
+
```ts
|
|
869
|
+
createNode(key, value?): BinaryTreeNode<K, V>;
|
|
870
|
+
```
|
|
871
|
+
|
|
872
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:397](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L397)
|
|
873
|
+
|
|
874
|
+
(Protected) Creates a new node.
|
|
875
|
+
|
|
876
|
+
#### Parameters
|
|
877
|
+
|
|
878
|
+
##### key
|
|
879
|
+
|
|
880
|
+
`K`
|
|
881
|
+
|
|
882
|
+
The key for the new node.
|
|
883
|
+
|
|
884
|
+
##### value?
|
|
885
|
+
|
|
886
|
+
`V`
|
|
887
|
+
|
|
888
|
+
The value for the new node (used if not in Map mode).
|
|
889
|
+
|
|
890
|
+
#### Returns
|
|
891
|
+
|
|
892
|
+
[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
893
|
+
|
|
894
|
+
The newly created node.
|
|
895
|
+
|
|
896
|
+
#### Remarks
|
|
897
|
+
|
|
898
|
+
Time O(1), Space O(1)
|
|
899
|
+
|
|
900
|
+
#### Implementation of
|
|
901
|
+
|
|
902
|
+
```ts
|
|
903
|
+
IBinaryTree.createNode
|
|
904
|
+
```
|
|
905
|
+
|
|
906
|
+
***
|
|
907
|
+
|
|
908
|
+
### createTree()
|
|
909
|
+
|
|
910
|
+
```ts
|
|
911
|
+
createTree(options?): this;
|
|
912
|
+
```
|
|
913
|
+
|
|
914
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:408](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L408)
|
|
915
|
+
|
|
916
|
+
Creates a new, empty tree of the same type and configuration.
|
|
917
|
+
|
|
918
|
+
#### Parameters
|
|
919
|
+
|
|
920
|
+
##### options?
|
|
921
|
+
|
|
922
|
+
`Partial`\<`BinaryTreeOptions`\<`K`, `V`, `R`\>\>
|
|
923
|
+
|
|
924
|
+
Optional overrides for the new tree's options.
|
|
925
|
+
|
|
926
|
+
#### Returns
|
|
927
|
+
|
|
928
|
+
`this`
|
|
929
|
+
|
|
930
|
+
A new, empty tree instance.
|
|
931
|
+
|
|
932
|
+
#### Remarks
|
|
933
|
+
|
|
934
|
+
Time O(1) (excluding options cloning), Space O(1)
|
|
935
|
+
|
|
936
|
+
#### Implementation of
|
|
937
|
+
|
|
938
|
+
```ts
|
|
939
|
+
IBinaryTree.createTree
|
|
940
|
+
```
|
|
941
|
+
|
|
942
|
+
***
|
|
943
|
+
|
|
944
|
+
### delete()
|
|
945
|
+
|
|
946
|
+
```ts
|
|
947
|
+
delete(keyNodeEntryRawOrPredicate): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[];
|
|
948
|
+
```
|
|
949
|
+
|
|
950
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:971](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L971)
|
|
951
|
+
|
|
952
|
+
Deletes a node from the tree.
|
|
953
|
+
|
|
954
|
+
#### Parameters
|
|
955
|
+
|
|
956
|
+
##### keyNodeEntryRawOrPredicate
|
|
957
|
+
|
|
958
|
+
\| `BTNRep`\<`K`, `V`, [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>\>
|
|
959
|
+
\| `NodePredicate`\<[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\> \| `null`\>
|
|
960
|
+
|
|
961
|
+
The node to delete.
|
|
962
|
+
|
|
963
|
+
#### Returns
|
|
964
|
+
|
|
965
|
+
`BinaryTreeDeleteResult`\<[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>\>[]
|
|
966
|
+
|
|
967
|
+
An array containing deletion results (for compatibility with self-balancing trees).
|
|
968
|
+
|
|
969
|
+
*
|
|
970
|
+
|
|
971
|
+
#### Remarks
|
|
972
|
+
|
|
973
|
+
Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation finds the node, and if it has two children, swaps it with the rightmost node of its left subtree (in-order predecessor) before deleting. Time O(N) in the worst case. O(N) to find the node (`getNode`) and O(H) (which is O(N) worst-case) to find the rightmost node. Space O(1) (if `getNode` is iterative, which it is).
|
|
974
|
+
|
|
975
|
+
#### Example
|
|
976
|
+
|
|
977
|
+
```ts
|
|
978
|
+
// Remove a node
|
|
979
|
+
const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
980
|
+
tree.delete(3);
|
|
981
|
+
console.log(tree.has(3)); // false;
|
|
982
|
+
console.log(tree.size); // 4;
|
|
983
|
+
```
|
|
984
|
+
|
|
985
|
+
#### Implementation of
|
|
986
|
+
|
|
987
|
+
```ts
|
|
988
|
+
IBinaryTree.delete
|
|
989
|
+
```
|
|
990
|
+
|
|
991
|
+
***
|
|
992
|
+
|
|
993
|
+
### dfs()
|
|
994
|
+
|
|
995
|
+
Performs a Depth-First Search (DFS) traversal.
|
|
996
|
+
|
|
997
|
+
#### Remarks
|
|
998
|
+
|
|
999
|
+
Time O(N), visits every node. Space O(H) for the call/explicit stack. O(N) worst-case.
|
|
1000
|
+
|
|
1001
|
+
#### Template
|
|
1002
|
+
|
|
1003
|
+
The type of the callback function.
|
|
1004
|
+
|
|
1005
|
+
#### Param
|
|
1006
|
+
|
|
1007
|
+
Function to call on each node.
|
|
1008
|
+
|
|
1009
|
+
#### Param
|
|
1010
|
+
|
|
1011
|
+
The traversal order ('IN', 'PRE', 'POST').
|
|
1012
|
+
|
|
1013
|
+
#### Param
|
|
1014
|
+
|
|
1015
|
+
If true, stops after the first callback.
|
|
1016
|
+
|
|
1017
|
+
#### Param
|
|
1018
|
+
|
|
1019
|
+
The node to start from.
|
|
1020
|
+
|
|
1021
|
+
#### Param
|
|
1022
|
+
|
|
1023
|
+
The traversal method.
|
|
1024
|
+
|
|
1025
|
+
#### Param
|
|
1026
|
+
|
|
1027
|
+
If true, includes null nodes in the traversal.
|
|
1028
|
+
|
|
1029
|
+
#### Call Signature
|
|
1030
|
+
|
|
1031
|
+
```ts
|
|
1032
|
+
dfs(): (K | undefined)[];
|
|
1033
|
+
```
|
|
1034
|
+
|
|
1035
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:2081](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L2081)
|
|
1036
|
+
|
|
1037
|
+
Depth-first search traversal
|
|
1038
|
+
|
|
1039
|
+
*
|
|
1040
|
+
|
|
1041
|
+
##### Returns
|
|
1042
|
+
|
|
1043
|
+
(`K` \| `undefined`)[]
|
|
1044
|
+
|
|
1045
|
+
##### Example
|
|
1046
|
+
|
|
1047
|
+
```ts
|
|
1048
|
+
// Depth-first search traversal
|
|
1049
|
+
const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
1050
|
+
const inOrder = tree.dfs(node => node.key, 'IN');
|
|
1051
|
+
console.log(inOrder); // [4, 2, 5, 1, 3];
|
|
1052
|
+
```
|
|
1053
|
+
|
|
1054
|
+
##### Implementation of
|
|
1055
|
+
|
|
1056
|
+
```ts
|
|
1057
|
+
IBinaryTree.dfs
|
|
1058
|
+
```
|
|
1059
|
+
|
|
1060
|
+
#### Call Signature
|
|
1061
|
+
|
|
1062
|
+
```ts
|
|
1063
|
+
dfs<C>(
|
|
1064
|
+
callback?,
|
|
1065
|
+
pattern?,
|
|
1066
|
+
onlyOne?,
|
|
1067
|
+
startNode?,
|
|
1068
|
+
iterationType?): ReturnType<C>[];
|
|
1069
|
+
```
|
|
1070
|
+
|
|
1071
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:2083](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L2083)
|
|
1072
|
+
|
|
1073
|
+
Depth-first search traversal
|
|
1074
|
+
|
|
1075
|
+
*
|
|
1076
|
+
|
|
1077
|
+
##### Type Parameters
|
|
1078
|
+
|
|
1079
|
+
###### C
|
|
1080
|
+
|
|
1081
|
+
`C` *extends* `NodeCallback`\<[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>\>
|
|
1082
|
+
|
|
1083
|
+
##### Parameters
|
|
1084
|
+
|
|
1085
|
+
###### callback?
|
|
1086
|
+
|
|
1087
|
+
`C`
|
|
1088
|
+
|
|
1089
|
+
###### pattern?
|
|
1090
|
+
|
|
1091
|
+
`DFSOrderPattern`
|
|
1092
|
+
|
|
1093
|
+
###### onlyOne?
|
|
1094
|
+
|
|
1095
|
+
`boolean`
|
|
1096
|
+
|
|
1097
|
+
###### startNode?
|
|
1098
|
+
|
|
1099
|
+
\| `K`
|
|
1100
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
1101
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
1102
|
+
\| `null`
|
|
1103
|
+
|
|
1104
|
+
###### iterationType?
|
|
1105
|
+
|
|
1106
|
+
`IterationType`
|
|
1107
|
+
|
|
1108
|
+
##### Returns
|
|
1109
|
+
|
|
1110
|
+
`ReturnType`\<`C`\>[]
|
|
1111
|
+
|
|
1112
|
+
##### Example
|
|
1113
|
+
|
|
1114
|
+
```ts
|
|
1115
|
+
// Depth-first search traversal
|
|
1116
|
+
const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
1117
|
+
const inOrder = tree.dfs(node => node.key, 'IN');
|
|
1118
|
+
console.log(inOrder); // [4, 2, 5, 1, 3];
|
|
1119
|
+
```
|
|
1120
|
+
|
|
1121
|
+
##### Implementation of
|
|
1122
|
+
|
|
1123
|
+
```ts
|
|
1124
|
+
IBinaryTree.dfs
|
|
1125
|
+
```
|
|
1126
|
+
|
|
1127
|
+
#### Call Signature
|
|
1128
|
+
|
|
1129
|
+
```ts
|
|
1130
|
+
dfs<C>(
|
|
1131
|
+
callback?,
|
|
1132
|
+
pattern?,
|
|
1133
|
+
onlyOne?,
|
|
1134
|
+
startNode?,
|
|
1135
|
+
iterationType?,
|
|
1136
|
+
includeNull?): ReturnType<C>[];
|
|
1137
|
+
```
|
|
1138
|
+
|
|
1139
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:2091](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L2091)
|
|
1140
|
+
|
|
1141
|
+
Depth-first search traversal
|
|
1142
|
+
|
|
1143
|
+
*
|
|
1144
|
+
|
|
1145
|
+
##### Type Parameters
|
|
1146
|
+
|
|
1147
|
+
###### C
|
|
1148
|
+
|
|
1149
|
+
`C` *extends* `NodeCallback`\<[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\> \| `null`\>
|
|
1150
|
+
|
|
1151
|
+
##### Parameters
|
|
1152
|
+
|
|
1153
|
+
###### callback?
|
|
1154
|
+
|
|
1155
|
+
`C`
|
|
1156
|
+
|
|
1157
|
+
###### pattern?
|
|
1158
|
+
|
|
1159
|
+
`DFSOrderPattern`
|
|
1160
|
+
|
|
1161
|
+
###### onlyOne?
|
|
1162
|
+
|
|
1163
|
+
`boolean`
|
|
1164
|
+
|
|
1165
|
+
###### startNode?
|
|
1166
|
+
|
|
1167
|
+
\| `K`
|
|
1168
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
1169
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
1170
|
+
\| `null`
|
|
1171
|
+
|
|
1172
|
+
###### iterationType?
|
|
1173
|
+
|
|
1174
|
+
`IterationType`
|
|
1175
|
+
|
|
1176
|
+
###### includeNull?
|
|
1177
|
+
|
|
1178
|
+
`boolean`
|
|
1179
|
+
|
|
1180
|
+
##### Returns
|
|
1181
|
+
|
|
1182
|
+
`ReturnType`\<`C`\>[]
|
|
1183
|
+
|
|
1184
|
+
##### Example
|
|
1185
|
+
|
|
1186
|
+
```ts
|
|
1187
|
+
// Depth-first search traversal
|
|
1188
|
+
const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
1189
|
+
const inOrder = tree.dfs(node => node.key, 'IN');
|
|
1190
|
+
console.log(inOrder); // [4, 2, 5, 1, 3];
|
|
1191
|
+
```
|
|
1192
|
+
|
|
1193
|
+
##### Implementation of
|
|
1194
|
+
|
|
1195
|
+
```ts
|
|
1196
|
+
IBinaryTree.dfs
|
|
1197
|
+
```
|
|
1198
|
+
|
|
1199
|
+
***
|
|
1200
|
+
|
|
1201
|
+
### ensureNode()
|
|
1202
|
+
|
|
1203
|
+
```ts
|
|
1204
|
+
ensureNode(keyNodeOrEntry, iterationType?): BinaryTreeNode<K, V> | null | undefined;
|
|
1205
|
+
```
|
|
1206
|
+
|
|
1207
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:420](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L420)
|
|
1208
|
+
|
|
1209
|
+
Ensures the input is a node. If it's a key or entry, it searches for the node.
|
|
1210
|
+
|
|
1211
|
+
#### Parameters
|
|
1212
|
+
|
|
1213
|
+
##### keyNodeOrEntry
|
|
1214
|
+
|
|
1215
|
+
\| `K`
|
|
1216
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
1217
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
1218
|
+
\| `null`
|
|
1219
|
+
\| `undefined`
|
|
1220
|
+
|
|
1221
|
+
The item to resolve to a node.
|
|
1222
|
+
|
|
1223
|
+
##### iterationType?
|
|
1224
|
+
|
|
1225
|
+
`IterationType` = `...`
|
|
1226
|
+
|
|
1227
|
+
The traversal method to use if searching.
|
|
1228
|
+
|
|
1229
|
+
#### Returns
|
|
1230
|
+
|
|
1231
|
+
[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\> \| `null` \| `undefined`
|
|
1232
|
+
|
|
1233
|
+
The resolved node, or null/undefined if not found or input is null/undefined.
|
|
1234
|
+
|
|
1235
|
+
#### Remarks
|
|
1236
|
+
|
|
1237
|
+
Time O(1) if a node is passed. O(N) if a key or entry is passed (due to `getNode` performing a full search). Space O(1) if iterative search, O(H) if recursive (where H is height, O(N) worst-case).
|
|
1238
|
+
|
|
1239
|
+
***
|
|
1240
|
+
|
|
1241
|
+
### entries()
|
|
1242
|
+
|
|
1243
|
+
```ts
|
|
1244
|
+
entries(): IterableIterator<[K, V | undefined]>;
|
|
1245
|
+
```
|
|
1246
|
+
|
|
1247
|
+
Defined in: [data-structures/base/iterable-entry-base.ts:31](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-entry-base.ts#L31)
|
|
1248
|
+
|
|
1249
|
+
Iterate over `[key, value]` pairs (may yield `undefined` values).
|
|
1250
|
+
|
|
1251
|
+
#### Returns
|
|
1252
|
+
|
|
1253
|
+
`IterableIterator`\<\[`K`, `V` \| `undefined`\]\>
|
|
1254
|
+
|
|
1255
|
+
Iterator of `[K, V | undefined]`.
|
|
1256
|
+
|
|
1257
|
+
#### Remarks
|
|
1258
|
+
|
|
1259
|
+
Time O(n), Space O(1)
|
|
1260
|
+
|
|
1261
|
+
#### Implementation of
|
|
1262
|
+
|
|
1263
|
+
```ts
|
|
1264
|
+
IBinaryTree.entries
|
|
1265
|
+
```
|
|
1266
|
+
|
|
1267
|
+
#### Inherited from
|
|
1268
|
+
|
|
1269
|
+
[`IterableEntryBase`](IterableEntryBase.md).[`entries`](IterableEntryBase.md#entries)
|
|
1270
|
+
|
|
1271
|
+
***
|
|
1272
|
+
|
|
1273
|
+
### every()
|
|
1274
|
+
|
|
1275
|
+
```ts
|
|
1276
|
+
every(predicate, thisArg?): boolean;
|
|
1277
|
+
```
|
|
1278
|
+
|
|
1279
|
+
Defined in: [data-structures/base/iterable-entry-base.ts:66](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-entry-base.ts#L66)
|
|
1280
|
+
|
|
1281
|
+
Test whether all entries satisfy the predicate.
|
|
1282
|
+
|
|
1283
|
+
#### Parameters
|
|
1284
|
+
|
|
1285
|
+
##### predicate
|
|
1286
|
+
|
|
1287
|
+
`EntryCallback`\<`K`, `V` \| `undefined`, `boolean`\>
|
|
1288
|
+
|
|
1289
|
+
`(key, value, index, self) => boolean`.
|
|
1290
|
+
|
|
1291
|
+
##### thisArg?
|
|
1292
|
+
|
|
1293
|
+
`unknown`
|
|
1294
|
+
|
|
1295
|
+
Optional `this` for callback.
|
|
1296
|
+
|
|
1297
|
+
#### Returns
|
|
1298
|
+
|
|
1299
|
+
`boolean`
|
|
1300
|
+
|
|
1301
|
+
`true` if all pass; otherwise `false`.
|
|
1302
|
+
|
|
1303
|
+
#### Remarks
|
|
1304
|
+
|
|
1305
|
+
Time O(n), Space O(1)
|
|
1306
|
+
|
|
1307
|
+
#### Implementation of
|
|
1308
|
+
|
|
1309
|
+
```ts
|
|
1310
|
+
IBinaryTree.every
|
|
1311
|
+
```
|
|
1312
|
+
|
|
1313
|
+
#### Inherited from
|
|
1314
|
+
|
|
1315
|
+
[`IterableEntryBase`](IterableEntryBase.md).[`every`](IterableEntryBase.md#every)
|
|
1316
|
+
|
|
1317
|
+
***
|
|
1318
|
+
|
|
1319
|
+
### filter()
|
|
1320
|
+
|
|
1321
|
+
```ts
|
|
1322
|
+
filter(predicate, thisArg?): this;
|
|
1323
|
+
```
|
|
1324
|
+
|
|
1325
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:2749](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L2749)
|
|
1326
|
+
|
|
1327
|
+
Creates a new tree containing only the entries that satisfy the predicate.
|
|
1328
|
+
|
|
1329
|
+
#### Parameters
|
|
1330
|
+
|
|
1331
|
+
##### predicate
|
|
1332
|
+
|
|
1333
|
+
`EntryCallback`\<`K`, `V` \| `undefined`, `boolean`\>
|
|
1334
|
+
|
|
1335
|
+
A function to test each [key, value] pair.
|
|
1336
|
+
|
|
1337
|
+
##### thisArg?
|
|
1338
|
+
|
|
1339
|
+
`unknown`
|
|
1340
|
+
|
|
1341
|
+
`this` context for the predicate.
|
|
1342
|
+
|
|
1343
|
+
#### Returns
|
|
1344
|
+
|
|
1345
|
+
`this`
|
|
1346
|
+
|
|
1347
|
+
A new, filtered tree.
|
|
1348
|
+
|
|
1349
|
+
*
|
|
1350
|
+
|
|
1351
|
+
#### Remarks
|
|
1352
|
+
|
|
1353
|
+
Time O(N * M), where N is nodes in this tree, and M is size of the new tree during insertion (O(N) iteration + O(M) `set` for each item). Space O(N) for the new tree.
|
|
1354
|
+
|
|
1355
|
+
#### Example
|
|
1356
|
+
|
|
1357
|
+
```ts
|
|
1358
|
+
// Filter nodes by condition
|
|
1359
|
+
const tree = new BinaryTree<number>([1, 2, 3, 4]);
|
|
1360
|
+
const result = tree.filter((_, key) => key > 2);
|
|
1361
|
+
console.log(result.size); // 2;
|
|
1362
|
+
```
|
|
1363
|
+
|
|
1364
|
+
#### Implementation of
|
|
1365
|
+
|
|
1366
|
+
```ts
|
|
1367
|
+
IBinaryTree.filter
|
|
1368
|
+
```
|
|
1369
|
+
|
|
1370
|
+
#### Overrides
|
|
1371
|
+
|
|
1372
|
+
[`IterableEntryBase`](IterableEntryBase.md).[`filter`](IterableEntryBase.md#filter)
|
|
1373
|
+
|
|
1374
|
+
***
|
|
1375
|
+
|
|
1376
|
+
### find()
|
|
1377
|
+
|
|
1378
|
+
```ts
|
|
1379
|
+
find(callbackfn, thisArg?): [K, V | undefined] | undefined;
|
|
1380
|
+
```
|
|
1381
|
+
|
|
1382
|
+
Defined in: [data-structures/base/iterable-entry-base.ts:114](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-entry-base.ts#L114)
|
|
1383
|
+
|
|
1384
|
+
Find the first entry that matches a predicate.
|
|
1385
|
+
|
|
1386
|
+
#### Parameters
|
|
1387
|
+
|
|
1388
|
+
##### callbackfn
|
|
1389
|
+
|
|
1390
|
+
`EntryCallback`\<`K`, `V` \| `undefined`, `boolean`\>
|
|
1391
|
+
|
|
1392
|
+
`(key, value, index, self) => boolean`.
|
|
1393
|
+
|
|
1394
|
+
##### thisArg?
|
|
1395
|
+
|
|
1396
|
+
`unknown`
|
|
1397
|
+
|
|
1398
|
+
Optional `this` for callback.
|
|
1399
|
+
|
|
1400
|
+
#### Returns
|
|
1401
|
+
|
|
1402
|
+
\[`K`, `V` \| `undefined`\] \| `undefined`
|
|
1403
|
+
|
|
1404
|
+
Matching `[key, value]` or `undefined`.
|
|
1405
|
+
|
|
1406
|
+
#### Remarks
|
|
1407
|
+
|
|
1408
|
+
Time O(n), Space O(1)
|
|
1409
|
+
|
|
1410
|
+
#### Implementation of
|
|
1411
|
+
|
|
1412
|
+
```ts
|
|
1413
|
+
IBinaryTree.find
|
|
1414
|
+
```
|
|
1415
|
+
|
|
1416
|
+
#### Inherited from
|
|
1417
|
+
|
|
1418
|
+
[`IterableEntryBase`](IterableEntryBase.md).[`find`](IterableEntryBase.md#find)
|
|
1419
|
+
|
|
1420
|
+
***
|
|
1421
|
+
|
|
1422
|
+
### forEach()
|
|
1423
|
+
|
|
1424
|
+
```ts
|
|
1425
|
+
forEach(callbackfn, thisArg?): void;
|
|
1426
|
+
```
|
|
1427
|
+
|
|
1428
|
+
Defined in: [data-structures/base/iterable-entry-base.ts:99](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-entry-base.ts#L99)
|
|
1429
|
+
|
|
1430
|
+
Visit each entry, left-to-right.
|
|
1431
|
+
|
|
1432
|
+
#### Parameters
|
|
1433
|
+
|
|
1434
|
+
##### callbackfn
|
|
1435
|
+
|
|
1436
|
+
`EntryCallback`\<`K`, `V` \| `undefined`, `void`\>
|
|
1437
|
+
|
|
1438
|
+
`(key, value, index, self) => void`.
|
|
1439
|
+
|
|
1440
|
+
##### thisArg?
|
|
1441
|
+
|
|
1442
|
+
`unknown`
|
|
1443
|
+
|
|
1444
|
+
Optional `this` for callback.
|
|
1445
|
+
|
|
1446
|
+
#### Returns
|
|
1447
|
+
|
|
1448
|
+
`void`
|
|
1449
|
+
|
|
1450
|
+
#### Remarks
|
|
1451
|
+
|
|
1452
|
+
Time O(n), Space O(1)
|
|
1453
|
+
|
|
1454
|
+
#### Implementation of
|
|
1455
|
+
|
|
1456
|
+
```ts
|
|
1457
|
+
IBinaryTree.forEach
|
|
1458
|
+
```
|
|
1459
|
+
|
|
1460
|
+
#### Inherited from
|
|
1461
|
+
|
|
1462
|
+
[`IterableEntryBase`](IterableEntryBase.md).[`forEach`](IterableEntryBase.md#foreach)
|
|
1463
|
+
|
|
1464
|
+
***
|
|
1465
|
+
|
|
1466
|
+
### get()
|
|
1467
|
+
|
|
1468
|
+
```ts
|
|
1469
|
+
get(
|
|
1470
|
+
keyNodeEntryOrPredicate,
|
|
1471
|
+
startNode?,
|
|
1472
|
+
iterationType?): V | undefined;
|
|
1473
|
+
```
|
|
1474
|
+
|
|
1475
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:1349](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L1349)
|
|
1476
|
+
|
|
1477
|
+
Gets the value associated with a key.
|
|
1478
|
+
|
|
1479
|
+
#### Parameters
|
|
1480
|
+
|
|
1481
|
+
##### keyNodeEntryOrPredicate
|
|
1482
|
+
|
|
1483
|
+
\| `K`
|
|
1484
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
1485
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
1486
|
+
\| `null`
|
|
1487
|
+
\| `undefined`
|
|
1488
|
+
|
|
1489
|
+
The key, node, or entry to get the value for.
|
|
1490
|
+
|
|
1491
|
+
##### startNode?
|
|
1492
|
+
|
|
1493
|
+
\| `K`
|
|
1494
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
1495
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
1496
|
+
\| `null`
|
|
1497
|
+
|
|
1498
|
+
The node to start searching from (if not in Map mode).
|
|
1499
|
+
|
|
1500
|
+
##### iterationType?
|
|
1501
|
+
|
|
1502
|
+
`IterationType` = `...`
|
|
1503
|
+
|
|
1504
|
+
The traversal method (if not in Map mode).
|
|
1505
|
+
|
|
1506
|
+
#### Returns
|
|
1507
|
+
|
|
1508
|
+
`V` \| `undefined`
|
|
1509
|
+
|
|
1510
|
+
The associated value, or undefined.
|
|
1511
|
+
|
|
1512
|
+
*
|
|
1513
|
+
|
|
1514
|
+
#### Remarks
|
|
1515
|
+
|
|
1516
|
+
Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). Time O(1) if in Map mode. O(N) if not in Map mode (uses `getNode`). Space O(1) if in Map mode. O(H) or O(N) otherwise.
|
|
1517
|
+
|
|
1518
|
+
#### Example
|
|
1519
|
+
|
|
1520
|
+
```ts
|
|
1521
|
+
// Retrieve value by key
|
|
1522
|
+
const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'left'], [3, 'right']]);
|
|
1523
|
+
console.log(tree.get(2)); // 'left';
|
|
1524
|
+
console.log(tree.get(99)); // undefined;
|
|
1525
|
+
```
|
|
1526
|
+
|
|
1527
|
+
#### Implementation of
|
|
1528
|
+
|
|
1529
|
+
```ts
|
|
1530
|
+
IBinaryTree.get
|
|
1531
|
+
```
|
|
1532
|
+
|
|
1533
|
+
#### Overrides
|
|
1534
|
+
|
|
1535
|
+
[`IterableEntryBase`](IterableEntryBase.md).[`get`](IterableEntryBase.md#get)
|
|
1536
|
+
|
|
1537
|
+
***
|
|
1538
|
+
|
|
1539
|
+
### getDepth()
|
|
1540
|
+
|
|
1541
|
+
```ts
|
|
1542
|
+
getDepth(dist, startNode?): number;
|
|
1543
|
+
```
|
|
1544
|
+
|
|
1545
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:1710](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L1710)
|
|
1546
|
+
|
|
1547
|
+
Gets the depth of a node (distance from `startNode`).
|
|
1548
|
+
|
|
1549
|
+
#### Parameters
|
|
1550
|
+
|
|
1551
|
+
##### dist
|
|
1552
|
+
|
|
1553
|
+
\| `K`
|
|
1554
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
1555
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
1556
|
+
\| `null`
|
|
1557
|
+
\| `undefined`
|
|
1558
|
+
|
|
1559
|
+
The node to find the depth of.
|
|
1560
|
+
|
|
1561
|
+
##### startNode?
|
|
1562
|
+
|
|
1563
|
+
\| `K`
|
|
1564
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
1565
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
1566
|
+
\| `null`
|
|
1567
|
+
|
|
1568
|
+
The node to measure depth from (defaults to root).
|
|
1569
|
+
|
|
1570
|
+
#### Returns
|
|
1571
|
+
|
|
1572
|
+
`number`
|
|
1573
|
+
|
|
1574
|
+
The depth (0 if `dist` is `startNode`).
|
|
1575
|
+
|
|
1576
|
+
*
|
|
1577
|
+
|
|
1578
|
+
#### Remarks
|
|
1579
|
+
|
|
1580
|
+
Time O(H), where H is the depth of the `dist` node relative to `startNode`. O(N) worst-case. Space O(1).
|
|
1581
|
+
|
|
1582
|
+
#### Example
|
|
1583
|
+
|
|
1584
|
+
```ts
|
|
1585
|
+
// Get depth of a node
|
|
1586
|
+
const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
1587
|
+
const node = tree.getNode(4);
|
|
1588
|
+
console.log(tree.getDepth(node!)); // 2;
|
|
1589
|
+
```
|
|
1590
|
+
|
|
1591
|
+
#### Implementation of
|
|
1592
|
+
|
|
1593
|
+
```ts
|
|
1594
|
+
IBinaryTree.getDepth
|
|
1595
|
+
```
|
|
1596
|
+
|
|
1597
|
+
***
|
|
1598
|
+
|
|
1599
|
+
### getHeight()
|
|
1600
|
+
|
|
1601
|
+
```ts
|
|
1602
|
+
getHeight(startNode?, iterationType?): number;
|
|
1603
|
+
```
|
|
1604
|
+
|
|
1605
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:1774](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L1774)
|
|
1606
|
+
|
|
1607
|
+
Gets the maximum height of the tree (longest path from startNode to a leaf).
|
|
1608
|
+
|
|
1609
|
+
#### Parameters
|
|
1610
|
+
|
|
1611
|
+
##### startNode?
|
|
1612
|
+
|
|
1613
|
+
\| `K`
|
|
1614
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
1615
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
1616
|
+
\| `null`
|
|
1617
|
+
|
|
1618
|
+
The node to start measuring from.
|
|
1619
|
+
|
|
1620
|
+
##### iterationType?
|
|
1621
|
+
|
|
1622
|
+
`IterationType` = `...`
|
|
1623
|
+
|
|
1624
|
+
The traversal method.
|
|
1625
|
+
|
|
1626
|
+
#### Returns
|
|
1627
|
+
|
|
1628
|
+
`number`
|
|
1629
|
+
|
|
1630
|
+
The height ( -1 for an empty tree, 0 for a single-node tree).
|
|
1631
|
+
|
|
1632
|
+
*
|
|
1633
|
+
|
|
1634
|
+
#### Remarks
|
|
1635
|
+
|
|
1636
|
+
Time O(N), as it must visit every node. Space O(H) for recursive stack (O(N) worst-case) or O(N) for iterative stack (storing node + depth).
|
|
1637
|
+
|
|
1638
|
+
#### Example
|
|
1639
|
+
|
|
1640
|
+
```ts
|
|
1641
|
+
// Get tree height
|
|
1642
|
+
const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
1643
|
+
console.log(tree.getHeight()); // 2;
|
|
1644
|
+
```
|
|
1645
|
+
|
|
1646
|
+
#### Implementation of
|
|
1647
|
+
|
|
1648
|
+
```ts
|
|
1649
|
+
IBinaryTree.getHeight
|
|
1650
|
+
```
|
|
1651
|
+
|
|
1652
|
+
***
|
|
1653
|
+
|
|
1654
|
+
### getLeftMost()
|
|
1655
|
+
|
|
1656
|
+
Finds the leftmost node in a subtree (the node with the smallest key in a BST).
|
|
1657
|
+
|
|
1658
|
+
#### Remarks
|
|
1659
|
+
|
|
1660
|
+
Time O(H), where H is the height of the left spine. O(N) worst-case. Space O(H) for recursive/trampoline stack.
|
|
1661
|
+
|
|
1662
|
+
#### Template
|
|
1663
|
+
|
|
1664
|
+
The type of the callback function.
|
|
1665
|
+
|
|
1666
|
+
#### Param
|
|
1667
|
+
|
|
1668
|
+
A function to call on the leftmost node.
|
|
1669
|
+
|
|
1670
|
+
#### Param
|
|
1671
|
+
|
|
1672
|
+
The subtree root to search from.
|
|
1673
|
+
|
|
1674
|
+
#### Param
|
|
1675
|
+
|
|
1676
|
+
The traversal method.
|
|
1677
|
+
|
|
1678
|
+
#### Call Signature
|
|
1679
|
+
|
|
1680
|
+
```ts
|
|
1681
|
+
getLeftMost(): K | undefined;
|
|
1682
|
+
```
|
|
1683
|
+
|
|
1684
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:1901](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L1901)
|
|
1685
|
+
|
|
1686
|
+
##### Returns
|
|
1687
|
+
|
|
1688
|
+
`K` \| `undefined`
|
|
1689
|
+
|
|
1690
|
+
##### Implementation of
|
|
1691
|
+
|
|
1692
|
+
```ts
|
|
1693
|
+
IBinaryTree.getLeftMost
|
|
1694
|
+
```
|
|
1695
|
+
|
|
1696
|
+
#### Call Signature
|
|
1697
|
+
|
|
1698
|
+
```ts
|
|
1699
|
+
getLeftMost<C>(
|
|
1700
|
+
callback,
|
|
1701
|
+
startNode?,
|
|
1702
|
+
iterationType?): ReturnType<C>;
|
|
1703
|
+
```
|
|
1704
|
+
|
|
1705
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:1903](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L1903)
|
|
1706
|
+
|
|
1707
|
+
##### Type Parameters
|
|
1708
|
+
|
|
1709
|
+
###### C
|
|
1710
|
+
|
|
1711
|
+
`C` *extends* `NodeCallback`\<[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\> \| `undefined`\>
|
|
1712
|
+
|
|
1713
|
+
##### Parameters
|
|
1714
|
+
|
|
1715
|
+
###### callback
|
|
1716
|
+
|
|
1717
|
+
`C`
|
|
1718
|
+
|
|
1719
|
+
###### startNode?
|
|
1720
|
+
|
|
1721
|
+
\| `K`
|
|
1722
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
1723
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
1724
|
+
\| `null`
|
|
1725
|
+
|
|
1726
|
+
###### iterationType?
|
|
1727
|
+
|
|
1728
|
+
`IterationType`
|
|
1729
|
+
|
|
1730
|
+
##### Returns
|
|
1731
|
+
|
|
1732
|
+
`ReturnType`\<`C`\>
|
|
1733
|
+
|
|
1734
|
+
##### Implementation of
|
|
1735
|
+
|
|
1736
|
+
```ts
|
|
1737
|
+
IBinaryTree.getLeftMost
|
|
1738
|
+
```
|
|
1739
|
+
|
|
1740
|
+
***
|
|
1741
|
+
|
|
1742
|
+
### getMinHeight()
|
|
1743
|
+
|
|
1744
|
+
```ts
|
|
1745
|
+
getMinHeight(startNode?, iterationType?): number;
|
|
1746
|
+
```
|
|
1747
|
+
|
|
1748
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:1816](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L1816)
|
|
1749
|
+
|
|
1750
|
+
Gets the minimum height of the tree (shortest path from startNode to a leaf).
|
|
1751
|
+
|
|
1752
|
+
#### Parameters
|
|
1753
|
+
|
|
1754
|
+
##### startNode?
|
|
1755
|
+
|
|
1756
|
+
\| `K`
|
|
1757
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
1758
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
1759
|
+
\| `null`
|
|
1760
|
+
|
|
1761
|
+
The node to start measuring from.
|
|
1762
|
+
|
|
1763
|
+
##### iterationType?
|
|
1764
|
+
|
|
1765
|
+
`IterationType` = `...`
|
|
1766
|
+
|
|
1767
|
+
The traversal method.
|
|
1768
|
+
|
|
1769
|
+
#### Returns
|
|
1770
|
+
|
|
1771
|
+
`number`
|
|
1772
|
+
|
|
1773
|
+
The minimum height (-1 for empty, 0 for single node).
|
|
1774
|
+
|
|
1775
|
+
#### Remarks
|
|
1776
|
+
|
|
1777
|
+
Time O(N), as it must visit every node. Space O(H) for recursive stack (O(N) worst-case) or O(N) for iterative (due to `depths` Map).
|
|
1778
|
+
|
|
1779
|
+
#### Implementation of
|
|
1780
|
+
|
|
1781
|
+
```ts
|
|
1782
|
+
IBinaryTree.getMinHeight
|
|
1783
|
+
```
|
|
1784
|
+
|
|
1785
|
+
***
|
|
1786
|
+
|
|
1787
|
+
### getNode()
|
|
1788
|
+
|
|
1789
|
+
```ts
|
|
1790
|
+
getNode(
|
|
1791
|
+
keyNodeEntryOrPredicate,
|
|
1792
|
+
startNode?,
|
|
1793
|
+
iterationType?): BinaryTreeNode<K, V> | null | undefined;
|
|
1794
|
+
```
|
|
1795
|
+
|
|
1796
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:1279](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L1279)
|
|
1797
|
+
|
|
1798
|
+
Gets the first node matching a predicate.
|
|
1799
|
+
|
|
1800
|
+
#### Parameters
|
|
1801
|
+
|
|
1802
|
+
##### keyNodeEntryOrPredicate
|
|
1803
|
+
|
|
1804
|
+
\| `K`
|
|
1805
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
1806
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
1807
|
+
\| `NodePredicate`\<[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\> \| `null`\>
|
|
1808
|
+
\| `null`
|
|
1809
|
+
\| `undefined`
|
|
1810
|
+
|
|
1811
|
+
The key, node, entry, or predicate function to search for.
|
|
1812
|
+
|
|
1813
|
+
##### startNode?
|
|
1814
|
+
|
|
1815
|
+
\| `K`
|
|
1816
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
1817
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
1818
|
+
\| `null`
|
|
1819
|
+
|
|
1820
|
+
The node to start the search from.
|
|
1821
|
+
|
|
1822
|
+
##### iterationType?
|
|
1823
|
+
|
|
1824
|
+
`IterationType` = `...`
|
|
1825
|
+
|
|
1826
|
+
The traversal method.
|
|
1827
|
+
|
|
1828
|
+
#### Returns
|
|
1829
|
+
|
|
1830
|
+
[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\> \| `null` \| `undefined`
|
|
1831
|
+
|
|
1832
|
+
The first matching node, or undefined if not found.
|
|
1833
|
+
|
|
1834
|
+
*
|
|
1835
|
+
|
|
1836
|
+
#### Remarks
|
|
1837
|
+
|
|
1838
|
+
Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). Time O(N) in the worst case (via `search`). Space O(H) or O(N) (via `search`).
|
|
1839
|
+
|
|
1840
|
+
#### Example
|
|
1841
|
+
|
|
1842
|
+
```ts
|
|
1843
|
+
// Get node by key
|
|
1844
|
+
const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'child']]);
|
|
1845
|
+
console.log(tree.getNode(2)?.value); // 'child';
|
|
1846
|
+
```
|
|
1847
|
+
|
|
1848
|
+
#### Implementation of
|
|
1849
|
+
|
|
1850
|
+
```ts
|
|
1851
|
+
IBinaryTree.getNode
|
|
1852
|
+
```
|
|
1853
|
+
|
|
1854
|
+
***
|
|
1855
|
+
|
|
1856
|
+
### getNodes()
|
|
1857
|
+
|
|
1858
|
+
```ts
|
|
1859
|
+
getNodes(
|
|
1860
|
+
keyNodeEntryOrPredicate,
|
|
1861
|
+
onlyOne?,
|
|
1862
|
+
startNode?,
|
|
1863
|
+
iterationType?): BinaryTreeNode<K, V>[];
|
|
1864
|
+
```
|
|
1865
|
+
|
|
1866
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:1205](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L1205)
|
|
1867
|
+
|
|
1868
|
+
Gets all nodes matching a predicate.
|
|
1869
|
+
|
|
1870
|
+
#### Parameters
|
|
1871
|
+
|
|
1872
|
+
##### keyNodeEntryOrPredicate
|
|
1873
|
+
|
|
1874
|
+
\| `K`
|
|
1875
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
1876
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
1877
|
+
\| `NodePredicate`\<[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>\>
|
|
1878
|
+
\| `null`
|
|
1879
|
+
\| `undefined`
|
|
1880
|
+
|
|
1881
|
+
The key, node, entry, or predicate function to search for.
|
|
1882
|
+
|
|
1883
|
+
##### onlyOne?
|
|
1884
|
+
|
|
1885
|
+
`boolean`
|
|
1886
|
+
|
|
1887
|
+
If true, stops after finding the first match.
|
|
1888
|
+
|
|
1889
|
+
##### startNode?
|
|
1890
|
+
|
|
1891
|
+
\| `K`
|
|
1892
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
1893
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
1894
|
+
\| `null`
|
|
1895
|
+
|
|
1896
|
+
The node to start the search from.
|
|
1897
|
+
|
|
1898
|
+
##### iterationType?
|
|
1899
|
+
|
|
1900
|
+
`IterationType`
|
|
1901
|
+
|
|
1902
|
+
The traversal method.
|
|
1903
|
+
|
|
1904
|
+
#### Returns
|
|
1905
|
+
|
|
1906
|
+
[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>[]
|
|
1907
|
+
|
|
1908
|
+
An array of matching nodes.
|
|
1909
|
+
|
|
1910
|
+
*
|
|
1911
|
+
|
|
1912
|
+
#### Remarks
|
|
1913
|
+
|
|
1914
|
+
Time O(N) (via `search`). Space O(H) or O(N) (via `search`).
|
|
1915
|
+
|
|
1916
|
+
#### Example
|
|
1917
|
+
|
|
1918
|
+
```ts
|
|
1919
|
+
// Get nodes by condition
|
|
1920
|
+
const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
1921
|
+
const nodes = tree.getNodes(node => node.key > 3);
|
|
1922
|
+
console.log(nodes.length); // 2;
|
|
1923
|
+
```
|
|
1924
|
+
|
|
1925
|
+
***
|
|
1926
|
+
|
|
1927
|
+
### getPathToRoot()
|
|
1928
|
+
|
|
1929
|
+
Gets the path from a given node up to the root.
|
|
1930
|
+
|
|
1931
|
+
#### Remarks
|
|
1932
|
+
|
|
1933
|
+
Time O(H), where H is the depth of the `beginNode`. O(N) worst-case. Space O(H) for the result array.
|
|
1934
|
+
|
|
1935
|
+
#### Template
|
|
1936
|
+
|
|
1937
|
+
The type of the callback function.
|
|
1938
|
+
|
|
1939
|
+
#### Param
|
|
1940
|
+
|
|
1941
|
+
The node to start the path from.
|
|
1942
|
+
|
|
1943
|
+
#### Param
|
|
1944
|
+
|
|
1945
|
+
A function to call on each node in the path.
|
|
1946
|
+
|
|
1947
|
+
#### Param
|
|
1948
|
+
|
|
1949
|
+
If true, returns the path from root-to-node.
|
|
1950
|
+
|
|
1951
|
+
#### Call Signature
|
|
1952
|
+
|
|
1953
|
+
```ts
|
|
1954
|
+
getPathToRoot(beginNode): (K | undefined)[];
|
|
1955
|
+
```
|
|
1956
|
+
|
|
1957
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:1863](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L1863)
|
|
1958
|
+
|
|
1959
|
+
##### Parameters
|
|
1960
|
+
|
|
1961
|
+
###### beginNode
|
|
1962
|
+
|
|
1963
|
+
\| `K`
|
|
1964
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
1965
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
1966
|
+
\| `null`
|
|
1967
|
+
\| `undefined`
|
|
1968
|
+
|
|
1969
|
+
##### Returns
|
|
1970
|
+
|
|
1971
|
+
(`K` \| `undefined`)[]
|
|
1972
|
+
|
|
1973
|
+
##### Implementation of
|
|
1974
|
+
|
|
1975
|
+
```ts
|
|
1976
|
+
IBinaryTree.getPathToRoot
|
|
1977
|
+
```
|
|
1978
|
+
|
|
1979
|
+
#### Call Signature
|
|
1980
|
+
|
|
1981
|
+
```ts
|
|
1982
|
+
getPathToRoot<C>(
|
|
1983
|
+
beginNode,
|
|
1984
|
+
callback,
|
|
1985
|
+
isReverse?): ReturnType<C>[];
|
|
1986
|
+
```
|
|
1987
|
+
|
|
1988
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:1867](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L1867)
|
|
1989
|
+
|
|
1990
|
+
##### Type Parameters
|
|
1991
|
+
|
|
1992
|
+
###### C
|
|
1993
|
+
|
|
1994
|
+
`C` *extends* `NodeCallback`\<[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\> \| `undefined`\>
|
|
1995
|
+
|
|
1996
|
+
##### Parameters
|
|
1997
|
+
|
|
1998
|
+
###### beginNode
|
|
1999
|
+
|
|
2000
|
+
\| `K`
|
|
2001
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
2002
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
2003
|
+
\| `null`
|
|
2004
|
+
\| `undefined`
|
|
2005
|
+
|
|
2006
|
+
###### callback
|
|
2007
|
+
|
|
2008
|
+
`C`
|
|
2009
|
+
|
|
2010
|
+
###### isReverse?
|
|
2011
|
+
|
|
2012
|
+
`boolean`
|
|
2013
|
+
|
|
2014
|
+
##### Returns
|
|
2015
|
+
|
|
2016
|
+
`ReturnType`\<`C`\>[]
|
|
2017
|
+
|
|
2018
|
+
##### Implementation of
|
|
2019
|
+
|
|
2020
|
+
```ts
|
|
2021
|
+
IBinaryTree.getPathToRoot
|
|
2022
|
+
```
|
|
2023
|
+
|
|
2024
|
+
***
|
|
2025
|
+
|
|
2026
|
+
### getPredecessor()
|
|
2027
|
+
|
|
2028
|
+
```ts
|
|
2029
|
+
getPredecessor(node): BinaryTreeNode<K, V>;
|
|
2030
|
+
```
|
|
2031
|
+
|
|
2032
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:2001](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L2001)
|
|
2033
|
+
|
|
2034
|
+
Gets the Morris traversal predecessor (rightmost node in the left subtree, or node itself).
|
|
2035
|
+
|
|
2036
|
+
#### Parameters
|
|
2037
|
+
|
|
2038
|
+
##### node
|
|
2039
|
+
|
|
2040
|
+
[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
2041
|
+
|
|
2042
|
+
The node to find the predecessor for.
|
|
2043
|
+
|
|
2044
|
+
#### Returns
|
|
2045
|
+
|
|
2046
|
+
[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
2047
|
+
|
|
2048
|
+
The Morris predecessor.
|
|
2049
|
+
|
|
2050
|
+
#### Remarks
|
|
2051
|
+
|
|
2052
|
+
This is primarily a helper for Morris traversal. Time O(H), where H is the height of the left subtree. O(N) worst-case. Space O(1).
|
|
2053
|
+
|
|
2054
|
+
***
|
|
2055
|
+
|
|
2056
|
+
### getRightMost()
|
|
2057
|
+
|
|
2058
|
+
Finds the rightmost node in a subtree (the node with the largest key in a BST).
|
|
2059
|
+
|
|
2060
|
+
#### Remarks
|
|
2061
|
+
|
|
2062
|
+
Time O(H), where H is the height of the right spine. O(N) worst-case. Space O(H) for recursive/trampoline stack.
|
|
2063
|
+
|
|
2064
|
+
#### Template
|
|
2065
|
+
|
|
2066
|
+
The type of the callback function.
|
|
2067
|
+
|
|
2068
|
+
#### Param
|
|
2069
|
+
|
|
2070
|
+
A function to call on the rightmost node.
|
|
2071
|
+
|
|
2072
|
+
#### Param
|
|
2073
|
+
|
|
2074
|
+
The subtree root to search from.
|
|
2075
|
+
|
|
2076
|
+
#### Param
|
|
2077
|
+
|
|
2078
|
+
The traversal method.
|
|
2079
|
+
|
|
2080
|
+
#### Call Signature
|
|
2081
|
+
|
|
2082
|
+
```ts
|
|
2083
|
+
getRightMost(): K | undefined;
|
|
2084
|
+
```
|
|
2085
|
+
|
|
2086
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:1948](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L1948)
|
|
2087
|
+
|
|
2088
|
+
##### Returns
|
|
2089
|
+
|
|
2090
|
+
`K` \| `undefined`
|
|
2091
|
+
|
|
2092
|
+
##### Implementation of
|
|
2093
|
+
|
|
2094
|
+
```ts
|
|
2095
|
+
IBinaryTree.getRightMost
|
|
2096
|
+
```
|
|
2097
|
+
|
|
2098
|
+
#### Call Signature
|
|
2099
|
+
|
|
2100
|
+
```ts
|
|
2101
|
+
getRightMost<C>(
|
|
2102
|
+
callback,
|
|
2103
|
+
startNode?,
|
|
2104
|
+
iterationType?): ReturnType<C>;
|
|
2105
|
+
```
|
|
2106
|
+
|
|
2107
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:1950](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L1950)
|
|
2108
|
+
|
|
2109
|
+
##### Type Parameters
|
|
2110
|
+
|
|
2111
|
+
###### C
|
|
2112
|
+
|
|
2113
|
+
`C` *extends* `NodeCallback`\<[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\> \| `undefined`\>
|
|
2114
|
+
|
|
2115
|
+
##### Parameters
|
|
2116
|
+
|
|
2117
|
+
###### callback
|
|
2118
|
+
|
|
2119
|
+
`C`
|
|
2120
|
+
|
|
2121
|
+
###### startNode?
|
|
2122
|
+
|
|
2123
|
+
\| `K`
|
|
2124
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
2125
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
2126
|
+
\| `null`
|
|
2127
|
+
|
|
2128
|
+
###### iterationType?
|
|
2129
|
+
|
|
2130
|
+
`IterationType`
|
|
2131
|
+
|
|
2132
|
+
##### Returns
|
|
2133
|
+
|
|
2134
|
+
`ReturnType`\<`C`\>
|
|
2135
|
+
|
|
2136
|
+
##### Implementation of
|
|
2137
|
+
|
|
2138
|
+
```ts
|
|
2139
|
+
IBinaryTree.getRightMost
|
|
2140
|
+
```
|
|
2141
|
+
|
|
2142
|
+
***
|
|
2143
|
+
|
|
2144
|
+
### getSuccessor()
|
|
2145
|
+
|
|
2146
|
+
```ts
|
|
2147
|
+
getSuccessor(x?): BinaryTreeNode<K, V> | null | undefined;
|
|
2148
|
+
```
|
|
2149
|
+
|
|
2150
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:2022](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L2022)
|
|
2151
|
+
|
|
2152
|
+
Gets the in-order successor of a node in a BST.
|
|
2153
|
+
|
|
2154
|
+
#### Parameters
|
|
2155
|
+
|
|
2156
|
+
##### x?
|
|
2157
|
+
|
|
2158
|
+
`K` \| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\> \| `null`
|
|
2159
|
+
|
|
2160
|
+
The node to find the successor of.
|
|
2161
|
+
|
|
2162
|
+
#### Returns
|
|
2163
|
+
|
|
2164
|
+
[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\> \| `null` \| `undefined`
|
|
2165
|
+
|
|
2166
|
+
The successor node, or null/undefined if none exists.
|
|
2167
|
+
|
|
2168
|
+
#### Remarks
|
|
2169
|
+
|
|
2170
|
+
Time O(H), where H is the tree height. O(N) worst-case. Space O(H) (due to `getLeftMost` stack).
|
|
2171
|
+
|
|
2172
|
+
***
|
|
2173
|
+
|
|
2174
|
+
### has()
|
|
2175
|
+
|
|
2176
|
+
```ts
|
|
2177
|
+
has(
|
|
2178
|
+
keyNodeEntryOrPredicate?,
|
|
2179
|
+
startNode?,
|
|
2180
|
+
iterationType?): boolean;
|
|
2181
|
+
```
|
|
2182
|
+
|
|
2183
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:1434](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L1434)
|
|
2184
|
+
|
|
2185
|
+
Checks if a node matching the predicate exists in the tree.
|
|
2186
|
+
|
|
2187
|
+
#### Parameters
|
|
2188
|
+
|
|
2189
|
+
##### keyNodeEntryOrPredicate?
|
|
2190
|
+
|
|
2191
|
+
\| `K`
|
|
2192
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
2193
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
2194
|
+
\| `NodePredicate`\<[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>\>
|
|
2195
|
+
\| `null`
|
|
2196
|
+
|
|
2197
|
+
The key, node, entry, or predicate to check for.
|
|
2198
|
+
|
|
2199
|
+
##### startNode?
|
|
2200
|
+
|
|
2201
|
+
\| `K`
|
|
2202
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
2203
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
2204
|
+
\| `null`
|
|
2205
|
+
|
|
2206
|
+
The node to start the search from.
|
|
2207
|
+
|
|
2208
|
+
##### iterationType?
|
|
2209
|
+
|
|
2210
|
+
`IterationType`
|
|
2211
|
+
|
|
2212
|
+
The traversal method.
|
|
2213
|
+
|
|
2214
|
+
#### Returns
|
|
2215
|
+
|
|
2216
|
+
`boolean`
|
|
2217
|
+
|
|
2218
|
+
True if a matching node exists, false otherwise.
|
|
2219
|
+
|
|
2220
|
+
*
|
|
2221
|
+
|
|
2222
|
+
#### Remarks
|
|
2223
|
+
|
|
2224
|
+
Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). Time O(N) in the worst case (via `search`). Space O(H) or O(N) (via `search`).
|
|
2225
|
+
|
|
2226
|
+
#### Example
|
|
2227
|
+
|
|
2228
|
+
```ts
|
|
2229
|
+
// BinaryTree get and has operations
|
|
2230
|
+
const tree = new BinaryTree(
|
|
2231
|
+
[
|
|
2232
|
+
[5, 'five'],
|
|
2233
|
+
[3, 'three'],
|
|
2234
|
+
[7, 'seven'],
|
|
2235
|
+
[1, 'one'],
|
|
2236
|
+
[4, 'four'],
|
|
2237
|
+
[6, 'six'],
|
|
2238
|
+
[8, 'eight']
|
|
2239
|
+
],
|
|
2240
|
+
{ isMapMode: false }
|
|
2241
|
+
);
|
|
2242
|
+
|
|
2243
|
+
// Check if key exists
|
|
2244
|
+
console.log(tree.has(5)); // true;
|
|
2245
|
+
console.log(tree.has(10)); // false;
|
|
2246
|
+
|
|
2247
|
+
// Get value by key
|
|
2248
|
+
console.log(tree.get(3)); // 'three';
|
|
2249
|
+
console.log(tree.get(7)); // 'seven';
|
|
2250
|
+
console.log(tree.get(100)); // undefined;
|
|
2251
|
+
|
|
2252
|
+
// Get node structure
|
|
2253
|
+
const node = tree.getNode(5);
|
|
2254
|
+
console.log(node?.key); // 5;
|
|
2255
|
+
console.log(node?.value); // 'five';
|
|
2256
|
+
```
|
|
2257
|
+
|
|
2258
|
+
#### Implementation of
|
|
2259
|
+
|
|
2260
|
+
```ts
|
|
2261
|
+
IBinaryTree.has
|
|
2262
|
+
```
|
|
2263
|
+
|
|
2264
|
+
#### Overrides
|
|
2265
|
+
|
|
2266
|
+
[`IterableEntryBase`](IterableEntryBase.md).[`has`](IterableEntryBase.md#has)
|
|
2267
|
+
|
|
2268
|
+
***
|
|
2269
|
+
|
|
2270
|
+
### hasValue()
|
|
2271
|
+
|
|
2272
|
+
```ts
|
|
2273
|
+
hasValue(value): boolean;
|
|
2274
|
+
```
|
|
2275
|
+
|
|
2276
|
+
Defined in: [data-structures/base/iterable-entry-base.ts:143](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-entry-base.ts#L143)
|
|
2277
|
+
|
|
2278
|
+
Whether there exists an entry with the given value.
|
|
2279
|
+
|
|
2280
|
+
#### Parameters
|
|
2281
|
+
|
|
2282
|
+
##### value
|
|
2283
|
+
|
|
2284
|
+
`V` \| `undefined`
|
|
2285
|
+
|
|
2286
|
+
Value to test.
|
|
2287
|
+
|
|
2288
|
+
#### Returns
|
|
2289
|
+
|
|
2290
|
+
`boolean`
|
|
2291
|
+
|
|
2292
|
+
`true` if found; otherwise `false`.
|
|
2293
|
+
|
|
2294
|
+
#### Remarks
|
|
2295
|
+
|
|
2296
|
+
Time O(n), Space O(1)
|
|
2297
|
+
|
|
2298
|
+
#### Implementation of
|
|
2299
|
+
|
|
2300
|
+
```ts
|
|
2301
|
+
IBinaryTree.hasValue
|
|
2302
|
+
```
|
|
2303
|
+
|
|
2304
|
+
#### Inherited from
|
|
2305
|
+
|
|
2306
|
+
[`IterableEntryBase`](IterableEntryBase.md).[`hasValue`](IterableEntryBase.md#hasvalue)
|
|
2307
|
+
|
|
2308
|
+
***
|
|
2309
|
+
|
|
2310
|
+
### isBST()
|
|
2311
|
+
|
|
2312
|
+
```ts
|
|
2313
|
+
isBST(startNode?, iterationType?): boolean;
|
|
2314
|
+
```
|
|
2315
|
+
|
|
2316
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:1619](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L1619)
|
|
2317
|
+
|
|
2318
|
+
Checks if the tree is a valid Binary Search Tree (BST).
|
|
2319
|
+
|
|
2320
|
+
#### Parameters
|
|
2321
|
+
|
|
2322
|
+
##### startNode?
|
|
2323
|
+
|
|
2324
|
+
\| `K`
|
|
2325
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
2326
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
2327
|
+
\| `null`
|
|
2328
|
+
|
|
2329
|
+
The node to start checking from.
|
|
2330
|
+
|
|
2331
|
+
##### iterationType?
|
|
2332
|
+
|
|
2333
|
+
`IterationType` = `...`
|
|
2334
|
+
|
|
2335
|
+
The traversal method.
|
|
2336
|
+
|
|
2337
|
+
#### Returns
|
|
2338
|
+
|
|
2339
|
+
`boolean`
|
|
2340
|
+
|
|
2341
|
+
True if it's a valid BST, false otherwise.
|
|
2342
|
+
|
|
2343
|
+
*
|
|
2344
|
+
|
|
2345
|
+
#### Remarks
|
|
2346
|
+
|
|
2347
|
+
Time O(N), as it must visit every node. Space O(H) for the call stack (recursive) or explicit stack (iterative), where H is the tree height (O(N) worst-case).
|
|
2348
|
+
|
|
2349
|
+
#### Example
|
|
2350
|
+
|
|
2351
|
+
```ts
|
|
2352
|
+
// Check BST property
|
|
2353
|
+
const tree = new BinaryTree<number>([1, 2, 3]);
|
|
2354
|
+
// BinaryTree doesn't guarantee BST order
|
|
2355
|
+
console.log(typeof tree.isBST()); // 'boolean';
|
|
2356
|
+
```
|
|
2357
|
+
|
|
2358
|
+
#### Implementation of
|
|
2359
|
+
|
|
2360
|
+
```ts
|
|
2361
|
+
IBinaryTree.isBST
|
|
2362
|
+
```
|
|
2363
|
+
|
|
2364
|
+
***
|
|
2365
|
+
|
|
2366
|
+
### isEmpty()
|
|
2367
|
+
|
|
2368
|
+
```ts
|
|
2369
|
+
isEmpty(): boolean;
|
|
2370
|
+
```
|
|
2371
|
+
|
|
2372
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:1556](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L1556)
|
|
2373
|
+
|
|
2374
|
+
Checks if the tree is empty.
|
|
2375
|
+
|
|
2376
|
+
#### Returns
|
|
2377
|
+
|
|
2378
|
+
`boolean`
|
|
2379
|
+
|
|
2380
|
+
True if the tree has no nodes, false otherwise.
|
|
2381
|
+
|
|
2382
|
+
*
|
|
2383
|
+
|
|
2384
|
+
#### Remarks
|
|
2385
|
+
|
|
2386
|
+
Time O(1), Space O(1)
|
|
2387
|
+
|
|
2388
|
+
#### Example
|
|
2389
|
+
|
|
2390
|
+
```ts
|
|
2391
|
+
// Check empty
|
|
2392
|
+
console.log(new BinaryTree().isEmpty()); // true;
|
|
2393
|
+
```
|
|
2394
|
+
|
|
2395
|
+
#### Implementation of
|
|
2396
|
+
|
|
2397
|
+
```ts
|
|
2398
|
+
IBinaryTree.isEmpty
|
|
2399
|
+
```
|
|
2400
|
+
|
|
2401
|
+
#### Overrides
|
|
2402
|
+
|
|
2403
|
+
[`IterableEntryBase`](IterableEntryBase.md).[`isEmpty`](IterableEntryBase.md#isempty)
|
|
2404
|
+
|
|
2405
|
+
***
|
|
2406
|
+
|
|
2407
|
+
### isEntry()
|
|
2408
|
+
|
|
2409
|
+
```ts
|
|
2410
|
+
isEntry(keyNodeOrEntry): keyNodeOrEntry is BTNEntry<K, V>;
|
|
2411
|
+
```
|
|
2412
|
+
|
|
2413
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:545](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L545)
|
|
2414
|
+
|
|
2415
|
+
Checks if the given item is a [key, value] entry pair.
|
|
2416
|
+
|
|
2417
|
+
#### Parameters
|
|
2418
|
+
|
|
2419
|
+
##### keyNodeOrEntry
|
|
2420
|
+
|
|
2421
|
+
\| `K`
|
|
2422
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
2423
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
2424
|
+
\| `null`
|
|
2425
|
+
\| `undefined`
|
|
2426
|
+
|
|
2427
|
+
The item to check.
|
|
2428
|
+
|
|
2429
|
+
#### Returns
|
|
2430
|
+
|
|
2431
|
+
`keyNodeOrEntry is BTNEntry<K, V>`
|
|
2432
|
+
|
|
2433
|
+
True if it's an entry, false otherwise.
|
|
2434
|
+
|
|
2435
|
+
#### Remarks
|
|
2436
|
+
|
|
2437
|
+
Time O(1), Space O(1)
|
|
2438
|
+
|
|
2439
|
+
***
|
|
2440
|
+
|
|
2441
|
+
### isLeaf()
|
|
2442
|
+
|
|
2443
|
+
```ts
|
|
2444
|
+
isLeaf(keyNodeOrEntry): boolean;
|
|
2445
|
+
```
|
|
2446
|
+
|
|
2447
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:531](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L531)
|
|
2448
|
+
|
|
2449
|
+
Checks if a node is a leaf (has no real children).
|
|
2450
|
+
|
|
2451
|
+
#### Parameters
|
|
2452
|
+
|
|
2453
|
+
##### keyNodeOrEntry
|
|
2454
|
+
|
|
2455
|
+
\| `K`
|
|
2456
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
2457
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
2458
|
+
\| `null`
|
|
2459
|
+
\| `undefined`
|
|
2460
|
+
|
|
2461
|
+
The node to check.
|
|
2462
|
+
|
|
2463
|
+
#### Returns
|
|
2464
|
+
|
|
2465
|
+
`boolean`
|
|
2466
|
+
|
|
2467
|
+
True if the node is a leaf, false otherwise.
|
|
2468
|
+
|
|
2469
|
+
#### Remarks
|
|
2470
|
+
|
|
2471
|
+
Time O(N) if a key/entry is passed (due to `ensureNode`). O(1) if a node is passed. Space O(1) or O(H) (from `ensureNode`).
|
|
2472
|
+
|
|
2473
|
+
***
|
|
2474
|
+
|
|
2475
|
+
### isNIL()
|
|
2476
|
+
|
|
2477
|
+
```ts
|
|
2478
|
+
isNIL(keyNodeOrEntry): boolean;
|
|
2479
|
+
```
|
|
2480
|
+
|
|
2481
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:500](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L500)
|
|
2482
|
+
|
|
2483
|
+
Checks if the given item is the sentinel NIL node.
|
|
2484
|
+
|
|
2485
|
+
#### Parameters
|
|
2486
|
+
|
|
2487
|
+
##### keyNodeOrEntry
|
|
2488
|
+
|
|
2489
|
+
\| `K`
|
|
2490
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
2491
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
2492
|
+
\| `null`
|
|
2493
|
+
\| `undefined`
|
|
2494
|
+
|
|
2495
|
+
The item to check.
|
|
2496
|
+
|
|
2497
|
+
#### Returns
|
|
2498
|
+
|
|
2499
|
+
`boolean`
|
|
2500
|
+
|
|
2501
|
+
True if it's the NIL node, false otherwise.
|
|
2502
|
+
|
|
2503
|
+
#### Remarks
|
|
2504
|
+
|
|
2505
|
+
Time O(1), Space O(1)
|
|
2506
|
+
|
|
2507
|
+
***
|
|
2508
|
+
|
|
2509
|
+
### isNode()
|
|
2510
|
+
|
|
2511
|
+
```ts
|
|
2512
|
+
isNode(keyNodeOrEntry): keyNodeOrEntry is BinaryTreeNode<K, V>;
|
|
2513
|
+
```
|
|
2514
|
+
|
|
2515
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:447](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L447)
|
|
2516
|
+
|
|
2517
|
+
Checks if the given item is a `BinaryTreeNode` instance.
|
|
2518
|
+
|
|
2519
|
+
#### Parameters
|
|
2520
|
+
|
|
2521
|
+
##### keyNodeOrEntry
|
|
2522
|
+
|
|
2523
|
+
\| `K`
|
|
2524
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
2525
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
2526
|
+
\| `null`
|
|
2527
|
+
\| `undefined`
|
|
2528
|
+
|
|
2529
|
+
The item to check.
|
|
2530
|
+
|
|
2531
|
+
#### Returns
|
|
2532
|
+
|
|
2533
|
+
`keyNodeOrEntry is BinaryTreeNode<K, V>`
|
|
2534
|
+
|
|
2535
|
+
True if it's a node, false otherwise.
|
|
2536
|
+
|
|
2537
|
+
#### Remarks
|
|
2538
|
+
|
|
2539
|
+
Time O(1), Space O(1)
|
|
2540
|
+
|
|
2541
|
+
***
|
|
2542
|
+
|
|
2543
|
+
### isPerfectlyBalanced()
|
|
2544
|
+
|
|
2545
|
+
```ts
|
|
2546
|
+
isPerfectlyBalanced(startNode?): boolean;
|
|
2547
|
+
```
|
|
2548
|
+
|
|
2549
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:1567](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L1567)
|
|
2550
|
+
|
|
2551
|
+
Checks if the tree is perfectly balanced.
|
|
2552
|
+
|
|
2553
|
+
#### Parameters
|
|
2554
|
+
|
|
2555
|
+
##### startNode?
|
|
2556
|
+
|
|
2557
|
+
\| `K`
|
|
2558
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
2559
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
2560
|
+
\| `null`
|
|
2561
|
+
|
|
2562
|
+
The node to start checking from.
|
|
2563
|
+
|
|
2564
|
+
#### Returns
|
|
2565
|
+
|
|
2566
|
+
`boolean`
|
|
2567
|
+
|
|
2568
|
+
True if perfectly balanced, false otherwise.
|
|
2569
|
+
|
|
2570
|
+
#### Remarks
|
|
2571
|
+
|
|
2572
|
+
A tree is perfectly balanced if the difference between min and max height is at most 1. Time O(N), as it requires two full traversals (`getMinHeight` and `getHeight`). Space O(H) or O(N) (from height calculation).
|
|
2573
|
+
|
|
2574
|
+
#### Implementation of
|
|
2575
|
+
|
|
2576
|
+
```ts
|
|
2577
|
+
IBinaryTree.isPerfectlyBalanced
|
|
2578
|
+
```
|
|
2579
|
+
|
|
2580
|
+
***
|
|
2581
|
+
|
|
2582
|
+
### isRange()
|
|
2583
|
+
|
|
2584
|
+
```ts
|
|
2585
|
+
isRange(keyNodeEntryOrPredicate): keyNodeEntryOrPredicate is Range<K>;
|
|
2586
|
+
```
|
|
2587
|
+
|
|
2588
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:511](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L511)
|
|
2589
|
+
|
|
2590
|
+
Checks if the given item is a `Range` object.
|
|
2591
|
+
|
|
2592
|
+
#### Parameters
|
|
2593
|
+
|
|
2594
|
+
##### keyNodeEntryOrPredicate
|
|
2595
|
+
|
|
2596
|
+
\| `K`
|
|
2597
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
2598
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
2599
|
+
\| `NodePredicate`\<[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>\>
|
|
2600
|
+
\| `Range`\<`K`\>
|
|
2601
|
+
\| `null`
|
|
2602
|
+
\| `undefined`
|
|
2603
|
+
|
|
2604
|
+
The item to check.
|
|
2605
|
+
|
|
2606
|
+
#### Returns
|
|
2607
|
+
|
|
2608
|
+
`keyNodeEntryOrPredicate is Range<K>`
|
|
2609
|
+
|
|
2610
|
+
True if it's a Range, false otherwise.
|
|
2611
|
+
|
|
2612
|
+
#### Remarks
|
|
2613
|
+
|
|
2614
|
+
Time O(1), Space O(1)
|
|
2615
|
+
|
|
2616
|
+
***
|
|
2617
|
+
|
|
2618
|
+
### isRaw()
|
|
2619
|
+
|
|
2620
|
+
```ts
|
|
2621
|
+
isRaw(keyNodeEntryOrRaw): keyNodeEntryOrRaw is R;
|
|
2622
|
+
```
|
|
2623
|
+
|
|
2624
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:460](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L460)
|
|
2625
|
+
|
|
2626
|
+
Checks if the given item is a raw data object (R) that needs conversion via `toEntryFn`.
|
|
2627
|
+
|
|
2628
|
+
#### Parameters
|
|
2629
|
+
|
|
2630
|
+
##### keyNodeEntryOrRaw
|
|
2631
|
+
|
|
2632
|
+
\| `K`
|
|
2633
|
+
\| `R`
|
|
2634
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
2635
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
2636
|
+
\| `null`
|
|
2637
|
+
\| `undefined`
|
|
2638
|
+
|
|
2639
|
+
The item to check.
|
|
2640
|
+
|
|
2641
|
+
#### Returns
|
|
2642
|
+
|
|
2643
|
+
`keyNodeEntryOrRaw is R`
|
|
2644
|
+
|
|
2645
|
+
True if it's a raw object, false otherwise.
|
|
2646
|
+
|
|
2647
|
+
#### Remarks
|
|
2648
|
+
|
|
2649
|
+
Time O(1), Space O(1)
|
|
2650
|
+
|
|
2651
|
+
***
|
|
2652
|
+
|
|
2653
|
+
### isRealNode()
|
|
2654
|
+
|
|
2655
|
+
```ts
|
|
2656
|
+
isRealNode(keyNodeOrEntry): keyNodeOrEntry is BinaryTreeNode<K, V>;
|
|
2657
|
+
```
|
|
2658
|
+
|
|
2659
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:473](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L473)
|
|
2660
|
+
|
|
2661
|
+
Checks if the given item is a "real" node (i.e., not null, undefined, or NIL).
|
|
2662
|
+
|
|
2663
|
+
#### Parameters
|
|
2664
|
+
|
|
2665
|
+
##### keyNodeOrEntry
|
|
2666
|
+
|
|
2667
|
+
\| `K`
|
|
2668
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
2669
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
2670
|
+
\| `null`
|
|
2671
|
+
\| `undefined`
|
|
2672
|
+
|
|
2673
|
+
The item to check.
|
|
2674
|
+
|
|
2675
|
+
#### Returns
|
|
2676
|
+
|
|
2677
|
+
`keyNodeOrEntry is BinaryTreeNode<K, V>`
|
|
2678
|
+
|
|
2679
|
+
True if it's a real node, false otherwise.
|
|
2680
|
+
|
|
2681
|
+
#### Remarks
|
|
2682
|
+
|
|
2683
|
+
Time O(1), Space O(1)
|
|
2684
|
+
|
|
2685
|
+
***
|
|
2686
|
+
|
|
2687
|
+
### isRealNodeOrNull()
|
|
2688
|
+
|
|
2689
|
+
```ts
|
|
2690
|
+
isRealNodeOrNull(keyNodeOrEntry): keyNodeOrEntry is BinaryTreeNode<K, V> | null;
|
|
2691
|
+
```
|
|
2692
|
+
|
|
2693
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:487](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L487)
|
|
2694
|
+
|
|
2695
|
+
Checks if the given item is either a "real" node or null.
|
|
2696
|
+
|
|
2697
|
+
#### Parameters
|
|
2698
|
+
|
|
2699
|
+
##### keyNodeOrEntry
|
|
2700
|
+
|
|
2701
|
+
\| `K`
|
|
2702
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
2703
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
2704
|
+
\| `null`
|
|
2705
|
+
\| `undefined`
|
|
2706
|
+
|
|
2707
|
+
The item to check.
|
|
2708
|
+
|
|
2709
|
+
#### Returns
|
|
2710
|
+
|
|
2711
|
+
keyNodeOrEntry is BinaryTreeNode\<K, V\> \| null
|
|
2712
|
+
|
|
2713
|
+
True if it's a real node or null, false otherwise.
|
|
2714
|
+
|
|
2715
|
+
#### Remarks
|
|
2716
|
+
|
|
2717
|
+
Time O(1), Space O(1)
|
|
2718
|
+
|
|
2719
|
+
***
|
|
2720
|
+
|
|
2721
|
+
### isValidKey()
|
|
2722
|
+
|
|
2723
|
+
```ts
|
|
2724
|
+
isValidKey(key): key is K;
|
|
2725
|
+
```
|
|
2726
|
+
|
|
2727
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:558](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L558)
|
|
2728
|
+
|
|
2729
|
+
Checks if the given key is valid (comparable or null).
|
|
2730
|
+
|
|
2731
|
+
#### Parameters
|
|
2732
|
+
|
|
2733
|
+
##### key
|
|
2734
|
+
|
|
2735
|
+
`unknown`
|
|
2736
|
+
|
|
2737
|
+
The key to validate.
|
|
2738
|
+
|
|
2739
|
+
#### Returns
|
|
2740
|
+
|
|
2741
|
+
`key is K`
|
|
2742
|
+
|
|
2743
|
+
True if the key is valid, false otherwise.
|
|
2744
|
+
|
|
2745
|
+
#### Remarks
|
|
2746
|
+
|
|
2747
|
+
Time O(1), Space O(1)
|
|
2748
|
+
|
|
2749
|
+
***
|
|
2750
|
+
|
|
2751
|
+
### keys()
|
|
2752
|
+
|
|
2753
|
+
```ts
|
|
2754
|
+
keys(): IterableIterator<K>;
|
|
2755
|
+
```
|
|
2756
|
+
|
|
2757
|
+
Defined in: [data-structures/base/iterable-entry-base.ts:42](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-entry-base.ts#L42)
|
|
2758
|
+
|
|
2759
|
+
Iterate over keys only.
|
|
2760
|
+
|
|
2761
|
+
#### Returns
|
|
2762
|
+
|
|
2763
|
+
`IterableIterator`\<`K`\>
|
|
2764
|
+
|
|
2765
|
+
Iterator of keys.
|
|
2766
|
+
|
|
2767
|
+
#### Remarks
|
|
2768
|
+
|
|
2769
|
+
Time O(n), Space O(1)
|
|
2770
|
+
|
|
2771
|
+
#### Implementation of
|
|
2772
|
+
|
|
2773
|
+
```ts
|
|
2774
|
+
IBinaryTree.keys
|
|
2775
|
+
```
|
|
2776
|
+
|
|
2777
|
+
#### Inherited from
|
|
2778
|
+
|
|
2779
|
+
[`IterableEntryBase`](IterableEntryBase.md).[`keys`](IterableEntryBase.md#keys)
|
|
2780
|
+
|
|
2781
|
+
***
|
|
2782
|
+
|
|
2783
|
+
### leaves()
|
|
2784
|
+
|
|
2785
|
+
Finds all leaf nodes in the tree.
|
|
2786
|
+
|
|
2787
|
+
#### Remarks
|
|
2788
|
+
|
|
2789
|
+
Time O(N), visits every node. Space O(H) for recursive or iterative stack.
|
|
2790
|
+
|
|
2791
|
+
#### Template
|
|
2792
|
+
|
|
2793
|
+
The type of the callback function.
|
|
2794
|
+
|
|
2795
|
+
#### Param
|
|
2796
|
+
|
|
2797
|
+
Function to call on each leaf node.
|
|
2798
|
+
|
|
2799
|
+
#### Param
|
|
2800
|
+
|
|
2801
|
+
The node to start from.
|
|
2802
|
+
|
|
2803
|
+
#### Param
|
|
2804
|
+
|
|
2805
|
+
The traversal method.
|
|
2806
|
+
|
|
2807
|
+
#### Call Signature
|
|
2808
|
+
|
|
2809
|
+
```ts
|
|
2810
|
+
leaves(): (K | undefined)[];
|
|
2811
|
+
```
|
|
2812
|
+
|
|
2813
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:2316](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L2316)
|
|
2814
|
+
|
|
2815
|
+
Get leaf nodes
|
|
2816
|
+
|
|
2817
|
+
*
|
|
2818
|
+
|
|
2819
|
+
##### Returns
|
|
2820
|
+
|
|
2821
|
+
(`K` \| `undefined`)[]
|
|
2822
|
+
|
|
2823
|
+
##### Example
|
|
2824
|
+
|
|
2825
|
+
```ts
|
|
2826
|
+
// Get leaf nodes
|
|
2827
|
+
const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
2828
|
+
const leafKeys = tree.leaves(node => node.key);
|
|
2829
|
+
console.log(leafKeys.length); // > 0;
|
|
2830
|
+
```
|
|
2831
|
+
|
|
2832
|
+
##### Implementation of
|
|
2833
|
+
|
|
2834
|
+
```ts
|
|
2835
|
+
IBinaryTree.leaves
|
|
2836
|
+
```
|
|
2837
|
+
|
|
2838
|
+
#### Call Signature
|
|
2839
|
+
|
|
2840
|
+
```ts
|
|
2841
|
+
leaves<C>(
|
|
2842
|
+
callback,
|
|
2843
|
+
startNode?,
|
|
2844
|
+
iterationType?): ReturnType<C>[];
|
|
2845
|
+
```
|
|
2846
|
+
|
|
2847
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:2318](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L2318)
|
|
2848
|
+
|
|
2849
|
+
Get leaf nodes
|
|
2850
|
+
|
|
2851
|
+
*
|
|
2852
|
+
|
|
2853
|
+
##### Type Parameters
|
|
2854
|
+
|
|
2855
|
+
###### C
|
|
2856
|
+
|
|
2857
|
+
`C` *extends* `NodeCallback`\<[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>\>
|
|
2858
|
+
|
|
2859
|
+
##### Parameters
|
|
2860
|
+
|
|
2861
|
+
###### callback
|
|
2862
|
+
|
|
2863
|
+
`C`
|
|
2864
|
+
|
|
2865
|
+
###### startNode?
|
|
2866
|
+
|
|
2867
|
+
\| `K`
|
|
2868
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
2869
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
2870
|
+
\| `null`
|
|
2871
|
+
|
|
2872
|
+
###### iterationType?
|
|
2873
|
+
|
|
2874
|
+
`IterationType`
|
|
2875
|
+
|
|
2876
|
+
##### Returns
|
|
2877
|
+
|
|
2878
|
+
`ReturnType`\<`C`\>[]
|
|
2879
|
+
|
|
2880
|
+
##### Example
|
|
2881
|
+
|
|
2882
|
+
```ts
|
|
2883
|
+
// Get leaf nodes
|
|
2884
|
+
const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
2885
|
+
const leafKeys = tree.leaves(node => node.key);
|
|
2886
|
+
console.log(leafKeys.length); // > 0;
|
|
2887
|
+
```
|
|
2888
|
+
|
|
2889
|
+
##### Implementation of
|
|
2890
|
+
|
|
2891
|
+
```ts
|
|
2892
|
+
IBinaryTree.leaves
|
|
2893
|
+
```
|
|
2894
|
+
|
|
2895
|
+
***
|
|
2896
|
+
|
|
2897
|
+
### listLevels()
|
|
2898
|
+
|
|
2899
|
+
Returns a 2D array of nodes, grouped by level.
|
|
2900
|
+
|
|
2901
|
+
#### Remarks
|
|
2902
|
+
|
|
2903
|
+
Time O(N), visits every node. Space O(N) for the result array and the queue/stack.
|
|
2904
|
+
|
|
2905
|
+
#### Template
|
|
2906
|
+
|
|
2907
|
+
The type of the callback function.
|
|
2908
|
+
|
|
2909
|
+
#### Param
|
|
2910
|
+
|
|
2911
|
+
Function to call on each node.
|
|
2912
|
+
|
|
2913
|
+
#### Param
|
|
2914
|
+
|
|
2915
|
+
The node to start from.
|
|
2916
|
+
|
|
2917
|
+
#### Param
|
|
2918
|
+
|
|
2919
|
+
The traversal method.
|
|
2920
|
+
|
|
2921
|
+
#### Param
|
|
2922
|
+
|
|
2923
|
+
If true, includes null nodes.
|
|
2924
|
+
|
|
2925
|
+
#### Call Signature
|
|
2926
|
+
|
|
2927
|
+
```ts
|
|
2928
|
+
listLevels(): (K | undefined)[][];
|
|
2929
|
+
```
|
|
2930
|
+
|
|
2931
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:2417](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L2417)
|
|
2932
|
+
|
|
2933
|
+
Level-order grouping
|
|
2934
|
+
|
|
2935
|
+
*
|
|
2936
|
+
|
|
2937
|
+
##### Returns
|
|
2938
|
+
|
|
2939
|
+
(`K` \| `undefined`)[][]
|
|
2940
|
+
|
|
2941
|
+
##### Example
|
|
2942
|
+
|
|
2943
|
+
```ts
|
|
2944
|
+
// Level-order grouping
|
|
2945
|
+
const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
2946
|
+
const levels = tree.listLevels(node => node.key);
|
|
2947
|
+
console.log(levels[0]); // [1];
|
|
2948
|
+
console.log(levels[1].sort()); // [2, 3];
|
|
2949
|
+
```
|
|
2950
|
+
|
|
2951
|
+
##### Implementation of
|
|
2952
|
+
|
|
2953
|
+
```ts
|
|
2954
|
+
IBinaryTree.listLevels
|
|
2955
|
+
```
|
|
2956
|
+
|
|
2957
|
+
#### Call Signature
|
|
2958
|
+
|
|
2959
|
+
```ts
|
|
2960
|
+
listLevels<C>(
|
|
2961
|
+
callback?,
|
|
2962
|
+
startNode?,
|
|
2963
|
+
iterationType?,
|
|
2964
|
+
includeNull?): ReturnType<C>[][];
|
|
2965
|
+
```
|
|
2966
|
+
|
|
2967
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:2419](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L2419)
|
|
2968
|
+
|
|
2969
|
+
Level-order grouping
|
|
2970
|
+
|
|
2971
|
+
*
|
|
2972
|
+
|
|
2973
|
+
##### Type Parameters
|
|
2974
|
+
|
|
2975
|
+
###### C
|
|
2976
|
+
|
|
2977
|
+
`C` *extends* `NodeCallback`\<[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>\>
|
|
2978
|
+
|
|
2979
|
+
##### Parameters
|
|
2980
|
+
|
|
2981
|
+
###### callback?
|
|
2982
|
+
|
|
2983
|
+
`C`
|
|
2984
|
+
|
|
2985
|
+
###### startNode?
|
|
2986
|
+
|
|
2987
|
+
\| `K`
|
|
2988
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
2989
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
2990
|
+
\| `null`
|
|
2991
|
+
|
|
2992
|
+
###### iterationType?
|
|
2993
|
+
|
|
2994
|
+
`IterationType`
|
|
2995
|
+
|
|
2996
|
+
###### includeNull?
|
|
2997
|
+
|
|
2998
|
+
`false`
|
|
2999
|
+
|
|
3000
|
+
##### Returns
|
|
3001
|
+
|
|
3002
|
+
`ReturnType`\<`C`\>[][]
|
|
3003
|
+
|
|
3004
|
+
##### Example
|
|
3005
|
+
|
|
3006
|
+
```ts
|
|
3007
|
+
// Level-order grouping
|
|
3008
|
+
const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
3009
|
+
const levels = tree.listLevels(node => node.key);
|
|
3010
|
+
console.log(levels[0]); // [1];
|
|
3011
|
+
console.log(levels[1].sort()); // [2, 3];
|
|
3012
|
+
```
|
|
3013
|
+
|
|
3014
|
+
##### Implementation of
|
|
3015
|
+
|
|
3016
|
+
```ts
|
|
3017
|
+
IBinaryTree.listLevels
|
|
3018
|
+
```
|
|
3019
|
+
|
|
3020
|
+
#### Call Signature
|
|
3021
|
+
|
|
3022
|
+
```ts
|
|
3023
|
+
listLevels<C>(
|
|
3024
|
+
callback?,
|
|
3025
|
+
startNode?,
|
|
3026
|
+
iterationType?,
|
|
3027
|
+
includeNull?): ReturnType<C>[][];
|
|
3028
|
+
```
|
|
3029
|
+
|
|
3030
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:2426](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L2426)
|
|
3031
|
+
|
|
3032
|
+
Level-order grouping
|
|
3033
|
+
|
|
3034
|
+
*
|
|
3035
|
+
|
|
3036
|
+
##### Type Parameters
|
|
3037
|
+
|
|
3038
|
+
###### C
|
|
3039
|
+
|
|
3040
|
+
`C` *extends* `NodeCallback`\<[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\> \| `null`\>
|
|
3041
|
+
|
|
3042
|
+
##### Parameters
|
|
3043
|
+
|
|
3044
|
+
###### callback?
|
|
3045
|
+
|
|
3046
|
+
`C`
|
|
3047
|
+
|
|
3048
|
+
###### startNode?
|
|
3049
|
+
|
|
3050
|
+
\| `K`
|
|
3051
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
3052
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
3053
|
+
\| `null`
|
|
3054
|
+
|
|
3055
|
+
###### iterationType?
|
|
3056
|
+
|
|
3057
|
+
`IterationType`
|
|
3058
|
+
|
|
3059
|
+
###### includeNull?
|
|
3060
|
+
|
|
3061
|
+
`true`
|
|
3062
|
+
|
|
3063
|
+
##### Returns
|
|
3064
|
+
|
|
3065
|
+
`ReturnType`\<`C`\>[][]
|
|
3066
|
+
|
|
3067
|
+
##### Example
|
|
3068
|
+
|
|
3069
|
+
```ts
|
|
3070
|
+
// Level-order grouping
|
|
3071
|
+
const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
3072
|
+
const levels = tree.listLevels(node => node.key);
|
|
3073
|
+
console.log(levels[0]); // [1];
|
|
3074
|
+
console.log(levels[1].sort()); // [2, 3];
|
|
3075
|
+
```
|
|
3076
|
+
|
|
3077
|
+
##### Implementation of
|
|
3078
|
+
|
|
3079
|
+
```ts
|
|
3080
|
+
IBinaryTree.listLevels
|
|
3081
|
+
```
|
|
3082
|
+
|
|
3083
|
+
***
|
|
3084
|
+
|
|
3085
|
+
### map()
|
|
3086
|
+
|
|
3087
|
+
```ts
|
|
3088
|
+
map<MK, MV, MR>(
|
|
3089
|
+
cb,
|
|
3090
|
+
options?,
|
|
3091
|
+
thisArg?): BinaryTree<MK, MV, MR>;
|
|
3092
|
+
```
|
|
3093
|
+
|
|
3094
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:2806](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L2806)
|
|
3095
|
+
|
|
3096
|
+
Creates a new tree by mapping each [key, value] pair to a new entry.
|
|
3097
|
+
|
|
3098
|
+
#### Type Parameters
|
|
3099
|
+
|
|
3100
|
+
##### MK
|
|
3101
|
+
|
|
3102
|
+
`MK` = `K`
|
|
3103
|
+
|
|
3104
|
+
New key type.
|
|
3105
|
+
|
|
3106
|
+
##### MV
|
|
3107
|
+
|
|
3108
|
+
`MV` = `V`
|
|
3109
|
+
|
|
3110
|
+
New value type.
|
|
3111
|
+
|
|
3112
|
+
##### MR
|
|
3113
|
+
|
|
3114
|
+
`MR` = `any`
|
|
3115
|
+
|
|
3116
|
+
New raw type.
|
|
3117
|
+
|
|
3118
|
+
#### Parameters
|
|
3119
|
+
|
|
3120
|
+
##### cb
|
|
3121
|
+
|
|
3122
|
+
`EntryCallback`\<`K`, `V` \| `undefined`, \[`MK`, `MV`\]\>
|
|
3123
|
+
|
|
3124
|
+
A function to map each [key, value] pair.
|
|
3125
|
+
|
|
3126
|
+
##### options?
|
|
3127
|
+
|
|
3128
|
+
`Partial`\<`BinaryTreeOptions`\<`MK`, `MV`, `MR`\>\>
|
|
3129
|
+
|
|
3130
|
+
Options for the new tree.
|
|
3131
|
+
|
|
3132
|
+
##### thisArg?
|
|
3133
|
+
|
|
3134
|
+
`unknown`
|
|
3135
|
+
|
|
3136
|
+
`this` context for the callback.
|
|
3137
|
+
|
|
3138
|
+
#### Returns
|
|
3139
|
+
|
|
3140
|
+
`BinaryTree`\<`MK`, `MV`, `MR`\>
|
|
3141
|
+
|
|
3142
|
+
A new, mapped tree.
|
|
3143
|
+
|
|
3144
|
+
*
|
|
3145
|
+
|
|
3146
|
+
#### Remarks
|
|
3147
|
+
|
|
3148
|
+
Time O(N * M), where N is nodes in this tree, and M is size of the new tree during insertion. Space O(N) for the new tree.
|
|
3149
|
+
|
|
3150
|
+
#### Example
|
|
3151
|
+
|
|
3152
|
+
```ts
|
|
3153
|
+
// Transform to new tree
|
|
3154
|
+
const tree = new BinaryTree<number, number>([[1, 10], [2, 20]]);
|
|
3155
|
+
const mapped = tree.map((v, key) => [key, (v ?? 0) + 1] as [number, number]);
|
|
3156
|
+
console.log([...mapped.values()]); // contains 11;
|
|
3157
|
+
```
|
|
3158
|
+
|
|
3159
|
+
#### Implementation of
|
|
3160
|
+
|
|
3161
|
+
```ts
|
|
3162
|
+
IBinaryTree.map
|
|
3163
|
+
```
|
|
3164
|
+
|
|
3165
|
+
#### Overrides
|
|
3166
|
+
|
|
3167
|
+
[`IterableEntryBase`](IterableEntryBase.md).[`map`](IterableEntryBase.md#map)
|
|
3168
|
+
|
|
3169
|
+
***
|
|
3170
|
+
|
|
3171
|
+
### merge()
|
|
3172
|
+
|
|
3173
|
+
```ts
|
|
3174
|
+
merge(anotherTree): void;
|
|
3175
|
+
```
|
|
3176
|
+
|
|
3177
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:902](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L902)
|
|
3178
|
+
|
|
3179
|
+
Merges another tree into this one by seting all its nodes.
|
|
3180
|
+
|
|
3181
|
+
#### Parameters
|
|
3182
|
+
|
|
3183
|
+
##### anotherTree
|
|
3184
|
+
|
|
3185
|
+
`BinaryTree`\<`K`, `V`, `R`\>
|
|
3186
|
+
|
|
3187
|
+
The tree to merge.
|
|
3188
|
+
|
|
3189
|
+
*
|
|
3190
|
+
|
|
3191
|
+
#### Returns
|
|
3192
|
+
|
|
3193
|
+
`void`
|
|
3194
|
+
|
|
3195
|
+
#### Remarks
|
|
3196
|
+
|
|
3197
|
+
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`).
|
|
3198
|
+
|
|
3199
|
+
#### Example
|
|
3200
|
+
|
|
3201
|
+
```ts
|
|
3202
|
+
// Combine trees
|
|
3203
|
+
const t1 = new BinaryTree<number>([1, 2]);
|
|
3204
|
+
const t2 = new BinaryTree<number>([3, 4]);
|
|
3205
|
+
t1.merge(t2);
|
|
3206
|
+
console.log(t1.size); // 4;
|
|
3207
|
+
```
|
|
3208
|
+
|
|
3209
|
+
#### Implementation of
|
|
3210
|
+
|
|
3211
|
+
```ts
|
|
3212
|
+
IBinaryTree.merge
|
|
3213
|
+
```
|
|
3214
|
+
|
|
3215
|
+
***
|
|
3216
|
+
|
|
3217
|
+
### morris()
|
|
3218
|
+
|
|
3219
|
+
Performs a Morris (threaded) traversal.
|
|
3220
|
+
|
|
3221
|
+
#### Remarks
|
|
3222
|
+
|
|
3223
|
+
This traversal uses O(1) extra space (excluding the result array) by temporarily modifying the tree's right child pointers. Time O(N), as each node is visited a constant number of times. Space O(1) (excluding the `ans` array).
|
|
3224
|
+
|
|
3225
|
+
#### Template
|
|
3226
|
+
|
|
3227
|
+
The type of the callback function.
|
|
3228
|
+
|
|
3229
|
+
#### Param
|
|
3230
|
+
|
|
3231
|
+
Function to call on each node.
|
|
3232
|
+
|
|
3233
|
+
#### Param
|
|
3234
|
+
|
|
3235
|
+
The traversal order ('IN', 'PRE', 'POST').
|
|
3236
|
+
|
|
3237
|
+
#### Param
|
|
3238
|
+
|
|
3239
|
+
The node to start from.
|
|
3240
|
+
|
|
3241
|
+
#### Call Signature
|
|
3242
|
+
|
|
3243
|
+
```ts
|
|
3244
|
+
morris(): (K | undefined)[];
|
|
3245
|
+
```
|
|
3246
|
+
|
|
3247
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:2534](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L2534)
|
|
3248
|
+
|
|
3249
|
+
Morris traversal (O(1) space)
|
|
3250
|
+
|
|
3251
|
+
*
|
|
3252
|
+
|
|
3253
|
+
##### Returns
|
|
3254
|
+
|
|
3255
|
+
(`K` \| `undefined`)[]
|
|
3256
|
+
|
|
3257
|
+
##### Example
|
|
3258
|
+
|
|
3259
|
+
```ts
|
|
3260
|
+
// Morris traversal (O(1) space)
|
|
3261
|
+
const tree = new BinaryTree<number>([1, 2, 3]);
|
|
3262
|
+
const result = tree.morris(node => node.key, 'IN');
|
|
3263
|
+
console.log(result.length); // 3;
|
|
3264
|
+
```
|
|
3265
|
+
|
|
3266
|
+
##### Implementation of
|
|
3267
|
+
|
|
3268
|
+
```ts
|
|
3269
|
+
IBinaryTree.morris
|
|
3270
|
+
```
|
|
3271
|
+
|
|
3272
|
+
#### Call Signature
|
|
3273
|
+
|
|
3274
|
+
```ts
|
|
3275
|
+
morris<C>(
|
|
3276
|
+
callback?,
|
|
3277
|
+
pattern?,
|
|
3278
|
+
startNode?): ReturnType<C>[];
|
|
3279
|
+
```
|
|
3280
|
+
|
|
3281
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:2536](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L2536)
|
|
3282
|
+
|
|
3283
|
+
Morris traversal (O(1) space)
|
|
3284
|
+
|
|
3285
|
+
*
|
|
3286
|
+
|
|
3287
|
+
##### Type Parameters
|
|
3288
|
+
|
|
3289
|
+
###### C
|
|
3290
|
+
|
|
3291
|
+
`C` *extends* `NodeCallback`\<[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>\>
|
|
3292
|
+
|
|
3293
|
+
##### Parameters
|
|
3294
|
+
|
|
3295
|
+
###### callback?
|
|
3296
|
+
|
|
3297
|
+
`C`
|
|
3298
|
+
|
|
3299
|
+
###### pattern?
|
|
3300
|
+
|
|
3301
|
+
`DFSOrderPattern`
|
|
3302
|
+
|
|
3303
|
+
###### startNode?
|
|
3304
|
+
|
|
3305
|
+
\| `K`
|
|
3306
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
3307
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
3308
|
+
\| `null`
|
|
3309
|
+
|
|
3310
|
+
##### Returns
|
|
3311
|
+
|
|
3312
|
+
`ReturnType`\<`C`\>[]
|
|
3313
|
+
|
|
3314
|
+
##### Example
|
|
3315
|
+
|
|
3316
|
+
```ts
|
|
3317
|
+
// Morris traversal (O(1) space)
|
|
3318
|
+
const tree = new BinaryTree<number>([1, 2, 3]);
|
|
3319
|
+
const result = tree.morris(node => node.key, 'IN');
|
|
3320
|
+
console.log(result.length); // 3;
|
|
3321
|
+
```
|
|
3322
|
+
|
|
3323
|
+
##### Implementation of
|
|
3324
|
+
|
|
3325
|
+
```ts
|
|
3326
|
+
IBinaryTree.morris
|
|
3327
|
+
```
|
|
3328
|
+
|
|
3329
|
+
***
|
|
3330
|
+
|
|
3331
|
+
### print()
|
|
3332
|
+
|
|
3333
|
+
```ts
|
|
3334
|
+
print(options?, startNode?): void;
|
|
3335
|
+
```
|
|
3336
|
+
|
|
3337
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:2895](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L2895)
|
|
3338
|
+
|
|
3339
|
+
Prints a visual representation of the tree to the console.
|
|
3340
|
+
|
|
3341
|
+
#### Parameters
|
|
3342
|
+
|
|
3343
|
+
##### options?
|
|
3344
|
+
|
|
3345
|
+
`BinaryTreePrintOptions`
|
|
3346
|
+
|
|
3347
|
+
Options to control the output.
|
|
3348
|
+
|
|
3349
|
+
##### startNode?
|
|
3350
|
+
|
|
3351
|
+
\| `K`
|
|
3352
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
3353
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
3354
|
+
\| `null`
|
|
3355
|
+
|
|
3356
|
+
The node to start printing from.
|
|
3357
|
+
|
|
3358
|
+
*
|
|
3359
|
+
|
|
3360
|
+
#### Returns
|
|
3361
|
+
|
|
3362
|
+
`void`
|
|
3363
|
+
|
|
3364
|
+
#### Remarks
|
|
3365
|
+
|
|
3366
|
+
Time O(N) (via `toVisual`). Space O(N*H) or O(N^2) (via `toVisual`).
|
|
3367
|
+
|
|
3368
|
+
#### Example
|
|
3369
|
+
|
|
3370
|
+
```ts
|
|
3371
|
+
// Display tree
|
|
3372
|
+
const tree = new BinaryTree<number>([1, 2, 3]);
|
|
3373
|
+
expect(() => tree.print()).not.toThrow();
|
|
3374
|
+
```
|
|
3375
|
+
|
|
3376
|
+
#### Overrides
|
|
3377
|
+
|
|
3378
|
+
[`IterableEntryBase`](IterableEntryBase.md).[`print`](IterableEntryBase.md#print)
|
|
3379
|
+
|
|
3380
|
+
***
|
|
3381
|
+
|
|
3382
|
+
### reduce()
|
|
3383
|
+
|
|
3384
|
+
```ts
|
|
3385
|
+
reduce<U>(callbackfn, initialValue): U;
|
|
3386
|
+
```
|
|
3387
|
+
|
|
3388
|
+
Defined in: [data-structures/base/iterable-entry-base.ts:171](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-entry-base.ts#L171)
|
|
3389
|
+
|
|
3390
|
+
Reduce entries into a single accumulator.
|
|
3391
|
+
|
|
3392
|
+
#### Type Parameters
|
|
3393
|
+
|
|
3394
|
+
##### U
|
|
3395
|
+
|
|
3396
|
+
`U`
|
|
3397
|
+
|
|
3398
|
+
#### Parameters
|
|
3399
|
+
|
|
3400
|
+
##### callbackfn
|
|
3401
|
+
|
|
3402
|
+
`ReduceEntryCallback`\<`K`, `V` \| `undefined`, `U`\>
|
|
3403
|
+
|
|
3404
|
+
`(acc, value, key, index, self) => acc`.
|
|
3405
|
+
|
|
3406
|
+
##### initialValue
|
|
3407
|
+
|
|
3408
|
+
`U`
|
|
3409
|
+
|
|
3410
|
+
Initial accumulator.
|
|
3411
|
+
|
|
3412
|
+
#### Returns
|
|
3413
|
+
|
|
3414
|
+
`U`
|
|
3415
|
+
|
|
3416
|
+
Final accumulator.
|
|
3417
|
+
|
|
3418
|
+
#### Remarks
|
|
3419
|
+
|
|
3420
|
+
Time O(n), Space O(1)
|
|
3421
|
+
|
|
3422
|
+
#### Implementation of
|
|
3423
|
+
|
|
3424
|
+
```ts
|
|
3425
|
+
IBinaryTree.reduce
|
|
3426
|
+
```
|
|
3427
|
+
|
|
3428
|
+
#### Inherited from
|
|
3429
|
+
|
|
3430
|
+
[`IterableEntryBase`](IterableEntryBase.md).[`reduce`](IterableEntryBase.md#reduce)
|
|
3431
|
+
|
|
3432
|
+
***
|
|
3433
|
+
|
|
3434
|
+
### refill()
|
|
3435
|
+
|
|
3436
|
+
```ts
|
|
3437
|
+
refill(keysNodesEntriesOrRaws, values?): void;
|
|
3438
|
+
```
|
|
3439
|
+
|
|
3440
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:913](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L913)
|
|
3441
|
+
|
|
3442
|
+
Clears the tree and refills it with new items.
|
|
3443
|
+
|
|
3444
|
+
#### Parameters
|
|
3445
|
+
|
|
3446
|
+
##### keysNodesEntriesOrRaws
|
|
3447
|
+
|
|
3448
|
+
`Iterable`\<
|
|
3449
|
+
\| `K`
|
|
3450
|
+
\| `R`
|
|
3451
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
3452
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
3453
|
+
\| `null`
|
|
3454
|
+
\| `undefined`\>
|
|
3455
|
+
|
|
3456
|
+
An iterable of items to set.
|
|
3457
|
+
|
|
3458
|
+
##### values?
|
|
3459
|
+
|
|
3460
|
+
`Iterable`\<`V` \| `undefined`, `any`, `any`\>
|
|
3461
|
+
|
|
3462
|
+
An optional parallel iterable of values.
|
|
3463
|
+
|
|
3464
|
+
#### Returns
|
|
3465
|
+
|
|
3466
|
+
`void`
|
|
3467
|
+
|
|
3468
|
+
#### Remarks
|
|
3469
|
+
|
|
3470
|
+
Time O(N) (for `clear`) + O(N * M) (for `setMany`) = O(N * M). Space O(M) (from `setMany`).
|
|
3471
|
+
|
|
3472
|
+
#### Implementation of
|
|
3473
|
+
|
|
3474
|
+
```ts
|
|
3475
|
+
IBinaryTree.refill
|
|
3476
|
+
```
|
|
3477
|
+
|
|
3478
|
+
***
|
|
3479
|
+
|
|
3480
|
+
### search()
|
|
3481
|
+
|
|
3482
|
+
Searches the tree for nodes matching a predicate.
|
|
3483
|
+
|
|
3484
|
+
#### Remarks
|
|
3485
|
+
|
|
3486
|
+
Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). Performs a full DFS (pre-order) scan of the tree. Time O(N), as it may visit every node. Space O(H) for the call stack (recursive) or explicit stack (iterative), where H is the tree height (O(N) worst-case).
|
|
3487
|
+
|
|
3488
|
+
#### Template
|
|
3489
|
+
|
|
3490
|
+
The type of the callback function.
|
|
3491
|
+
|
|
3492
|
+
#### Param
|
|
3493
|
+
|
|
3494
|
+
The key, node, entry, or predicate function to search for.
|
|
3495
|
+
|
|
3496
|
+
#### Param
|
|
3497
|
+
|
|
3498
|
+
If true, stops after finding the first match.
|
|
3499
|
+
|
|
3500
|
+
#### Param
|
|
3501
|
+
|
|
3502
|
+
A function to call on matching nodes.
|
|
3503
|
+
|
|
3504
|
+
#### Param
|
|
3505
|
+
|
|
3506
|
+
The node to start the search from.
|
|
3507
|
+
|
|
3508
|
+
#### Param
|
|
3509
|
+
|
|
3510
|
+
Whether to use 'RECURSIVE' or 'ITERATIVE' search.
|
|
3511
|
+
|
|
3512
|
+
#### Call Signature
|
|
3513
|
+
|
|
3514
|
+
```ts
|
|
3515
|
+
search(keyNodeEntryOrPredicate, onlyOne?): (K | undefined)[];
|
|
3516
|
+
```
|
|
3517
|
+
|
|
3518
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:1069](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L1069)
|
|
3519
|
+
|
|
3520
|
+
Search by predicate
|
|
3521
|
+
|
|
3522
|
+
*
|
|
3523
|
+
|
|
3524
|
+
##### Parameters
|
|
3525
|
+
|
|
3526
|
+
###### keyNodeEntryOrPredicate
|
|
3527
|
+
|
|
3528
|
+
\| `K`
|
|
3529
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
3530
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
3531
|
+
\| `NodePredicate`\<[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\> \| `null`\>
|
|
3532
|
+
\| `null`
|
|
3533
|
+
\| `undefined`
|
|
3534
|
+
|
|
3535
|
+
###### onlyOne?
|
|
3536
|
+
|
|
3537
|
+
`boolean`
|
|
3538
|
+
|
|
3539
|
+
##### Returns
|
|
3540
|
+
|
|
3541
|
+
(`K` \| `undefined`)[]
|
|
3542
|
+
|
|
3543
|
+
##### Example
|
|
3544
|
+
|
|
3545
|
+
```ts
|
|
3546
|
+
// Search by predicate
|
|
3547
|
+
const tree = new BinaryTree<number>([5, 3, 7, 1, 9]);
|
|
3548
|
+
const found = tree.search(n => n!.key > 5, true);
|
|
3549
|
+
console.log(found.length); // >= 1;
|
|
3550
|
+
```
|
|
3551
|
+
|
|
3552
|
+
##### Implementation of
|
|
3553
|
+
|
|
3554
|
+
```ts
|
|
3555
|
+
IBinaryTree.search
|
|
3556
|
+
```
|
|
3557
|
+
|
|
3558
|
+
#### Call Signature
|
|
3559
|
+
|
|
3560
|
+
```ts
|
|
3561
|
+
search<C>(
|
|
3562
|
+
keyNodeEntryOrPredicate,
|
|
3563
|
+
onlyOne,
|
|
3564
|
+
callback,
|
|
3565
|
+
startNode?,
|
|
3566
|
+
iterationType?): ReturnType<C>[];
|
|
3567
|
+
```
|
|
3568
|
+
|
|
3569
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:1080](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L1080)
|
|
3570
|
+
|
|
3571
|
+
Search by predicate
|
|
3572
|
+
|
|
3573
|
+
*
|
|
3574
|
+
|
|
3575
|
+
##### Type Parameters
|
|
3576
|
+
|
|
3577
|
+
###### C
|
|
3578
|
+
|
|
3579
|
+
`C` *extends* `NodeCallback`\<[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\> \| `null`\>
|
|
3580
|
+
|
|
3581
|
+
##### Parameters
|
|
3582
|
+
|
|
3583
|
+
###### keyNodeEntryOrPredicate
|
|
3584
|
+
|
|
3585
|
+
\| `K`
|
|
3586
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
3587
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
3588
|
+
\| `NodePredicate`\<[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\> \| `null`\>
|
|
3589
|
+
\| `null`
|
|
3590
|
+
\| `undefined`
|
|
3591
|
+
|
|
3592
|
+
###### onlyOne
|
|
3593
|
+
|
|
3594
|
+
`boolean`
|
|
3595
|
+
|
|
3596
|
+
###### callback
|
|
3597
|
+
|
|
3598
|
+
`C`
|
|
3599
|
+
|
|
3600
|
+
###### startNode?
|
|
3601
|
+
|
|
3602
|
+
\| `K`
|
|
3603
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
3604
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
3605
|
+
\| `null`
|
|
3606
|
+
|
|
3607
|
+
###### iterationType?
|
|
3608
|
+
|
|
3609
|
+
`IterationType`
|
|
3610
|
+
|
|
3611
|
+
##### Returns
|
|
3612
|
+
|
|
3613
|
+
`ReturnType`\<`C`\>[]
|
|
3614
|
+
|
|
3615
|
+
##### Example
|
|
3616
|
+
|
|
3617
|
+
```ts
|
|
3618
|
+
// Search by predicate
|
|
3619
|
+
const tree = new BinaryTree<number>([5, 3, 7, 1, 9]);
|
|
3620
|
+
const found = tree.search(n => n!.key > 5, true);
|
|
3621
|
+
console.log(found.length); // >= 1;
|
|
3622
|
+
```
|
|
3623
|
+
|
|
3624
|
+
##### Implementation of
|
|
3625
|
+
|
|
3626
|
+
```ts
|
|
3627
|
+
IBinaryTree.search
|
|
3628
|
+
```
|
|
3629
|
+
|
|
3630
|
+
***
|
|
3631
|
+
|
|
3632
|
+
### set()
|
|
3633
|
+
|
|
3634
|
+
```ts
|
|
3635
|
+
set(keyNodeOrEntry, value?): boolean;
|
|
3636
|
+
```
|
|
3637
|
+
|
|
3638
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:680](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L680)
|
|
3639
|
+
|
|
3640
|
+
Adds or updates a new node to the tree.
|
|
3641
|
+
|
|
3642
|
+
#### Parameters
|
|
3643
|
+
|
|
3644
|
+
##### keyNodeOrEntry
|
|
3645
|
+
|
|
3646
|
+
\| `K`
|
|
3647
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
3648
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
3649
|
+
\| `null`
|
|
3650
|
+
\| `undefined`
|
|
3651
|
+
|
|
3652
|
+
The key, node, or entry to set or update.
|
|
3653
|
+
|
|
3654
|
+
##### value?
|
|
3655
|
+
|
|
3656
|
+
`V`
|
|
3657
|
+
|
|
3658
|
+
The value, if providing just a key.
|
|
3659
|
+
|
|
3660
|
+
#### Returns
|
|
3661
|
+
|
|
3662
|
+
`boolean`
|
|
3663
|
+
|
|
3664
|
+
True if the addition was successful, false otherwise.
|
|
3665
|
+
|
|
3666
|
+
*
|
|
3667
|
+
|
|
3668
|
+
#### Remarks
|
|
3669
|
+
|
|
3670
|
+
Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation sets 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).
|
|
3671
|
+
|
|
3672
|
+
#### Example
|
|
3673
|
+
|
|
3674
|
+
```ts
|
|
3675
|
+
// basic BinaryTree creation and insertion
|
|
3676
|
+
// Create a BinaryTree with entries
|
|
3677
|
+
const entries: [number, string][] = [
|
|
3678
|
+
[6, 'six'],
|
|
3679
|
+
[1, 'one'],
|
|
3680
|
+
[2, 'two'],
|
|
3681
|
+
[7, 'seven'],
|
|
3682
|
+
[5, 'five'],
|
|
3683
|
+
[3, 'three'],
|
|
3684
|
+
[4, 'four'],
|
|
3685
|
+
[9, 'nine'],
|
|
3686
|
+
[8, 'eight']
|
|
3687
|
+
];
|
|
3688
|
+
|
|
3689
|
+
const tree = new BinaryTree(entries);
|
|
3690
|
+
|
|
3691
|
+
// Verify size
|
|
3692
|
+
console.log(tree.size); // 9;
|
|
3693
|
+
|
|
3694
|
+
// Add new element
|
|
3695
|
+
tree.set(10, 'ten');
|
|
3696
|
+
console.log(tree.size); // 10;
|
|
3697
|
+
```
|
|
3698
|
+
|
|
3699
|
+
#### Implementation of
|
|
3700
|
+
|
|
3701
|
+
```ts
|
|
3702
|
+
IBinaryTree.set
|
|
3703
|
+
```
|
|
3704
|
+
|
|
3705
|
+
***
|
|
3706
|
+
|
|
3707
|
+
### setMany()
|
|
3708
|
+
|
|
3709
|
+
```ts
|
|
3710
|
+
setMany(keysNodesEntriesOrRaws, values?): boolean[];
|
|
3711
|
+
```
|
|
3712
|
+
|
|
3713
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:828](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L828)
|
|
3714
|
+
|
|
3715
|
+
Adds or updates multiple items to the tree.
|
|
3716
|
+
|
|
3717
|
+
#### Parameters
|
|
3718
|
+
|
|
3719
|
+
##### keysNodesEntriesOrRaws
|
|
3720
|
+
|
|
3721
|
+
`Iterable`\<
|
|
3722
|
+
\| `K`
|
|
3723
|
+
\| `R`
|
|
3724
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
3725
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
3726
|
+
\| `null`
|
|
3727
|
+
\| `undefined`\>
|
|
3728
|
+
|
|
3729
|
+
An iterable of items to set or update.
|
|
3730
|
+
|
|
3731
|
+
##### values?
|
|
3732
|
+
|
|
3733
|
+
`Iterable`\<`V` \| `undefined`, `any`, `any`\>
|
|
3734
|
+
|
|
3735
|
+
An optional parallel iterable of values.
|
|
3736
|
+
|
|
3737
|
+
#### Returns
|
|
3738
|
+
|
|
3739
|
+
`boolean`[]
|
|
3740
|
+
|
|
3741
|
+
An array of booleans indicating the success of each individual `set` operation.
|
|
3742
|
+
|
|
3743
|
+
*
|
|
3744
|
+
|
|
3745
|
+
#### Remarks
|
|
3746
|
+
|
|
3747
|
+
Time O(N * M), where N is the number of items to set and M is the size of the tree at insertion (due to O(M) `set` operation). Space O(M) (from `set`) + O(N) (for the `inserted` array).
|
|
3748
|
+
|
|
3749
|
+
#### Example
|
|
3750
|
+
|
|
3751
|
+
```ts
|
|
3752
|
+
// Set multiple entries
|
|
3753
|
+
const tree = new BinaryTree<number, string>();
|
|
3754
|
+
tree.setMany([[1, 'a'], [2, 'b'], [3, 'c']]);
|
|
3755
|
+
console.log(tree.size); // 3;
|
|
3756
|
+
```
|
|
3757
|
+
|
|
3758
|
+
***
|
|
3759
|
+
|
|
3760
|
+
### some()
|
|
3761
|
+
|
|
3762
|
+
```ts
|
|
3763
|
+
some(predicate, thisArg?): boolean;
|
|
3764
|
+
```
|
|
3765
|
+
|
|
3766
|
+
Defined in: [data-structures/base/iterable-entry-base.ts:83](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-entry-base.ts#L83)
|
|
3767
|
+
|
|
3768
|
+
Test whether any entry satisfies the predicate.
|
|
3769
|
+
|
|
3770
|
+
#### Parameters
|
|
3771
|
+
|
|
3772
|
+
##### predicate
|
|
3773
|
+
|
|
3774
|
+
`EntryCallback`\<`K`, `V` \| `undefined`, `boolean`\>
|
|
3775
|
+
|
|
3776
|
+
`(key, value, index, self) => boolean`.
|
|
3777
|
+
|
|
3778
|
+
##### thisArg?
|
|
3779
|
+
|
|
3780
|
+
`unknown`
|
|
3781
|
+
|
|
3782
|
+
Optional `this` for callback.
|
|
3783
|
+
|
|
3784
|
+
#### Returns
|
|
3785
|
+
|
|
3786
|
+
`boolean`
|
|
3787
|
+
|
|
3788
|
+
`true` if any passes; otherwise `false`.
|
|
3789
|
+
|
|
3790
|
+
#### Remarks
|
|
3791
|
+
|
|
3792
|
+
Time O(n), Space O(1)
|
|
3793
|
+
|
|
3794
|
+
#### Implementation of
|
|
3795
|
+
|
|
3796
|
+
```ts
|
|
3797
|
+
IBinaryTree.some
|
|
3798
|
+
```
|
|
3799
|
+
|
|
3800
|
+
#### Inherited from
|
|
3801
|
+
|
|
3802
|
+
[`IterableEntryBase`](IterableEntryBase.md).[`some`](IterableEntryBase.md#some)
|
|
3803
|
+
|
|
3804
|
+
***
|
|
3805
|
+
|
|
3806
|
+
### toArray()
|
|
3807
|
+
|
|
3808
|
+
```ts
|
|
3809
|
+
toArray(): [K, V | undefined][];
|
|
3810
|
+
```
|
|
3811
|
+
|
|
3812
|
+
Defined in: [data-structures/base/iterable-entry-base.ts:186](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-entry-base.ts#L186)
|
|
3813
|
+
|
|
3814
|
+
Converts data structure to `[key, value]` pairs.
|
|
3815
|
+
|
|
3816
|
+
#### Returns
|
|
3817
|
+
|
|
3818
|
+
\[`K`, `V` \| `undefined`\][]
|
|
3819
|
+
|
|
3820
|
+
Array of entries.
|
|
3821
|
+
|
|
3822
|
+
#### Remarks
|
|
3823
|
+
|
|
3824
|
+
Time O(n), Space O(n)
|
|
3825
|
+
|
|
3826
|
+
#### Inherited from
|
|
3827
|
+
|
|
3828
|
+
[`IterableEntryBase`](IterableEntryBase.md).[`toArray`](IterableEntryBase.md#toarray)
|
|
3829
|
+
|
|
3830
|
+
***
|
|
3831
|
+
|
|
3832
|
+
### toVisual()
|
|
3833
|
+
|
|
3834
|
+
```ts
|
|
3835
|
+
toVisual(startNode?, options?): string;
|
|
3836
|
+
```
|
|
3837
|
+
|
|
3838
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:2825](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L2825)
|
|
3839
|
+
|
|
3840
|
+
Generates a string representation of the tree for visualization.
|
|
3841
|
+
|
|
3842
|
+
#### Parameters
|
|
3843
|
+
|
|
3844
|
+
##### startNode?
|
|
3845
|
+
|
|
3846
|
+
\| `K`
|
|
3847
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
3848
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
3849
|
+
\| `null`
|
|
3850
|
+
|
|
3851
|
+
The node to start printing from.
|
|
3852
|
+
|
|
3853
|
+
##### options?
|
|
3854
|
+
|
|
3855
|
+
`BinaryTreePrintOptions`
|
|
3856
|
+
|
|
3857
|
+
Options to control the output (e.g., show nulls).
|
|
3858
|
+
|
|
3859
|
+
#### Returns
|
|
3860
|
+
|
|
3861
|
+
`string`
|
|
3862
|
+
|
|
3863
|
+
The string representation of the tree.
|
|
3864
|
+
|
|
3865
|
+
#### Remarks
|
|
3866
|
+
|
|
3867
|
+
Time O(N), visits every node. Space O(N*H) or O(N^2) in the worst case, as the string width can grow significantly.
|
|
3868
|
+
|
|
3869
|
+
#### Overrides
|
|
3870
|
+
|
|
3871
|
+
[`IterableEntryBase`](IterableEntryBase.md).[`toVisual`](IterableEntryBase.md#tovisual)
|
|
3872
|
+
|
|
3873
|
+
***
|
|
3874
|
+
|
|
3875
|
+
### values()
|
|
3876
|
+
|
|
3877
|
+
```ts
|
|
3878
|
+
values(): IterableIterator<V | undefined>;
|
|
3879
|
+
```
|
|
3880
|
+
|
|
3881
|
+
Defined in: [data-structures/base/iterable-entry-base.ts:53](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-entry-base.ts#L53)
|
|
3882
|
+
|
|
3883
|
+
Iterate over values only.
|
|
3884
|
+
|
|
3885
|
+
#### Returns
|
|
3886
|
+
|
|
3887
|
+
`IterableIterator`\<`V` \| `undefined`\>
|
|
3888
|
+
|
|
3889
|
+
Iterator of values.
|
|
3890
|
+
|
|
3891
|
+
#### Remarks
|
|
3892
|
+
|
|
3893
|
+
Time O(n), Space O(1)
|
|
3894
|
+
|
|
3895
|
+
#### Implementation of
|
|
3896
|
+
|
|
3897
|
+
```ts
|
|
3898
|
+
IBinaryTree.values
|
|
3899
|
+
```
|
|
3900
|
+
|
|
3901
|
+
#### Inherited from
|
|
3902
|
+
|
|
3903
|
+
[`IterableEntryBase`](IterableEntryBase.md).[`values`](IterableEntryBase.md#values)
|
|
3904
|
+
|
|
3905
|
+
|
|
3906
|
+
---
|
|
3907
|
+
|
|
3908
|
+
## Protected Members
|
|
3909
|
+
|
|
3910
|
+
### \_DEFAULT\_NODE\_CALLBACK
|
|
3911
|
+
|
|
3912
|
+
```ts
|
|
3913
|
+
protected readonly _DEFAULT_NODE_CALLBACK: NodeCallback<BinaryTreeNode<K, V> | null | undefined, K | undefined>;
|
|
3914
|
+
```
|
|
3915
|
+
|
|
3916
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:3091](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L3091)
|
|
3917
|
+
|
|
3918
|
+
(Protected) Default callback function, returns the node's key.
|
|
3919
|
+
|
|
3920
|
+
#### Remarks
|
|
3921
|
+
|
|
3922
|
+
Time O(1)
|
|
3923
|
+
|
|
3924
|
+
#### Param
|
|
3925
|
+
|
|
3926
|
+
The node.
|
|
3927
|
+
|
|
3928
|
+
#### Returns
|
|
3929
|
+
|
|
3930
|
+
The node's key or undefined.
|
|
3931
|
+
|
|
3932
|
+
## Accessors
|
|
3933
|
+
|
|
3934
|
+
### \_clearNodes()
|
|
3935
|
+
|
|
3936
|
+
```ts
|
|
3937
|
+
protected _clearNodes(): void;
|
|
3938
|
+
```
|
|
3939
|
+
|
|
3940
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:3525](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L3525)
|
|
3941
|
+
|
|
3942
|
+
(Protected) Clears all nodes from the tree.
|
|
3943
|
+
|
|
3944
|
+
#### Returns
|
|
3945
|
+
|
|
3946
|
+
`void`
|
|
3947
|
+
|
|
3948
|
+
#### Remarks
|
|
3949
|
+
|
|
3950
|
+
Time O(1)
|
|
3951
|
+
|
|
3952
|
+
***
|
|
3953
|
+
|
|
3954
|
+
### \_clearValues()
|
|
3955
|
+
|
|
3956
|
+
```ts
|
|
3957
|
+
protected _clearValues(): void;
|
|
3958
|
+
```
|
|
3959
|
+
|
|
3960
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:3534](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L3534)
|
|
3961
|
+
|
|
3962
|
+
(Protected) Clears all values from the external store.
|
|
3963
|
+
|
|
3964
|
+
#### Returns
|
|
3965
|
+
|
|
3966
|
+
`void`
|
|
3967
|
+
|
|
3968
|
+
#### Remarks
|
|
3969
|
+
|
|
3970
|
+
Time O(N)
|
|
3971
|
+
|
|
3972
|
+
***
|
|
3973
|
+
|
|
3974
|
+
### \_clone()
|
|
3975
|
+
|
|
3976
|
+
```ts
|
|
3977
|
+
protected _clone(cloned): void;
|
|
3978
|
+
```
|
|
3979
|
+
|
|
3980
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:3184](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L3184)
|
|
3981
|
+
|
|
3982
|
+
(Protected) Helper for cloning. Performs a BFS and sets all nodes to the new tree.
|
|
3983
|
+
|
|
3984
|
+
#### Parameters
|
|
3985
|
+
|
|
3986
|
+
##### cloned
|
|
3987
|
+
|
|
3988
|
+
`BinaryTree`\<`K`, `V`, `R`\>
|
|
3989
|
+
|
|
3990
|
+
The new, empty tree instance to populate.
|
|
3991
|
+
|
|
3992
|
+
#### Returns
|
|
3993
|
+
|
|
3994
|
+
`void`
|
|
3995
|
+
|
|
3996
|
+
#### Remarks
|
|
3997
|
+
|
|
3998
|
+
Time O(N * M) (O(N) BFS + O(M) `set` for each node).
|
|
3999
|
+
|
|
4000
|
+
***
|
|
4001
|
+
|
|
4002
|
+
### \_createInstance()
|
|
4003
|
+
|
|
4004
|
+
```ts
|
|
4005
|
+
protected _createInstance<TK, TV, TR>(options?): this;
|
|
4006
|
+
```
|
|
4007
|
+
|
|
4008
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:3118](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L3118)
|
|
4009
|
+
|
|
4010
|
+
(Protected) Creates a new, empty instance of the same tree constructor.
|
|
4011
|
+
|
|
4012
|
+
#### Type Parameters
|
|
4013
|
+
|
|
4014
|
+
##### TK
|
|
4015
|
+
|
|
4016
|
+
`TK` = `K`
|
|
4017
|
+
|
|
4018
|
+
##### TV
|
|
4019
|
+
|
|
4020
|
+
`TV` = `V`
|
|
4021
|
+
|
|
4022
|
+
##### TR
|
|
4023
|
+
|
|
4024
|
+
`TR` = `R`
|
|
4025
|
+
|
|
4026
|
+
#### Parameters
|
|
4027
|
+
|
|
4028
|
+
##### options?
|
|
4029
|
+
|
|
4030
|
+
`Partial`\<`BinaryTreeOptions`\<`TK`, `TV`, `TR`\>\>
|
|
4031
|
+
|
|
4032
|
+
Options for the new tree.
|
|
4033
|
+
|
|
4034
|
+
#### Returns
|
|
4035
|
+
|
|
4036
|
+
`this`
|
|
4037
|
+
|
|
4038
|
+
A new, empty tree.
|
|
4039
|
+
|
|
4040
|
+
#### Remarks
|
|
4041
|
+
|
|
4042
|
+
Time O(1)
|
|
4043
|
+
|
|
4044
|
+
***
|
|
4045
|
+
|
|
4046
|
+
### \_createLike()
|
|
4047
|
+
|
|
4048
|
+
```ts
|
|
4049
|
+
protected _createLike<TK, TV, TR>(iter?, options?): BinaryTree<TK, TV, TR>;
|
|
4050
|
+
```
|
|
4051
|
+
|
|
4052
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:3135](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L3135)
|
|
4053
|
+
|
|
4054
|
+
(Protected) Creates a new instance of the same tree constructor, potentially with different generic types.
|
|
4055
|
+
|
|
4056
|
+
#### Type Parameters
|
|
4057
|
+
|
|
4058
|
+
##### TK
|
|
4059
|
+
|
|
4060
|
+
`TK` = `K`
|
|
4061
|
+
|
|
4062
|
+
##### TV
|
|
4063
|
+
|
|
4064
|
+
`TV` = `V`
|
|
4065
|
+
|
|
4066
|
+
##### TR
|
|
4067
|
+
|
|
4068
|
+
`TR` = `R`
|
|
4069
|
+
|
|
4070
|
+
#### Parameters
|
|
4071
|
+
|
|
4072
|
+
##### iter?
|
|
4073
|
+
|
|
4074
|
+
`Iterable`\<
|
|
4075
|
+
\| `TK`
|
|
4076
|
+
\| `TR`
|
|
4077
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`TK`, `TV`\>
|
|
4078
|
+
\| \[`TK` \| `null` \| `undefined`, `TV` \| `undefined`\]
|
|
4079
|
+
\| `null`
|
|
4080
|
+
\| `undefined`\> = `[]`
|
|
4081
|
+
|
|
4082
|
+
An iterable to populate the new tree.
|
|
4083
|
+
|
|
4084
|
+
##### options?
|
|
4085
|
+
|
|
4086
|
+
`Partial`\<`BinaryTreeOptions`\<`TK`, `TV`, `TR`\>\>
|
|
4087
|
+
|
|
4088
|
+
Options for the new tree.
|
|
4089
|
+
|
|
4090
|
+
#### Returns
|
|
4091
|
+
|
|
4092
|
+
`BinaryTree`\<`TK`, `TV`, `TR`\>
|
|
4093
|
+
|
|
4094
|
+
A new tree.
|
|
4095
|
+
|
|
4096
|
+
#### Remarks
|
|
4097
|
+
|
|
4098
|
+
Time O(N) (or as per constructor) due to processing the iterable.
|
|
4099
|
+
|
|
4100
|
+
***
|
|
4101
|
+
|
|
4102
|
+
### \_dfs()
|
|
4103
|
+
|
|
4104
|
+
```ts
|
|
4105
|
+
protected _dfs<C>(
|
|
4106
|
+
callback,
|
|
4107
|
+
pattern?,
|
|
4108
|
+
onlyOne?,
|
|
4109
|
+
startNode?,
|
|
4110
|
+
iterationType?,
|
|
4111
|
+
includeNull?,
|
|
4112
|
+
shouldVisitLeft?,
|
|
4113
|
+
shouldVisitRight?,
|
|
4114
|
+
shouldVisitRoot?,
|
|
4115
|
+
shouldProcessRoot?): ReturnType<C>[];
|
|
4116
|
+
```
|
|
4117
|
+
|
|
4118
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:2902](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L2902)
|
|
4119
|
+
|
|
4120
|
+
#### Type Parameters
|
|
4121
|
+
|
|
4122
|
+
##### C
|
|
4123
|
+
|
|
4124
|
+
`C` *extends* `NodeCallback`\<[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>\>
|
|
4125
|
+
|
|
4126
|
+
Callback type.
|
|
4127
|
+
|
|
4128
|
+
#### Parameters
|
|
4129
|
+
|
|
4130
|
+
##### callback
|
|
4131
|
+
|
|
4132
|
+
`C`
|
|
4133
|
+
|
|
4134
|
+
Function to call on nodes.
|
|
4135
|
+
|
|
4136
|
+
##### pattern?
|
|
4137
|
+
|
|
4138
|
+
`DFSOrderPattern`
|
|
4139
|
+
|
|
4140
|
+
Traversal order.
|
|
4141
|
+
|
|
4142
|
+
##### onlyOne?
|
|
4143
|
+
|
|
4144
|
+
`boolean`
|
|
4145
|
+
|
|
4146
|
+
Stop after first match.
|
|
4147
|
+
|
|
4148
|
+
##### startNode?
|
|
4149
|
+
|
|
4150
|
+
\| `K`
|
|
4151
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
4152
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
4153
|
+
\| `null`
|
|
4154
|
+
|
|
4155
|
+
Starting node.
|
|
4156
|
+
|
|
4157
|
+
##### iterationType?
|
|
4158
|
+
|
|
4159
|
+
`IterationType`
|
|
4160
|
+
|
|
4161
|
+
Traversal method.
|
|
4162
|
+
|
|
4163
|
+
##### includeNull?
|
|
4164
|
+
|
|
4165
|
+
`boolean`
|
|
4166
|
+
|
|
4167
|
+
Include nulls.
|
|
4168
|
+
|
|
4169
|
+
##### shouldVisitLeft?
|
|
4170
|
+
|
|
4171
|
+
(`node`) => `boolean`
|
|
4172
|
+
|
|
4173
|
+
Predicate to traverse left.
|
|
4174
|
+
|
|
4175
|
+
##### shouldVisitRight?
|
|
4176
|
+
|
|
4177
|
+
(`node`) => `boolean`
|
|
4178
|
+
|
|
4179
|
+
Predicate to traverse right.
|
|
4180
|
+
|
|
4181
|
+
##### shouldVisitRoot?
|
|
4182
|
+
|
|
4183
|
+
(`node`) => `boolean`
|
|
4184
|
+
|
|
4185
|
+
Predicate to visit root.
|
|
4186
|
+
|
|
4187
|
+
##### shouldProcessRoot?
|
|
4188
|
+
|
|
4189
|
+
(`node`) => `boolean`
|
|
4190
|
+
|
|
4191
|
+
Predicate to process root.
|
|
4192
|
+
|
|
4193
|
+
#### Returns
|
|
4194
|
+
|
|
4195
|
+
`ReturnType`\<`C`\>[]
|
|
4196
|
+
|
|
4197
|
+
Array of callback results.
|
|
4198
|
+
|
|
4199
|
+
***
|
|
4200
|
+
|
|
4201
|
+
### \_displayAux()
|
|
4202
|
+
|
|
4203
|
+
```ts
|
|
4204
|
+
protected _displayAux(node, options): NodeDisplayLayout;
|
|
4205
|
+
```
|
|
4206
|
+
|
|
4207
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:3208](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L3208)
|
|
4208
|
+
|
|
4209
|
+
(Protected) Recursive helper for `toVisual`.
|
|
4210
|
+
|
|
4211
|
+
#### Parameters
|
|
4212
|
+
|
|
4213
|
+
##### node
|
|
4214
|
+
|
|
4215
|
+
[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\> \| `null` \| `undefined`
|
|
4216
|
+
|
|
4217
|
+
The current node.
|
|
4218
|
+
|
|
4219
|
+
##### options
|
|
4220
|
+
|
|
4221
|
+
`BinaryTreePrintOptions`
|
|
4222
|
+
|
|
4223
|
+
Print options.
|
|
4224
|
+
|
|
4225
|
+
#### Returns
|
|
4226
|
+
|
|
4227
|
+
`NodeDisplayLayout`
|
|
4228
|
+
|
|
4229
|
+
Layout information for this subtree.
|
|
4230
|
+
|
|
4231
|
+
#### Remarks
|
|
4232
|
+
|
|
4233
|
+
Time O(N), Space O(N*H) or O(N^2)
|
|
4234
|
+
|
|
4235
|
+
***
|
|
4236
|
+
|
|
4237
|
+
### \_ensurePredicate()
|
|
4238
|
+
|
|
4239
|
+
```ts
|
|
4240
|
+
protected _ensurePredicate(keyNodeEntryOrPredicate): NodePredicate<BinaryTreeNode<K, V>>;
|
|
4241
|
+
```
|
|
4242
|
+
|
|
4243
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:3431](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L3431)
|
|
4244
|
+
|
|
4245
|
+
(Protected) Converts a key, node, entry, or predicate into a standardized predicate function.
|
|
4246
|
+
|
|
4247
|
+
#### Parameters
|
|
4248
|
+
|
|
4249
|
+
##### keyNodeEntryOrPredicate
|
|
4250
|
+
|
|
4251
|
+
\| `K`
|
|
4252
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
4253
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
4254
|
+
\| `NodePredicate`\<[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>\>
|
|
4255
|
+
\| `null`
|
|
4256
|
+
\| `undefined`
|
|
4257
|
+
|
|
4258
|
+
The item to convert.
|
|
4259
|
+
|
|
4260
|
+
#### Returns
|
|
4261
|
+
|
|
4262
|
+
`NodePredicate`\<[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>\>
|
|
4263
|
+
|
|
4264
|
+
A predicate function.
|
|
4265
|
+
|
|
4266
|
+
#### Remarks
|
|
4267
|
+
|
|
4268
|
+
Time O(1)
|
|
4269
|
+
|
|
4270
|
+
***
|
|
4271
|
+
|
|
4272
|
+
### \_extractKey()
|
|
4273
|
+
|
|
4274
|
+
```ts
|
|
4275
|
+
protected _extractKey(keyNodeOrEntry): K | null | undefined;
|
|
4276
|
+
```
|
|
4277
|
+
|
|
4278
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:3491](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L3491)
|
|
4279
|
+
|
|
4280
|
+
(Protected) Extracts the key from a key, node, or entry.
|
|
4281
|
+
|
|
4282
|
+
#### Parameters
|
|
4283
|
+
|
|
4284
|
+
##### keyNodeOrEntry
|
|
4285
|
+
|
|
4286
|
+
\| `K`
|
|
4287
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
4288
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
4289
|
+
\| `null`
|
|
4290
|
+
\| `undefined`
|
|
4291
|
+
|
|
4292
|
+
The item.
|
|
4293
|
+
|
|
4294
|
+
#### Returns
|
|
4295
|
+
|
|
4296
|
+
`K` \| `null` \| `undefined`
|
|
4297
|
+
|
|
4298
|
+
The extracted key.
|
|
4299
|
+
|
|
4300
|
+
#### Remarks
|
|
4301
|
+
|
|
4302
|
+
Time O(1)
|
|
4303
|
+
|
|
4304
|
+
***
|
|
4305
|
+
|
|
4306
|
+
### \_getIterator()
|
|
4307
|
+
|
|
4308
|
+
```ts
|
|
4309
|
+
protected _getIterator(node?): IterableIterator<[K, V | undefined]>;
|
|
4310
|
+
```
|
|
4311
|
+
|
|
4312
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:3047](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L3047)
|
|
4313
|
+
|
|
4314
|
+
(Protected) Gets the iterator for the tree (default in-order).
|
|
4315
|
+
|
|
4316
|
+
#### Parameters
|
|
4317
|
+
|
|
4318
|
+
##### node?
|
|
4319
|
+
|
|
4320
|
+
[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\> \| `null`
|
|
4321
|
+
|
|
4322
|
+
The node to start iteration from.
|
|
4323
|
+
|
|
4324
|
+
#### Returns
|
|
4325
|
+
|
|
4326
|
+
`IterableIterator`\<\[`K`, `V` \| `undefined`\]\>
|
|
4327
|
+
|
|
4328
|
+
An iterator for [key, value] pairs.
|
|
4329
|
+
|
|
4330
|
+
#### Remarks
|
|
4331
|
+
|
|
4332
|
+
Time O(N) for full iteration. O(H) to get the first element. Space O(H) for the iterative stack. O(H) for recursive stack.
|
|
4333
|
+
|
|
4334
|
+
#### Overrides
|
|
4335
|
+
|
|
4336
|
+
[`IterableEntryBase`](IterableEntryBase.md).[`_getIterator`](IterableEntryBase.md#_getiterator)
|
|
4337
|
+
|
|
4338
|
+
***
|
|
4339
|
+
|
|
4340
|
+
### \_isDisplayLeaf()
|
|
4341
|
+
|
|
4342
|
+
```ts
|
|
4343
|
+
protected _isDisplayLeaf(node, options): boolean;
|
|
4344
|
+
```
|
|
4345
|
+
|
|
4346
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:3303](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L3303)
|
|
4347
|
+
|
|
4348
|
+
Check if a node is a display leaf (empty, null, undefined, NIL, or real leaf).
|
|
4349
|
+
|
|
4350
|
+
#### Parameters
|
|
4351
|
+
|
|
4352
|
+
##### node
|
|
4353
|
+
|
|
4354
|
+
[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\> \| `null` \| `undefined`
|
|
4355
|
+
|
|
4356
|
+
##### options
|
|
4357
|
+
|
|
4358
|
+
`BinaryTreePrintOptions`
|
|
4359
|
+
|
|
4360
|
+
#### Returns
|
|
4361
|
+
|
|
4362
|
+
`boolean`
|
|
4363
|
+
|
|
4364
|
+
***
|
|
4365
|
+
|
|
4366
|
+
### \_isPredicate()
|
|
4367
|
+
|
|
4368
|
+
```ts
|
|
4369
|
+
protected _isPredicate(p): p is NodePredicate<BinaryTreeNode<K, V>>;
|
|
4370
|
+
```
|
|
4371
|
+
|
|
4372
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:3480](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L3480)
|
|
4373
|
+
|
|
4374
|
+
(Protected) Checks if an item is a predicate function.
|
|
4375
|
+
|
|
4376
|
+
#### Parameters
|
|
4377
|
+
|
|
4378
|
+
##### p
|
|
4379
|
+
|
|
4380
|
+
`unknown`
|
|
4381
|
+
|
|
4382
|
+
The item to check.
|
|
4383
|
+
|
|
4384
|
+
#### Returns
|
|
4385
|
+
|
|
4386
|
+
`p is NodePredicate<BinaryTreeNode<K, V>>`
|
|
4387
|
+
|
|
4388
|
+
True if it's a function.
|
|
4389
|
+
|
|
4390
|
+
#### Remarks
|
|
4391
|
+
|
|
4392
|
+
Time O(1)
|
|
4393
|
+
|
|
4394
|
+
***
|
|
4395
|
+
|
|
4396
|
+
### \_keyValueNodeOrEntryToNodeAndValue()
|
|
4397
|
+
|
|
4398
|
+
```ts
|
|
4399
|
+
protected _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value?): [BinaryTreeNode<K, V> | null | undefined, V | undefined];
|
|
4400
|
+
```
|
|
4401
|
+
|
|
4402
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:3158](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L3158)
|
|
4403
|
+
|
|
4404
|
+
(Protected) Converts a key, node, or entry into a standardized [node, value] tuple.
|
|
4405
|
+
|
|
4406
|
+
#### Parameters
|
|
4407
|
+
|
|
4408
|
+
##### keyNodeOrEntry
|
|
4409
|
+
|
|
4410
|
+
\| `K`
|
|
4411
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
4412
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
4413
|
+
\| `null`
|
|
4414
|
+
\| `undefined`
|
|
4415
|
+
|
|
4416
|
+
The input item.
|
|
4417
|
+
|
|
4418
|
+
##### value?
|
|
4419
|
+
|
|
4420
|
+
`V`
|
|
4421
|
+
|
|
4422
|
+
An optional value (used if input is just a key).
|
|
4423
|
+
|
|
4424
|
+
#### Returns
|
|
4425
|
+
|
|
4426
|
+
\[[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\> \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
4427
|
+
|
|
4428
|
+
A tuple of [node, value].
|
|
4429
|
+
|
|
4430
|
+
#### Remarks
|
|
4431
|
+
|
|
4432
|
+
Time O(1)
|
|
4433
|
+
|
|
4434
|
+
***
|
|
4435
|
+
|
|
4436
|
+
### \_replaceNode()
|
|
4437
|
+
|
|
4438
|
+
```ts
|
|
4439
|
+
protected _replaceNode(oldNode, newNode): BinaryTreeNode<K, V>;
|
|
4440
|
+
```
|
|
4441
|
+
|
|
4442
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:3393](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L3393)
|
|
4443
|
+
|
|
4444
|
+
(Protected) Replaces a node in the tree with a new node, maintaining children and parent links.
|
|
4445
|
+
|
|
4446
|
+
#### Parameters
|
|
4447
|
+
|
|
4448
|
+
##### oldNode
|
|
4449
|
+
|
|
4450
|
+
[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
4451
|
+
|
|
4452
|
+
The node to be replaced.
|
|
4453
|
+
|
|
4454
|
+
##### newNode
|
|
4455
|
+
|
|
4456
|
+
[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
4457
|
+
|
|
4458
|
+
The node to insert.
|
|
4459
|
+
|
|
4460
|
+
#### Returns
|
|
4461
|
+
|
|
4462
|
+
[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
4463
|
+
|
|
4464
|
+
The `newNode`.
|
|
4465
|
+
|
|
4466
|
+
#### Remarks
|
|
4467
|
+
|
|
4468
|
+
Time O(1)
|
|
4469
|
+
|
|
4470
|
+
***
|
|
4471
|
+
|
|
4472
|
+
### \_resolveDisplayLeaf()
|
|
4473
|
+
|
|
4474
|
+
```ts
|
|
4475
|
+
protected _resolveDisplayLeaf(
|
|
4476
|
+
node,
|
|
4477
|
+
options,
|
|
4478
|
+
emptyDisplayLayout): NodeDisplayLayout;
|
|
4479
|
+
```
|
|
4480
|
+
|
|
4481
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:3333](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L3333)
|
|
4482
|
+
|
|
4483
|
+
Resolve a display leaf node to its layout.
|
|
4484
|
+
|
|
4485
|
+
#### Parameters
|
|
4486
|
+
|
|
4487
|
+
##### node
|
|
4488
|
+
|
|
4489
|
+
[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\> \| `null` \| `undefined`
|
|
4490
|
+
|
|
4491
|
+
##### options
|
|
4492
|
+
|
|
4493
|
+
`BinaryTreePrintOptions`
|
|
4494
|
+
|
|
4495
|
+
##### emptyDisplayLayout
|
|
4496
|
+
|
|
4497
|
+
`NodeDisplayLayout`
|
|
4498
|
+
|
|
4499
|
+
#### Returns
|
|
4500
|
+
|
|
4501
|
+
`NodeDisplayLayout`
|
|
4502
|
+
|
|
4503
|
+
***
|
|
4504
|
+
|
|
4505
|
+
### \_setRoot()
|
|
4506
|
+
|
|
4507
|
+
```ts
|
|
4508
|
+
protected _setRoot(v): void;
|
|
4509
|
+
```
|
|
4510
|
+
|
|
4511
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:3417](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L3417)
|
|
4512
|
+
|
|
4513
|
+
(Protected) Sets the root node and clears its parent reference.
|
|
4514
|
+
|
|
4515
|
+
#### Parameters
|
|
4516
|
+
|
|
4517
|
+
##### v
|
|
4518
|
+
|
|
4519
|
+
[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\> \| `null` \| `undefined`
|
|
4520
|
+
|
|
4521
|
+
The node to set as root.
|
|
4522
|
+
|
|
4523
|
+
#### Returns
|
|
4524
|
+
|
|
4525
|
+
`void`
|
|
4526
|
+
|
|
4527
|
+
#### Remarks
|
|
4528
|
+
|
|
4529
|
+
Time O(1)
|
|
4530
|
+
|
|
4531
|
+
***
|
|
4532
|
+
|
|
4533
|
+
### \_setValue()
|
|
4534
|
+
|
|
4535
|
+
```ts
|
|
4536
|
+
protected _setValue(key, value): boolean;
|
|
4537
|
+
```
|
|
4538
|
+
|
|
4539
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:3512](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L3512)
|
|
4540
|
+
|
|
4541
|
+
(Protected) Sets a value in the external store (Map mode).
|
|
4542
|
+
|
|
4543
|
+
#### Parameters
|
|
4544
|
+
|
|
4545
|
+
##### key
|
|
4546
|
+
|
|
4547
|
+
`K` \| `null` \| `undefined`
|
|
4548
|
+
|
|
4549
|
+
The key.
|
|
4550
|
+
|
|
4551
|
+
##### value
|
|
4552
|
+
|
|
4553
|
+
`V` \| `undefined`
|
|
4554
|
+
|
|
4555
|
+
The value.
|
|
4556
|
+
|
|
4557
|
+
#### Returns
|
|
4558
|
+
|
|
4559
|
+
`boolean`
|
|
4560
|
+
|
|
4561
|
+
True if successful.
|
|
4562
|
+
|
|
4563
|
+
#### Remarks
|
|
4564
|
+
|
|
4565
|
+
Time O(1) (average for Map.set).
|
|
4566
|
+
|
|
4567
|
+
***
|
|
4568
|
+
|
|
4569
|
+
### \_snapshotOptions()
|
|
4570
|
+
|
|
4571
|
+
```ts
|
|
4572
|
+
protected _snapshotOptions<TK, TV, TR>(): BinaryTreeOptions<TK, TV, TR>;
|
|
4573
|
+
```
|
|
4574
|
+
|
|
4575
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:3101](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L3101)
|
|
4576
|
+
|
|
4577
|
+
(Protected) Snapshots the current tree's configuration options.
|
|
4578
|
+
|
|
4579
|
+
#### Type Parameters
|
|
4580
|
+
|
|
4581
|
+
##### TK
|
|
4582
|
+
|
|
4583
|
+
`TK` = `K`
|
|
4584
|
+
|
|
4585
|
+
##### TV
|
|
4586
|
+
|
|
4587
|
+
`TV` = `V`
|
|
4588
|
+
|
|
4589
|
+
##### TR
|
|
4590
|
+
|
|
4591
|
+
`TR` = `R`
|
|
4592
|
+
|
|
4593
|
+
#### Returns
|
|
4594
|
+
|
|
4595
|
+
`BinaryTreeOptions`\<`TK`, `TV`, `TR`\>
|
|
4596
|
+
|
|
4597
|
+
The options object.
|
|
4598
|
+
|
|
4599
|
+
#### Remarks
|
|
4600
|
+
|
|
4601
|
+
Time O(1)
|
|
4602
|
+
|
|
4603
|
+
***
|
|
4604
|
+
|
|
4605
|
+
### \_swapProperties()
|
|
4606
|
+
|
|
4607
|
+
```ts
|
|
4608
|
+
protected _swapProperties(srcNode, destNode): BinaryTreeNode<K, V> | undefined;
|
|
4609
|
+
```
|
|
4610
|
+
|
|
4611
|
+
Defined in: [data-structures/binary-tree/binary-tree.ts:3359](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/binary-tree/binary-tree.ts#L3359)
|
|
4612
|
+
|
|
4613
|
+
(Protected) Swaps the key/value properties of two nodes.
|
|
4614
|
+
|
|
4615
|
+
#### Parameters
|
|
4616
|
+
|
|
4617
|
+
##### srcNode
|
|
4618
|
+
|
|
4619
|
+
\| `K`
|
|
4620
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
4621
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
4622
|
+
\| `null`
|
|
4623
|
+
\| `undefined`
|
|
4624
|
+
|
|
4625
|
+
The source node.
|
|
4626
|
+
|
|
4627
|
+
##### destNode
|
|
4628
|
+
|
|
4629
|
+
\| `K`
|
|
4630
|
+
\| [`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\>
|
|
4631
|
+
\| \[`K` \| `null` \| `undefined`, `V` \| `undefined`\]
|
|
4632
|
+
\| `null`
|
|
4633
|
+
\| `undefined`
|
|
4634
|
+
|
|
4635
|
+
The destination node.
|
|
4636
|
+
|
|
4637
|
+
#### Returns
|
|
4638
|
+
|
|
4639
|
+
[`BinaryTreeNode`](BinaryTreeNode.md)\<`K`, `V`\> \| `undefined`
|
|
4640
|
+
|
|
4641
|
+
The `destNode` (now holding `srcNode`'s properties).
|
|
4642
|
+
|
|
4643
|
+
#### Remarks
|
|
4644
|
+
|
|
4645
|
+
Time O(1)
|
|
4646
|
+
|
|
4647
|
+
***
|