data-structure-typed 1.47.6 → 1.47.8
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/.github/ISSUE_TEMPLATE/bug_report.md +10 -7
- package/.github/workflows/ci.yml +1 -1
- package/.github/workflows/release-package.yml +1 -1
- package/CHANGELOG.md +1 -1
- package/CODE_OF_CONDUCT.md +32 -10
- package/COMMANDS.md +3 -1
- package/CONTRIBUTING.md +4 -3
- package/README.md +188 -32
- package/SECURITY.md +1 -1
- package/benchmark/report.html +46 -1
- package/benchmark/report.json +563 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +40 -22
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +45 -36
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +105 -113
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +133 -119
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +53 -44
- package/dist/cjs/data-structures/binary-tree/bst.js +137 -154
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +48 -15
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +70 -33
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +6 -6
- package/dist/cjs/data-structures/binary-tree/segment-tree.js +7 -7
- package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +26 -37
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +58 -137
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +17 -17
- package/dist/cjs/data-structures/graph/abstract-graph.js +30 -30
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +24 -24
- package/dist/cjs/data-structures/graph/directed-graph.js +28 -28
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +14 -14
- package/dist/cjs/data-structures/graph/undirected-graph.js +18 -18
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/cjs/data-structures/hash/hash-map.js +5 -8
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +28 -28
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +33 -33
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +21 -21
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +27 -27
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +4 -4
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +13 -13
- package/dist/cjs/data-structures/queue/queue.js +13 -13
- package/dist/cjs/data-structures/stack/stack.d.ts +6 -6
- package/dist/cjs/data-structures/stack/stack.js +7 -7
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +3 -0
- package/dist/cjs/data-structures/trie/trie.js +19 -4
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/cjs/types/common.d.ts +6 -1
- package/dist/cjs/types/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +40 -22
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +45 -36
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +105 -113
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +133 -128
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +53 -44
- package/dist/mjs/data-structures/binary-tree/bst.js +137 -154
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +48 -15
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +70 -33
- package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +6 -6
- package/dist/mjs/data-structures/binary-tree/segment-tree.js +7 -7
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +26 -37
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +59 -138
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +17 -17
- package/dist/mjs/data-structures/graph/abstract-graph.js +30 -30
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +24 -24
- package/dist/mjs/data-structures/graph/directed-graph.js +28 -28
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +14 -14
- package/dist/mjs/data-structures/graph/undirected-graph.js +18 -18
- package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/mjs/data-structures/hash/hash-map.js +5 -8
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +28 -28
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +33 -33
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +21 -21
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +27 -27
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +4 -4
- package/dist/mjs/data-structures/queue/queue.d.ts +13 -13
- package/dist/mjs/data-structures/queue/queue.js +13 -13
- package/dist/mjs/data-structures/stack/stack.d.ts +6 -6
- package/dist/mjs/data-structures/stack/stack.js +7 -7
- package/dist/mjs/data-structures/trie/trie.d.ts +3 -0
- package/dist/mjs/data-structures/trie/trie.js +20 -4
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/mjs/types/common.d.ts +6 -1
- package/dist/mjs/types/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/umd/data-structure-typed.js +583 -627
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +59 -39
- package/src/data-structures/binary-tree/binary-tree.ts +192 -180
- package/src/data-structures/binary-tree/bst.ts +157 -154
- package/src/data-structures/binary-tree/rb-tree.ts +78 -37
- package/src/data-structures/binary-tree/segment-tree.ts +10 -10
- package/src/data-structures/binary-tree/tree-multimap.ts +67 -145
- package/src/data-structures/graph/abstract-graph.ts +46 -46
- package/src/data-structures/graph/directed-graph.ts +40 -40
- package/src/data-structures/graph/undirected-graph.ts +26 -26
- package/src/data-structures/hash/hash-map.ts +8 -8
- package/src/data-structures/linked-list/doubly-linked-list.ts +45 -45
- package/src/data-structures/linked-list/singly-linked-list.ts +38 -38
- package/src/data-structures/linked-list/skip-linked-list.ts +4 -4
- package/src/data-structures/queue/queue.ts +13 -13
- package/src/data-structures/stack/stack.ts +9 -9
- package/src/data-structures/trie/trie.ts +23 -4
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +11 -1
- package/src/types/data-structures/graph/abstract-graph.ts +2 -2
- package/src/types/data-structures/hash/hash-map.ts +1 -2
- package/test/integration/{all-in-one.ts → all-in-one.test.ts} +1 -1
- package/test/integration/index.html +158 -2
- package/test/performance/data-structures/comparison/comparison.test.ts +5 -5
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +1 -1
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +19 -19
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +50 -51
- package/test/unit/data-structures/binary-tree/bst.test.ts +49 -54
- package/test/unit/data-structures/binary-tree/overall.test.ts +17 -18
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/segment-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +118 -66
- package/test/unit/data-structures/graph/abstract-graph.test.ts +4 -4
- package/test/unit/data-structures/graph/directed-graph.test.ts +10 -10
- package/test/unit/data-structures/graph/undirected-graph.test.ts +3 -3
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +14 -14
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +3 -3
- package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
- package/test/unit/data-structures/queue/deque.test.ts +1 -1
- package/test/unit/data-structures/stack/stack.test.ts +2 -2
- package/test/unit/unrestricted-interconversion.test.ts +61 -5
- package/tsconfig-cjs.json +1 -1
|
@@ -14,38 +14,17 @@ import { Queue } from '../queue';
|
|
|
14
14
|
* @template N - The type of the family relationship in the binary tree.
|
|
15
15
|
*/
|
|
16
16
|
export class BinaryTreeNode {
|
|
17
|
-
/**
|
|
18
|
-
* The key associated with the node.
|
|
19
|
-
*/
|
|
20
17
|
key;
|
|
21
|
-
/**
|
|
22
|
-
* The value stored in the node.
|
|
23
|
-
*/
|
|
24
18
|
value;
|
|
25
|
-
/**
|
|
26
|
-
* The parent node of the current node.
|
|
27
|
-
*/
|
|
28
19
|
parent;
|
|
29
|
-
/**
|
|
30
|
-
* Creates a new instance of BinaryTreeNode.
|
|
31
|
-
* @param {BTNKey} key - The key associated with the node.
|
|
32
|
-
* @param {V} value - The value stored in the node.
|
|
33
|
-
*/
|
|
34
20
|
constructor(key, value) {
|
|
35
21
|
this.key = key;
|
|
36
22
|
this.value = value;
|
|
37
23
|
}
|
|
38
24
|
_left;
|
|
39
|
-
/**
|
|
40
|
-
* Get the left child node.
|
|
41
|
-
*/
|
|
42
25
|
get left() {
|
|
43
26
|
return this._left;
|
|
44
27
|
}
|
|
45
|
-
/**
|
|
46
|
-
* Set the left child node.
|
|
47
|
-
* @param {N | null | undefined} v - The left child node.
|
|
48
|
-
*/
|
|
49
28
|
set left(v) {
|
|
50
29
|
if (v) {
|
|
51
30
|
v.parent = this;
|
|
@@ -53,16 +32,9 @@ export class BinaryTreeNode {
|
|
|
53
32
|
this._left = v;
|
|
54
33
|
}
|
|
55
34
|
_right;
|
|
56
|
-
/**
|
|
57
|
-
* Get the right child node.
|
|
58
|
-
*/
|
|
59
35
|
get right() {
|
|
60
36
|
return this._right;
|
|
61
37
|
}
|
|
62
|
-
/**
|
|
63
|
-
* Set the right child node.
|
|
64
|
-
* @param {N | null | undefined} v - The right child node.
|
|
65
|
-
*/
|
|
66
38
|
set right(v) {
|
|
67
39
|
if (v) {
|
|
68
40
|
v.parent = this;
|
|
@@ -88,14 +60,26 @@ export class BinaryTreeNode {
|
|
|
88
60
|
}
|
|
89
61
|
}
|
|
90
62
|
/**
|
|
91
|
-
*
|
|
92
|
-
*
|
|
63
|
+
* 1. Two Children Maximum: Each node has at most two children.
|
|
64
|
+
* 2. Left and Right Children: Nodes have distinct left and right children.
|
|
65
|
+
* 3. Depth and Height: Depth is the number of edges from the root to a node; height is the maximum depth in the tree.
|
|
66
|
+
* 4. Subtrees: Each child of a node forms the root of a subtree.
|
|
67
|
+
* 5. Leaf Nodes: Nodes without children are leaves.
|
|
68
|
+
* 6. Internal Nodes: Nodes with at least one child are internal.
|
|
69
|
+
* 7. Balanced Trees: The heights of the left and right subtrees of any node differ by no more than one.
|
|
70
|
+
* 8. Full Trees: Every node has either 0 or 2 children.
|
|
71
|
+
* 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
|
|
93
72
|
*/
|
|
94
73
|
export class BinaryTree {
|
|
95
74
|
iterationType = IterationType.ITERATIVE;
|
|
96
75
|
/**
|
|
97
|
-
*
|
|
98
|
-
* @param
|
|
76
|
+
* The constructor function initializes a binary tree object with optional elements and options.
|
|
77
|
+
* @param [elements] - An optional iterable of BTNodeExemplar objects. These objects represent the
|
|
78
|
+
* elements to be added to the binary tree.
|
|
79
|
+
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
80
|
+
* configuration options for the binary tree. In this case, it is of type
|
|
81
|
+
* `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
|
|
82
|
+
* required.
|
|
99
83
|
*/
|
|
100
84
|
constructor(elements, options) {
|
|
101
85
|
if (options) {
|
|
@@ -106,19 +90,13 @@ export class BinaryTree {
|
|
|
106
90
|
}
|
|
107
91
|
this._size = 0;
|
|
108
92
|
if (elements)
|
|
109
|
-
this.
|
|
93
|
+
this.addMany(elements);
|
|
110
94
|
}
|
|
111
95
|
_root;
|
|
112
|
-
/**
|
|
113
|
-
* Get the root node of the binary tree.
|
|
114
|
-
*/
|
|
115
96
|
get root() {
|
|
116
97
|
return this._root;
|
|
117
98
|
}
|
|
118
99
|
_size;
|
|
119
|
-
/**
|
|
120
|
-
* Get the number of nodes in the binary tree.
|
|
121
|
-
*/
|
|
122
100
|
get size() {
|
|
123
101
|
return this._size;
|
|
124
102
|
}
|
|
@@ -131,30 +109,46 @@ export class BinaryTree {
|
|
|
131
109
|
createNode(key, value) {
|
|
132
110
|
return new BinaryTreeNode(key, value);
|
|
133
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* The function creates a binary tree with the given options.
|
|
114
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
115
|
+
* behavior of the `BinaryTree` class. It is of type `Partial<BinaryTreeOptions>`, which means that
|
|
116
|
+
* you can provide only a subset of the properties defined in the `BinaryTreeOptions` interface.
|
|
117
|
+
* @returns a new instance of a binary tree.
|
|
118
|
+
*/
|
|
134
119
|
createTree(options) {
|
|
135
120
|
return new BinaryTree([], { iterationType: this.iterationType, ...options });
|
|
136
121
|
}
|
|
137
122
|
/**
|
|
138
|
-
*
|
|
139
|
-
*
|
|
123
|
+
* The function checks if a given value is an entry in a binary tree node.
|
|
124
|
+
* @param kne - BTNodeExemplar<V, N> - A generic type representing a node in a binary tree. It has
|
|
125
|
+
* two type parameters V and N, representing the value and node type respectively.
|
|
126
|
+
* @returns a boolean value.
|
|
127
|
+
*/
|
|
128
|
+
isEntry(kne) {
|
|
129
|
+
return Array.isArray(kne) && kne.length === 2;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Time Complexity O(log n) - O(n)
|
|
133
|
+
* Space Complexity O(1)
|
|
134
|
+
*/
|
|
135
|
+
/**
|
|
136
|
+
* Time Complexity O(log n) - O(n)
|
|
137
|
+
* Space Complexity O(1)
|
|
140
138
|
*
|
|
141
|
-
* The `add` function adds a new node
|
|
142
|
-
*
|
|
143
|
-
* @
|
|
144
|
-
* following types:
|
|
145
|
-
* @param {V} [value] - The value to be associated with the key or node being added to the binary
|
|
146
|
-
* tree.
|
|
147
|
-
* @returns The function `add` returns a node (`N`) if it was successfully inserted into the binary
|
|
148
|
-
* tree, or `null` or `undefined` if the insertion was not successful.
|
|
139
|
+
* The `add` function adds a new node to a binary tree, either by key or by providing a node object.
|
|
140
|
+
* @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be one of the following:
|
|
141
|
+
* @returns The function `add` returns the inserted node (`N`), `null`, or `undefined`.
|
|
149
142
|
*/
|
|
150
|
-
add(
|
|
143
|
+
add(keyOrNodeOrEntry) {
|
|
144
|
+
let inserted, needInsert;
|
|
151
145
|
const _bfs = (root, newNode) => {
|
|
152
146
|
const queue = new Queue([root]);
|
|
153
147
|
while (queue.size > 0) {
|
|
154
148
|
const cur = queue.shift();
|
|
155
149
|
if (newNode && cur.key === newNode.key) {
|
|
156
|
-
cur
|
|
157
|
-
return;
|
|
150
|
+
this._replaceNode(cur, newNode);
|
|
151
|
+
return newNode;
|
|
158
152
|
}
|
|
159
153
|
const inserted = this._addTo(newNode, cur);
|
|
160
154
|
if (inserted !== undefined)
|
|
@@ -165,15 +159,26 @@ export class BinaryTree {
|
|
|
165
159
|
queue.push(cur.right);
|
|
166
160
|
}
|
|
167
161
|
};
|
|
168
|
-
|
|
169
|
-
if (keyOrNode === null) {
|
|
162
|
+
if (keyOrNodeOrEntry === null) {
|
|
170
163
|
needInsert = null;
|
|
171
164
|
}
|
|
172
|
-
else if (this.isNodeKey(
|
|
173
|
-
needInsert = this.createNode(
|
|
165
|
+
else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
166
|
+
needInsert = this.createNode(keyOrNodeOrEntry);
|
|
167
|
+
}
|
|
168
|
+
else if (keyOrNodeOrEntry instanceof BinaryTreeNode) {
|
|
169
|
+
needInsert = keyOrNodeOrEntry;
|
|
174
170
|
}
|
|
175
|
-
else if (
|
|
176
|
-
|
|
171
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
172
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
173
|
+
if (key === undefined) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
else if (key === null) {
|
|
177
|
+
needInsert = null;
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
needInsert = this.createNode(key, value);
|
|
181
|
+
}
|
|
177
182
|
}
|
|
178
183
|
else {
|
|
179
184
|
return;
|
|
@@ -194,36 +199,28 @@ export class BinaryTree {
|
|
|
194
199
|
return inserted;
|
|
195
200
|
}
|
|
196
201
|
/**
|
|
197
|
-
* Time Complexity: O(n)
|
|
202
|
+
* Time Complexity: O(k log n) - O(k * n)
|
|
198
203
|
* Space Complexity: O(1)
|
|
199
204
|
* Comments: The time complexity for adding a node depends on the depth of the tree. In the best case (when the tree is empty), it's O(1). In the worst case (when the tree is a degenerate tree), it's O(n). The space complexity is constant.
|
|
200
205
|
*/
|
|
201
206
|
/**
|
|
202
|
-
* Time Complexity: O(k
|
|
207
|
+
* Time Complexity: O(k log n) - O(k * n)
|
|
203
208
|
* Space Complexity: O(1)
|
|
204
209
|
*
|
|
205
|
-
* The `addMany`
|
|
206
|
-
*
|
|
207
|
-
* @param
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
214
|
-
*/
|
|
215
|
-
addMany(keysOrNodes, values) {
|
|
210
|
+
* The function `addMany` takes in an iterable of `BTNodeExemplar` objects, adds each object to the
|
|
211
|
+
* current instance, and returns an array of the inserted nodes.
|
|
212
|
+
* @param nodes - The `nodes` parameter is an iterable (such as an array or a set) of
|
|
213
|
+
* `BTNodeExemplar<V, N>` objects.
|
|
214
|
+
* @returns The function `addMany` returns an array of values, where each value is either of type
|
|
215
|
+
* `N`, `null`, or `undefined`.
|
|
216
|
+
*/
|
|
217
|
+
addMany(nodes) {
|
|
216
218
|
// TODO not sure addMany not be run multi times
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
return this.add(null);
|
|
223
|
-
}
|
|
224
|
-
const value = values?.[i];
|
|
225
|
-
return this.add(keyOrNode, value);
|
|
226
|
-
});
|
|
219
|
+
const inserted = [];
|
|
220
|
+
for (const kne of nodes) {
|
|
221
|
+
inserted.push(this.add(kne));
|
|
222
|
+
}
|
|
223
|
+
return inserted;
|
|
227
224
|
}
|
|
228
225
|
/**
|
|
229
226
|
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
@@ -233,17 +230,13 @@ export class BinaryTree {
|
|
|
233
230
|
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
234
231
|
* Space Complexity: O(1)
|
|
235
232
|
*
|
|
236
|
-
* The `refill` function clears the
|
|
237
|
-
* @param
|
|
238
|
-
* `
|
|
239
|
-
* @param {N[] | Array<V>} [values] - The `data` parameter is an optional array of values that will be assigned to
|
|
240
|
-
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
|
|
241
|
-
* array. Each value in the `data` array will be assigned to the
|
|
242
|
-
* @returns The method is returning a boolean value.
|
|
233
|
+
* The `refill` function clears the current collection and adds new nodes, keys, or entries to it.
|
|
234
|
+
* @param nodesOrKeysOrEntries - The parameter `nodesOrKeysOrEntries` is an iterable object that can
|
|
235
|
+
* contain either `BTNodeExemplar` objects, keys, or entries.
|
|
243
236
|
*/
|
|
244
|
-
refill(
|
|
237
|
+
refill(nodesOrKeysOrEntries) {
|
|
245
238
|
this.clear();
|
|
246
|
-
|
|
239
|
+
this.addMany(nodesOrKeysOrEntries);
|
|
247
240
|
}
|
|
248
241
|
/**
|
|
249
242
|
* Time Complexity: O(n)
|
|
@@ -293,7 +286,7 @@ export class BinaryTree {
|
|
|
293
286
|
const leftSubTreeRightMost = this.getRightMost(curr.left);
|
|
294
287
|
if (leftSubTreeRightMost) {
|
|
295
288
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
296
|
-
orgCurrent = this.
|
|
289
|
+
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
297
290
|
if (parentOfLeftSubTreeMax) {
|
|
298
291
|
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
299
292
|
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
@@ -326,8 +319,8 @@ export class BinaryTree {
|
|
|
326
319
|
* @returns the depth of the `distNode` relative to the `beginRoot`.
|
|
327
320
|
*/
|
|
328
321
|
getDepth(distNode, beginRoot = this.root) {
|
|
329
|
-
distNode = this.
|
|
330
|
-
beginRoot = this.
|
|
322
|
+
distNode = this.ensureNode(distNode);
|
|
323
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
331
324
|
let depth = 0;
|
|
332
325
|
while (distNode?.parent) {
|
|
333
326
|
if (distNode === beginRoot) {
|
|
@@ -357,7 +350,7 @@ export class BinaryTree {
|
|
|
357
350
|
* @returns the height of the binary tree.
|
|
358
351
|
*/
|
|
359
352
|
getHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
360
|
-
beginRoot = this.
|
|
353
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
361
354
|
if (!beginRoot)
|
|
362
355
|
return -1;
|
|
363
356
|
if (iterationType === IterationType.RECURSIVE) {
|
|
@@ -403,7 +396,7 @@ export class BinaryTree {
|
|
|
403
396
|
* @returns The function `getMinHeight` returns the minimum height of a binary tree.
|
|
404
397
|
*/
|
|
405
398
|
getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
406
|
-
beginRoot = this.
|
|
399
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
407
400
|
if (!beginRoot)
|
|
408
401
|
return -1;
|
|
409
402
|
if (iterationType === IterationType.RECURSIVE) {
|
|
@@ -493,7 +486,7 @@ export class BinaryTree {
|
|
|
493
486
|
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
494
487
|
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
495
488
|
callback = (node => node);
|
|
496
|
-
beginRoot = this.
|
|
489
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
497
490
|
if (!beginRoot)
|
|
498
491
|
return [];
|
|
499
492
|
const ans = [];
|
|
@@ -631,7 +624,7 @@ export class BinaryTree {
|
|
|
631
624
|
* Space Complexity: O(log n)
|
|
632
625
|
*/
|
|
633
626
|
/**
|
|
634
|
-
* The function `
|
|
627
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a valid node
|
|
635
628
|
* key, otherwise it returns the key itself.
|
|
636
629
|
* @param {BTNKey | N | null | undefined} key - The `key` parameter can be of type `BTNKey`, `N`,
|
|
637
630
|
* `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
|
|
@@ -641,7 +634,7 @@ export class BinaryTree {
|
|
|
641
634
|
* @returns either the node corresponding to the given key if it is a valid node key, or the key
|
|
642
635
|
* itself if it is not a valid node key.
|
|
643
636
|
*/
|
|
644
|
-
|
|
637
|
+
ensureNode(key, iterationType = IterationType.ITERATIVE) {
|
|
645
638
|
return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
646
639
|
}
|
|
647
640
|
/**
|
|
@@ -706,7 +699,7 @@ export class BinaryTree {
|
|
|
706
699
|
getPathToRoot(beginRoot, isReverse = true) {
|
|
707
700
|
// TODO to support get path through passing key
|
|
708
701
|
const result = [];
|
|
709
|
-
beginRoot = this.
|
|
702
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
710
703
|
if (!beginRoot)
|
|
711
704
|
return result;
|
|
712
705
|
while (beginRoot.parent) {
|
|
@@ -737,7 +730,7 @@ export class BinaryTree {
|
|
|
737
730
|
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
738
731
|
*/
|
|
739
732
|
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
740
|
-
beginRoot = this.
|
|
733
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
741
734
|
if (!beginRoot)
|
|
742
735
|
return beginRoot;
|
|
743
736
|
if (iterationType === IterationType.RECURSIVE) {
|
|
@@ -779,7 +772,7 @@ export class BinaryTree {
|
|
|
779
772
|
*/
|
|
780
773
|
getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
781
774
|
// TODO support get right most by passing key in
|
|
782
|
-
beginRoot = this.
|
|
775
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
783
776
|
if (!beginRoot)
|
|
784
777
|
return beginRoot;
|
|
785
778
|
if (iterationType === IterationType.RECURSIVE) {
|
|
@@ -818,7 +811,7 @@ export class BinaryTree {
|
|
|
818
811
|
*/
|
|
819
812
|
isSubtreeBST(beginRoot, iterationType = this.iterationType) {
|
|
820
813
|
// TODO there is a bug
|
|
821
|
-
beginRoot = this.
|
|
814
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
822
815
|
if (!beginRoot)
|
|
823
816
|
return true;
|
|
824
817
|
if (iterationType === IterationType.RECURSIVE) {
|
|
@@ -891,7 +884,7 @@ export class BinaryTree {
|
|
|
891
884
|
* by the return type of the `callback` function.
|
|
892
885
|
*/
|
|
893
886
|
subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
894
|
-
beginRoot = this.
|
|
887
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
895
888
|
const ans = [];
|
|
896
889
|
if (!beginRoot)
|
|
897
890
|
return ans;
|
|
@@ -992,7 +985,7 @@ export class BinaryTree {
|
|
|
992
985
|
* @returns an array of values that are the return values of the callback function.
|
|
993
986
|
*/
|
|
994
987
|
dfs(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root, iterationType = IterationType.ITERATIVE, includeNull = false) {
|
|
995
|
-
beginRoot = this.
|
|
988
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
996
989
|
if (!beginRoot)
|
|
997
990
|
return [];
|
|
998
991
|
const ans = [];
|
|
@@ -1119,7 +1112,7 @@ export class BinaryTree {
|
|
|
1119
1112
|
* the breadth-first traversal of a binary tree.
|
|
1120
1113
|
*/
|
|
1121
1114
|
bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
1122
|
-
beginRoot = this.
|
|
1115
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
1123
1116
|
if (!beginRoot)
|
|
1124
1117
|
return [];
|
|
1125
1118
|
const ans = [];
|
|
@@ -1192,7 +1185,7 @@ export class BinaryTree {
|
|
|
1192
1185
|
* @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
|
|
1193
1186
|
*/
|
|
1194
1187
|
listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
1195
|
-
beginRoot = this.
|
|
1188
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
1196
1189
|
const levelsNodes = [];
|
|
1197
1190
|
if (!beginRoot)
|
|
1198
1191
|
return levelsNodes;
|
|
@@ -1247,7 +1240,7 @@ export class BinaryTree {
|
|
|
1247
1240
|
* @returns The function `getPredecessor` returns a value of type `N | undefined`.
|
|
1248
1241
|
*/
|
|
1249
1242
|
getPredecessor(node) {
|
|
1250
|
-
node = this.
|
|
1243
|
+
node = this.ensureNode(node);
|
|
1251
1244
|
if (!this.isRealNode(node))
|
|
1252
1245
|
return undefined;
|
|
1253
1246
|
if (node.left) {
|
|
@@ -1270,7 +1263,7 @@ export class BinaryTree {
|
|
|
1270
1263
|
* after the given node in the inorder traversal of the binary tree.
|
|
1271
1264
|
*/
|
|
1272
1265
|
getSuccessor(x) {
|
|
1273
|
-
x = this.
|
|
1266
|
+
x = this.ensureNode(x);
|
|
1274
1267
|
if (!x)
|
|
1275
1268
|
return undefined;
|
|
1276
1269
|
if (x.right) {
|
|
@@ -1302,7 +1295,7 @@ export class BinaryTree {
|
|
|
1302
1295
|
* by the return type of the `callback` function.
|
|
1303
1296
|
*/
|
|
1304
1297
|
morris(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root) {
|
|
1305
|
-
beginRoot = this.
|
|
1298
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
1306
1299
|
if (beginRoot === null)
|
|
1307
1300
|
return [];
|
|
1308
1301
|
const ans = [];
|
|
@@ -1413,7 +1406,7 @@ export class BinaryTree {
|
|
|
1413
1406
|
const newTree = this.createTree();
|
|
1414
1407
|
for (const [key, value] of this) {
|
|
1415
1408
|
if (predicate([key, value], this)) {
|
|
1416
|
-
newTree.add(key, value);
|
|
1409
|
+
newTree.add([key, value]);
|
|
1417
1410
|
}
|
|
1418
1411
|
}
|
|
1419
1412
|
return newTree;
|
|
@@ -1427,7 +1420,7 @@ export class BinaryTree {
|
|
|
1427
1420
|
map(callback) {
|
|
1428
1421
|
const newTree = this.createTree();
|
|
1429
1422
|
for (const [key, value] of this) {
|
|
1430
|
-
newTree.add(key, callback([key, value], this));
|
|
1423
|
+
newTree.add([key, callback([key, value], this)]);
|
|
1431
1424
|
}
|
|
1432
1425
|
return newTree;
|
|
1433
1426
|
}
|
|
@@ -1504,7 +1497,7 @@ export class BinaryTree {
|
|
|
1504
1497
|
*/
|
|
1505
1498
|
print(beginRoot = this.root, options) {
|
|
1506
1499
|
const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
|
|
1507
|
-
beginRoot = this.
|
|
1500
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
1508
1501
|
if (!beginRoot)
|
|
1509
1502
|
return;
|
|
1510
1503
|
if (opts.isShowUndefined)
|
|
@@ -1524,19 +1517,6 @@ export class BinaryTree {
|
|
|
1524
1517
|
};
|
|
1525
1518
|
display(beginRoot);
|
|
1526
1519
|
}
|
|
1527
|
-
init(elements) {
|
|
1528
|
-
if (elements) {
|
|
1529
|
-
for (const entryOrKey of elements) {
|
|
1530
|
-
if (Array.isArray(entryOrKey)) {
|
|
1531
|
-
const [key, value] = entryOrKey;
|
|
1532
|
-
this.add(key, value);
|
|
1533
|
-
}
|
|
1534
|
-
else {
|
|
1535
|
-
this.add(entryOrKey);
|
|
1536
|
-
}
|
|
1537
|
-
}
|
|
1538
|
-
}
|
|
1539
|
-
}
|
|
1540
1520
|
_displayAux(node, options) {
|
|
1541
1521
|
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
1542
1522
|
const emptyDisplayLayout = [['─'], 1, 0, 0];
|
|
@@ -1587,9 +1567,9 @@ export class BinaryTree {
|
|
|
1587
1567
|
* @param {N} destNode - The destination node to swap.
|
|
1588
1568
|
* @returns {N} - The destination node after the swap.
|
|
1589
1569
|
*/
|
|
1590
|
-
|
|
1591
|
-
srcNode = this.
|
|
1592
|
-
destNode = this.
|
|
1570
|
+
_swapProperties(srcNode, destNode) {
|
|
1571
|
+
srcNode = this.ensureNode(srcNode);
|
|
1572
|
+
destNode = this.ensureNode(destNode);
|
|
1593
1573
|
if (srcNode && destNode) {
|
|
1594
1574
|
const { key, value } = destNode;
|
|
1595
1575
|
const tempNode = this.createNode(key, value);
|
|
@@ -1603,6 +1583,31 @@ export class BinaryTree {
|
|
|
1603
1583
|
}
|
|
1604
1584
|
return undefined;
|
|
1605
1585
|
}
|
|
1586
|
+
/**
|
|
1587
|
+
* The function replaces an old node with a new node in a binary tree.
|
|
1588
|
+
* @param {N} oldNode - The oldNode parameter represents the node that needs to be replaced in the
|
|
1589
|
+
* tree.
|
|
1590
|
+
* @param {N} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
1591
|
+
* tree.
|
|
1592
|
+
* @returns The method is returning the newNode.
|
|
1593
|
+
*/
|
|
1594
|
+
_replaceNode(oldNode, newNode) {
|
|
1595
|
+
if (oldNode.parent) {
|
|
1596
|
+
if (oldNode.parent.left === oldNode) {
|
|
1597
|
+
oldNode.parent.left = newNode;
|
|
1598
|
+
}
|
|
1599
|
+
else if (oldNode.parent.right === oldNode) {
|
|
1600
|
+
oldNode.parent.right = newNode;
|
|
1601
|
+
}
|
|
1602
|
+
}
|
|
1603
|
+
newNode.left = oldNode.left;
|
|
1604
|
+
newNode.right = oldNode.right;
|
|
1605
|
+
newNode.parent = oldNode.parent;
|
|
1606
|
+
if (this.root === oldNode) {
|
|
1607
|
+
this._root = newNode;
|
|
1608
|
+
}
|
|
1609
|
+
return newNode;
|
|
1610
|
+
}
|
|
1606
1611
|
/**
|
|
1607
1612
|
* The function `_addTo` adds a new node to a binary tree if there is an available position.
|
|
1608
1613
|
* @param {N | null | undefined} newNode - The `newNode` parameter represents the node that you want to add to
|