data-structure-typed 1.47.9 → 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 (36) 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/mjs/data-structures/binary-tree/avl-tree.d.ts +6 -0
  19. package/dist/mjs/data-structures/binary-tree/avl-tree.js +8 -0
  20. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +14 -0
  21. package/dist/mjs/data-structures/binary-tree/binary-tree.js +52 -28
  22. package/dist/mjs/data-structures/binary-tree/bst.d.ts +13 -0
  23. package/dist/mjs/data-structures/binary-tree/bst.js +41 -21
  24. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +16 -0
  25. package/dist/mjs/data-structures/binary-tree/rb-tree.js +54 -31
  26. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +16 -0
  27. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +44 -21
  28. package/dist/umd/data-structure-typed.js +186 -89
  29. package/dist/umd/data-structure-typed.min.js +2 -2
  30. package/dist/umd/data-structure-typed.min.js.map +1 -1
  31. package/package.json +1 -1
  32. package/src/data-structures/binary-tree/avl-tree.ts +9 -0
  33. package/src/data-structures/binary-tree/binary-tree.ts +47 -23
  34. package/src/data-structures/binary-tree/bst.ts +38 -19
  35. package/src/data-structures/binary-tree/rb-tree.ts +55 -31
  36. package/src/data-structures/binary-tree/tree-multimap.ts +42 -17
@@ -6981,6 +6981,45 @@ var dataStructureTyped = (() => {
6981
6981
  createTree(options) {
6982
6982
  return new _BinaryTree([], __spreadValues({ iterationType: this.iterationType }, options));
6983
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
+ }
6984
7023
  /**
6985
7024
  * The function checks if a given value is an entry in a binary tree node.
6986
7025
  * @param kne - BTNodeExemplar<V, N> - A generic type representing a node in a binary tree. It has
@@ -7003,16 +7042,19 @@ var dataStructureTyped = (() => {
7003
7042
  * @returns The function `add` returns the inserted node (`N`), `null`, or `undefined`.
7004
7043
  */
7005
7044
  add(keyOrNodeOrEntry) {
7006
- let inserted, needInsert;
7007
- 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) => {
7008
7050
  const queue = new Queue([root]);
7009
7051
  while (queue.size > 0) {
7010
7052
  const cur = queue.shift();
7011
- if (newNode && cur.key === newNode.key) {
7012
- this._replaceNode(cur, newNode);
7013
- return newNode;
7053
+ if (newNode2 && cur.key === newNode2.key) {
7054
+ this._replaceNode(cur, newNode2);
7055
+ return newNode2;
7014
7056
  }
7015
- const inserted2 = this._addTo(newNode, cur);
7057
+ const inserted2 = this._addTo(newNode2, cur);
7016
7058
  if (inserted2 !== void 0)
7017
7059
  return inserted2;
7018
7060
  if (cur.left)
@@ -7021,29 +7063,11 @@ var dataStructureTyped = (() => {
7021
7063
  queue.push(cur.right);
7022
7064
  }
7023
7065
  };
7024
- if (keyOrNodeOrEntry === null) {
7025
- needInsert = null;
7026
- } else if (this.isNodeKey(keyOrNodeOrEntry)) {
7027
- needInsert = this.createNode(keyOrNodeOrEntry);
7028
- } else if (keyOrNodeOrEntry instanceof BinaryTreeNode) {
7029
- needInsert = keyOrNodeOrEntry;
7030
- } else if (this.isEntry(keyOrNodeOrEntry)) {
7031
- const [key, value] = keyOrNodeOrEntry;
7032
- if (key === void 0) {
7033
- return;
7034
- } else if (key === null) {
7035
- needInsert = null;
7036
- } else {
7037
- needInsert = this.createNode(key, value);
7038
- }
7039
- } else {
7040
- return;
7041
- }
7042
7066
  if (this.root) {
7043
- inserted = _bfs(this.root, needInsert);
7067
+ inserted = _bfs(this.root, newNode);
7044
7068
  } else {
7045
- this._setRoot(needInsert);
7046
- if (needInsert) {
7069
+ this._setRoot(newNode);
7070
+ if (newNode) {
7047
7071
  this._size = 1;
7048
7072
  } else {
7049
7073
  this._size = 0;
@@ -8549,6 +8573,40 @@ var dataStructureTyped = (() => {
8549
8573
  comparator: this.comparator
8550
8574
  }, options));
8551
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
+ }
8552
8610
  /**
8553
8611
  * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
8554
8612
  * Space Complexity: O(1) - Constant space is used.
@@ -8564,24 +8622,9 @@ var dataStructureTyped = (() => {
8564
8622
  * (`keyOrNodeOrEntry`) is null, undefined, or does not match any of the expected types.
8565
8623
  */
8566
8624
  add(keyOrNodeOrEntry) {
8567
- if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === void 0) {
8568
- return void 0;
8569
- }
8570
- let newNode;
8571
- if (keyOrNodeOrEntry instanceof BSTNode) {
8572
- newNode = keyOrNodeOrEntry;
8573
- } else if (this.isNodeKey(keyOrNodeOrEntry)) {
8574
- newNode = this.createNode(keyOrNodeOrEntry);
8575
- } else if (this.isEntry(keyOrNodeOrEntry)) {
8576
- const [key, value] = keyOrNodeOrEntry;
8577
- if (key === void 0 || key === null) {
8578
- return;
8579
- } else {
8580
- newNode = this.createNode(key, value);
8581
- }
8582
- } else {
8625
+ const newNode = this.exemplarToNode(keyOrNodeOrEntry);
8626
+ if (newNode === void 0)
8583
8627
  return;
8584
- }
8585
8628
  if (this.root === void 0) {
8586
8629
  this._setRoot(newNode);
8587
8630
  this._size++;
@@ -9545,6 +9588,14 @@ var dataStructureTyped = (() => {
9545
9588
  comparator: this.comparator
9546
9589
  }, options));
9547
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
+ }
9548
9599
  /**
9549
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.
9550
9601
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
@@ -9966,70 +10017,93 @@ var dataStructureTyped = (() => {
9966
10017
  }, options));
9967
10018
  }
9968
10019
  /**
9969
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
9970
- * 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.
9971
10024
  */
10025
+ isNode(exemplar) {
10026
+ return exemplar instanceof RedBlackTreeNode;
10027
+ }
9972
10028
  /**
9973
- * The function adds a node to a Red-Black Tree data structure.
9974
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
9975
- * @returns The method `add` returns either an instance of `N` (the node that was added) or
9976
- * `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`.
9977
10035
  */
9978
- add(keyOrNodeOrEntry) {
10036
+ exemplarToNode(exemplar) {
9979
10037
  let node;
9980
- if (this.isNodeKey(keyOrNodeOrEntry)) {
9981
- node = this.createNode(keyOrNodeOrEntry, void 0, 1 /* RED */);
9982
- } else if (keyOrNodeOrEntry instanceof RedBlackTreeNode) {
9983
- node = keyOrNodeOrEntry;
9984
- } else if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === void 0) {
10038
+ if (exemplar === null || exemplar === void 0) {
9985
10039
  return;
9986
- } else if (this.isEntry(keyOrNodeOrEntry)) {
9987
- const [key, value] = keyOrNodeOrEntry;
10040
+ } else if (this.isNode(exemplar)) {
10041
+ node = exemplar;
10042
+ } else if (this.isEntry(exemplar)) {
10043
+ const [key, value] = exemplar;
9988
10044
  if (key === void 0 || key === null) {
9989
10045
  return;
9990
10046
  } else {
9991
10047
  node = this.createNode(key, value, 1 /* RED */);
9992
10048
  }
10049
+ } else if (this.isNodeKey(exemplar)) {
10050
+ node = this.createNode(exemplar, void 0, 1 /* RED */);
9993
10051
  } else {
9994
10052
  return;
9995
10053
  }
9996
- node.left = this.Sentinel;
9997
- 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;
9998
10072
  let y = void 0;
9999
10073
  let x = this.root;
10000
10074
  while (x !== this.Sentinel) {
10001
10075
  y = x;
10002
10076
  if (x) {
10003
- if (node.key < x.key) {
10077
+ if (newNode.key < x.key) {
10004
10078
  x = x.left;
10005
- } else if (node.key > x.key) {
10079
+ } else if (newNode.key > x.key) {
10006
10080
  x = x == null ? void 0 : x.right;
10007
10081
  } else {
10008
- if (node !== x) {
10009
- this._replaceNode(x, node);
10082
+ if (newNode !== x) {
10083
+ this._replaceNode(x, newNode);
10010
10084
  }
10011
10085
  return;
10012
10086
  }
10013
10087
  }
10014
10088
  }
10015
- node.parent = y;
10089
+ newNode.parent = y;
10016
10090
  if (y === void 0) {
10017
- this._setRoot(node);
10018
- } else if (node.key < y.key) {
10019
- y.left = node;
10091
+ this._setRoot(newNode);
10092
+ } else if (newNode.key < y.key) {
10093
+ y.left = newNode;
10020
10094
  } else {
10021
- y.right = node;
10095
+ y.right = newNode;
10022
10096
  }
10023
- if (node.parent === void 0) {
10024
- node.color = 0 /* BLACK */;
10097
+ if (newNode.parent === void 0) {
10098
+ newNode.color = 0 /* BLACK */;
10025
10099
  this._size++;
10026
10100
  return;
10027
10101
  }
10028
- if (node.parent.parent === void 0) {
10102
+ if (newNode.parent.parent === void 0) {
10029
10103
  this._size++;
10030
10104
  return;
10031
10105
  }
10032
- this._fixInsert(node);
10106
+ this._fixInsert(newNode);
10033
10107
  this._size++;
10034
10108
  }
10035
10109
  /**
@@ -10480,6 +10554,43 @@ var dataStructureTyped = (() => {
10480
10554
  comparator: this.comparator
10481
10555
  }, options));
10482
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
+ }
10483
10594
  /**
10484
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.
10485
10596
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
@@ -10497,23 +10608,9 @@ var dataStructureTyped = (() => {
10497
10608
  * @returns either a node (`N`) or `undefined`.
10498
10609
  */
10499
10610
  add(keyOrNodeOrEntry, count = 1) {
10500
- let newNode;
10501
- if (keyOrNodeOrEntry === void 0 || keyOrNodeOrEntry === null) {
10502
- return;
10503
- } else if (keyOrNodeOrEntry instanceof TreeMultimapNode) {
10504
- newNode = keyOrNodeOrEntry;
10505
- } else if (this.isNodeKey(keyOrNodeOrEntry)) {
10506
- newNode = this.createNode(keyOrNodeOrEntry, void 0, count);
10507
- } else if (this.isEntry(keyOrNodeOrEntry)) {
10508
- const [key, value] = keyOrNodeOrEntry;
10509
- if (key === void 0 || key === null) {
10510
- return;
10511
- } else {
10512
- newNode = this.createNode(key, value, count);
10513
- }
10514
- } else {
10611
+ const newNode = this.exemplarToNode(keyOrNodeOrEntry, count);
10612
+ if (newNode === void 0)
10515
10613
  return;
10516
- }
10517
10614
  const orgNodeCount = (newNode == null ? void 0 : newNode.count) || 0;
10518
10615
  const inserted = super.add(newNode);
10519
10616
  if (inserted) {