data-structure-typed 1.12.10 → 1.12.21

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.
Files changed (158) hide show
  1. package/README.md +7 -0
  2. package/dist/data-structures/binary-tree/avl-tree.d.ts +14 -5
  3. package/dist/data-structures/binary-tree/avl-tree.js +15 -6
  4. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +11 -2
  5. package/dist/data-structures/binary-tree/binary-indexed-tree.js +11 -2
  6. package/dist/data-structures/binary-tree/binary-tree.d.ts +26 -17
  7. package/dist/data-structures/binary-tree/binary-tree.js +72 -62
  8. package/dist/data-structures/binary-tree/bst.d.ts +92 -5
  9. package/dist/data-structures/binary-tree/bst.js +89 -5
  10. package/dist/data-structures/binary-tree/segment-tree.d.ts +41 -2
  11. package/dist/data-structures/binary-tree/segment-tree.js +41 -2
  12. package/dist/data-structures/binary-tree/tree-multiset.d.ts +34 -3
  13. package/dist/data-structures/binary-tree/tree-multiset.js +35 -4
  14. package/dist/data-structures/graph/abstract-graph.d.ts +5 -0
  15. package/dist/data-structures/graph/abstract-graph.js +12 -4
  16. package/dist/data-structures/graph/directed-graph.d.ts +18 -4
  17. package/dist/data-structures/graph/directed-graph.js +24 -37
  18. package/dist/data-structures/graph/undirected-graph.d.ts +13 -0
  19. package/dist/data-structures/graph/undirected-graph.js +18 -2
  20. package/dist/data-structures/hash/coordinate-map.d.ts +5 -2
  21. package/dist/data-structures/hash/coordinate-map.js +5 -2
  22. package/dist/data-structures/hash/coordinate-set.d.ts +5 -2
  23. package/dist/data-structures/hash/coordinate-set.js +5 -2
  24. package/dist/data-structures/heap/heap.d.ts +9 -6
  25. package/dist/data-structures/heap/heap.js +8 -8
  26. package/dist/data-structures/heap/max-heap.d.ts +5 -2
  27. package/dist/data-structures/heap/max-heap.js +5 -2
  28. package/dist/data-structures/heap/min-heap.d.ts +5 -2
  29. package/dist/data-structures/heap/min-heap.js +5 -2
  30. package/dist/data-structures/index.d.ts +1 -0
  31. package/dist/data-structures/index.js +1 -0
  32. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
  33. package/dist/data-structures/linked-list/doubly-linked-list.js +4 -4
  34. package/dist/data-structures/linked-list/singly-linked-list.d.ts +5 -2
  35. package/dist/data-structures/linked-list/singly-linked-list.js +5 -2
  36. package/dist/data-structures/matrix/matrix.d.ts +5 -2
  37. package/dist/data-structures/matrix/matrix.js +5 -2
  38. package/dist/data-structures/matrix/matrix2d.d.ts +5 -2
  39. package/dist/data-structures/matrix/matrix2d.js +5 -2
  40. package/dist/data-structures/matrix/navigator.d.ts +5 -2
  41. package/dist/data-structures/matrix/vector2d.d.ts +5 -2
  42. package/dist/data-structures/matrix/vector2d.js +5 -2
  43. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +5 -2
  44. package/dist/data-structures/priority-queue/max-priority-queue.js +5 -2
  45. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +5 -2
  46. package/dist/data-structures/priority-queue/min-priority-queue.js +5 -2
  47. package/dist/data-structures/priority-queue/priority-queue.d.ts +7 -4
  48. package/dist/data-structures/priority-queue/priority-queue.js +2 -2
  49. package/dist/data-structures/queue/deque.d.ts +12 -9
  50. package/dist/data-structures/queue/deque.js +12 -9
  51. package/dist/data-structures/queue/queue.d.ts +4 -4
  52. package/dist/data-structures/queue/queue.js +4 -4
  53. package/dist/data-structures/stack/stack.d.ts +1 -1
  54. package/dist/data-structures/stack/stack.js +1 -1
  55. package/dist/data-structures/trie/trie.d.ts +6 -3
  56. package/dist/data-structures/trie/trie.js +7 -4
  57. package/dist/utils/index.d.ts +1 -0
  58. package/dist/utils/index.js +1 -0
  59. package/dist/utils/types/utils.d.ts +8 -10
  60. package/dist/utils/types/utils.js +0 -1
  61. package/dist/utils/utils.d.ts +18 -8
  62. package/dist/utils/utils.js +93 -47
  63. package/package.json +3 -3
  64. package/src/assets/logo.png +0 -0
  65. package/src/data-structures/binary-tree/avl-tree.ts +15 -6
  66. package/src/data-structures/binary-tree/binary-indexed-tree.ts +11 -2
  67. package/src/data-structures/binary-tree/binary-tree.ts +70 -58
  68. package/src/data-structures/binary-tree/bst.ts +94 -7
  69. package/src/data-structures/binary-tree/segment-tree.ts +41 -2
  70. package/src/data-structures/binary-tree/tree-multiset.ts +35 -4
  71. package/src/data-structures/graph/abstract-graph.ts +12 -4
  72. package/src/data-structures/graph/directed-graph.ts +26 -39
  73. package/src/data-structures/graph/undirected-graph.ts +18 -2
  74. package/src/data-structures/hash/coordinate-map.ts +5 -2
  75. package/src/data-structures/hash/coordinate-set.ts +5 -2
  76. package/src/data-structures/heap/heap.ts +13 -10
  77. package/src/data-structures/heap/max-heap.ts +5 -2
  78. package/src/data-structures/heap/min-heap.ts +5 -2
  79. package/src/data-structures/index.ts +2 -0
  80. package/src/data-structures/linked-list/doubly-linked-list.ts +9 -6
  81. package/src/data-structures/linked-list/singly-linked-list.ts +5 -2
  82. package/src/data-structures/matrix/matrix.ts +5 -2
  83. package/src/data-structures/matrix/matrix2d.ts +5 -2
  84. package/src/data-structures/matrix/navigator.ts +5 -2
  85. package/src/data-structures/matrix/vector2d.ts +5 -2
  86. package/src/data-structures/priority-queue/max-priority-queue.ts +5 -2
  87. package/src/data-structures/priority-queue/min-priority-queue.ts +5 -2
  88. package/src/data-structures/priority-queue/priority-queue.ts +7 -4
  89. package/src/data-structures/queue/deque.ts +12 -9
  90. package/src/data-structures/queue/queue.ts +4 -4
  91. package/src/data-structures/stack/stack.ts +1 -1
  92. package/src/data-structures/trie/trie.ts +7 -4
  93. package/src/utils/index.ts +2 -1
  94. package/src/utils/types/utils.ts +10 -12
  95. package/src/utils/utils.ts +57 -11
  96. package/tests/unit/data-structures/binary-tree/bst.test.ts +1 -1
  97. package/tests/unit/data-structures/graph/directed-graph.test.ts +1 -0
  98. package/dist/utils/trampoline.d.ts +0 -14
  99. package/dist/utils/trampoline.js +0 -130
  100. package/docs/.nojekyll +0 -1
  101. package/docs/assets/highlight.css +0 -85
  102. package/docs/assets/main.js +0 -58
  103. package/docs/assets/search.js +0 -1
  104. package/docs/assets/style.css +0 -1367
  105. package/docs/classes/AVLTree.html +0 -2046
  106. package/docs/classes/AVLTreeNode.html +0 -423
  107. package/docs/classes/AaTree.html +0 -117
  108. package/docs/classes/AbstractEdge.html +0 -198
  109. package/docs/classes/AbstractGraph.html +0 -891
  110. package/docs/classes/AbstractVertex.html +0 -164
  111. package/docs/classes/ArrayDeque.html +0 -384
  112. package/docs/classes/BST.html +0 -1893
  113. package/docs/classes/BSTNode.html +0 -425
  114. package/docs/classes/BTree.html +0 -117
  115. package/docs/classes/BinaryIndexedTree.html +0 -244
  116. package/docs/classes/BinaryTree.html +0 -1754
  117. package/docs/classes/BinaryTreeNode.html +0 -396
  118. package/docs/classes/Character.html +0 -165
  119. package/docs/classes/CoordinateMap.html +0 -394
  120. package/docs/classes/CoordinateSet.html +0 -355
  121. package/docs/classes/Deque.html +0 -617
  122. package/docs/classes/DirectedEdge.html +0 -247
  123. package/docs/classes/DirectedGraph.html +0 -1207
  124. package/docs/classes/DirectedVertex.html +0 -154
  125. package/docs/classes/DoublyLinkedList.html +0 -619
  126. package/docs/classes/DoublyLinkedListNode.html +0 -160
  127. package/docs/classes/Heap.html +0 -315
  128. package/docs/classes/Matrix2D.html +0 -447
  129. package/docs/classes/MatrixNTI2D.html +0 -181
  130. package/docs/classes/MaxHeap.html +0 -325
  131. package/docs/classes/MaxPriorityQueue.html +0 -668
  132. package/docs/classes/MinHeap.html +0 -326
  133. package/docs/classes/MinPriorityQueue.html +0 -668
  134. package/docs/classes/Navigator.html +0 -285
  135. package/docs/classes/ObjectDeque.html +0 -289
  136. package/docs/classes/PriorityQueue.html +0 -643
  137. package/docs/classes/Queue.html +0 -337
  138. package/docs/classes/RBTree.html +0 -117
  139. package/docs/classes/SegmentTree.html +0 -234
  140. package/docs/classes/SegmentTreeNode.html +0 -302
  141. package/docs/classes/SinglyLinkedList.html +0 -1035
  142. package/docs/classes/SinglyLinkedListNode.html +0 -304
  143. package/docs/classes/SplayTree.html +0 -117
  144. package/docs/classes/Stack.html +0 -313
  145. package/docs/classes/TreeMultiSet.html +0 -1897
  146. package/docs/classes/Trie.html +0 -317
  147. package/docs/classes/TrieNode.html +0 -221
  148. package/docs/classes/TwoThreeTree.html +0 -117
  149. package/docs/classes/UndirectedEdge.html +0 -220
  150. package/docs/classes/UndirectedGraph.html +0 -1006
  151. package/docs/classes/UndirectedVertex.html +0 -154
  152. package/docs/classes/Vector2D.html +0 -746
  153. package/docs/enums/CP.html +0 -126
  154. package/docs/enums/FamilyPosition.html +0 -126
  155. package/docs/enums/LoopType.html +0 -119
  156. package/docs/index.html +0 -288
  157. package/docs/modules.html +0 -146
  158. package/src/utils/trampoline.ts +0 -51
@@ -1,9 +1,12 @@
1
1
  /**
2
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
- * @license MIT
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
4
7
  */
5
8
 
6
- import {trampoline} from '../../utils/trampoline';
9
+ import {trampoline} from '../../utils';
7
10
  import type {
8
11
  BinaryTreeDeleted,
9
12
  BinaryTreeNodeId,
@@ -14,11 +17,19 @@ import type {
14
17
  ResultsByProperty
15
18
  } from '../types';
16
19
 
20
+ /* This enumeration defines the position of a node within a family tree composed of three associated nodes, where 'root' represents the root node of the family tree, 'left' represents the left child node, and 'right' represents the right child node. */
17
21
  export enum FamilyPosition {root, left, right}
18
22
 
23
+ /**
24
+ * Enum representing different loop types.
25
+ *
26
+ * - `iterative`: Indicates the iterative loop type (with loops that use iterations).
27
+ * - `recursive`: Indicates the recursive loop type (with loops that call themselves).
28
+ */
19
29
  export enum LoopType { iterative = 1, recursive = 2}
20
30
 
21
31
  export class BinaryTreeNode<T> {
32
+
22
33
  constructor(id: BinaryTreeNodeId, val: T, count?: number) {
23
34
  this._id = id;
24
35
  this._val = val;
@@ -239,58 +250,17 @@ export class BinaryTree<T> {
239
250
  }
240
251
 
241
252
  /**
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> ) {
250
- if (parent) {
251
- if (parent.left === undefined) {
252
- if (newNode) {
253
- newNode.parent = parent;
254
- newNode.familyPosition = FamilyPosition.left;
255
- }
256
- parent.left = newNode;
257
- if (newNode !== null) {
258
- this.size++;
259
- this.count += newNode?.count ?? 0;
260
- }
261
-
262
- return parent.left;
263
- } else if (parent.right === undefined) {
264
- if (newNode) {
265
- newNode.parent = parent;
266
- newNode.familyPosition = FamilyPosition.right;
267
- }
268
- parent.right = newNode;
269
- if (newNode !== null) {
270
- this.size++;
271
- this.count += newNode?.count ?? 0;
272
- }
273
- return parent.right;
274
- } else {
275
- return;
276
- }
277
- } else {
278
- return;
279
- }
280
- }
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
253
+ * The `add` function inserts a new node with a given ID and value into a binary tree, updating the count if the node
284
254
  * already exists.
285
255
  * @param {BinaryTreeNodeId} id - The id parameter is the identifier of the binary tree node. It is used to uniquely
286
256
  * identify each node in the binary tree.
287
257
  * @param {T} val - The value to be inserted into the binary tree.
288
258
  * @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the
289
259
  * 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
260
+ * @returns The function `add` returns a `BinaryTreeNode<T>` object if a new node is inserted, or `null` if no new node
291
261
  * is inserted, or `undefined` if the insertion fails.
292
262
  */
293
- put(id: BinaryTreeNodeId, val: T, count?: number): BinaryTreeNode<T> | null | undefined {
263
+ add(id: BinaryTreeNodeId, val: T, count?: number): BinaryTreeNode<T> | null | undefined {
294
264
  count = count ?? 1;
295
265
 
296
266
  const _bfs = (root: BinaryTreeNode<T>, newNode: BinaryTreeNode<T> | null): BinaryTreeNode<T> | undefined | null => {
@@ -298,7 +268,7 @@ export class BinaryTree<T> {
298
268
  while (queue.length > 0) {
299
269
  const cur = queue.shift();
300
270
  if (cur) {
301
- const inserted = this.putTo(newNode, cur);
271
+ const inserted = this.addTo(newNode, cur);
302
272
  if (inserted !== undefined) return inserted;
303
273
  if (cur.left) queue.push(cur.left);
304
274
  if (cur.right) queue.push(cur.right);
@@ -333,13 +303,54 @@ export class BinaryTree<T> {
333
303
  }
334
304
 
335
305
  /**
336
- * The `insertMany` function inserts multiple items into a binary tree and returns an array of the inserted nodes or
306
+ * The function inserts a new node into a binary tree as the left or right child of a given parent node.
307
+ * @param {BinaryTreeNode<T> | null} newNode - The `newNode` parameter is an instance of the `BinaryTreeNode` class or
308
+ * `null`. It represents the node that needs to be inserted into the binary tree.
309
+ * @param parent - The `parent` parameter is a BinaryTreeNode object representing the parent node to which the new node
310
+ * will be inserted as a child.
311
+ * @returns The method returns the newly inserted node, either as the left child or the right child of the parent node.
312
+ */
313
+ addTo(newNode: BinaryTreeNode<T> | null, parent: BinaryTreeNode<T>) {
314
+ if (parent) {
315
+ if (parent.left === undefined) {
316
+ if (newNode) {
317
+ newNode.parent = parent;
318
+ newNode.familyPosition = FamilyPosition.left;
319
+ }
320
+ parent.left = newNode;
321
+ if (newNode !== null) {
322
+ this.size++;
323
+ this.count += newNode?.count ?? 0;
324
+ }
325
+
326
+ return parent.left;
327
+ } else if (parent.right === undefined) {
328
+ if (newNode) {
329
+ newNode.parent = parent;
330
+ newNode.familyPosition = FamilyPosition.right;
331
+ }
332
+ parent.right = newNode;
333
+ if (newNode !== null) {
334
+ this.size++;
335
+ this.count += newNode?.count ?? 0;
336
+ }
337
+ return parent.right;
338
+ } else {
339
+ return;
340
+ }
341
+ } else {
342
+ return;
343
+ }
344
+ }
345
+
346
+ /**
347
+ * The `addMany` function inserts multiple items into a binary tree and returns an array of the inserted nodes or
337
348
  * null/undefined values.
338
349
  * @param {T[] | BinaryTreeNode<T>[]} data - The `data` parameter can be either an array of elements of type `T` or an
339
350
  * array of `BinaryTreeNode<T>` objects.
340
- * @returns The function `insertMany` returns an array of `BinaryTreeNode<T>`, `null`, or `undefined` values.
351
+ * @returns The function `addMany` returns an array of `BinaryTreeNode<T>`, `null`, or `undefined` values.
341
352
  */
342
- insertMany(data: T[] | BinaryTreeNode<T>[]): (BinaryTreeNode<T> | null | undefined)[] {
353
+ addMany(data: T[] | BinaryTreeNode<T>[]): (BinaryTreeNode<T> | null | undefined)[] {
343
354
  const inserted: (BinaryTreeNode<T> | null | undefined)[] = [];
344
355
  const map: Map<T | BinaryTreeNode<T>, number> = new Map();
345
356
 
@@ -351,28 +362,28 @@ export class BinaryTree<T> {
351
362
  const count = this._isDuplicatedVal ? 1 : map.get(item);
352
363
 
353
364
  if (item instanceof BinaryTreeNode) {
354
- inserted.push(this.put(item.id, item.val, item.count));
365
+ inserted.push(this.add(item.id, item.val, item.count));
355
366
  } else if (typeof item === 'number' && !this._autoIncrementId) {
356
367
  if (!this._isDuplicatedVal) {
357
368
  if (map.get(item) !== undefined) {
358
- inserted.push(this.put(item, item, count));
369
+ inserted.push(this.add(item, item, count));
359
370
  map.delete(item);
360
371
  }
361
372
  } else {
362
- inserted.push(this.put(item, item, 1));
373
+ inserted.push(this.add(item, item, 1));
363
374
  }
364
375
  } else {
365
376
  if (item !== null) {
366
377
  if (!this._isDuplicatedVal) {
367
378
  if (map.get(item) !== undefined) {
368
- inserted.push(this.put(++this._maxId, item, count));
379
+ inserted.push(this.add(++this._maxId, item, count));
369
380
  map.delete(item);
370
381
  }
371
382
  } else {
372
- inserted.push(this.put(++this._maxId, item, 1));
383
+ inserted.push(this.add(++this._maxId, item, 1));
373
384
  }
374
385
  } else {
375
- inserted.push(this.put(Number.MAX_SAFE_INTEGER, item, 0));
386
+ inserted.push(this.add(Number.MAX_SAFE_INTEGER, item, 0));
376
387
  }
377
388
  }
378
389
  }
@@ -388,7 +399,7 @@ export class BinaryTree<T> {
388
399
  */
389
400
  fill(data: T[] | BinaryTreeNode<T>[]): boolean {
390
401
  this.clear();
391
- return data.length === this.insertMany(data).length;
402
+ return data.length === this.addMany(data).length;
392
403
  }
393
404
 
394
405
  /**
@@ -718,6 +729,7 @@ export class BinaryTree<T> {
718
729
  }
719
730
 
720
731
  // --- start additional methods ---
732
+
721
733
  /**
722
734
  * The `isBST` function checks if a binary tree is a binary search tree.
723
735
  * @param {BinaryTreeNode<T> | null} [node] - The `node` parameter is an optional parameter of type `BinaryTreeNode<T>
@@ -1,6 +1,9 @@
1
1
  /**
2
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
- * @license MIT
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
4
7
  */
5
8
  import type {BinaryTreeNodeId, BinaryTreeNodePropertyName, BSTComparator, BSTDeletedResult} from '../types';
6
9
  import {BinaryTree, BinaryTreeNode, FamilyPosition, LoopType,} from './binary-tree';
@@ -14,6 +17,10 @@ export class BSTNode<T> extends BinaryTreeNode<T> {
14
17
  }
15
18
 
16
19
  export class BST<T> extends BinaryTree<T> {
20
+ /**
21
+ * The constructor function accepts an optional options object and sets the comparator property if provided.
22
+ * @param [options] - An optional object that can contain the following properties:
23
+ */
17
24
  constructor(options?: {
18
25
  comparator?: BSTComparator,
19
26
  loopType?: LoopType
@@ -32,7 +39,7 @@ export class BST<T> extends BinaryTree<T> {
32
39
  }
33
40
 
34
41
  /**
35
- * The `put` function inserts a new node into a binary search tree, updating the count and value of an existing node if
42
+ * The `add` function inserts a new node into a binary search tree, updating the count and value of an existing node if
36
43
  * the ID matches, and returns the inserted node.
37
44
  * @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node. It is used to
38
45
  * determine the position of the node in the binary search tree.
@@ -41,9 +48,9 @@ export class BST<T> extends BinaryTree<T> {
41
48
  * @param {number} [count=1] - The `count` parameter represents the number of times the value should be inserted into
42
49
  * the binary search tree. By default, it is set to 1, meaning that if no count is specified, the value will be
43
50
  * inserted once.
44
- * @returns The method `put` returns a `BSTNode<T>` object or `null`.
51
+ * @returns The method `add` returns a `BSTNode<T>` object or `null`.
45
52
  */
46
- override put(id: BinaryTreeNodeId, val: T | null, count: number = 1): BSTNode<T> | null {
53
+ override add(id: BinaryTreeNodeId, val: T | null, count: number = 1): BSTNode<T> | null {
47
54
  let inserted: BSTNode<T> | null = null;
48
55
  const newNode = this.createNode(id, val, count);
49
56
  if (this.root === null) {
@@ -108,17 +115,44 @@ export class BST<T> extends BinaryTree<T> {
108
115
  return inserted;
109
116
  }
110
117
 
118
+ /**
119
+ * The `get` function returns the first node in a binary search tree that matches the given property value or name.
120
+ * @param {BinaryTreeNodeId | T} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
121
+ * generic type `T`. It represents the value of the property that you want to search for in the binary search tree.
122
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
123
+ * specifies the property name to use for searching the binary search tree nodes. If not provided, it defaults to
124
+ * `'id'`.
125
+ * @returns The method is returning a BSTNode<T> object or null.
126
+ */
111
127
  override get(nodeProperty: BinaryTreeNodeId | T, propertyName ?: BinaryTreeNodePropertyName): BSTNode<T> | null {
112
128
  propertyName = propertyName ?? 'id';
113
129
  return this.getNodes(nodeProperty, propertyName, true)[0] ?? null;
114
130
  }
115
131
 
132
+ /**
133
+ * The function returns the id of the rightmost node if the comparison between two values is less than, the id of the
134
+ * leftmost node if the comparison is greater than, and the id of the rightmost node otherwise.
135
+ * @returns The function `lastKey()` returns the ID of the rightmost node in a binary tree. If the comparison between
136
+ * the first two elements in the tree is less than, it returns the ID of the rightmost node. If the comparison is
137
+ * greater than, it returns the ID of the leftmost node. Otherwise, it also returns the ID of the rightmost node. If
138
+ * there are no nodes in
139
+ */
116
140
  lastKey() {
117
141
  if (this._compare(0, 1) === CP.lt) return this.getRightMost()?.id ?? 0;
118
142
  else if (this._compare(0, 1) === CP.gt) return this.getLeftMost()?.id ?? 0;
119
143
  else return this.getRightMost()?.id ?? 0;
120
144
  }
121
145
 
146
+ /**
147
+ * The `remove` function in this TypeScript code removes a node from a binary search tree and returns information about
148
+ * the deleted node and any nodes that need to be balanced.
149
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that needs to be removed
150
+ * from the binary search tree.
151
+ * @param {boolean} [ignoreCount] - A boolean flag indicating whether to ignore the count of the node being removed. If
152
+ * set to true, the count of the node will not be considered and the node will be removed regardless of its count. If
153
+ * set to false or not provided, the count of the node will be taken into account and the
154
+ * @returns an array of `BSTDeletedResult<T>` objects.
155
+ */
122
156
  override remove(id: BinaryTreeNodeId, ignoreCount?: boolean): BSTDeletedResult<T>[] {
123
157
  const bstDeletedResult: BSTDeletedResult<T>[] = [];
124
158
  if (!this.root) return bstDeletedResult;
@@ -167,6 +201,19 @@ export class BST<T> extends BinaryTree<T> {
167
201
  return bstDeletedResult;
168
202
  }
169
203
 
204
+ /**
205
+ * The function `getNodes` returns an array of binary search tree nodes that match a given property value, with the
206
+ * option to specify the property name and whether to return only one node.
207
+ * @param {BinaryTreeNodeId | T} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
208
+ * generic type `T`. It represents the property value that you want to search for in the binary search tree.
209
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
210
+ * specifies the property of the nodes to compare with the `nodeProperty` parameter. If not provided, it defaults to
211
+ * `'id'`.
212
+ * @param {boolean} [onlyOne] - A boolean value indicating whether to return only one node that matches the given
213
+ * nodeProperty. If set to true, the function will stop traversing the tree and return the first matching node. If set
214
+ * to false or not provided, the function will return all nodes that match the given nodeProperty.
215
+ * @returns an array of BSTNode<T> objects.
216
+ */
170
217
  override getNodes(nodeProperty: BinaryTreeNodeId | T, propertyName ?: BinaryTreeNodePropertyName, onlyOne ?: boolean): BSTNode<T>[] {
171
218
  propertyName = propertyName ?? 'id';
172
219
  if (!this.root) return [];
@@ -208,6 +255,16 @@ export class BST<T> extends BinaryTree<T> {
208
255
  }
209
256
 
210
257
  // --- start additional functions
258
+ /**
259
+ * The `lesserSum` function calculates the sum of a specified property in all nodes with an ID less than a given ID in
260
+ * a binary search tree.
261
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node for which you want to
262
+ * calculate the lesser sum.
263
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
264
+ * specifies the property of the binary tree node to use for calculating the sum. If not provided, it defaults to 'id'.
265
+ * @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in the
266
+ * binary search tree that have a property value lesser than the given `id`.
267
+ */
211
268
  lesserSum(id: BinaryTreeNodeId, propertyName ?: BinaryTreeNodePropertyName): number {
212
269
  propertyName = propertyName ?? 'id';
213
270
  if (!this.root) return 0;
@@ -273,6 +330,18 @@ export class BST<T> extends BinaryTree<T> {
273
330
  return sum;
274
331
  }
275
332
 
333
+ /**
334
+ * The function `allGreaterNodesAdd` updates the value of a specified property for all nodes in a binary search tree
335
+ * that have a greater value than a given node.
336
+ * @param node - The `node` parameter is of type `BSTNode<T>`, which represents a node in a binary search tree. It
337
+ * contains properties such as `id` and `count`.
338
+ * @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
339
+ * each node should be increased.
340
+ * @param {BinaryTreeNodePropertyName} [propertyName] - propertyName is an optional parameter that specifies the
341
+ * property of the BSTNode to be modified. It can be either 'id' or 'count'. If propertyName is not provided, it
342
+ * defaults to 'id'.
343
+ * @returns a boolean value.
344
+ */
276
345
  allGreaterNodesAdd(node: BSTNode<T>, delta: number, propertyName ?: BinaryTreeNodePropertyName): boolean {
277
346
  propertyName = propertyName ?? 'id';
278
347
  if (!this.root) return false;
@@ -319,6 +388,11 @@ export class BST<T> extends BinaryTree<T> {
319
388
  }
320
389
  }
321
390
 
391
+ /**
392
+ * The `balance` function takes a sorted array of nodes and builds a balanced binary search tree using either a
393
+ * recursive or iterative approach.
394
+ * @returns The `balance()` function returns a boolean value.
395
+ */
322
396
  balance(): boolean {
323
397
  const sorted = this.DFS('in', 'node'), n = sorted.length;
324
398
  this.clear();
@@ -329,7 +403,7 @@ export class BST<T> extends BinaryTree<T> {
329
403
  if (l > r) return;
330
404
  const m = l + Math.floor((r - l) / 2);
331
405
  const midNode = sorted[m];
332
- this.put(midNode.id, midNode.val, midNode.count);
406
+ this.add(midNode.id, midNode.val, midNode.count);
333
407
  buildBalanceBST(l, m - 1);
334
408
  buildBalanceBST(m + 1, r);
335
409
  };
@@ -345,7 +419,7 @@ export class BST<T> extends BinaryTree<T> {
345
419
  if (l <= r) {
346
420
  const m = l + Math.floor((r - l) / 2);
347
421
  const midNode = sorted[m];
348
- this.put(midNode.id, midNode.val, midNode.count);
422
+ this.add(midNode.id, midNode.val, midNode.count);
349
423
  stack.push([m + 1, r]);
350
424
  stack.push([l, m - 1]);
351
425
  }
@@ -355,6 +429,11 @@ export class BST<T> extends BinaryTree<T> {
355
429
  }
356
430
  }
357
431
 
432
+ /**
433
+ * The function `isAVLBalanced` checks if a binary search tree is balanced according to the AVL tree property.
434
+ * @returns The function `isAVLBalanced()` returns a boolean value. It returns `true` if the binary search tree (BST)
435
+ * is balanced according to the AVL tree property, and `false` otherwise.
436
+ */
358
437
  isAVLBalanced(): boolean {
359
438
  if (!this.root) return true;
360
439
 
@@ -399,6 +478,14 @@ export class BST<T> extends BinaryTree<T> {
399
478
 
400
479
  protected _comparator: BSTComparator = (a, b) => a - b;
401
480
 
481
+ /**
482
+ * The function compares two binary tree node IDs using a comparator function and returns whether the first ID is
483
+ * greater than, less than, or equal to the second ID.
484
+ * @param {BinaryTreeNodeId} a - a is a BinaryTreeNodeId, which represents the identifier of a binary tree node.
485
+ * @param {BinaryTreeNodeId} b - The parameter "b" in the above code refers to a BinaryTreeNodeId.
486
+ * @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater than), CP.lt (less
487
+ * than), or CP.eq (equal).
488
+ */
402
489
  protected _compare(a: BinaryTreeNodeId, b: BinaryTreeNodeId): CP {
403
490
  const compared = this._comparator(a, b);
404
491
  if (compared > 0) return CP.gt;
@@ -1,6 +1,9 @@
1
1
  /**
2
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
- * @license MIT
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
4
7
  */
5
8
 
6
9
  import type {SegmentTreeNodeVal} from '../types';
@@ -79,6 +82,15 @@ export class SegmentTree {
79
82
  protected _start = 0;
80
83
  protected _end: number;
81
84
 
85
+ /**
86
+ * The constructor initializes the values, start, end, and root properties of an object.
87
+ * @param {number[]} values - An array of numbers that will be used to build a binary search tree.
88
+ * @param {number} [start] - The `start` parameter is the index of the first element in the `values` array that should
89
+ * be included in the range. If no value is provided for `start`, it defaults to 0, which means the range starts from
90
+ * the beginning of the array.
91
+ * @param {number} [end] - The "end" parameter is the index of the last element in the "values" array that should be
92
+ * included in the range. If not provided, it defaults to the index of the last element in the "values" array.
93
+ */
82
94
  constructor(values: number[], start?: number, end?: number) {
83
95
  start = start || 0;
84
96
  end = end || values.length - 1;
@@ -94,6 +106,15 @@ export class SegmentTree {
94
106
  return this._root;
95
107
  }
96
108
 
109
+ /**
110
+ * The function builds a segment tree by recursively dividing the given range into smaller segments and creating nodes
111
+ * for each segment.
112
+ * @param {number} start - The `start` parameter represents the starting index of the segment or range for which we are
113
+ * building the segment tree.
114
+ * @param {number} end - The `end` parameter represents the ending index of the segment or range for which we are
115
+ * building the segment tree.
116
+ * @returns a SegmentTreeNode object.
117
+ */
97
118
  build(start: number, end: number): SegmentTreeNode {
98
119
  if (start === end) {
99
120
  return new SegmentTreeNode(start, end, this._values[start]);
@@ -107,6 +128,17 @@ export class SegmentTree {
107
128
  return cur;
108
129
  }
109
130
 
131
+ /**
132
+ * The function updates the value of a node in a segment tree and recalculates the sum of its children if they exist.
133
+ * @param {number} index - The index parameter represents the index of the node in the segment tree that needs to be
134
+ * updated.
135
+ * @param {number} sum - The `sum` parameter represents the new value that should be assigned to the `sum` property of
136
+ * the `SegmentTreeNode` at the specified `index`.
137
+ * @param {SegmentTreeNodeVal} [val] - The `val` parameter is an optional value that can be assigned to the `val`
138
+ * property of the `SegmentTreeNode` object. It is not currently used in the code, but you can uncomment the line `//
139
+ * cur.val = val;` and pass a value for `val` in the
140
+ * @returns The function does not return anything.
141
+ */
110
142
  updateNode(index: number, sum: number, val?: SegmentTreeNodeVal) {
111
143
  const root = this.root || null;
112
144
  if (!root) {
@@ -136,6 +168,13 @@ export class SegmentTree {
136
168
  dfs(root, index, sum);
137
169
  }
138
170
 
171
+ /**
172
+ * The function `querySumByRange` calculates the sum of values within a given range in a segment tree.
173
+ * @param {number} indexA - The starting index of the range for which you want to calculate the sum.
174
+ * @param {number} indexB - The parameter `indexB` represents the ending index of the range for which you want to
175
+ * calculate the sum.
176
+ * @returns The function `querySumByRange` returns a number.
177
+ */
139
178
  querySumByRange(indexA: number, indexB: number): number {
140
179
  const root = this.root || null;
141
180
  if (!root) {
@@ -1,19 +1,50 @@
1
1
  /**
2
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
- * @license MIT
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
4
7
  */
5
8
  import {BST, BSTNode} from './bst';
6
9
  import type {BinaryTreeNodeId, TreeMultiSetDeletedResult} from '../types';
7
10
 
8
11
  export class TreeMultiSet<T> extends BST<T> {
12
+ /**
13
+ * The function creates a new BSTNode with the given id, value, and count.
14
+ * @param {BinaryTreeNodeId} id - The id parameter is the unique identifier for the binary tree node. It is used to
15
+ * distinguish one node from another in the tree.
16
+ * @param {T} val - The `val` parameter represents the value that will be stored in the binary search tree node.
17
+ * @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
18
+ * occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
19
+ * @returns A new instance of the BSTNode class with the specified id, value, and count (if provided).
20
+ */
9
21
  override createNode(id: BinaryTreeNodeId, val: T, count?: number): BSTNode<T> {
10
22
  return new BSTNode<T>(id, val, count);
11
23
  }
12
24
 
13
- override put(id: BinaryTreeNodeId, val: T | null, count?: number): BSTNode<T> | null {
14
- return super.put(id, val, count);
25
+ /**
26
+ * The function overrides the add method of the BinarySearchTree class in TypeScript.
27
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that you want to add.
28
+ * @param {T | null} val - The `val` parameter represents the value that you want to add to the binary search tree. It
29
+ * can be of type `T` (the generic type) or `null`.
30
+ * @param {number} [count] - The `count` parameter is an optional parameter of type `number`. It represents the number
31
+ * of times the value should be added to the binary search tree. If not provided, the default value is `undefined`.
32
+ * @returns The `add` method is returning a `BSTNode<T>` object or `null`.
33
+ */
34
+ override add(id: BinaryTreeNodeId, val: T | null, count?: number): BSTNode<T> | null {
35
+ return super.add(id, val, count);
15
36
  }
16
37
 
38
+ /**
39
+ * The function overrides the remove method of the superclass and returns the result of calling the superclass's remove
40
+ * method.
41
+ * @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node that needs to be
42
+ * removed from the tree.
43
+ * @param {boolean} [isUpdateAllLeftSum] - The `isUpdateAllLeftSum` parameter is an optional boolean value that
44
+ * determines whether to update the left sum of all nodes in the tree after removing a node. If `isUpdateAllLeftSum` is
45
+ * set to `true`, the left sum of all nodes will be recalculated. If it
46
+ * @returns The method is returning an array of TreeMultiSetDeletedResult objects.
47
+ */
17
48
  override remove(id: BinaryTreeNodeId, isUpdateAllLeftSum?: boolean): TreeMultiSetDeletedResult<T>[] {
18
49
  return super.remove(id, isUpdateAllLeftSum);
19
50
  }
@@ -1,6 +1,9 @@
1
1
  /**
2
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
- * @license MIT
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
4
7
  */
5
8
  import {arrayRemove, uuidV4} from '../../utils';
6
9
  import {PriorityQueue} from '../priority-queue';
@@ -26,6 +29,11 @@ export abstract class AbstractEdge {
26
29
 
27
30
  static DEFAULT_EDGE_WEIGHT = 1;
28
31
 
32
+ /**
33
+ * The function is a protected constructor that initializes the weight and generates a unique hash code for an edge.
34
+ * @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. If
35
+ * no weight is provided, it will default to the value of `AbstractEdge.DEFAULT_EDGE_WEIGHT`.
36
+ */
29
37
  protected constructor(weight?: number) {
30
38
  if (weight === undefined) weight = AbstractEdge.DEFAULT_EDGE_WEIGHT;
31
39
  this._weight = weight;
@@ -535,7 +543,7 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
535
543
  }
536
544
 
537
545
  const heap = new PriorityQueue<{ id: number, val: V }>({comparator: (a, b) => a.id - b.id});
538
- heap.offer({id: 0, val: srcVertex});
546
+ heap.add({id: 0, val: srcVertex});
539
547
 
540
548
  distMap.set(srcVertex, 0);
541
549
  preMap.set(srcVertex, null);
@@ -582,7 +590,7 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
582
590
  const distSrcToNeighbor = distMap.get(neighbor);
583
591
  if (distSrcToNeighbor) {
584
592
  if (dist + weight < distSrcToNeighbor) {
585
- heap.offer({id: dist + weight, val: neighbor});
593
+ heap.add({id: dist + weight, val: neighbor});
586
594
  preMap.set(neighbor, cur);
587
595
  distMap.set(neighbor, dist + weight);
588
596
  }