data-structure-typed 2.2.1 → 2.2.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 (83) hide show
  1. package/CHANGELOG.md +3 -1
  2. package/README.md +440 -1190
  3. package/README_CN.md +509 -0
  4. package/SECURITY.md +962 -11
  5. package/SECURITY.zh-CN.md +966 -0
  6. package/SPECIFICATION.md +689 -30
  7. package/SPECIFICATION.zh-CN.md +715 -0
  8. package/SPONSOR.zh-CN.md +62 -0
  9. package/SPONSOR_POLISHED.md +62 -0
  10. package/benchmark/report.html +1 -1
  11. package/benchmark/report.json +215 -172
  12. package/dist/cjs/index.cjs +163 -0
  13. package/dist/cjs/index.cjs.map +1 -1
  14. package/dist/cjs-legacy/index.cjs +164 -0
  15. package/dist/cjs-legacy/index.cjs.map +1 -1
  16. package/dist/esm/index.mjs +163 -0
  17. package/dist/esm/index.mjs.map +1 -1
  18. package/dist/esm-legacy/index.mjs +164 -0
  19. package/dist/esm-legacy/index.mjs.map +1 -1
  20. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +96 -2
  21. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +103 -7
  22. package/dist/types/data-structures/binary-tree/bst.d.ts +156 -13
  23. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +84 -35
  24. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
  25. package/dist/types/data-structures/graph/directed-graph.d.ts +126 -1
  26. package/dist/types/data-structures/graph/undirected-graph.d.ts +160 -1
  27. package/dist/types/data-structures/hash/hash-map.d.ts +110 -27
  28. package/dist/types/data-structures/heap/heap.d.ts +107 -58
  29. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +72 -404
  30. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -5
  31. package/dist/types/data-structures/queue/deque.d.ts +95 -67
  32. package/dist/types/data-structures/queue/queue.d.ts +90 -34
  33. package/dist/types/data-structures/stack/stack.d.ts +58 -40
  34. package/dist/types/data-structures/trie/trie.d.ts +109 -47
  35. package/dist/types/interfaces/binary-tree.d.ts +1 -0
  36. package/dist/umd/data-structure-typed.js +164 -0
  37. package/dist/umd/data-structure-typed.js.map +1 -1
  38. package/dist/umd/data-structure-typed.min.js +3 -3
  39. package/dist/umd/data-structure-typed.min.js.map +1 -1
  40. package/package.json +3 -2
  41. package/src/data-structures/binary-tree/avl-tree.ts +96 -2
  42. package/src/data-structures/binary-tree/binary-tree.ts +117 -7
  43. package/src/data-structures/binary-tree/bst.ts +322 -13
  44. package/src/data-structures/binary-tree/red-black-tree.ts +84 -35
  45. package/src/data-structures/binary-tree/tree-multi-map.ts +2 -2
  46. package/src/data-structures/graph/directed-graph.ts +126 -1
  47. package/src/data-structures/graph/undirected-graph.ts +160 -1
  48. package/src/data-structures/hash/hash-map.ts +110 -27
  49. package/src/data-structures/heap/heap.ts +107 -58
  50. package/src/data-structures/linked-list/doubly-linked-list.ts +72 -404
  51. package/src/data-structures/linked-list/singly-linked-list.ts +121 -5
  52. package/src/data-structures/queue/deque.ts +95 -67
  53. package/src/data-structures/queue/queue.ts +90 -34
  54. package/src/data-structures/stack/stack.ts +58 -40
  55. package/src/data-structures/trie/trie.ts +109 -47
  56. package/src/interfaces/binary-tree.ts +2 -0
  57. package/test/performance/benchmark-runner.ts +14 -11
  58. package/test/performance/data-structures/binary-tree/avl-tree.test.ts +8 -8
  59. package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +8 -8
  60. package/test/performance/data-structures/binary-tree/binary-tree.test.ts +6 -6
  61. package/test/performance/data-structures/binary-tree/bst.test.ts +5 -5
  62. package/test/performance/data-structures/binary-tree/red-black-tree.test.ts +10 -10
  63. package/test/performance/reportor.ts +2 -1
  64. package/test/performance/single-suite-runner.ts +7 -4
  65. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +117 -0
  66. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +166 -0
  67. package/test/unit/data-structures/binary-tree/bst.test.ts +766 -8
  68. package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +89 -37
  69. package/test/unit/data-structures/graph/directed-graph.test.ts +133 -0
  70. package/test/unit/data-structures/graph/undirected-graph.test.ts +167 -0
  71. package/test/unit/data-structures/hash/hash-map.test.ts +149 -3
  72. package/test/unit/data-structures/heap/heap.test.ts +182 -47
  73. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +118 -14
  74. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +121 -0
  75. package/test/unit/data-structures/queue/deque.test.ts +98 -67
  76. package/test/unit/data-structures/queue/queue.test.ts +85 -51
  77. package/test/unit/data-structures/stack/stack.test.ts +142 -33
  78. package/test/unit/data-structures/trie/trie.test.ts +135 -39
  79. package/tsup.leetcode.config.js +99 -0
  80. package/typedoc.json +2 -1
  81. package/POSTS_zh-CN.md +0 -54
  82. package/README_zh-CN.md +0 -1208
  83. package/SPECIFICATION_zh-CN.md +0 -81
@@ -7264,6 +7264,17 @@ var BinaryTree = class extends IterableEntryBase {
7264
7264
  }
7265
7265
  return false;
7266
7266
  }
7267
+ /**
7268
+ * Adds or updates a new node to the tree.
7269
+ * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation adds the node at the first available position in a level-order (BFS) traversal. This is NOT a Binary Search Tree insertion. Time O(N), where N is the number of nodes. It must traverse level-by-level to find an empty slot. Space O(N) in the worst case for the BFS queue (e.g., a full last level).
7270
+ *
7271
+ * @param keyNodeOrEntry - The key, node, or entry to add or update.
7272
+ * @param [value] - The value, if providing just a key.
7273
+ * @returns True if the addition was successful, false otherwise.
7274
+ */
7275
+ set(keyNodeOrEntry, value) {
7276
+ return this.add(keyNodeOrEntry, value);
7277
+ }
7267
7278
  /**
7268
7279
  * Adds multiple items to the tree.
7269
7280
  * @remarks Time O(N * M), where N is the number of items to add and M is the size of the tree at insertion (due to O(M) `add` operation). Space O(M) (from `add`) + O(N) (for the `inserted` array).
@@ -7291,6 +7302,17 @@ var BinaryTree = class extends IterableEntryBase {
7291
7302
  }
7292
7303
  return inserted;
7293
7304
  }
7305
+ /**
7306
+ * Adds or updates multiple items to the tree.
7307
+ * @remarks Time O(N * M), where N is the number of items to add and M is the size of the tree at insertion (due to O(M) `add` operation). Space O(M) (from `add`) + O(N) (for the `inserted` array).
7308
+ *
7309
+ * @param keysNodesEntriesOrRaws - An iterable of items to add or update.
7310
+ * @param [values] - An optional parallel iterable of values.
7311
+ * @returns An array of booleans indicating the success of each individual `add` operation.
7312
+ */
7313
+ setMany(keysNodesEntriesOrRaws, values) {
7314
+ return this.addMany(keysNodesEntriesOrRaws, values);
7315
+ }
7294
7316
  /**
7295
7317
  * Merges another tree into this one by adding all its nodes.
7296
7318
  * @remarks Time O(N * M), same as `addMany`, where N is the size of `anotherTree` and M is the size of this tree. Space O(M) (from `add`).
@@ -8989,6 +9011,32 @@ var BST = class extends BinaryTree {
8989
9011
  else _iterate();
8990
9012
  return inserted;
8991
9013
  }
9014
+ /**
9015
+ * Returns the first node with a key greater than or equal to the given key.
9016
+ * This is equivalent to C++ std::lower_bound on a BST.
9017
+ * Supports RECURSIVE and ITERATIVE implementations.
9018
+ * Time Complexity: O(log n) on average, O(h) where h is tree height.
9019
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
9020
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9021
+ * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
9022
+ * @returns The first node with key >= given key, or undefined if no such node exists.
9023
+ */
9024
+ lowerBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9025
+ return this._bound(keyNodeEntryOrPredicate, true, iterationType);
9026
+ }
9027
+ /**
9028
+ * Returns the first node with a key strictly greater than the given key.
9029
+ * This is equivalent to C++ std::upper_bound on a BST.
9030
+ * Supports RECURSIVE and ITERATIVE implementations.
9031
+ * Time Complexity: O(log n) on average, O(h) where h is tree height.
9032
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
9033
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9034
+ * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
9035
+ * @returns The first node with key > given key, or undefined if no such node exists.
9036
+ */
9037
+ upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9038
+ return this._bound(keyNodeEntryOrPredicate, false, iterationType);
9039
+ }
8992
9040
  /**
8993
9041
  * Traverses the tree and returns nodes that are lesser or greater than a target node.
8994
9042
  * @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
@@ -9149,6 +9197,121 @@ var BST = class extends BinaryTree {
9149
9197
  }
9150
9198
  return false;
9151
9199
  }
9200
+ /**
9201
+ * (Protected) Core bound search implementation supporting all parameter types.
9202
+ * Unified logic for both lowerBound and upperBound.
9203
+ * Resolves various input types (Key, Node, Entry, Predicate) using parent class utilities.
9204
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9205
+ * @param isLower - True for lowerBound (>=), false for upperBound (>).
9206
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9207
+ * @returns The first matching node, or undefined if no such node exists.
9208
+ */
9209
+ _bound(keyNodeEntryOrPredicate, isLower, iterationType) {
9210
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
9211
+ return void 0;
9212
+ }
9213
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
9214
+ return this._boundByPredicate(keyNodeEntryOrPredicate, iterationType);
9215
+ }
9216
+ let targetKey;
9217
+ if (this.isNode(keyNodeEntryOrPredicate)) {
9218
+ targetKey = keyNodeEntryOrPredicate.key;
9219
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
9220
+ const key = keyNodeEntryOrPredicate[0];
9221
+ if (key === null || key === void 0) {
9222
+ return void 0;
9223
+ }
9224
+ targetKey = key;
9225
+ } else {
9226
+ targetKey = keyNodeEntryOrPredicate;
9227
+ }
9228
+ if (targetKey !== void 0) {
9229
+ return this._boundByKey(targetKey, isLower, iterationType);
9230
+ }
9231
+ return void 0;
9232
+ }
9233
+ /**
9234
+ * (Protected) Binary search for bound by key with pruning optimization.
9235
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
9236
+ * For lowerBound: finds first node where key >= target.
9237
+ * For upperBound: finds first node where key > target.
9238
+ * @param key - The target key to search for.
9239
+ * @param isLower - True for lowerBound (>=), false for upperBound (>).
9240
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9241
+ * @returns The first node matching the bound condition, or undefined if none exists.
9242
+ */
9243
+ _boundByKey(key, isLower, iterationType) {
9244
+ if (iterationType === "RECURSIVE") {
9245
+ const dfs = /* @__PURE__ */ __name((cur) => {
9246
+ if (!this.isRealNode(cur)) return void 0;
9247
+ const cmp = this.comparator(cur.key, key);
9248
+ const condition = isLower ? cmp >= 0 : cmp > 0;
9249
+ if (condition) {
9250
+ const leftResult = dfs(cur.left);
9251
+ return leftResult ?? cur;
9252
+ } else {
9253
+ return dfs(cur.right);
9254
+ }
9255
+ }, "dfs");
9256
+ return dfs(this.root);
9257
+ } else {
9258
+ let current = this.root;
9259
+ let result = void 0;
9260
+ while (this.isRealNode(current)) {
9261
+ const cmp = this.comparator(current.key, key);
9262
+ const condition = isLower ? cmp >= 0 : cmp > 0;
9263
+ if (condition) {
9264
+ result = current;
9265
+ current = current.left ?? void 0;
9266
+ } else {
9267
+ current = current.right ?? void 0;
9268
+ }
9269
+ }
9270
+ return result;
9271
+ }
9272
+ }
9273
+ /**
9274
+ * (Protected) In-order traversal search by predicate.
9275
+ * Falls back to linear in-order traversal when predicate-based search is required.
9276
+ * Returns the first node that satisfies the predicate function.
9277
+ * Note: Predicate-based search cannot leverage BST's binary search optimization.
9278
+ * Time Complexity: O(n) since it may visit every node.
9279
+ * @param predicate - The predicate function to test nodes.
9280
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9281
+ * @returns The first node satisfying predicate, or undefined if none found.
9282
+ */
9283
+ _boundByPredicate(predicate, iterationType) {
9284
+ if (iterationType === "RECURSIVE") {
9285
+ let result = void 0;
9286
+ const dfs = /* @__PURE__ */ __name((cur) => {
9287
+ if (result || !this.isRealNode(cur)) return;
9288
+ if (this.isRealNode(cur.left)) dfs(cur.left);
9289
+ if (!result && predicate(cur)) {
9290
+ result = cur;
9291
+ }
9292
+ if (!result && this.isRealNode(cur.right)) dfs(cur.right);
9293
+ }, "dfs");
9294
+ dfs(this.root);
9295
+ return result;
9296
+ } else {
9297
+ const stack = [];
9298
+ let current = this.root;
9299
+ while (stack.length > 0 || this.isRealNode(current)) {
9300
+ if (this.isRealNode(current)) {
9301
+ stack.push(current);
9302
+ current = current.left;
9303
+ } else {
9304
+ const node = stack.pop();
9305
+ if (!this.isRealNode(node)) break;
9306
+ if (predicate(node)) {
9307
+ return node;
9308
+ }
9309
+ current = node.right;
9310
+ }
9311
+ }
9312
+ return void 0;
9313
+ }
9314
+ }
9152
9315
  /**
9153
9316
  * (Protected) Creates a new, empty instance of the same BST constructor.
9154
9317
  * @remarks Time O(1)