data-structure-typed 0.9.16 → 1.12.10
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/.dependency-cruiser.js +449 -0
- package/.idea/data-structure-typed.iml +2 -0
- package/README.md +102 -1
- package/dist/data-structures/binary-tree/avl-tree.d.ts +57 -0
- package/dist/data-structures/binary-tree/avl-tree.js +57 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +27 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +27 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +215 -9
- package/dist/data-structures/binary-tree/binary-tree.js +283 -15
- package/dist/data-structures/binary-tree/bst.d.ts +16 -0
- package/dist/data-structures/binary-tree/bst.js +12 -0
- package/dist/data-structures/binary-tree/segment-tree.d.ts +4 -0
- package/dist/data-structures/binary-tree/segment-tree.js +4 -0
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +4 -0
- package/dist/data-structures/binary-tree/tree-multiset.js +4 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +163 -13
- package/dist/data-structures/graph/abstract-graph.js +166 -12
- package/dist/data-structures/graph/directed-graph.d.ts +111 -1
- package/dist/data-structures/graph/directed-graph.js +122 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +63 -0
- package/dist/data-structures/graph/undirected-graph.js +67 -0
- package/dist/data-structures/hash/coordinate-map.d.ts +32 -0
- package/dist/data-structures/hash/coordinate-map.js +32 -0
- package/dist/data-structures/hash/coordinate-set.d.ts +25 -0
- package/dist/data-structures/hash/coordinate-set.js +25 -0
- package/dist/data-structures/heap/heap.d.ts +26 -31
- package/dist/data-structures/heap/heap.js +23 -32
- package/dist/data-structures/heap/max-heap.d.ts +6 -1
- package/dist/data-structures/heap/max-heap.js +6 -1
- package/dist/data-structures/heap/min-heap.d.ts +7 -1
- package/dist/data-structures/heap/min-heap.js +7 -1
- package/dist/data-structures/index.d.ts +0 -1
- package/dist/data-structures/index.js +0 -1
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +14 -6
- package/dist/data-structures/linked-list/doubly-linked-list.js +45 -8
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +11 -8
- package/dist/data-structures/linked-list/singly-linked-list.js +4 -0
- package/dist/data-structures/matrix/matrix.d.ts +9 -0
- package/dist/data-structures/matrix/matrix.js +11 -0
- package/dist/data-structures/matrix/matrix2d.d.ts +82 -2
- package/dist/data-structures/matrix/matrix2d.js +82 -2
- package/dist/data-structures/matrix/navigator.d.ts +32 -0
- package/dist/data-structures/matrix/navigator.js +28 -0
- package/dist/data-structures/matrix/vector2d.d.ts +139 -15
- package/dist/data-structures/matrix/vector2d.js +139 -15
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +8 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +8 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +8 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +8 -0
- package/dist/data-structures/priority-queue/priority-queue.d.ts +130 -0
- package/dist/data-structures/priority-queue/priority-queue.js +126 -0
- package/dist/data-structures/queue/deque.d.ts +69 -0
- package/dist/data-structures/queue/deque.js +69 -0
- package/dist/data-structures/queue/queue.d.ts +30 -32
- package/dist/data-structures/queue/queue.js +30 -32
- package/dist/data-structures/stack/stack.d.ts +26 -31
- package/dist/data-structures/stack/stack.js +26 -31
- package/dist/data-structures/trie/trie.d.ts +26 -6
- package/dist/data-structures/trie/trie.js +26 -7
- package/dist/data-structures/types/avl-tree.d.ts +1 -1
- package/dist/data-structures/types/binary-tree.d.ts +1 -1
- package/dist/data-structures/types/index.d.ts +1 -1
- package/dist/data-structures/types/index.js +1 -1
- package/dist/data-structures/types/singly-linked-list.d.ts +1 -5
- package/dist/data-structures/types/tree-multiset.d.ts +1 -1
- package/dist/{data-structures → utils}/trampoline.d.ts +5 -7
- package/dist/utils/types/index.d.ts +1 -0
- package/dist/utils/types/index.js +17 -0
- package/dist/{data-structures → utils}/types/utils.d.ts +8 -0
- package/dist/{data-structures → utils}/types/utils.js +1 -0
- package/dist/utils/utils.d.ts +1 -1
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +85 -0
- package/docs/assets/main.js +58 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1367 -0
- package/docs/classes/AVLTree.html +2046 -0
- package/docs/classes/AVLTreeNode.html +423 -0
- package/docs/classes/AaTree.html +117 -0
- package/docs/classes/AbstractEdge.html +198 -0
- package/docs/classes/AbstractGraph.html +891 -0
- package/docs/classes/AbstractVertex.html +164 -0
- package/docs/classes/ArrayDeque.html +384 -0
- package/docs/classes/BST.html +1893 -0
- package/docs/classes/BSTNode.html +425 -0
- package/docs/classes/BTree.html +117 -0
- package/docs/classes/BinaryIndexedTree.html +244 -0
- package/docs/classes/BinaryTree.html +1754 -0
- package/docs/classes/BinaryTreeNode.html +396 -0
- package/docs/classes/Character.html +165 -0
- package/docs/classes/CoordinateMap.html +394 -0
- package/docs/classes/CoordinateSet.html +355 -0
- package/docs/classes/Deque.html +617 -0
- package/docs/classes/DirectedEdge.html +247 -0
- package/docs/classes/DirectedGraph.html +1207 -0
- package/docs/classes/DirectedVertex.html +154 -0
- package/docs/classes/DoublyLinkedList.html +619 -0
- package/docs/classes/DoublyLinkedListNode.html +160 -0
- package/docs/classes/Heap.html +315 -0
- package/docs/classes/Matrix2D.html +447 -0
- package/docs/classes/MatrixNTI2D.html +181 -0
- package/docs/classes/MaxHeap.html +325 -0
- package/docs/classes/MaxPriorityQueue.html +668 -0
- package/docs/classes/MinHeap.html +326 -0
- package/docs/classes/MinPriorityQueue.html +668 -0
- package/docs/classes/Navigator.html +285 -0
- package/docs/classes/ObjectDeque.html +289 -0
- package/docs/classes/PriorityQueue.html +643 -0
- package/docs/classes/Queue.html +337 -0
- package/docs/classes/RBTree.html +117 -0
- package/docs/classes/SegmentTree.html +234 -0
- package/docs/classes/SegmentTreeNode.html +302 -0
- package/docs/classes/SinglyLinkedList.html +1035 -0
- package/docs/classes/SinglyLinkedListNode.html +304 -0
- package/docs/classes/SplayTree.html +117 -0
- package/docs/classes/Stack.html +313 -0
- package/docs/classes/TreeMultiSet.html +1897 -0
- package/docs/classes/Trie.html +317 -0
- package/docs/classes/TrieNode.html +221 -0
- package/docs/classes/TwoThreeTree.html +117 -0
- package/docs/classes/UndirectedEdge.html +220 -0
- package/docs/classes/UndirectedGraph.html +1006 -0
- package/docs/classes/UndirectedVertex.html +154 -0
- package/docs/classes/Vector2D.html +746 -0
- package/docs/enums/CP.html +126 -0
- package/docs/enums/FamilyPosition.html +126 -0
- package/docs/enums/LoopType.html +119 -0
- package/docs/index.html +288 -0
- package/docs/modules.html +146 -0
- package/jest.config.js +5 -0
- package/package.json +19 -11
- package/rename_clear_files.sh +29 -0
- package/src/assets/complexities-diff.jpg +0 -0
- package/src/assets/data-structure-complexities.jpg +0 -0
- package/src/data-structures/binary-tree/avl-tree.ts +57 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +27 -0
- package/src/data-structures/binary-tree/binary-tree.ts +288 -18
- package/src/data-structures/binary-tree/bst.ts +16 -1
- package/src/data-structures/binary-tree/segment-tree.ts +5 -0
- package/src/data-structures/binary-tree/tree-multiset.ts +4 -0
- package/src/data-structures/diagrams/README.md +5 -7
- package/src/data-structures/graph/abstract-graph.ts +167 -15
- package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
- package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
- package/src/data-structures/graph/diagrams/mst.jpg +0 -0
- package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
- package/src/data-structures/graph/directed-graph.ts +127 -5
- package/src/data-structures/graph/undirected-graph.ts +67 -1
- package/src/data-structures/hash/coordinate-map.ts +32 -0
- package/src/data-structures/hash/coordinate-set.ts +25 -0
- package/src/data-structures/heap/heap.ts +27 -32
- package/src/data-structures/heap/max-heap.ts +6 -1
- package/src/data-structures/heap/min-heap.ts +7 -1
- package/src/data-structures/index.ts +0 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +49 -8
- package/src/data-structures/linked-list/singly-linked-list.ts +39 -8
- package/src/data-structures/matrix/matrix.ts +11 -0
- package/src/data-structures/matrix/matrix2d.ts +82 -2
- package/src/data-structures/matrix/navigator.ts +32 -0
- package/src/data-structures/matrix/vector2d.ts +139 -15
- package/src/data-structures/priority-queue/max-priority-queue.ts +8 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +8 -0
- package/src/data-structures/priority-queue/priority-queue.ts +130 -0
- package/src/data-structures/queue/deque.ts +69 -0
- package/src/data-structures/queue/queue.ts +30 -32
- package/src/data-structures/stack/stack.ts +26 -31
- package/src/data-structures/trie/trie.ts +26 -7
- package/src/data-structures/types/avl-tree.ts +1 -1
- package/src/data-structures/types/binary-tree.ts +1 -1
- package/src/data-structures/types/bst.ts +1 -1
- package/src/data-structures/types/index.ts +1 -1
- package/src/data-structures/types/navigator.ts +1 -0
- package/src/data-structures/types/singly-linked-list.ts +1 -15
- package/src/data-structures/types/tree-multiset.ts +1 -1
- package/src/{data-structures → utils}/trampoline.ts +6 -6
- package/src/utils/types/index.ts +1 -0
- package/src/{data-structures → utils}/types/utils.ts +7 -0
- package/src/utils/utils.ts +12 -2
- package/tests/unit/data-structures/binary-tree/bst.test.ts +185 -0
- package/tests/unit/data-structures/graph/abstract-graph.ts +0 -0
- package/tests/unit/data-structures/graph/directed-graph.test.ts +71 -0
- package/tests/unit/data-structures/graph/index.ts +3 -0
- package/tests/unit/data-structures/graph/undirected-graph.ts +0 -0
- package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
- package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
- package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/edge-list.png +0 -0
- package/src/data-structures/graph/diagrams/max-flow.png +0 -0
- package/src/data-structures/graph/diagrams/mst.png +0 -0
- /package/dist/{data-structures → utils}/trampoline.js +0 -0
|
@@ -1,9 +1,16 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {trampoline} from '../../utils/trampoline';
|
|
2
7
|
import type {
|
|
3
8
|
BinaryTreeDeleted,
|
|
4
9
|
BinaryTreeNodeId,
|
|
5
|
-
BinaryTreeNodePropertyName,
|
|
6
|
-
|
|
10
|
+
BinaryTreeNodePropertyName,
|
|
11
|
+
DFSOrderPattern,
|
|
12
|
+
NodeOrPropertyName,
|
|
13
|
+
ResultByProperty,
|
|
7
14
|
ResultsByProperty
|
|
8
15
|
} from '../types';
|
|
9
16
|
|
|
@@ -141,6 +148,11 @@ export class BinaryTree<T> {
|
|
|
141
148
|
private _maxId: number = -1;
|
|
142
149
|
private readonly _isDuplicatedVal: boolean = false;
|
|
143
150
|
|
|
151
|
+
/**
|
|
152
|
+
* The constructor function accepts an optional options object and sets the values of loopType, autoIncrementId, and
|
|
153
|
+
* isDuplicatedVal based on the provided options.
|
|
154
|
+
* @param [options] - An optional object that can contain the following properties:
|
|
155
|
+
*/
|
|
144
156
|
constructor(options?: {
|
|
145
157
|
loopType?: LoopType,
|
|
146
158
|
autoIncrementId?: boolean,
|
|
@@ -160,7 +172,7 @@ export class BinaryTree<T> {
|
|
|
160
172
|
|
|
161
173
|
protected _root: BinaryTreeNode<T> | null = null;
|
|
162
174
|
|
|
163
|
-
|
|
175
|
+
get root(): BinaryTreeNode<T> | null {
|
|
164
176
|
return this._root;
|
|
165
177
|
}
|
|
166
178
|
|
|
@@ -174,7 +186,7 @@ export class BinaryTree<T> {
|
|
|
174
186
|
|
|
175
187
|
protected _size = 0;
|
|
176
188
|
|
|
177
|
-
|
|
189
|
+
get size(): number {
|
|
178
190
|
return this._size;
|
|
179
191
|
}
|
|
180
192
|
|
|
@@ -184,7 +196,7 @@ export class BinaryTree<T> {
|
|
|
184
196
|
|
|
185
197
|
protected _count = 0;
|
|
186
198
|
|
|
187
|
-
|
|
199
|
+
get count(): number {
|
|
188
200
|
return this._count;
|
|
189
201
|
}
|
|
190
202
|
|
|
@@ -192,14 +204,25 @@ export class BinaryTree<T> {
|
|
|
192
204
|
this._count = v;
|
|
193
205
|
}
|
|
194
206
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
207
|
+
/**
|
|
208
|
+
* The function creates a new binary tree node with the given id, value, and count, or returns null if the value is
|
|
209
|
+
* null.
|
|
210
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is of type
|
|
211
|
+
* `BinaryTreeNodeId`, which could be a string or a number, depending on how you want to identify your nodes.
|
|
212
|
+
* @param {T | null} val - The `val` parameter represents the value to be stored in the binary tree node. It can be of
|
|
213
|
+
* any type `T` or `null`.
|
|
214
|
+
* @param {number} [count] - The count parameter is an optional parameter that represents the number of occurrences of
|
|
215
|
+
* the value in the binary tree node. It is of type number.
|
|
216
|
+
* @returns The function `createNode` returns a `BinaryTreeNode<T>` object if the `val` parameter is not null.
|
|
217
|
+
* Otherwise, it returns null.
|
|
218
|
+
*/
|
|
199
219
|
createNode(id: BinaryTreeNodeId, val: T | null, count?: number): BinaryTreeNode<T> | null {
|
|
200
220
|
return val !== null ? new BinaryTreeNode(id, val, count) : null;
|
|
201
221
|
}
|
|
202
222
|
|
|
223
|
+
/**
|
|
224
|
+
* The clear function resets the state of an object by setting its properties to their initial values.
|
|
225
|
+
*/
|
|
203
226
|
clear() {
|
|
204
227
|
this.root = null;
|
|
205
228
|
this.size = 0;
|
|
@@ -207,11 +230,23 @@ export class BinaryTree<T> {
|
|
|
207
230
|
this._maxId = -1;
|
|
208
231
|
}
|
|
209
232
|
|
|
233
|
+
/**
|
|
234
|
+
* The function checks if the size of an object is equal to zero and returns a boolean value.
|
|
235
|
+
* @returns A boolean value indicating whether the size of the object is 0 or not.
|
|
236
|
+
*/
|
|
210
237
|
isEmpty(): boolean {
|
|
211
238
|
return this.size === 0;
|
|
212
239
|
}
|
|
213
240
|
|
|
214
|
-
|
|
241
|
+
/**
|
|
242
|
+
* The function inserts a new node into a binary tree as the left or right child of a given parent node.
|
|
243
|
+
* @param {BinaryTreeNode<T> | null} newNode - The `newNode` parameter is an instance of the `BinaryTreeNode` class or
|
|
244
|
+
* `null`. It represents the node that needs to be inserted into the binary tree.
|
|
245
|
+
* @param parent - The `parent` parameter is a BinaryTreeNode object representing the parent node to which the new node
|
|
246
|
+
* will be inserted as a child.
|
|
247
|
+
* @returns The method returns the newly inserted node, either as the left child or the right child of the parent node.
|
|
248
|
+
*/
|
|
249
|
+
putTo(newNode: BinaryTreeNode<T> | null, parent: BinaryTreeNode<T> ) {
|
|
215
250
|
if (parent) {
|
|
216
251
|
if (parent.left === undefined) {
|
|
217
252
|
if (newNode) {
|
|
@@ -244,6 +279,17 @@ export class BinaryTree<T> {
|
|
|
244
279
|
}
|
|
245
280
|
}
|
|
246
281
|
|
|
282
|
+
/**
|
|
283
|
+
* The `put` function inserts a new node with a given ID and value into a binary tree, updating the count if the node
|
|
284
|
+
* already exists.
|
|
285
|
+
* @param {BinaryTreeNodeId} id - The id parameter is the identifier of the binary tree node. It is used to uniquely
|
|
286
|
+
* identify each node in the binary tree.
|
|
287
|
+
* @param {T} val - The value to be inserted into the binary tree.
|
|
288
|
+
* @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the
|
|
289
|
+
* value should be inserted into the binary tree. If not provided, it defaults to 1.
|
|
290
|
+
* @returns The function `put` returns a `BinaryTreeNode<T>` object if a new node is inserted, or `null` if no new node
|
|
291
|
+
* is inserted, or `undefined` if the insertion fails.
|
|
292
|
+
*/
|
|
247
293
|
put(id: BinaryTreeNodeId, val: T, count?: number): BinaryTreeNode<T> | null | undefined {
|
|
248
294
|
count = count ?? 1;
|
|
249
295
|
|
|
@@ -252,7 +298,7 @@ export class BinaryTree<T> {
|
|
|
252
298
|
while (queue.length > 0) {
|
|
253
299
|
const cur = queue.shift();
|
|
254
300
|
if (cur) {
|
|
255
|
-
const inserted = this.
|
|
301
|
+
const inserted = this.putTo(newNode, cur);
|
|
256
302
|
if (inserted !== undefined) return inserted;
|
|
257
303
|
if (cur.left) queue.push(cur.left);
|
|
258
304
|
if (cur.right) queue.push(cur.right);
|
|
@@ -286,6 +332,13 @@ export class BinaryTree<T> {
|
|
|
286
332
|
return inserted;
|
|
287
333
|
}
|
|
288
334
|
|
|
335
|
+
/**
|
|
336
|
+
* The `insertMany` function inserts multiple items into a binary tree and returns an array of the inserted nodes or
|
|
337
|
+
* null/undefined values.
|
|
338
|
+
* @param {T[] | BinaryTreeNode<T>[]} data - The `data` parameter can be either an array of elements of type `T` or an
|
|
339
|
+
* array of `BinaryTreeNode<T>` objects.
|
|
340
|
+
* @returns The function `insertMany` returns an array of `BinaryTreeNode<T>`, `null`, or `undefined` values.
|
|
341
|
+
*/
|
|
289
342
|
insertMany(data: T[] | BinaryTreeNode<T>[]): (BinaryTreeNode<T> | null | undefined)[] {
|
|
290
343
|
const inserted: (BinaryTreeNode<T> | null | undefined)[] = [];
|
|
291
344
|
const map: Map<T | BinaryTreeNode<T>, number> = new Map();
|
|
@@ -326,11 +379,29 @@ export class BinaryTree<T> {
|
|
|
326
379
|
return inserted;
|
|
327
380
|
}
|
|
328
381
|
|
|
382
|
+
/**
|
|
383
|
+
* The `fill` function clears the current data and inserts new data, returning a boolean indicating if the insertion
|
|
384
|
+
* was successful.
|
|
385
|
+
* @param {T[] | BinaryTreeNode<T>[]} data - The `data` parameter can be either an array of elements of type `T` or an
|
|
386
|
+
* array of `BinaryTreeNode<T>` objects.
|
|
387
|
+
* @returns The method is returning a boolean value.
|
|
388
|
+
*/
|
|
329
389
|
fill(data: T[] | BinaryTreeNode<T>[]): boolean {
|
|
330
390
|
this.clear();
|
|
331
391
|
return data.length === this.insertMany(data).length;
|
|
332
392
|
}
|
|
333
393
|
|
|
394
|
+
/**
|
|
395
|
+
* The function removes a node from a binary tree and returns information about the deleted node.
|
|
396
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that you want to remove.
|
|
397
|
+
* It is of type `BinaryTreeNodeId`.
|
|
398
|
+
* @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
|
|
399
|
+
* whether to ignore the count of the node being removed. If `ignoreCount` is set to `true`, the count of the node will
|
|
400
|
+
* not be decremented and the overall count of the binary tree will not be updated. If `
|
|
401
|
+
* @returns An array of objects is being returned. Each object in the array has two properties: "deleted" and
|
|
402
|
+
* "needBalanced". The "deleted" property contains the deleted node or undefined if no node was deleted. The
|
|
403
|
+
* "needBalanced" property is always null.
|
|
404
|
+
*/
|
|
334
405
|
remove(id: BinaryTreeNodeId, ignoreCount?: boolean): BinaryTreeDeleted<T>[] {
|
|
335
406
|
const nodes = this.getNodes(id, 'id', true);
|
|
336
407
|
let node: BinaryTreeNode<T> | null | undefined = nodes[0];
|
|
@@ -367,6 +438,12 @@ export class BinaryTree<T> {
|
|
|
367
438
|
return [{deleted: node, needBalanced: null}];
|
|
368
439
|
}
|
|
369
440
|
|
|
441
|
+
/**
|
|
442
|
+
* The function calculates the depth of a binary tree node by traversing its parent nodes.
|
|
443
|
+
* @param node - BinaryTreeNode<T> - This is the node for which we want to calculate the depth. It is a generic type,
|
|
444
|
+
* meaning it can represent any type of data that we want to store in the node.
|
|
445
|
+
* @returns The depth of the given binary tree node.
|
|
446
|
+
*/
|
|
370
447
|
getDepth(node: BinaryTreeNode<T>): number {
|
|
371
448
|
let depth = 0;
|
|
372
449
|
while (node.parent) {
|
|
@@ -376,6 +453,14 @@ export class BinaryTree<T> {
|
|
|
376
453
|
return depth;
|
|
377
454
|
}
|
|
378
455
|
|
|
456
|
+
/**
|
|
457
|
+
* The `getHeight` function calculates the maximum height of a binary tree using either a recursive or iterative
|
|
458
|
+
* approach.
|
|
459
|
+
* @param {BinaryTreeNode<T> | null} [beginRoot] - The `beginRoot` parameter is an optional parameter of type
|
|
460
|
+
* `BinaryTreeNode<T> | null`. It represents the starting node from which to calculate the height of the binary tree.
|
|
461
|
+
* If no value is provided for `beginRoot`, the function will use the `root` property of the class instance as
|
|
462
|
+
* @returns the height of the binary tree.
|
|
463
|
+
*/
|
|
379
464
|
getHeight(beginRoot?: BinaryTreeNode<T> | null): number {
|
|
380
465
|
beginRoot = beginRoot ?? this.root;
|
|
381
466
|
if (!beginRoot) return -1;
|
|
@@ -417,6 +502,14 @@ export class BinaryTree<T> {
|
|
|
417
502
|
}
|
|
418
503
|
}
|
|
419
504
|
|
|
505
|
+
/**
|
|
506
|
+
* The `getMinHeight` function calculates the minimum height of a binary tree using either a recursive or iterative
|
|
507
|
+
* approach.
|
|
508
|
+
* @param {BinaryTreeNode<T> | null} [beginRoot] - The `beginRoot` parameter is an optional parameter of type
|
|
509
|
+
* `BinaryTreeNode<T> | null`. It represents the starting node from which to calculate the minimum height of the binary
|
|
510
|
+
* tree. If no value is provided for `beginRoot`, the function will use the root node of the binary tree.
|
|
511
|
+
* @returns The function `getMinHeight` returns the minimum height of the binary tree.
|
|
512
|
+
*/
|
|
420
513
|
getMinHeight(beginRoot?: BinaryTreeNode<T> | null): number {
|
|
421
514
|
beginRoot = beginRoot || this.root;
|
|
422
515
|
if (!beginRoot) return -1;
|
|
@@ -459,10 +552,28 @@ export class BinaryTree<T> {
|
|
|
459
552
|
}
|
|
460
553
|
}
|
|
461
554
|
|
|
555
|
+
/**
|
|
556
|
+
* The function checks if a binary tree is balanced by comparing the minimum height and the maximum height of the tree.
|
|
557
|
+
* @param {BinaryTreeNode<T> | null} [beginRoot] - The `beginRoot` parameter is the root node of a binary tree. It is
|
|
558
|
+
* of type `BinaryTreeNode<T> | null`, which means it can either be a `BinaryTreeNode` object or `null`.
|
|
559
|
+
* @returns The method is returning a boolean value.
|
|
560
|
+
*/
|
|
462
561
|
isBalanced(beginRoot?: BinaryTreeNode<T> | null): boolean {
|
|
463
562
|
return (this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot));
|
|
464
563
|
}
|
|
465
564
|
|
|
565
|
+
/**
|
|
566
|
+
* The function `getNodes` returns an array of binary tree nodes that match a given property value, with options for
|
|
567
|
+
* searching recursively or iteratively.
|
|
568
|
+
* @param {BinaryTreeNodeId | T} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
|
|
569
|
+
* generic type `T`. It represents the property of the binary tree node that you want to search for.
|
|
570
|
+
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
571
|
+
* specifies the property name to use when searching for nodes. If not provided, it defaults to 'id'.
|
|
572
|
+
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
573
|
+
* return only one node that matches the `nodeProperty` or `propertyName` criteria. If `onlyOne` is set to `true`, the
|
|
574
|
+
* function will stop traversing the tree and return the first matching node. If `
|
|
575
|
+
* @returns The function `getNodes` returns an array of `BinaryTreeNode<T> | null | undefined` objects.
|
|
576
|
+
*/
|
|
466
577
|
getNodes(nodeProperty: BinaryTreeNodeId | T, propertyName ?: BinaryTreeNodePropertyName, onlyOne ?: boolean) {
|
|
467
578
|
if (!this.root) return [] as null[];
|
|
468
579
|
propertyName = propertyName ?? 'id';
|
|
@@ -493,15 +604,40 @@ export class BinaryTree<T> {
|
|
|
493
604
|
return result;
|
|
494
605
|
}
|
|
495
606
|
|
|
607
|
+
/**
|
|
608
|
+
* The function checks if a binary tree node has a specific property or if any node in the tree has a specific
|
|
609
|
+
* property.
|
|
610
|
+
* @param {BinaryTreeNodeId | T} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
|
|
611
|
+
* generic type `T`. It represents the property of a binary tree node that you want to check.
|
|
612
|
+
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
613
|
+
* specifies the name of the property to check for in the nodes.
|
|
614
|
+
* @returns a boolean value.
|
|
615
|
+
*/
|
|
496
616
|
has(nodeProperty: BinaryTreeNodeId | T, propertyName ?: BinaryTreeNodePropertyName): boolean {
|
|
497
617
|
return this.getNodes(nodeProperty, propertyName).length > 0;
|
|
498
618
|
}
|
|
499
619
|
|
|
620
|
+
/**
|
|
621
|
+
* The function returns the first binary tree node that matches the given property name and value, or null if no match
|
|
622
|
+
* is found.
|
|
623
|
+
* @param {BinaryTreeNodeId | T} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
|
|
624
|
+
* generic type `T`. It represents the property of the binary tree node that you want to search for.
|
|
625
|
+
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
626
|
+
* specifies the property of the binary tree node to search for. If not provided, it defaults to `'id'`.
|
|
627
|
+
* @returns a BinaryTreeNode object or null.
|
|
628
|
+
*/
|
|
500
629
|
get(nodeProperty: BinaryTreeNodeId | T, propertyName ?: BinaryTreeNodePropertyName): BinaryTreeNode<T> | null {
|
|
501
630
|
propertyName = propertyName ?? 'id';
|
|
502
631
|
return this.getNodes(nodeProperty, propertyName, true)[0] ?? null;
|
|
503
632
|
}
|
|
504
633
|
|
|
634
|
+
/**
|
|
635
|
+
* The function getPathToRoot returns an array of BinaryTreeNode objects representing the path from a given node to the
|
|
636
|
+
* root of a binary tree.
|
|
637
|
+
* @param node - The `node` parameter is a BinaryTreeNode object.
|
|
638
|
+
* @returns The function `getPathToRoot` returns an array of `BinaryTreeNode<T>` objects, representing the path from
|
|
639
|
+
* the given `node` to the root of the binary tree.
|
|
640
|
+
*/
|
|
505
641
|
getPathToRoot(node: BinaryTreeNode<T>): BinaryTreeNode<T>[] {
|
|
506
642
|
const result: BinaryTreeNode<T>[] = [];
|
|
507
643
|
while (node.parent) {
|
|
@@ -512,14 +648,18 @@ export class BinaryTree<T> {
|
|
|
512
648
|
return result;
|
|
513
649
|
}
|
|
514
650
|
|
|
515
|
-
getRoot(): BinaryTreeNode<T> | null {
|
|
516
|
-
return this.root;
|
|
517
|
-
}
|
|
518
|
-
|
|
519
651
|
getLeftMost(): BinaryTreeNode<T> | null;
|
|
520
652
|
|
|
521
653
|
getLeftMost(node: BinaryTreeNode<T>): BinaryTreeNode<T>;
|
|
522
654
|
|
|
655
|
+
/**
|
|
656
|
+
* The `getLeftMost` function returns the leftmost node in a binary tree, either recursively or iteratively using tail
|
|
657
|
+
* recursion optimization.
|
|
658
|
+
* @param {BinaryTreeNode<T> | null} [node] - The `node` parameter is an optional parameter of type `BinaryTreeNode<T>
|
|
659
|
+
* | null`. It represents the starting node from which to find the leftmost node in a binary tree. If no node is
|
|
660
|
+
* provided, the function will use the root node of the binary tree.
|
|
661
|
+
* @returns The `getLeftMost` function returns the leftmost node in a binary tree.
|
|
662
|
+
*/
|
|
523
663
|
getLeftMost(node?: BinaryTreeNode<T> | null): BinaryTreeNode<T> | null {
|
|
524
664
|
node = node ?? this.root;
|
|
525
665
|
if (!node) return node;
|
|
@@ -547,6 +687,14 @@ export class BinaryTree<T> {
|
|
|
547
687
|
|
|
548
688
|
getRightMost(node: BinaryTreeNode<T>): BinaryTreeNode<T>;
|
|
549
689
|
|
|
690
|
+
/**
|
|
691
|
+
* The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using
|
|
692
|
+
* tail recursion optimization.
|
|
693
|
+
* @param {BinaryTreeNode<T> | null} [node] - The `node` parameter is an optional parameter of type `BinaryTreeNode<T>
|
|
694
|
+
* | null`. It represents the starting node from which to find the rightmost node in a binary tree. If no node is
|
|
695
|
+
* provided, the function will use the root node of the binary tree.
|
|
696
|
+
* @returns The `getRightMost` function returns the rightmost node in a binary tree.
|
|
697
|
+
*/
|
|
550
698
|
getRightMost(node?: BinaryTreeNode<T> | null): BinaryTreeNode<T> | null {
|
|
551
699
|
node = node ?? this.root;
|
|
552
700
|
if (!node) return node;
|
|
@@ -570,6 +718,14 @@ export class BinaryTree<T> {
|
|
|
570
718
|
}
|
|
571
719
|
|
|
572
720
|
// --- start additional methods ---
|
|
721
|
+
/**
|
|
722
|
+
* The `isBST` function checks if a binary tree is a binary search tree.
|
|
723
|
+
* @param {BinaryTreeNode<T> | null} [node] - The `node` parameter is an optional parameter of type `BinaryTreeNode<T>
|
|
724
|
+
* | null`. It represents the root node of the binary search tree (BST) that we want to check for validity. If no node
|
|
725
|
+
* is provided, the function will default to using the root node of the BST instance that
|
|
726
|
+
* @returns The `isBST` function returns a boolean value. It returns `true` if the binary tree is a valid binary search
|
|
727
|
+
* tree, and `false` otherwise.
|
|
728
|
+
*/
|
|
573
729
|
isBST(node?: BinaryTreeNode<T> | null): boolean {
|
|
574
730
|
node = node ?? this.root;
|
|
575
731
|
if (!node) return true;
|
|
@@ -599,6 +755,14 @@ export class BinaryTree<T> {
|
|
|
599
755
|
}
|
|
600
756
|
}
|
|
601
757
|
|
|
758
|
+
/**
|
|
759
|
+
* The function calculates the size and count of a subtree in a binary tree using either recursive or iterative
|
|
760
|
+
* traversal.
|
|
761
|
+
* @param {BinaryTreeNode<T> | null | undefined} subTreeRoot - The `subTreeRoot` parameter is the root node of a binary
|
|
762
|
+
* tree.
|
|
763
|
+
* @returns The function `getSubTreeSizeAndCount` returns an array `[number, number]`. The first element of the array
|
|
764
|
+
* represents the size of the subtree, and the second element represents the count of the nodes in the subtree.
|
|
765
|
+
*/
|
|
602
766
|
getSubTreeSizeAndCount(subTreeRoot: BinaryTreeNode<T> | null | undefined) {
|
|
603
767
|
const res: [number, number] = [0, 0];
|
|
604
768
|
if (!subTreeRoot) return res;
|
|
@@ -628,6 +792,16 @@ export class BinaryTree<T> {
|
|
|
628
792
|
}
|
|
629
793
|
}
|
|
630
794
|
|
|
795
|
+
/**
|
|
796
|
+
* The function `subTreeSum` calculates the sum of a specified property in a binary tree, either recursively or
|
|
797
|
+
* iteratively.
|
|
798
|
+
* @param subTreeRoot - The subTreeRoot parameter is the root node of the subtree for which you want to calculate the
|
|
799
|
+
* sum.
|
|
800
|
+
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
801
|
+
* specifies the property of the `BinaryTreeNode` object to use for calculating the sum. If `propertyName` is not
|
|
802
|
+
* provided, it defaults to `'val'`.
|
|
803
|
+
* @returns a number, which is the sum of the values of the nodes in the subtree rooted at `subTreeRoot`.
|
|
804
|
+
*/
|
|
631
805
|
subTreeSum(subTreeRoot: BinaryTreeNode<T>, propertyName ?: BinaryTreeNodePropertyName): number {
|
|
632
806
|
propertyName = propertyName ?? 'val';
|
|
633
807
|
if (!subTreeRoot) return 0;
|
|
@@ -675,6 +849,15 @@ export class BinaryTree<T> {
|
|
|
675
849
|
return sum;
|
|
676
850
|
}
|
|
677
851
|
|
|
852
|
+
/**
|
|
853
|
+
* The function `subTreeAdd` adds a specified delta value to a property of each node in a binary tree.
|
|
854
|
+
* @param subTreeRoot - The `subTreeRoot` parameter is the root node of the subtree where the values will be modified.
|
|
855
|
+
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
|
|
856
|
+
* each node in the subtree should be increased or decreased.
|
|
857
|
+
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
858
|
+
* specifies the property of the `BinaryTreeNode` that should be modified. It defaults to `'id'` if not provided.
|
|
859
|
+
* @returns a boolean value, which is `true`.
|
|
860
|
+
*/
|
|
678
861
|
subTreeAdd(subTreeRoot: BinaryTreeNode<T>, delta: number, propertyName ?: BinaryTreeNodePropertyName): boolean {
|
|
679
862
|
propertyName = propertyName ?? 'id';
|
|
680
863
|
if (!subTreeRoot) return false;
|
|
@@ -726,6 +909,15 @@ export class BinaryTree<T> {
|
|
|
726
909
|
|
|
727
910
|
BFS(nodeOrPropertyName: 'count'): number[];
|
|
728
911
|
|
|
912
|
+
/**
|
|
913
|
+
* The BFS function performs a breadth-first search on a binary tree and returns the results based on a specified node
|
|
914
|
+
* or property name.
|
|
915
|
+
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
|
|
916
|
+
* represents either a node or a property name. If a node is provided, the breadth-first search algorithm will be
|
|
917
|
+
* performed starting from that node. If a property name is provided, the breadth-first search algorithm will be
|
|
918
|
+
* performed starting from the root node
|
|
919
|
+
* @returns an object of type `ResultsByProperty<T>`.
|
|
920
|
+
*/
|
|
729
921
|
BFS(nodeOrPropertyName ?: NodeOrPropertyName): ResultsByProperty<T> {
|
|
730
922
|
nodeOrPropertyName = nodeOrPropertyName ?? 'id';
|
|
731
923
|
this._resetResults();
|
|
@@ -753,6 +945,18 @@ export class BinaryTree<T> {
|
|
|
753
945
|
|
|
754
946
|
DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'count'): number[];
|
|
755
947
|
|
|
948
|
+
/**
|
|
949
|
+
* The DFS function performs a depth-first search traversal on a binary tree and returns the results based on the
|
|
950
|
+
* specified pattern and node or property name.
|
|
951
|
+
* @param {'in' | 'pre' | 'post'} [pattern] - The "pattern" parameter is used to specify the order in which the nodes
|
|
952
|
+
* of a binary tree are traversed during the Depth-First Search (DFS) algorithm. It can take one of three values: 'in',
|
|
953
|
+
* 'pre', or 'post'.
|
|
954
|
+
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is a string that represents
|
|
955
|
+
* either the name of a property in the `BinaryTreeNode` object or the value of the `id` property in the
|
|
956
|
+
* `BinaryTreeNode` object. This parameter is used to accumulate the results based on the specified property name. If
|
|
957
|
+
* no value
|
|
958
|
+
* @returns an object of type `ResultsByProperty<T>`.
|
|
959
|
+
*/
|
|
756
960
|
DFS(pattern ?: 'in' | 'pre' | 'post', nodeOrPropertyName ?: NodeOrPropertyName): ResultsByProperty<T> {
|
|
757
961
|
pattern = pattern ?? 'in';
|
|
758
962
|
nodeOrPropertyName = nodeOrPropertyName ?? 'id';
|
|
@@ -850,6 +1054,18 @@ export class BinaryTree<T> {
|
|
|
850
1054
|
|
|
851
1055
|
levelIterative(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'count'): number[];
|
|
852
1056
|
|
|
1057
|
+
/**
|
|
1058
|
+
* The `levelIterative` function performs a level-order traversal on a binary tree and returns the values of the nodes
|
|
1059
|
+
* in an array, based on a specified property name.
|
|
1060
|
+
* @param {BinaryTreeNode<T> | null} node - The `node` parameter is a BinaryTreeNode object representing the starting
|
|
1061
|
+
* node for the level order traversal. It can be null if no specific node is provided, in which case the root node of
|
|
1062
|
+
* the tree is used as the starting node.
|
|
1063
|
+
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
|
|
1064
|
+
* can be either a `BinaryTreeNode` property name or the string `'id'`. If a property name is provided, the function
|
|
1065
|
+
* will accumulate results based on that property. If no property name is provided, the function will default to
|
|
1066
|
+
* accumulating results
|
|
1067
|
+
* @returns The function `levelIterative` returns an object of type `ResultsByProperty<T>`.
|
|
1068
|
+
*/
|
|
853
1069
|
levelIterative(node: BinaryTreeNode<T> | null, nodeOrPropertyName ?: NodeOrPropertyName): ResultsByProperty<T> {
|
|
854
1070
|
nodeOrPropertyName = nodeOrPropertyName || 'id';
|
|
855
1071
|
node = node || this.root;
|
|
@@ -884,6 +1100,15 @@ export class BinaryTree<T> {
|
|
|
884
1100
|
|
|
885
1101
|
listLevels(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'count'): number[][];
|
|
886
1102
|
|
|
1103
|
+
/**
|
|
1104
|
+
* The `listLevels` function collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
1105
|
+
* @param {BinaryTreeNode<T> | null} node - The `node` parameter is a BinaryTreeNode object or null. It represents the
|
|
1106
|
+
* root node of a binary tree. If it is null, the function will use the root node of the current binary tree instance.
|
|
1107
|
+
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
|
|
1108
|
+
* specifies the property of the `BinaryTreeNode` object to collect at each level. It can be one of the following
|
|
1109
|
+
* values:
|
|
1110
|
+
* @returns The function `listLevels` returns a 2D array of `ResultByProperty<T>` objects.
|
|
1111
|
+
*/
|
|
887
1112
|
listLevels(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: NodeOrPropertyName): ResultByProperty<T>[][] {
|
|
888
1113
|
nodeOrPropertyName = nodeOrPropertyName || 'id';
|
|
889
1114
|
node = node || this.root;
|
|
@@ -937,6 +1162,11 @@ export class BinaryTree<T> {
|
|
|
937
1162
|
return levelsNodes;
|
|
938
1163
|
}
|
|
939
1164
|
|
|
1165
|
+
/**
|
|
1166
|
+
* The function returns the predecessor of a given node in a binary tree.
|
|
1167
|
+
* @param node - The parameter `node` is a BinaryTreeNode object, representing a node in a binary tree.
|
|
1168
|
+
* @returns the predecessor of the given node in a binary tree.
|
|
1169
|
+
*/
|
|
940
1170
|
getPredecessor(node: BinaryTreeNode<T>): BinaryTreeNode<T> {
|
|
941
1171
|
if (node.left) {
|
|
942
1172
|
let predecessor: BinaryTreeNode<T> | null | undefined = node.left;
|
|
@@ -962,10 +1192,16 @@ export class BinaryTree<T> {
|
|
|
962
1192
|
morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'count'): number[];
|
|
963
1193
|
|
|
964
1194
|
/**
|
|
1195
|
+
* The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris
|
|
1196
|
+
* traversal algorithm and returns the results based on the specified property name.
|
|
965
1197
|
* The time complexity of Morris traversal is O(n), it's may slower than others
|
|
966
1198
|
* The space complexity Morris traversal is O(1) because no using stack
|
|
967
|
-
* @param pattern
|
|
968
|
-
*
|
|
1199
|
+
* @param {'in' | 'pre' | 'post'} [pattern] - The `pattern` parameter is an optional parameter that determines the
|
|
1200
|
+
* traversal pattern of the binary tree. It can have one of three values:
|
|
1201
|
+
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is used to specify the
|
|
1202
|
+
* property of the nodes that you want to retrieve in the results. It can be either the node itself or the name of the
|
|
1203
|
+
* property. If not provided, it defaults to `'id'`.
|
|
1204
|
+
* @returns The function `morris` returns an object of type `ResultsByProperty<T>`.
|
|
969
1205
|
*/
|
|
970
1206
|
morris(pattern?: 'in' | 'pre' | 'post', nodeOrPropertyName?: NodeOrPropertyName): ResultsByProperty<T> {
|
|
971
1207
|
if (this.root === null) return [];
|
|
@@ -1053,6 +1289,9 @@ export class BinaryTree<T> {
|
|
|
1053
1289
|
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
1054
1290
|
}
|
|
1055
1291
|
|
|
1292
|
+
/**
|
|
1293
|
+
* The function resets the values of several arrays used for tracking visited nodes and their properties.
|
|
1294
|
+
*/
|
|
1056
1295
|
protected _resetResults() {
|
|
1057
1296
|
this._visitedId = [];
|
|
1058
1297
|
this._visitedVal = [];
|
|
@@ -1061,6 +1300,22 @@ export class BinaryTree<T> {
|
|
|
1061
1300
|
this._visitedLeftSum = [];
|
|
1062
1301
|
}
|
|
1063
1302
|
|
|
1303
|
+
/**
|
|
1304
|
+
* The function checks if a given property of a binary tree node matches a specified value, and if so, adds the node to
|
|
1305
|
+
* a result array.
|
|
1306
|
+
* @param cur - The current binary tree node that is being checked.
|
|
1307
|
+
* @param {(BinaryTreeNode<T> | null | undefined)[]} result - An array that stores the matching nodes found during the
|
|
1308
|
+
* traversal.
|
|
1309
|
+
* @param {BinaryTreeNodeId | T} nodeProperty - The `nodeProperty` parameter is the value that we are searching for in
|
|
1310
|
+
* the binary tree nodes. It can be either the `id`, `count`, or `val` property of the node.
|
|
1311
|
+
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
1312
|
+
* specifies the property of the `BinaryTreeNode` object that you want to compare with the `nodeProperty` value. It can
|
|
1313
|
+
* be one of the following values: 'id', 'count', or 'val'. If no `propertyName` is provided,
|
|
1314
|
+
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
1315
|
+
* stop after finding the first matching node or continue searching for all matching nodes. If `onlyOne` is set to
|
|
1316
|
+
* `true`, the function will stop after finding the first matching node and return `true`. If `onlyOne
|
|
1317
|
+
* @returns a boolean value indicating whether or not a node was pushed into the result array.
|
|
1318
|
+
*/
|
|
1064
1319
|
protected _pushByPropertyNameStopOrNot(cur: BinaryTreeNode<T>, result: (BinaryTreeNode<T> | null | undefined)[], nodeProperty: BinaryTreeNodeId | T, propertyName ?: BinaryTreeNodePropertyName, onlyOne ?: boolean) {
|
|
1065
1320
|
switch (propertyName) {
|
|
1066
1321
|
case 'id':
|
|
@@ -1090,6 +1345,14 @@ export class BinaryTree<T> {
|
|
|
1090
1345
|
}
|
|
1091
1346
|
}
|
|
1092
1347
|
|
|
1348
|
+
/**
|
|
1349
|
+
* The function `_accumulatedByPropertyName` pushes a property value of a binary tree node into an array based on the
|
|
1350
|
+
* provided property name or a default property name.
|
|
1351
|
+
* @param node - The `node` parameter is of type `BinaryTreeNode<T>`, which represents a node in a binary tree.
|
|
1352
|
+
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
|
|
1353
|
+
* can be either a string representing a property name or a reference to a node object. If it is a string, it specifies
|
|
1354
|
+
* the property name of the node that should be accumulated. If it is a node object, it specifies the node itself
|
|
1355
|
+
*/
|
|
1093
1356
|
protected _accumulatedByPropertyName(node: BinaryTreeNode<T>, nodeOrPropertyName ?: NodeOrPropertyName) {
|
|
1094
1357
|
nodeOrPropertyName = nodeOrPropertyName ?? 'id';
|
|
1095
1358
|
|
|
@@ -1112,6 +1375,13 @@ export class BinaryTree<T> {
|
|
|
1112
1375
|
}
|
|
1113
1376
|
}
|
|
1114
1377
|
|
|
1378
|
+
/**
|
|
1379
|
+
* The function `_getResultByPropertyName` returns different results based on the provided property name or defaulting
|
|
1380
|
+
* to 'id'.
|
|
1381
|
+
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
|
|
1382
|
+
* can accept a value of type `NodeOrPropertyName`.
|
|
1383
|
+
* @returns The method returns an object of type `ResultsByProperty<T>`.
|
|
1384
|
+
*/
|
|
1115
1385
|
protected _getResultByPropertyName(nodeOrPropertyName ?: NodeOrPropertyName): ResultsByProperty<T> {
|
|
1116
1386
|
nodeOrPropertyName = nodeOrPropertyName ?? 'id';
|
|
1117
1387
|
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
1
5
|
import type {BinaryTreeNodeId, BinaryTreeNodePropertyName, BSTComparator, BSTDeletedResult} from '../types';
|
|
2
6
|
import {BinaryTree, BinaryTreeNode, FamilyPosition, LoopType,} from './binary-tree';
|
|
3
7
|
|
|
@@ -27,6 +31,18 @@ export class BST<T> extends BinaryTree<T> {
|
|
|
27
31
|
return val !== null ? new BSTNode<T>(id, val, count) : null;
|
|
28
32
|
}
|
|
29
33
|
|
|
34
|
+
/**
|
|
35
|
+
* The `put` function inserts a new node into a binary search tree, updating the count and value of an existing node if
|
|
36
|
+
* the ID matches, and returns the inserted node.
|
|
37
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node. It is used to
|
|
38
|
+
* determine the position of the node in the binary search tree.
|
|
39
|
+
* @param {T | null} val - The `val` parameter represents the value to be stored in the binary search tree node. It can
|
|
40
|
+
* be of type `T` (the generic type) or `null`.
|
|
41
|
+
* @param {number} [count=1] - The `count` parameter represents the number of times the value should be inserted into
|
|
42
|
+
* the binary search tree. By default, it is set to 1, meaning that if no count is specified, the value will be
|
|
43
|
+
* inserted once.
|
|
44
|
+
* @returns The method `put` returns a `BSTNode<T>` object or `null`.
|
|
45
|
+
*/
|
|
30
46
|
override put(id: BinaryTreeNodeId, val: T | null, count: number = 1): BSTNode<T> | null {
|
|
31
47
|
let inserted: BSTNode<T> | null = null;
|
|
32
48
|
const newNode = this.createNode(id, val, count);
|
|
@@ -105,7 +121,6 @@ export class BST<T> extends BinaryTree<T> {
|
|
|
105
121
|
|
|
106
122
|
override remove(id: BinaryTreeNodeId, ignoreCount?: boolean): BSTDeletedResult<T>[] {
|
|
107
123
|
const bstDeletedResult: BSTDeletedResult<T>[] = [];
|
|
108
|
-
|
|
109
124
|
if (!this.root) return bstDeletedResult;
|
|
110
125
|
|
|
111
126
|
const curr: BSTNode<T> | null = this.get(id);
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
// 操作
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
// 查看尾部 Last_Element peekLast [length - 1] back [-1] $array[-1] end last
|
|
7
|
-
// 查看头部 First_Element peekFirst [0] front [0] $array[0] reset first
|
|
1
|
+
// 操作 常见名称 Ada Java JavaScript C++ Python Perl PHP Ruby // 尾部插入 inject, snoc Append offerLast push push_back append push
|
|
2
|
+
array_push push // 头部插入 push, cons Prepend offerFirst unshift push_front appendleft unshift array_unshift unshift //
|
|
3
|
+
尾部删除 eject Delete_Last pollLast pop pop_back pop pop array_pop pop // 头部删除 pop Delete_First pollFirst shift pop_front
|
|
4
|
+
popleft shift array_shift shift // 查看尾部 Last_Element peekLast [length - 1] back [-1] $array[-1] end
|
|
5
|
+
last // 查看头部 First_Element peekFirst [0] front [0] $array[0] reset first
|