data-structure-typed 1.47.8 → 1.48.0

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 (48) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +33 -32
  3. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +6 -0
  4. package/dist/cjs/data-structures/binary-tree/avl-tree.js +8 -0
  5. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  6. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +14 -0
  7. package/dist/cjs/data-structures/binary-tree/binary-tree.js +52 -28
  8. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  9. package/dist/cjs/data-structures/binary-tree/bst.d.ts +13 -0
  10. package/dist/cjs/data-structures/binary-tree/bst.js +41 -21
  11. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  12. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +16 -0
  13. package/dist/cjs/data-structures/binary-tree/rb-tree.js +54 -31
  14. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  15. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +16 -0
  16. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +44 -21
  17. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  18. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +5 -0
  19. package/dist/cjs/data-structures/graph/abstract-graph.js +41 -0
  20. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  21. package/dist/cjs/data-structures/graph/directed-graph.js +1 -1
  22. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  23. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +6 -0
  24. package/dist/mjs/data-structures/binary-tree/avl-tree.js +8 -0
  25. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +14 -0
  26. package/dist/mjs/data-structures/binary-tree/binary-tree.js +52 -28
  27. package/dist/mjs/data-structures/binary-tree/bst.d.ts +13 -0
  28. package/dist/mjs/data-structures/binary-tree/bst.js +41 -21
  29. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +16 -0
  30. package/dist/mjs/data-structures/binary-tree/rb-tree.js +54 -31
  31. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +16 -0
  32. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +44 -21
  33. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +5 -0
  34. package/dist/mjs/data-structures/graph/abstract-graph.js +41 -0
  35. package/dist/mjs/data-structures/graph/directed-graph.js +1 -1
  36. package/dist/umd/data-structure-typed.js +228 -90
  37. package/dist/umd/data-structure-typed.min.js +2 -2
  38. package/dist/umd/data-structure-typed.min.js.map +1 -1
  39. package/package.json +1 -1
  40. package/src/data-structures/binary-tree/avl-tree.ts +9 -0
  41. package/src/data-structures/binary-tree/binary-tree.ts +47 -23
  42. package/src/data-structures/binary-tree/bst.ts +38 -19
  43. package/src/data-structures/binary-tree/rb-tree.ts +55 -31
  44. package/src/data-structures/binary-tree/tree-multimap.ts +42 -17
  45. package/src/data-structures/graph/abstract-graph.ts +46 -0
  46. package/src/data-structures/graph/directed-graph.ts +1 -1
  47. package/test/integration/index.html +2 -2
  48. package/test/unit/data-structures/graph/directed-graph.test.ts +41 -0
@@ -51,6 +51,48 @@ export class TreeMultimap extends AVLTree {
51
51
  comparator: this.comparator, ...options
52
52
  });
53
53
  }
54
+ /**
55
+ * The function checks if an exemplar is an instance of the TreeMultimapNode class.
56
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
57
+ * @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
58
+ * class.
59
+ */
60
+ isNode(exemplar) {
61
+ return exemplar instanceof TreeMultimapNode;
62
+ }
63
+ /**
64
+ * The function `exemplarToNode` converts an exemplar object into a node object.
65
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`, where `V` represents
66
+ * the value type and `N` represents the node type.
67
+ * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
68
+ * times the node should be created. If not provided, it defaults to 1.
69
+ * @returns a value of type `N` (the generic type parameter) or `undefined`.
70
+ */
71
+ exemplarToNode(exemplar, count = 1) {
72
+ let node;
73
+ if (exemplar === undefined || exemplar === null) {
74
+ return;
75
+ }
76
+ else if (this.isNode(exemplar)) {
77
+ node = exemplar;
78
+ }
79
+ else if (this.isEntry(exemplar)) {
80
+ const [key, value] = exemplar;
81
+ if (key === undefined || key === null) {
82
+ return;
83
+ }
84
+ else {
85
+ node = this.createNode(key, value, count);
86
+ }
87
+ }
88
+ else if (this.isNodeKey(exemplar)) {
89
+ node = this.createNode(exemplar, undefined, count);
90
+ }
91
+ else {
92
+ return;
93
+ }
94
+ return node;
95
+ }
54
96
  /**
55
97
  * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
56
98
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
@@ -68,28 +110,9 @@ export class TreeMultimap extends AVLTree {
68
110
  * @returns either a node (`N`) or `undefined`.
69
111
  */
70
112
  add(keyOrNodeOrEntry, count = 1) {
71
- let newNode;
72
- if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
73
- return;
74
- }
75
- else if (keyOrNodeOrEntry instanceof TreeMultimapNode) {
76
- newNode = keyOrNodeOrEntry;
77
- }
78
- else if (this.isNodeKey(keyOrNodeOrEntry)) {
79
- newNode = this.createNode(keyOrNodeOrEntry, undefined, count);
80
- }
81
- else if (this.isEntry(keyOrNodeOrEntry)) {
82
- const [key, value] = keyOrNodeOrEntry;
83
- if (key === undefined || key === null) {
84
- return;
85
- }
86
- else {
87
- newNode = this.createNode(key, value, count);
88
- }
89
- }
90
- else {
113
+ const newNode = this.exemplarToNode(keyOrNodeOrEntry, count);
114
+ if (newNode === undefined)
91
115
  return;
92
- }
93
116
  const orgNodeCount = newNode?.count || 0;
94
117
  const inserted = super.add(newNode);
95
118
  if (inserted) {
@@ -443,6 +443,11 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
443
443
  * @returns the bridges found using the Tarjan algorithm.
444
444
  */
445
445
  getBridges(): EO[];
446
+ [Symbol.iterator](): Iterator<[VertexKey, V | undefined]>;
447
+ forEach(callback: (entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => void): void;
448
+ filter(predicate: (entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => boolean): [VertexKey, V | undefined][];
449
+ map<T>(callback: (entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => T): T[];
450
+ reduce<T>(callback: (accumulator: T, entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => T, initialValue: T): T;
446
451
  protected abstract _addEdgeOnly(edge: EO): boolean;
447
452
  protected _addVertexOnly(newVertex: VO): boolean;
448
453
  protected _getVertex(vertexOrKey: VertexKey | VO): VO | undefined;
@@ -1021,6 +1021,47 @@ export class AbstractGraph {
1021
1021
  getBridges() {
1022
1022
  return this.tarjan(false, true, false, false).bridges;
1023
1023
  }
1024
+ *[Symbol.iterator]() {
1025
+ for (const vertex of this._vertices.values()) {
1026
+ yield [vertex.key, vertex.value];
1027
+ }
1028
+ }
1029
+ forEach(callback) {
1030
+ let index = 0;
1031
+ for (const vertex of this) {
1032
+ callback(vertex, index, this._vertices);
1033
+ index++;
1034
+ }
1035
+ }
1036
+ filter(predicate) {
1037
+ const filtered = [];
1038
+ let index = 0;
1039
+ for (const entry of this) {
1040
+ if (predicate(entry, index, this._vertices)) {
1041
+ filtered.push(entry);
1042
+ }
1043
+ index++;
1044
+ }
1045
+ return filtered;
1046
+ }
1047
+ map(callback) {
1048
+ const mapped = [];
1049
+ let index = 0;
1050
+ for (const entry of this) {
1051
+ mapped.push(callback(entry, index, this._vertices));
1052
+ index++;
1053
+ }
1054
+ return mapped;
1055
+ }
1056
+ reduce(callback, initialValue) {
1057
+ let accumulator = initialValue;
1058
+ let index = 0;
1059
+ for (const entry of this) {
1060
+ accumulator = callback(accumulator, entry, index, this._vertices);
1061
+ index++;
1062
+ }
1063
+ return accumulator;
1064
+ }
1024
1065
  _addVertexOnly(newVertex) {
1025
1066
  if (this.hasVertex(newVertex)) {
1026
1067
  return false;
@@ -68,7 +68,7 @@ export class DirectedGraph extends AbstractGraph {
68
68
  * @returns a new instance of a DirectedVertex object, casted as type VO.
69
69
  */
70
70
  createVertex(key, value) {
71
- return new DirectedVertex(key, value ?? key);
71
+ return new DirectedVertex(key, value);
72
72
  }
73
73
  /**
74
74
  * In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
@@ -5908,6 +5908,47 @@ var dataStructureTyped = (() => {
5908
5908
  getBridges() {
5909
5909
  return this.tarjan(false, true, false, false).bridges;
5910
5910
  }
5911
+ *[Symbol.iterator]() {
5912
+ for (const vertex of this._vertices.values()) {
5913
+ yield [vertex.key, vertex.value];
5914
+ }
5915
+ }
5916
+ forEach(callback) {
5917
+ let index = 0;
5918
+ for (const vertex of this) {
5919
+ callback(vertex, index, this._vertices);
5920
+ index++;
5921
+ }
5922
+ }
5923
+ filter(predicate) {
5924
+ const filtered = [];
5925
+ let index = 0;
5926
+ for (const entry of this) {
5927
+ if (predicate(entry, index, this._vertices)) {
5928
+ filtered.push(entry);
5929
+ }
5930
+ index++;
5931
+ }
5932
+ return filtered;
5933
+ }
5934
+ map(callback) {
5935
+ const mapped = [];
5936
+ let index = 0;
5937
+ for (const entry of this) {
5938
+ mapped.push(callback(entry, index, this._vertices));
5939
+ index++;
5940
+ }
5941
+ return mapped;
5942
+ }
5943
+ reduce(callback, initialValue) {
5944
+ let accumulator = initialValue;
5945
+ let index = 0;
5946
+ for (const entry of this) {
5947
+ accumulator = callback(accumulator, entry, index, this._vertices);
5948
+ index++;
5949
+ }
5950
+ return accumulator;
5951
+ }
5911
5952
  _addVertexOnly(newVertex) {
5912
5953
  if (this.hasVertex(newVertex)) {
5913
5954
  return false;
@@ -5986,7 +6027,7 @@ var dataStructureTyped = (() => {
5986
6027
  * @returns a new instance of a DirectedVertex object, casted as type VO.
5987
6028
  */
5988
6029
  createVertex(key, value) {
5989
- return new DirectedVertex(key, value != null ? value : key);
6030
+ return new DirectedVertex(key, value);
5990
6031
  }
5991
6032
  /**
5992
6033
  * In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
@@ -6940,6 +6981,45 @@ var dataStructureTyped = (() => {
6940
6981
  createTree(options) {
6941
6982
  return new _BinaryTree([], __spreadValues({ iterationType: this.iterationType }, options));
6942
6983
  }
6984
+ /**
6985
+ * The function "isNode" checks if an exemplar is an instance of the BinaryTreeNode class.
6986
+ * @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
6987
+ * @returns a boolean value indicating whether the exemplar is an instance of the class N.
6988
+ */
6989
+ isNode(exemplar) {
6990
+ return exemplar instanceof BinaryTreeNode;
6991
+ }
6992
+ /**
6993
+ * The function `exemplarToNode` converts an exemplar of a binary tree node into an actual node
6994
+ * object.
6995
+ * @param exemplar - BTNodeExemplar<V, N> - A generic type representing the exemplar parameter of the
6996
+ * function. It can be any type.
6997
+ * @returns a value of type `N` (which represents a node), or `null`, or `undefined`.
6998
+ */
6999
+ exemplarToNode(exemplar) {
7000
+ if (exemplar === void 0)
7001
+ return;
7002
+ let node;
7003
+ if (exemplar === null) {
7004
+ node = null;
7005
+ } else if (this.isEntry(exemplar)) {
7006
+ const [key, value] = exemplar;
7007
+ if (key === void 0) {
7008
+ return;
7009
+ } else if (key === null) {
7010
+ node = null;
7011
+ } else {
7012
+ node = this.createNode(key, value);
7013
+ }
7014
+ } else if (this.isNode(exemplar)) {
7015
+ node = exemplar;
7016
+ } else if (this.isNodeKey(exemplar)) {
7017
+ node = this.createNode(exemplar);
7018
+ } else {
7019
+ return;
7020
+ }
7021
+ return node;
7022
+ }
6943
7023
  /**
6944
7024
  * The function checks if a given value is an entry in a binary tree node.
6945
7025
  * @param kne - BTNodeExemplar<V, N> - A generic type representing a node in a binary tree. It has
@@ -6962,16 +7042,19 @@ var dataStructureTyped = (() => {
6962
7042
  * @returns The function `add` returns the inserted node (`N`), `null`, or `undefined`.
6963
7043
  */
6964
7044
  add(keyOrNodeOrEntry) {
6965
- let inserted, needInsert;
6966
- const _bfs = (root, newNode) => {
7045
+ let inserted;
7046
+ const newNode = this.exemplarToNode(keyOrNodeOrEntry);
7047
+ if (newNode === void 0)
7048
+ return;
7049
+ const _bfs = (root, newNode2) => {
6967
7050
  const queue = new Queue([root]);
6968
7051
  while (queue.size > 0) {
6969
7052
  const cur = queue.shift();
6970
- if (newNode && cur.key === newNode.key) {
6971
- this._replaceNode(cur, newNode);
6972
- return newNode;
7053
+ if (newNode2 && cur.key === newNode2.key) {
7054
+ this._replaceNode(cur, newNode2);
7055
+ return newNode2;
6973
7056
  }
6974
- const inserted2 = this._addTo(newNode, cur);
7057
+ const inserted2 = this._addTo(newNode2, cur);
6975
7058
  if (inserted2 !== void 0)
6976
7059
  return inserted2;
6977
7060
  if (cur.left)
@@ -6980,29 +7063,11 @@ var dataStructureTyped = (() => {
6980
7063
  queue.push(cur.right);
6981
7064
  }
6982
7065
  };
6983
- if (keyOrNodeOrEntry === null) {
6984
- needInsert = null;
6985
- } else if (this.isNodeKey(keyOrNodeOrEntry)) {
6986
- needInsert = this.createNode(keyOrNodeOrEntry);
6987
- } else if (keyOrNodeOrEntry instanceof BinaryTreeNode) {
6988
- needInsert = keyOrNodeOrEntry;
6989
- } else if (this.isEntry(keyOrNodeOrEntry)) {
6990
- const [key, value] = keyOrNodeOrEntry;
6991
- if (key === void 0) {
6992
- return;
6993
- } else if (key === null) {
6994
- needInsert = null;
6995
- } else {
6996
- needInsert = this.createNode(key, value);
6997
- }
6998
- } else {
6999
- return;
7000
- }
7001
7066
  if (this.root) {
7002
- inserted = _bfs(this.root, needInsert);
7067
+ inserted = _bfs(this.root, newNode);
7003
7068
  } else {
7004
- this._setRoot(needInsert);
7005
- if (needInsert) {
7069
+ this._setRoot(newNode);
7070
+ if (newNode) {
7006
7071
  this._size = 1;
7007
7072
  } else {
7008
7073
  this._size = 0;
@@ -8508,6 +8573,40 @@ var dataStructureTyped = (() => {
8508
8573
  comparator: this.comparator
8509
8574
  }, options));
8510
8575
  }
8576
+ /**
8577
+ * The function checks if an exemplar is an instance of BSTNode.
8578
+ * @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
8579
+ * @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
8580
+ */
8581
+ isNode(exemplar) {
8582
+ return exemplar instanceof BSTNode;
8583
+ }
8584
+ /**
8585
+ * The function `exemplarToNode` takes an exemplar and returns a corresponding node if the exemplar
8586
+ * is valid, otherwise it returns undefined.
8587
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
8588
+ * @returns a variable `node` which is of type `N` or `undefined`.
8589
+ */
8590
+ exemplarToNode(exemplar) {
8591
+ let node;
8592
+ if (exemplar === null || exemplar === void 0) {
8593
+ return;
8594
+ } else if (this.isNode(exemplar)) {
8595
+ node = exemplar;
8596
+ } else if (this.isEntry(exemplar)) {
8597
+ const [key, value] = exemplar;
8598
+ if (key === void 0 || key === null) {
8599
+ return;
8600
+ } else {
8601
+ node = this.createNode(key, value);
8602
+ }
8603
+ } else if (this.isNodeKey(exemplar)) {
8604
+ node = this.createNode(exemplar);
8605
+ } else {
8606
+ return;
8607
+ }
8608
+ return node;
8609
+ }
8511
8610
  /**
8512
8611
  * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
8513
8612
  * Space Complexity: O(1) - Constant space is used.
@@ -8523,24 +8622,9 @@ var dataStructureTyped = (() => {
8523
8622
  * (`keyOrNodeOrEntry`) is null, undefined, or does not match any of the expected types.
8524
8623
  */
8525
8624
  add(keyOrNodeOrEntry) {
8526
- if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === void 0) {
8527
- return void 0;
8528
- }
8529
- let newNode;
8530
- if (keyOrNodeOrEntry instanceof BSTNode) {
8531
- newNode = keyOrNodeOrEntry;
8532
- } else if (this.isNodeKey(keyOrNodeOrEntry)) {
8533
- newNode = this.createNode(keyOrNodeOrEntry);
8534
- } else if (this.isEntry(keyOrNodeOrEntry)) {
8535
- const [key, value] = keyOrNodeOrEntry;
8536
- if (key === void 0 || key === null) {
8537
- return;
8538
- } else {
8539
- newNode = this.createNode(key, value);
8540
- }
8541
- } else {
8625
+ const newNode = this.exemplarToNode(keyOrNodeOrEntry);
8626
+ if (newNode === void 0)
8542
8627
  return;
8543
- }
8544
8628
  if (this.root === void 0) {
8545
8629
  this._setRoot(newNode);
8546
8630
  this._size++;
@@ -9504,6 +9588,14 @@ var dataStructureTyped = (() => {
9504
9588
  comparator: this.comparator
9505
9589
  }, options));
9506
9590
  }
9591
+ /**
9592
+ * The function checks if an exemplar is an instance of AVLTreeNode.
9593
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
9594
+ * @returns a boolean value indicating whether the exemplar is an instance of the AVLTreeNode class.
9595
+ */
9596
+ isNode(exemplar) {
9597
+ return exemplar instanceof AVLTreeNode;
9598
+ }
9507
9599
  /**
9508
9600
  * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
9509
9601
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
@@ -9925,70 +10017,93 @@ var dataStructureTyped = (() => {
9925
10017
  }, options));
9926
10018
  }
9927
10019
  /**
9928
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
9929
- * Space Complexity: O(1)
10020
+ * The function checks if an exemplar is an instance of the RedBlackTreeNode class.
10021
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
10022
+ * @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
10023
+ * class.
9930
10024
  */
10025
+ isNode(exemplar) {
10026
+ return exemplar instanceof RedBlackTreeNode;
10027
+ }
9931
10028
  /**
9932
- * The function adds a node to a Red-Black Tree data structure.
9933
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
9934
- * @returns The method `add` returns either an instance of `N` (the node that was added) or
9935
- * `undefined`.
10029
+ * The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
10030
+ * otherwise it returns undefined.
10031
+ * @param exemplar - BTNodeExemplar<V, N> - A generic type representing an exemplar of a binary tree
10032
+ * node. It can be either a node itself, an entry (key-value pair), a node key, or any other value
10033
+ * that is not a valid exemplar.
10034
+ * @returns a variable `node` which is of type `N | undefined`.
9936
10035
  */
9937
- add(keyOrNodeOrEntry) {
10036
+ exemplarToNode(exemplar) {
9938
10037
  let node;
9939
- if (this.isNodeKey(keyOrNodeOrEntry)) {
9940
- node = this.createNode(keyOrNodeOrEntry, void 0, 1 /* RED */);
9941
- } else if (keyOrNodeOrEntry instanceof RedBlackTreeNode) {
9942
- node = keyOrNodeOrEntry;
9943
- } else if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === void 0) {
10038
+ if (exemplar === null || exemplar === void 0) {
9944
10039
  return;
9945
- } else if (this.isEntry(keyOrNodeOrEntry)) {
9946
- const [key, value] = keyOrNodeOrEntry;
10040
+ } else if (this.isNode(exemplar)) {
10041
+ node = exemplar;
10042
+ } else if (this.isEntry(exemplar)) {
10043
+ const [key, value] = exemplar;
9947
10044
  if (key === void 0 || key === null) {
9948
10045
  return;
9949
10046
  } else {
9950
10047
  node = this.createNode(key, value, 1 /* RED */);
9951
10048
  }
10049
+ } else if (this.isNodeKey(exemplar)) {
10050
+ node = this.createNode(exemplar, void 0, 1 /* RED */);
9952
10051
  } else {
9953
10052
  return;
9954
10053
  }
9955
- node.left = this.Sentinel;
9956
- node.right = this.Sentinel;
10054
+ return node;
10055
+ }
10056
+ /**
10057
+ * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
10058
+ * Space Complexity: O(1)
10059
+ */
10060
+ /**
10061
+ * The function adds a node to a Red-Black Tree data structure.
10062
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
10063
+ * @returns The method `add` returns either an instance of `N` (the node that was added) or
10064
+ * `undefined`.
10065
+ */
10066
+ add(keyOrNodeOrEntry) {
10067
+ const newNode = this.exemplarToNode(keyOrNodeOrEntry);
10068
+ if (newNode === void 0)
10069
+ return;
10070
+ newNode.left = this.Sentinel;
10071
+ newNode.right = this.Sentinel;
9957
10072
  let y = void 0;
9958
10073
  let x = this.root;
9959
10074
  while (x !== this.Sentinel) {
9960
10075
  y = x;
9961
10076
  if (x) {
9962
- if (node.key < x.key) {
10077
+ if (newNode.key < x.key) {
9963
10078
  x = x.left;
9964
- } else if (node.key > x.key) {
10079
+ } else if (newNode.key > x.key) {
9965
10080
  x = x == null ? void 0 : x.right;
9966
10081
  } else {
9967
- if (node !== x) {
9968
- this._replaceNode(x, node);
10082
+ if (newNode !== x) {
10083
+ this._replaceNode(x, newNode);
9969
10084
  }
9970
10085
  return;
9971
10086
  }
9972
10087
  }
9973
10088
  }
9974
- node.parent = y;
10089
+ newNode.parent = y;
9975
10090
  if (y === void 0) {
9976
- this._setRoot(node);
9977
- } else if (node.key < y.key) {
9978
- y.left = node;
10091
+ this._setRoot(newNode);
10092
+ } else if (newNode.key < y.key) {
10093
+ y.left = newNode;
9979
10094
  } else {
9980
- y.right = node;
10095
+ y.right = newNode;
9981
10096
  }
9982
- if (node.parent === void 0) {
9983
- node.color = 0 /* BLACK */;
10097
+ if (newNode.parent === void 0) {
10098
+ newNode.color = 0 /* BLACK */;
9984
10099
  this._size++;
9985
10100
  return;
9986
10101
  }
9987
- if (node.parent.parent === void 0) {
10102
+ if (newNode.parent.parent === void 0) {
9988
10103
  this._size++;
9989
10104
  return;
9990
10105
  }
9991
- this._fixInsert(node);
10106
+ this._fixInsert(newNode);
9992
10107
  this._size++;
9993
10108
  }
9994
10109
  /**
@@ -10439,6 +10554,43 @@ var dataStructureTyped = (() => {
10439
10554
  comparator: this.comparator
10440
10555
  }, options));
10441
10556
  }
10557
+ /**
10558
+ * The function checks if an exemplar is an instance of the TreeMultimapNode class.
10559
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
10560
+ * @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
10561
+ * class.
10562
+ */
10563
+ isNode(exemplar) {
10564
+ return exemplar instanceof TreeMultimapNode;
10565
+ }
10566
+ /**
10567
+ * The function `exemplarToNode` converts an exemplar object into a node object.
10568
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`, where `V` represents
10569
+ * the value type and `N` represents the node type.
10570
+ * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
10571
+ * times the node should be created. If not provided, it defaults to 1.
10572
+ * @returns a value of type `N` (the generic type parameter) or `undefined`.
10573
+ */
10574
+ exemplarToNode(exemplar, count = 1) {
10575
+ let node;
10576
+ if (exemplar === void 0 || exemplar === null) {
10577
+ return;
10578
+ } else if (this.isNode(exemplar)) {
10579
+ node = exemplar;
10580
+ } else if (this.isEntry(exemplar)) {
10581
+ const [key, value] = exemplar;
10582
+ if (key === void 0 || key === null) {
10583
+ return;
10584
+ } else {
10585
+ node = this.createNode(key, value, count);
10586
+ }
10587
+ } else if (this.isNodeKey(exemplar)) {
10588
+ node = this.createNode(exemplar, void 0, count);
10589
+ } else {
10590
+ return;
10591
+ }
10592
+ return node;
10593
+ }
10442
10594
  /**
10443
10595
  * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
10444
10596
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
@@ -10456,23 +10608,9 @@ var dataStructureTyped = (() => {
10456
10608
  * @returns either a node (`N`) or `undefined`.
10457
10609
  */
10458
10610
  add(keyOrNodeOrEntry, count = 1) {
10459
- let newNode;
10460
- if (keyOrNodeOrEntry === void 0 || keyOrNodeOrEntry === null) {
10461
- return;
10462
- } else if (keyOrNodeOrEntry instanceof TreeMultimapNode) {
10463
- newNode = keyOrNodeOrEntry;
10464
- } else if (this.isNodeKey(keyOrNodeOrEntry)) {
10465
- newNode = this.createNode(keyOrNodeOrEntry, void 0, count);
10466
- } else if (this.isEntry(keyOrNodeOrEntry)) {
10467
- const [key, value] = keyOrNodeOrEntry;
10468
- if (key === void 0 || key === null) {
10469
- return;
10470
- } else {
10471
- newNode = this.createNode(key, value, count);
10472
- }
10473
- } else {
10611
+ const newNode = this.exemplarToNode(keyOrNodeOrEntry, count);
10612
+ if (newNode === void 0)
10474
10613
  return;
10475
- }
10476
10614
  const orgNodeCount = (newNode == null ? void 0 : newNode.count) || 0;
10477
10615
  const inserted = super.add(newNode);
10478
10616
  if (inserted) {