heap-typed 1.52.5 → 1.52.8

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 (77) hide show
  1. package/dist/constants/index.d.ts +4 -0
  2. package/dist/constants/index.js +8 -0
  3. package/dist/data-structures/base/iterable-element-base.d.ts +8 -1
  4. package/dist/data-structures/base/iterable-element-base.js +10 -1
  5. package/dist/data-structures/base/iterable-entry-base.d.ts +8 -1
  6. package/dist/data-structures/base/iterable-entry-base.js +10 -10
  7. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +31 -32
  8. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +43 -44
  9. package/dist/data-structures/binary-tree/avl-tree.d.ts +23 -24
  10. package/dist/data-structures/binary-tree/avl-tree.js +71 -64
  11. package/dist/data-structures/binary-tree/binary-indexed-tree.js +2 -2
  12. package/dist/data-structures/binary-tree/binary-tree.d.ts +534 -402
  13. package/dist/data-structures/binary-tree/binary-tree.js +669 -598
  14. package/dist/data-structures/binary-tree/bst.d.ts +72 -65
  15. package/dist/data-structures/binary-tree/bst.js +115 -113
  16. package/dist/data-structures/binary-tree/rb-tree.d.ts +21 -24
  17. package/dist/data-structures/binary-tree/rb-tree.js +40 -39
  18. package/dist/data-structures/binary-tree/segment-tree.d.ts +2 -2
  19. package/dist/data-structures/binary-tree/segment-tree.js +2 -2
  20. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +28 -31
  21. package/dist/data-structures/binary-tree/tree-multi-map.js +44 -43
  22. package/dist/data-structures/graph/abstract-graph.d.ts +2 -2
  23. package/dist/data-structures/graph/abstract-graph.js +7 -4
  24. package/dist/data-structures/graph/directed-graph.d.ts +2 -2
  25. package/dist/data-structures/graph/directed-graph.js +4 -2
  26. package/dist/data-structures/graph/undirected-graph.d.ts +2 -2
  27. package/dist/data-structures/hash/hash-map.d.ts +2 -2
  28. package/dist/data-structures/hash/hash-map.js +1 -1
  29. package/dist/data-structures/heap/heap.js +3 -3
  30. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
  31. package/dist/data-structures/linked-list/doubly-linked-list.js +7 -7
  32. package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  33. package/dist/data-structures/linked-list/singly-linked-list.js +6 -6
  34. package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -2
  35. package/dist/data-structures/matrix/matrix.d.ts +2 -2
  36. package/dist/data-structures/matrix/navigator.d.ts +2 -2
  37. package/dist/data-structures/matrix/navigator.js +4 -2
  38. package/dist/data-structures/queue/deque.d.ts +3 -3
  39. package/dist/data-structures/queue/deque.js +29 -29
  40. package/dist/data-structures/queue/queue.d.ts +1 -1
  41. package/dist/data-structures/stack/stack.d.ts +2 -2
  42. package/dist/data-structures/trie/trie.d.ts +2 -2
  43. package/dist/data-structures/trie/trie.js +1 -1
  44. package/dist/index.d.ts +1 -0
  45. package/dist/index.js +1 -0
  46. package/dist/interfaces/binary-tree.d.ts +2 -2
  47. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -4
  48. package/dist/types/data-structures/binary-tree/binary-tree.js +0 -6
  49. package/package.json +2 -2
  50. package/src/constants/index.ts +4 -0
  51. package/src/data-structures/base/iterable-element-base.ts +11 -1
  52. package/src/data-structures/base/iterable-entry-base.ts +11 -19
  53. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +47 -50
  54. package/src/data-structures/binary-tree/avl-tree.ts +69 -71
  55. package/src/data-structures/binary-tree/binary-indexed-tree.ts +2 -2
  56. package/src/data-structures/binary-tree/binary-tree.ts +698 -726
  57. package/src/data-structures/binary-tree/bst.ts +123 -129
  58. package/src/data-structures/binary-tree/rb-tree.ts +44 -46
  59. package/src/data-structures/binary-tree/segment-tree.ts +2 -2
  60. package/src/data-structures/binary-tree/tree-multi-map.ts +48 -49
  61. package/src/data-structures/graph/abstract-graph.ts +6 -6
  62. package/src/data-structures/graph/directed-graph.ts +4 -4
  63. package/src/data-structures/graph/undirected-graph.ts +2 -2
  64. package/src/data-structures/hash/hash-map.ts +3 -3
  65. package/src/data-structures/heap/heap.ts +3 -3
  66. package/src/data-structures/linked-list/doubly-linked-list.ts +9 -9
  67. package/src/data-structures/linked-list/singly-linked-list.ts +8 -8
  68. package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
  69. package/src/data-structures/matrix/matrix.ts +2 -2
  70. package/src/data-structures/matrix/navigator.ts +4 -4
  71. package/src/data-structures/queue/deque.ts +31 -31
  72. package/src/data-structures/queue/queue.ts +1 -1
  73. package/src/data-structures/stack/stack.ts +2 -2
  74. package/src/data-structures/trie/trie.ts +3 -3
  75. package/src/index.ts +2 -1
  76. package/src/interfaces/binary-tree.ts +3 -3
  77. package/src/types/data-structures/binary-tree/binary-tree.ts +3 -5
@@ -4,8 +4,8 @@ exports.AVLTree = exports.AVLTreeNode = void 0;
4
4
  /**
5
5
  * data-structure-typed
6
6
  *
7
- * @author Tyler Zeng
8
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
7
+ * @author Pablo Zeng
8
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
9
9
  * @license MIT License
10
10
  */
11
11
  const bst_1 = require("./bst");
@@ -52,7 +52,7 @@ class AVLTree extends bst_1.BST {
52
52
  /**
53
53
  * This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
54
54
  * entries, or raw elements.
55
- * @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
55
+ * @param keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter is an
56
56
  * iterable object that can contain either keys, nodes, entries, or raw elements. These elements will
57
57
  * be used to initialize the AVLTree.
58
58
  * @param [options] - The `options` parameter is an optional object that can be used to customize the
@@ -60,10 +60,10 @@ class AVLTree extends bst_1.BST {
60
60
  * keys), `allowDuplicates` (a boolean indicating whether duplicate keys are allowed), and
61
61
  * `nodeBuilder` (
62
62
  */
63
- constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
63
+ constructor(keysOrNodesOrEntriesOrRaws = [], options) {
64
64
  super([], options);
65
- if (keysOrNodesOrEntriesOrRawElements)
66
- super.addMany(keysOrNodesOrEntriesOrRawElements);
65
+ if (keysOrNodesOrEntriesOrRaws)
66
+ super.addMany(keysOrNodesOrEntriesOrRaws);
67
67
  }
68
68
  /**
69
69
  * The function creates a new AVL tree node with the given key and value.
@@ -85,17 +85,17 @@ class AVLTree extends bst_1.BST {
85
85
  * @returns a new AVLTree object.
86
86
  */
87
87
  createTree(options) {
88
- return new AVLTree([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
88
+ return new AVLTree([], Object.assign({ iterationType: this.iterationType, comparator: this._comparator, toEntryFn: this._toEntryFn }, options));
89
89
  }
90
90
  /**
91
91
  * The function checks if the input is an instance of AVLTreeNode.
92
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
93
- * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
94
- * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
92
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
93
+ * `keyOrNodeOrEntryOrRaw` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
94
+ * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRaw` is
95
95
  * an instance of the `AVLTreeNode` class.
96
96
  */
97
- isNode(keyOrNodeOrEntryOrRawElement) {
98
- return keyOrNodeOrEntryOrRawElement instanceof AVLTreeNode;
97
+ isNode(keyOrNodeOrEntryOrRaw) {
98
+ return keyOrNodeOrEntryOrRaw instanceof AVLTreeNode;
99
99
  }
100
100
  /**
101
101
  * Time Complexity: O(log n)
@@ -103,36 +103,36 @@ class AVLTree extends bst_1.BST {
103
103
  *
104
104
  * The function overrides the add method of a class and inserts a key-value pair into a data
105
105
  * structure, then balances the path.
106
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
107
- * `keyOrNodeOrEntryOrRawElement` can accept values of type `R`, `BTNKeyOrNodeOrEntry<K, V, NODE>`, or
106
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
107
+ * `keyOrNodeOrEntryOrRaw` can accept values of type `R`, `BTNKeyOrNodeOrEntry<K, V, NODE>`, or
108
108
  * `RawElement`.
109
109
  * @param {V} [value] - The `value` parameter is an optional value that you want to associate with
110
110
  * the key or node being added to the data structure.
111
111
  * @returns The method is returning a boolean value.
112
112
  */
113
- add(keyOrNodeOrEntryOrRawElement, value) {
114
- if (keyOrNodeOrEntryOrRawElement === null)
113
+ add(keyOrNodeOrEntryOrRaw, value) {
114
+ if (keyOrNodeOrEntryOrRaw === null)
115
115
  return false;
116
- const inserted = super.add(keyOrNodeOrEntryOrRawElement, value);
116
+ const inserted = super.add(keyOrNodeOrEntryOrRaw, value);
117
117
  if (inserted)
118
- this._balancePath(keyOrNodeOrEntryOrRawElement);
118
+ this._balancePath(keyOrNodeOrEntryOrRaw);
119
119
  return inserted;
120
120
  }
121
121
  /**
122
122
  * Time Complexity: O(log n)
123
123
  * Space Complexity: O(1)
124
124
  *
125
- * The function overrides the delete method of a binary tree class and performs additional operations
126
- * to balance the tree after deletion.
127
- * @param identifier - The `identifier` parameter is the value or condition used to identify the
128
- * node(s) to be deleted from the binary tree. It can be of any type that is compatible with the
129
- * binary tree's node type.
130
- * @param {C} callback - The `callback` parameter is a function that will be used to determine if a
131
- * node should be deleted or not. It is optional and has a default value of `this._DEFAULT_CALLBACK`.
132
- * @returns The method is returning an array of BinaryTreeDeleteResult<NODE> objects.
125
+ * The function overrides the delete method in a TypeScript class, performs deletion, and then
126
+ * balances the tree if necessary.
127
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
128
+ * parameter in the `override delete` method can be one of the following types:
129
+ * @returns The `delete` method is being overridden in this code snippet. It first calls the `delete`
130
+ * method from the superclass (presumably a parent class) with the provided `predicate`, which could
131
+ * be a key, node, entry, or a custom predicate. The result of this deletion operation is stored in
132
+ * `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
133
133
  */
134
- delete(identifier, callback = this._DEFAULT_CALLBACK) {
135
- const deletedResults = super.delete(identifier, callback);
134
+ delete(predicate) {
135
+ const deletedResults = super.delete(predicate);
136
136
  for (const { needBalanced } of deletedResults) {
137
137
  if (needBalanced) {
138
138
  this._balancePath(needBalanced);
@@ -298,8 +298,10 @@ class AVLTree extends bst_1.BST {
298
298
  C.right = A;
299
299
  }
300
300
  this._updateHeight(A);
301
- B && this._updateHeight(B);
302
- C && this._updateHeight(C);
301
+ if (B)
302
+ this._updateHeight(B);
303
+ if (C)
304
+ this._updateHeight(C);
303
305
  }
304
306
  /**
305
307
  * Time Complexity: O(1)
@@ -337,7 +339,8 @@ class AVLTree extends bst_1.BST {
337
339
  B.left = A;
338
340
  }
339
341
  this._updateHeight(A);
340
- B && this._updateHeight(B);
342
+ if (B)
343
+ this._updateHeight(B);
341
344
  }
342
345
  /**
343
346
  * Time Complexity: O(1)
@@ -388,8 +391,10 @@ class AVLTree extends bst_1.BST {
388
391
  if (C)
389
392
  C.right = B;
390
393
  this._updateHeight(A);
391
- B && this._updateHeight(B);
392
- C && this._updateHeight(C);
394
+ if (B)
395
+ this._updateHeight(B);
396
+ if (C)
397
+ this._updateHeight(C);
393
398
  }
394
399
  /**
395
400
  * Time Complexity: O(log n)
@@ -397,47 +402,49 @@ class AVLTree extends bst_1.BST {
397
402
  *
398
403
  * The `_balancePath` function is used to update the heights of nodes and perform rotation operations
399
404
  * to restore balance in an AVL tree after inserting a node.
400
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The `node` parameter can be of type `R` or
405
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} node - The `node` parameter can be of type `R` or
401
406
  * `BTNKeyOrNodeOrEntry<K, V, NODE>`.
402
407
  */
403
408
  _balancePath(node) {
404
409
  node = this.ensureNode(node);
405
- const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
410
+ const path = this.getPathToRoot(node => node, node, false); // first O(log n) + O(log n)
406
411
  for (let i = 0; i < path.length; i++) {
407
412
  // second O(log n)
408
413
  const A = path[i];
409
- // Update Heights: After inserting a node, backtrack from the insertion point to the root node, updating the height of each node along the way.
410
- this._updateHeight(A); // first O(1)
411
- // Check Balance: Simultaneously with height updates, check if each node violates the balance property of an AVL tree.
412
- // Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
413
- switch (this._balanceFactor(A) // second O(1)
414
- ) {
415
- case -2:
416
- if (A && A.left) {
417
- if (this._balanceFactor(A.left) <= 0) {
418
- // second O(1)
419
- // Left Rotation (LL Rotation): When the inserted node is in the left subtree of the left subtree, causing an imbalance.
420
- this._balanceLL(A);
421
- }
422
- else {
423
- // Left-Right Rotation (LR Rotation): When the inserted node is in the right subtree of the left subtree, causing an imbalance.
424
- this._balanceLR(A);
414
+ if (A) {
415
+ // Update Heights: After inserting a node, backtrack from the insertion point to the root node, updating the height of each node along the way.
416
+ this._updateHeight(A); // first O(1)
417
+ // Check Balance: Simultaneously with height updates, check if each node violates the balance property of an AVL tree.
418
+ // Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
419
+ switch (this._balanceFactor(A) // second O(1)
420
+ ) {
421
+ case -2:
422
+ if (A && A.left) {
423
+ if (this._balanceFactor(A.left) <= 0) {
424
+ // second O(1)
425
+ // Left Rotation (LL Rotation): When the inserted node is in the left subtree of the left subtree, causing an imbalance.
426
+ this._balanceLL(A);
427
+ }
428
+ else {
429
+ // Left-Right Rotation (LR Rotation): When the inserted node is in the right subtree of the left subtree, causing an imbalance.
430
+ this._balanceLR(A);
431
+ }
425
432
  }
426
- }
427
- break;
428
- case +2:
429
- if (A && A.right) {
430
- if (this._balanceFactor(A.right) >= 0) {
431
- // Right Rotation (RR Rotation): When the inserted node is in the right subtree of the right subtree, causing an imbalance.
432
- this._balanceRR(A);
433
+ break;
434
+ case +2:
435
+ if (A && A.right) {
436
+ if (this._balanceFactor(A.right) >= 0) {
437
+ // Right Rotation (RR Rotation): When the inserted node is in the right subtree of the right subtree, causing an imbalance.
438
+ this._balanceRR(A);
439
+ }
440
+ else {
441
+ // Right-Left Rotation (RL Rotation): When the inserted node is in the left subtree of the right subtree, causing an imbalance.
442
+ this._balanceRL(A);
443
+ }
433
444
  }
434
- else {
435
- // Right-Left Rotation (RL Rotation): When the inserted node is in the left subtree of the right subtree, causing an imbalance.
436
- this._balanceRL(A);
437
- }
438
- }
445
+ }
446
+ // TODO So far, no sure if this is necessary that Recursive Repair: Once rotation operations are executed, it may cause imbalance issues at higher levels of the tree. Therefore, you need to recursively check and repair imbalance problems upwards until you reach the root node.
439
447
  }
440
- // TODO So far, no sure if this is necessary that Recursive Repair: Once rotation operations are executed, it may cause imbalance issues at higher levels of the tree. Therefore, you need to recursively check and repair imbalance problems upwards until you reach the root node.
441
448
  }
442
449
  }
443
450
  /**
@@ -4,8 +4,8 @@ exports.BinaryIndexedTree = void 0;
4
4
  /**
5
5
  * data-structure-typed
6
6
  *
7
- * @author Tyler Zeng
8
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
7
+ * @author Pablo Zeng
8
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
9
9
  * @license MIT License
10
10
  */
11
11
  const utils_1 = require("../../utils");