data-structure-typed 1.12.9 → 1.12.11

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 (95) hide show
  1. package/README.md +7 -0
  2. package/package.json +3 -3
  3. package/src/assets/logo.png +0 -0
  4. package/src/data-structures/binary-tree/avl-tree.ts +15 -6
  5. package/src/data-structures/binary-tree/binary-indexed-tree.ts +11 -2
  6. package/src/data-structures/binary-tree/binary-tree.ts +70 -58
  7. package/src/data-structures/binary-tree/bst.ts +94 -7
  8. package/src/data-structures/binary-tree/segment-tree.ts +41 -2
  9. package/src/data-structures/binary-tree/tree-multiset.ts +35 -4
  10. package/src/data-structures/graph/abstract-graph.ts +12 -4
  11. package/src/data-structures/graph/directed-graph.ts +26 -39
  12. package/src/data-structures/graph/undirected-graph.ts +18 -2
  13. package/src/data-structures/hash/coordinate-map.ts +5 -2
  14. package/src/data-structures/hash/coordinate-set.ts +5 -2
  15. package/src/data-structures/heap/heap.ts +13 -10
  16. package/src/data-structures/heap/max-heap.ts +5 -2
  17. package/src/data-structures/heap/min-heap.ts +5 -2
  18. package/src/data-structures/index.ts +2 -0
  19. package/src/data-structures/linked-list/doubly-linked-list.ts +9 -6
  20. package/src/data-structures/linked-list/singly-linked-list.ts +5 -2
  21. package/src/data-structures/matrix/matrix.ts +5 -2
  22. package/src/data-structures/matrix/matrix2d.ts +5 -2
  23. package/src/data-structures/matrix/navigator.ts +5 -2
  24. package/src/data-structures/matrix/vector2d.ts +5 -2
  25. package/src/data-structures/priority-queue/max-priority-queue.ts +5 -2
  26. package/src/data-structures/priority-queue/min-priority-queue.ts +5 -2
  27. package/src/data-structures/priority-queue/priority-queue.ts +7 -4
  28. package/src/data-structures/queue/deque.ts +12 -9
  29. package/src/data-structures/queue/queue.ts +4 -4
  30. package/src/data-structures/stack/stack.ts +1 -1
  31. package/src/data-structures/trie/trie.ts +7 -4
  32. package/src/utils/index.ts +2 -1
  33. package/src/utils/types/utils.ts +10 -12
  34. package/src/utils/utils.ts +57 -11
  35. package/tests/unit/data-structures/binary-tree/bst.test.ts +1 -1
  36. package/tests/unit/data-structures/graph/directed-graph.test.ts +1 -0
  37. package/docs/.nojekyll +0 -1
  38. package/docs/assets/highlight.css +0 -85
  39. package/docs/assets/main.js +0 -58
  40. package/docs/assets/search.js +0 -1
  41. package/docs/assets/style.css +0 -1367
  42. package/docs/classes/AVLTree.html +0 -2046
  43. package/docs/classes/AVLTreeNode.html +0 -423
  44. package/docs/classes/AaTree.html +0 -117
  45. package/docs/classes/AbstractEdge.html +0 -198
  46. package/docs/classes/AbstractGraph.html +0 -891
  47. package/docs/classes/AbstractVertex.html +0 -164
  48. package/docs/classes/ArrayDeque.html +0 -384
  49. package/docs/classes/BST.html +0 -1893
  50. package/docs/classes/BSTNode.html +0 -425
  51. package/docs/classes/BTree.html +0 -117
  52. package/docs/classes/BinaryIndexedTree.html +0 -244
  53. package/docs/classes/BinaryTree.html +0 -1754
  54. package/docs/classes/BinaryTreeNode.html +0 -396
  55. package/docs/classes/Character.html +0 -165
  56. package/docs/classes/CoordinateMap.html +0 -394
  57. package/docs/classes/CoordinateSet.html +0 -355
  58. package/docs/classes/Deque.html +0 -617
  59. package/docs/classes/DirectedEdge.html +0 -247
  60. package/docs/classes/DirectedGraph.html +0 -1207
  61. package/docs/classes/DirectedVertex.html +0 -154
  62. package/docs/classes/DoublyLinkedList.html +0 -619
  63. package/docs/classes/DoublyLinkedListNode.html +0 -160
  64. package/docs/classes/Heap.html +0 -315
  65. package/docs/classes/Matrix2D.html +0 -447
  66. package/docs/classes/MatrixNTI2D.html +0 -181
  67. package/docs/classes/MaxHeap.html +0 -325
  68. package/docs/classes/MaxPriorityQueue.html +0 -668
  69. package/docs/classes/MinHeap.html +0 -326
  70. package/docs/classes/MinPriorityQueue.html +0 -668
  71. package/docs/classes/Navigator.html +0 -285
  72. package/docs/classes/ObjectDeque.html +0 -289
  73. package/docs/classes/PriorityQueue.html +0 -643
  74. package/docs/classes/Queue.html +0 -337
  75. package/docs/classes/RBTree.html +0 -117
  76. package/docs/classes/SegmentTree.html +0 -234
  77. package/docs/classes/SegmentTreeNode.html +0 -302
  78. package/docs/classes/SinglyLinkedList.html +0 -1035
  79. package/docs/classes/SinglyLinkedListNode.html +0 -304
  80. package/docs/classes/SplayTree.html +0 -117
  81. package/docs/classes/Stack.html +0 -313
  82. package/docs/classes/TreeMultiSet.html +0 -1897
  83. package/docs/classes/Trie.html +0 -317
  84. package/docs/classes/TrieNode.html +0 -221
  85. package/docs/classes/TwoThreeTree.html +0 -117
  86. package/docs/classes/UndirectedEdge.html +0 -220
  87. package/docs/classes/UndirectedGraph.html +0 -1006
  88. package/docs/classes/UndirectedVertex.html +0 -154
  89. package/docs/classes/Vector2D.html +0 -746
  90. package/docs/enums/CP.html +0 -126
  91. package/docs/enums/FamilyPosition.html +0 -126
  92. package/docs/enums/LoopType.html +0 -119
  93. package/docs/index.html +0 -288
  94. package/docs/modules.html +0 -146
  95. package/src/utils/trampoline.ts +0 -51
package/README.md CHANGED
@@ -15,6 +15,13 @@ yarn add data-structure-typed
15
15
  ```bash
16
16
  npm install data-structure-typed
17
17
  ```
18
+ ## Online examples
19
+
20
+ [Online Examples](https://data-structure-typed-examples.vercel.app)
21
+
22
+ ## Examples Repository
23
+
24
+ [Example Repository](https://github.com/zrwusa/data-structure-typed-examples)
18
25
 
19
26
  ## api docs
20
27
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.12.9",
3
+ "version": "1.12.11",
4
4
  "description": "Explore our comprehensive Javascript Data Structure / TypeScript Data Structure Library, meticulously crafted to empower developers with a versatile set of essential data structures. Our library includes a wide range of data structures, such as Binary Tree, AVL Tree, Binary Search Tree (BST), Tree Multiset, Segment Tree, Binary Indexed Tree, Graph, Directed Graph, Undirected Graph, Singly Linked List, Hash, CoordinateSet, CoordinateMap, Heap, Doubly Linked List, Priority Queue, Max Priority Queue, Min Priority Queue, Queue, ObjectDeque, ArrayDeque, Stack, and Trie. Each data structure is thoughtfully designed and implemented using TypeScript to provide efficient, reliable, and easy-to-use solutions for your programming needs. Whether you're optimizing algorithms, managing data, or enhancing performance, our TypeScript Data Structure Library is your go-to resource. Elevate your coding experience with these fundamental building blocks for software development.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -39,8 +39,8 @@
39
39
  "Min Priority Queue",
40
40
  "Trie"
41
41
  ],
42
- "author": "Tyler Zeng",
43
- "license": "ISC",
42
+ "author": "Tyler Zeng zrwusa@gmail.com",
43
+ "license": "MIT",
44
44
  "bugs": {
45
45
  "url": "https://github.com/zrwusa/data-structure-typed/issues"
46
46
  },
Binary file
@@ -1,11 +1,20 @@
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 {AVLTreeDeleted, BinaryTreeNodeId} from '../types';
7
10
 
8
11
  export class AVLTreeNode<T> extends BSTNode<T> {
12
+ /**
13
+ * The function overrides the clone method of the AVLTreeNode class to create a new AVLTreeNode object with the same
14
+ * id, value, and count.
15
+ * @returns The method is returning a new instance of the AVLTreeNode class with the same id, val, and count values as
16
+ * the current instance.
17
+ */
9
18
  override clone(): AVLTreeNode<T> {
10
19
  return new AVLTreeNode<T>(this.id, this.val, this.count);
11
20
  }
@@ -18,9 +27,9 @@ export class AVLTree<T> extends BST<T> {
18
27
  }
19
28
 
20
29
  /**
21
- * The function overrides the put method of a Binary Search Tree to insert a node with a given id and value, and then
30
+ * The function overrides the add method of a Binary Search Tree to insert a node with a given id and value, and then
22
31
  * balances the tree.
23
- * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to put or
32
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add or
24
33
  * update in the AVL tree.
25
34
  * @param {T | null} val - The `val` parameter represents the value that you want to assign to the node with the given
26
35
  * `id`. It can be of type `T` (the generic type) or `null`.
@@ -29,8 +38,8 @@ export class AVLTree<T> extends BST<T> {
29
38
  * to `1`, indicating that the value should be inserted once.
30
39
  * @returns The method is returning either an AVLTreeNode<T> object or null.
31
40
  */
32
- override put(id: BinaryTreeNodeId, val: T | null, count?: number): AVLTreeNode<T> | null {
33
- const inserted = super.put(id, val, count);
41
+ override add(id: BinaryTreeNodeId, val: T | null, count?: number): AVLTreeNode<T> | null {
42
+ const inserted = super.add(id, val, count);
34
43
  if (inserted) this.balancePath(inserted);
35
44
  return inserted;
36
45
  }
@@ -1,10 +1,19 @@
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
  export class BinaryIndexedTree {
6
9
  private readonly _sumTree: number[];
7
10
 
11
+ /**
12
+ * The constructor initializes an array with a specified length and fills it with zeros.
13
+ * @param {number} n - The parameter `n` represents the size of the array that will be used to store the sum tree. The
14
+ * sum tree is a binary tree data structure used to efficiently calculate the sum of a range of elements in an array.
15
+ * The size of the sum tree array is `n + 1` because
16
+ */
8
17
  constructor(n: number) {
9
18
  this._sumTree = new Array<number>(n + 1).fill(0);
10
19
  }
@@ -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
  }