data-structure-typed 1.51.1 → 1.51.3

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 (30) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +208 -185
  3. package/benchmark/report.html +13 -13
  4. package/benchmark/report.json +158 -146
  5. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +16 -10
  6. package/dist/cjs/data-structures/binary-tree/binary-tree.js +31 -25
  7. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/bst.d.ts +27 -1
  9. package/dist/cjs/data-structures/binary-tree/bst.js +29 -0
  10. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +1 -47
  12. package/dist/cjs/data-structures/binary-tree/rb-tree.js +6 -61
  13. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  14. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +16 -10
  15. package/dist/mjs/data-structures/binary-tree/binary-tree.js +31 -25
  16. package/dist/mjs/data-structures/binary-tree/bst.d.ts +27 -1
  17. package/dist/mjs/data-structures/binary-tree/bst.js +28 -0
  18. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +1 -47
  19. package/dist/mjs/data-structures/binary-tree/rb-tree.js +6 -60
  20. package/dist/umd/data-structure-typed.js +66 -86
  21. package/dist/umd/data-structure-typed.min.js +2 -2
  22. package/dist/umd/data-structure-typed.min.js.map +1 -1
  23. package/package.json +92 -69
  24. package/src/data-structures/binary-tree/binary-tree.ts +33 -28
  25. package/src/data-structures/binary-tree/bst.ts +36 -1
  26. package/src/data-structures/binary-tree/rb-tree.ts +6 -72
  27. package/test/performance/data-structures/binary-tree/avl-tree.test.ts +4 -0
  28. package/test/performance/data-structures/binary-tree/rb-tree.test.ts +4 -0
  29. package/test/unit/data-structures/binary-tree/overall.test.ts +2 -2
  30. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +40 -40
@@ -7740,6 +7740,7 @@ var dataStructureTyped = (() => {
7740
7740
  __publicField(this, "_extractor", (key) => typeof key === "number" ? key : Number(key));
7741
7741
  __publicField(this, "_root");
7742
7742
  __publicField(this, "_size");
7743
+ __publicField(this, "_NIL", new BinaryTreeNode(NaN));
7743
7744
  __publicField(this, "_DEFAULT_CALLBACK", (node) => node ? node.key : void 0);
7744
7745
  if (options) {
7745
7746
  const { iterationType, extractor } = options;
@@ -7774,6 +7775,13 @@ var dataStructureTyped = (() => {
7774
7775
  get size() {
7775
7776
  return this._size;
7776
7777
  }
7778
+ /**
7779
+ * The function returns the value of the _NIL property.
7780
+ * @returns The method is returning the value of the `_NIL` property.
7781
+ */
7782
+ get NIL() {
7783
+ return this._NIL;
7784
+ }
7777
7785
  /**
7778
7786
  * Creates a new instance of BinaryTreeNode with the given key and value.
7779
7787
  * @param {K} key - The key for the new node.
@@ -7860,6 +7868,14 @@ var dataStructureTyped = (() => {
7860
7868
  return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
7861
7869
  }
7862
7870
  }
7871
+ /**
7872
+ * The function checks if a given node is a real node or null.
7873
+ * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
7874
+ * @returns a boolean value.
7875
+ */
7876
+ isNodeOrNull(node) {
7877
+ return this.isRealNode(node) || node === null;
7878
+ }
7863
7879
  /**
7864
7880
  * The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
7865
7881
  * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,NODE>`.
@@ -7868,15 +7884,6 @@ var dataStructureTyped = (() => {
7868
7884
  isNode(keyOrNodeOrEntry) {
7869
7885
  return keyOrNodeOrEntry instanceof BinaryTreeNode;
7870
7886
  }
7871
- /**
7872
- * The function checks if a given value is an entry in a binary tree node.
7873
- * @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,NODE> - A generic type representing a node in a binary tree. It has
7874
- * two type parameters V and NODE, representing the value and node type respectively.
7875
- * @returns a boolean value.
7876
- */
7877
- isEntry(keyOrNodeOrEntry) {
7878
- return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
7879
- }
7880
7887
  /**
7881
7888
  * The function checks if a given node is a real node by verifying if it is an instance of
7882
7889
  * BinaryTreeNode and its key is not NaN.
@@ -7884,7 +7891,9 @@ var dataStructureTyped = (() => {
7884
7891
  * @returns a boolean value.
7885
7892
  */
7886
7893
  isRealNode(node) {
7887
- return node instanceof BinaryTreeNode && String(node.key) !== "NaN";
7894
+ if (!this.isNode(node))
7895
+ return false;
7896
+ return node !== this.NIL;
7888
7897
  }
7889
7898
  /**
7890
7899
  * The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
@@ -7892,15 +7901,16 @@ var dataStructureTyped = (() => {
7892
7901
  * @returns a boolean value.
7893
7902
  */
7894
7903
  isNIL(node) {
7895
- return node instanceof BinaryTreeNode && String(node.key) === "NaN";
7904
+ return node === this.NIL;
7896
7905
  }
7897
7906
  /**
7898
- * The function checks if a given node is a real node or null.
7899
- * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
7907
+ * The function checks if a given value is an entry in a binary tree node.
7908
+ * @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,NODE> - A generic type representing a node in a binary tree. It has
7909
+ * two type parameters V and NODE, representing the value and node type respectively.
7900
7910
  * @returns a boolean value.
7901
7911
  */
7902
- isNodeOrNull(node) {
7903
- return this.isRealNode(node) || node === null;
7912
+ isEntry(keyOrNodeOrEntry) {
7913
+ return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
7904
7914
  }
7905
7915
  /**
7906
7916
  * Time Complexity O(n)
@@ -8116,24 +8126,24 @@ var dataStructureTyped = (() => {
8116
8126
  if (onlyOne)
8117
8127
  return;
8118
8128
  }
8119
- if (!cur.left && !cur.right)
8129
+ if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
8120
8130
  return;
8121
- cur.left && dfs(cur.left);
8122
- cur.right && dfs(cur.right);
8131
+ this.isRealNode(cur.left) && dfs(cur.left);
8132
+ this.isRealNode(cur.right) && dfs(cur.right);
8123
8133
  };
8124
8134
  dfs(beginRoot);
8125
8135
  } else {
8126
8136
  const stack = [beginRoot];
8127
8137
  while (stack.length > 0) {
8128
8138
  const cur = stack.pop();
8129
- if (cur) {
8139
+ if (this.isRealNode(cur)) {
8130
8140
  if (callback(cur) === identifier) {
8131
8141
  ans.push(cur);
8132
8142
  if (onlyOne)
8133
8143
  return ans;
8134
8144
  }
8135
- cur.left && stack.push(cur.left);
8136
- cur.right && stack.push(cur.right);
8145
+ this.isRealNode(cur.left) && stack.push(cur.left);
8146
+ this.isRealNode(cur.right) && stack.push(cur.right);
8137
8147
  }
8138
8148
  }
8139
8149
  }
@@ -8166,8 +8176,6 @@ var dataStructureTyped = (() => {
8166
8176
  */
8167
8177
  getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
8168
8178
  var _a;
8169
- if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
8170
- callback = (node) => node;
8171
8179
  return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) != null ? _a : null;
8172
8180
  }
8173
8181
  /**
@@ -8244,8 +8252,6 @@ var dataStructureTyped = (() => {
8244
8252
  */
8245
8253
  get(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
8246
8254
  var _a, _b;
8247
- if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
8248
- callback = (node) => node;
8249
8255
  return (_b = (_a = this.getNode(identifier, callback, beginRoot, iterationType)) == null ? void 0 : _a.value) != null ? _b : void 0;
8250
8256
  }
8251
8257
  /**
@@ -9779,6 +9785,35 @@ var dataStructureTyped = (() => {
9779
9785
  }
9780
9786
  return ans;
9781
9787
  }
9788
+ /**
9789
+ * Time Complexity: O(log n)
9790
+ * Space Complexity: O(1)
9791
+ */
9792
+ /**
9793
+ * Time Complexity: O(log n)
9794
+ * Space Complexity: O(1)
9795
+ *
9796
+ * The `getNode` function retrieves a node from a Red-Black Tree based on the provided identifier and
9797
+ * callback function.
9798
+ * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value or key
9799
+ * that you want to search for in the binary search tree. It can be of any type that is compatible
9800
+ * with the type of nodes in the tree.
9801
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
9802
+ * the tree. It is used to determine whether a node matches the given identifier. The `callback`
9803
+ * function should take a node as its parameter and return a value that can be compared to the
9804
+ * `identifier` parameter.
9805
+ * @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
9806
+ * search tree. It can be either a key or a node. If it is a key, it will be converted to a node
9807
+ * using the `ensureNode` method. If it is not provided, the `root`
9808
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
9809
+ * be performed when searching for nodes in the binary search tree. It is an optional parameter and
9810
+ * its default value is taken from the `iterationType` property of the class.
9811
+ * @returns The method is returning a value of type `NODE | null | undefined`.
9812
+ */
9813
+ getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
9814
+ var _a;
9815
+ return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) != null ? _a : void 0;
9816
+ }
9782
9817
  /**
9783
9818
  * Time complexity: O(n)
9784
9819
  * Space complexity: O(n)
@@ -11178,20 +11213,12 @@ var dataStructureTyped = (() => {
11178
11213
  */
11179
11214
  constructor(keysOrNodesOrEntries = [], options) {
11180
11215
  super([], options);
11181
- __publicField(this, "_SENTINEL", new RedBlackTreeNode(NaN));
11182
11216
  __publicField(this, "_root");
11183
- this._root = this.SENTINEL;
11217
+ this._root = this.NIL;
11184
11218
  if (keysOrNodesOrEntries) {
11185
11219
  this.addMany(keysOrNodesOrEntries);
11186
11220
  }
11187
11221
  }
11188
- /**
11189
- * The function returns the value of the _SENTINEL property.
11190
- * @returns The method is returning the value of the `_SENTINEL` property.
11191
- */
11192
- get SENTINEL() {
11193
- return this._SENTINEL;
11194
- }
11195
11222
  /**
11196
11223
  * The function returns the root node of a tree or undefined if there is no root.
11197
11224
  * @returns The root node of the tree structure, or undefined if there is no root node.
@@ -11276,53 +11303,6 @@ var dataStructureTyped = (() => {
11276
11303
  isNode(keyOrNodeOrEntry) {
11277
11304
  return keyOrNodeOrEntry instanceof RedBlackTreeNode;
11278
11305
  }
11279
- /**
11280
- * Time Complexity: O(1)
11281
- * Space Complexity: O(1)
11282
- */
11283
- /**
11284
- * Time Complexity: O(1)
11285
- * Space Complexity: O(1)
11286
- *
11287
- * The function checks if a given node is a real node in a Red-Black Tree.
11288
- * @param {NODE | undefined} node - The `node` parameter is of type `NODE | undefined`, which means
11289
- * it can either be of type `NODE` or `undefined`.
11290
- * @returns a boolean value.
11291
- */
11292
- isRealNode(node) {
11293
- if (node === this.SENTINEL || node === void 0)
11294
- return false;
11295
- return node instanceof RedBlackTreeNode;
11296
- }
11297
- /**
11298
- * Time Complexity: O(log n)
11299
- * Space Complexity: O(1)
11300
- */
11301
- /**
11302
- * Time Complexity: O(log n)
11303
- * Space Complexity: O(1)
11304
- *
11305
- * The `getNode` function retrieves a node from a Red-Black Tree based on the provided identifier and
11306
- * callback function.
11307
- * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value or key
11308
- * that you want to search for in the binary search tree. It can be of any type that is compatible
11309
- * with the type of nodes in the tree.
11310
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
11311
- * the tree. It is used to determine whether a node matches the given identifier. The `callback`
11312
- * function should take a node as its parameter and return a value that can be compared to the
11313
- * `identifier` parameter.
11314
- * @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
11315
- * search tree. It can be either a key or a node. If it is a key, it will be converted to a node
11316
- * using the `ensureNode` method. If it is not provided, the `root`
11317
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
11318
- * be performed when searching for nodes in the binary search tree. It is an optional parameter and
11319
- * its default value is taken from the `iterationType` property of the class.
11320
- * @returns The method is returning a value of type `NODE | null | undefined`.
11321
- */
11322
- getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
11323
- var _a;
11324
- return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) != null ? _a : void 0;
11325
- }
11326
11306
  /**
11327
11307
  * Time Complexity: O(1)
11328
11308
  * Space Complexity: O(1)
@@ -11336,7 +11316,7 @@ var dataStructureTyped = (() => {
11336
11316
  */
11337
11317
  clear() {
11338
11318
  super.clear();
11339
- this._root = this.SENTINEL;
11319
+ this._root = this.NIL;
11340
11320
  }
11341
11321
  /**
11342
11322
  * Time Complexity: O(log n)
@@ -11491,9 +11471,9 @@ var dataStructureTyped = (() => {
11491
11471
  while (this.isRealNode(current)) {
11492
11472
  parent = current;
11493
11473
  if (node.key < current.key) {
11494
- current = (_a = current.left) != null ? _a : this.SENTINEL;
11474
+ current = (_a = current.left) != null ? _a : this.NIL;
11495
11475
  } else if (node.key > current.key) {
11496
- current = (_b = current.right) != null ? _b : this.SENTINEL;
11476
+ current = (_b = current.right) != null ? _b : this.NIL;
11497
11477
  } else {
11498
11478
  this._replaceNode(current, node);
11499
11479
  return "UPDATED";
@@ -11507,8 +11487,8 @@ var dataStructureTyped = (() => {
11507
11487
  } else {
11508
11488
  parent.right = node;
11509
11489
  }
11510
- node.left = this.SENTINEL;
11511
- node.right = this.SENTINEL;
11490
+ node.left = this.NIL;
11491
+ node.right = this.NIL;
11512
11492
  node.color = "RED";
11513
11493
  this._insertFixup(node);
11514
11494
  return "CREATED";