data-structure-typed 1.37.9 → 1.38.1

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 (81) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +9 -9
  2. package/dist/data-structures/binary-tree/avl-tree.js +22 -22
  3. package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
  4. package/dist/data-structures/binary-tree/binary-tree.d.ts +31 -31
  5. package/dist/data-structures/binary-tree/binary-tree.js +32 -32
  6. package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
  7. package/dist/data-structures/binary-tree/bst.js.map +1 -1
  8. package/dist/data-structures/binary-tree/rb-tree.d.ts +1 -1
  9. package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
  10. package/dist/data-structures/binary-tree/tree-multiset.d.ts +9 -9
  11. package/dist/data-structures/binary-tree/tree-multiset.js +23 -23
  12. package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
  13. package/dist/data-structures/hash/hash-map.d.ts +25 -25
  14. package/dist/data-structures/hash/hash-map.js +59 -59
  15. package/dist/data-structures/hash/hash-map.js.map +1 -1
  16. package/dist/data-structures/hash/hash-table.d.ts +34 -34
  17. package/dist/data-structures/hash/hash-table.js +99 -99
  18. package/dist/data-structures/hash/hash-table.js.map +1 -1
  19. package/dist/data-structures/heap/heap.d.ts +67 -68
  20. package/dist/data-structures/heap/heap.js +167 -167
  21. package/dist/data-structures/heap/heap.js.map +1 -1
  22. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +1 -1
  23. package/dist/data-structures/linked-list/doubly-linked-list.js +3 -3
  24. package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  25. package/dist/data-structures/linked-list/skip-linked-list.d.ts +17 -17
  26. package/dist/data-structures/linked-list/skip-linked-list.js +34 -34
  27. package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -1
  28. package/dist/data-structures/matrix/matrix2d.d.ts +7 -7
  29. package/dist/data-structures/matrix/matrix2d.js +9 -9
  30. package/dist/data-structures/matrix/matrix2d.js.map +1 -1
  31. package/dist/data-structures/trie/trie.d.ts +2 -2
  32. package/dist/data-structures/trie/trie.js +6 -6
  33. package/dist/data-structures/trie/trie.js.map +1 -1
  34. package/dist/types/data-structures/binary-tree/bst.d.ts +0 -5
  35. package/dist/types/data-structures/binary-tree/bst.js +0 -7
  36. package/dist/types/data-structures/binary-tree/bst.js.map +1 -1
  37. package/dist/types/helpers.d.ts +5 -0
  38. package/dist/types/helpers.js +7 -0
  39. package/dist/types/helpers.js.map +1 -1
  40. package/lib/data-structures/binary-tree/avl-tree.d.ts +9 -9
  41. package/lib/data-structures/binary-tree/avl-tree.js +22 -22
  42. package/lib/data-structures/binary-tree/binary-tree.d.ts +31 -31
  43. package/lib/data-structures/binary-tree/binary-tree.js +32 -32
  44. package/lib/data-structures/binary-tree/rb-tree.d.ts +1 -1
  45. package/lib/data-structures/binary-tree/tree-multiset.d.ts +9 -9
  46. package/lib/data-structures/binary-tree/tree-multiset.js +23 -23
  47. package/lib/data-structures/hash/hash-map.d.ts +25 -25
  48. package/lib/data-structures/hash/hash-map.js +59 -59
  49. package/lib/data-structures/hash/hash-table.d.ts +34 -34
  50. package/lib/data-structures/hash/hash-table.js +99 -99
  51. package/lib/data-structures/heap/heap.d.ts +67 -68
  52. package/lib/data-structures/heap/heap.js +167 -167
  53. package/lib/data-structures/linked-list/doubly-linked-list.d.ts +1 -1
  54. package/lib/data-structures/linked-list/doubly-linked-list.js +3 -3
  55. package/lib/data-structures/linked-list/skip-linked-list.d.ts +17 -17
  56. package/lib/data-structures/linked-list/skip-linked-list.js +34 -34
  57. package/lib/data-structures/matrix/matrix2d.d.ts +7 -7
  58. package/lib/data-structures/matrix/matrix2d.js +9 -9
  59. package/lib/data-structures/trie/trie.d.ts +2 -2
  60. package/lib/data-structures/trie/trie.js +6 -6
  61. package/lib/types/data-structures/binary-tree/bst.d.ts +0 -5
  62. package/lib/types/data-structures/binary-tree/bst.js +1 -6
  63. package/lib/types/helpers.d.ts +5 -0
  64. package/lib/types/helpers.js +6 -1
  65. package/package.json +7 -6
  66. package/src/data-structures/binary-tree/avl-tree.ts +27 -27
  67. package/src/data-structures/binary-tree/binary-tree.ts +55 -55
  68. package/src/data-structures/binary-tree/bst.ts +4 -0
  69. package/src/data-structures/binary-tree/rb-tree.ts +2 -2
  70. package/src/data-structures/binary-tree/tree-multiset.ts +29 -29
  71. package/src/data-structures/hash/hash-map.ts +81 -75
  72. package/src/data-structures/hash/hash-table.ts +112 -109
  73. package/src/data-structures/heap/heap.ts +183 -183
  74. package/src/data-structures/linked-list/doubly-linked-list.ts +4 -4
  75. package/src/data-structures/linked-list/skip-linked-list.ts +45 -38
  76. package/src/data-structures/matrix/matrix2d.ts +10 -10
  77. package/src/data-structures/trie/trie.ts +9 -9
  78. package/src/types/helpers.ts +5 -1
  79. package/test/unit/data-structures/binary-tree/binary-index-tree.test.ts +1 -1
  80. package/umd/bundle.min.js +1 -1
  81. package/umd/bundle.min.js.map +1 -1
@@ -2,3 +2,8 @@ export type Comparator<T> = (a: T, b: T) => number;
2
2
  export type DFSOrderPattern = 'pre' | 'in' | 'post';
3
3
  export type MapCallback<N> = (node: N) => any;
4
4
  export type MapCallbackReturn<N> = ReturnType<MapCallback<N>>;
5
+ export declare enum CP {
6
+ lt = "lt",
7
+ eq = "eq",
8
+ gt = "gt"
9
+ }
@@ -1 +1,6 @@
1
- export {};
1
+ export var CP;
2
+ (function (CP) {
3
+ CP["lt"] = "lt";
4
+ CP["eq"] = "eq";
5
+ CP["gt"] = "gt";
6
+ })(CP || (CP = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.37.9",
3
+ "version": "1.38.1",
4
4
  "description": "Data Structures of Javascript & TypeScript. Binary Tree, BST, Graph, Heap, Priority Queue, Linked List, Queue, Deque, Stack, AVL Tree, Tree Multiset, Trie, Directed Graph, Undirected Graph, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue.",
5
5
  "main": "dist/index.js",
6
6
  "module": "lib/index.js",
@@ -34,7 +34,8 @@
34
34
  "changelog": "auto-changelog",
35
35
  "coverage:badge": "istanbul-badges-readme",
36
36
  "ci": "env && git fetch --tags && npm run lint && npm run build && npm run update:subs && npm run test && npm run changelog",
37
- "publish:subs": "sh scripts/publish_all_subs.sh",
37
+ "copy:to-subs": "sh scripts/copy_to_all_subs.sh",
38
+ "publish:subs": "npm run copy:to-subs && sh scripts/publish_all_subs.sh",
38
39
  "publish:docs": "sh scripts/publish_docs.sh",
39
40
  "publish:all": "npm run ci && npm publish && npm run publish:subs && npm run publish:docs"
40
41
  },
@@ -58,17 +59,17 @@
58
59
  "@typescript-eslint/eslint-plugin": "^6.7.4",
59
60
  "@typescript-eslint/parser": "^6.7.4",
60
61
  "auto-changelog": "^2.4.0",
61
- "avl-tree-typed": "^1.37.8",
62
+ "avl-tree-typed": "^1.38.0",
62
63
  "benchmark": "^2.1.4",
63
- "binary-tree-typed": "^1.37.8",
64
- "bst-typed": "^1.37.8",
64
+ "binary-tree-typed": "^1.38.0",
65
+ "bst-typed": "^1.38.0",
65
66
  "dependency-cruiser": "^14.1.0",
66
67
  "eslint": "^8.50.0",
67
68
  "eslint-config-prettier": "^9.0.0",
68
69
  "eslint-import-resolver-alias": "^1.1.2",
69
70
  "eslint-import-resolver-typescript": "^3.6.1",
70
71
  "eslint-plugin-import": "^2.28.1",
71
- "heap-typed": "^1.37.8",
72
+ "heap-typed": "^1.38.0",
72
73
  "istanbul-badges-readme": "^1.8.5",
73
74
  "jest": "^29.7.0",
74
75
  "prettier": "^3.0.3",
@@ -32,33 +32,6 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
32
32
  super(options);
33
33
  }
34
34
 
35
- /**
36
- * The function swaps the key, value, and height properties between two nodes in a binary tree.
37
- * @param {N} srcNode - The `srcNode` parameter represents the source node that needs to be swapped
38
- * with the `destNode`.
39
- * @param {N} destNode - The `destNode` parameter represents the destination node where the values
40
- * from the source node (`srcNode`) will be swapped to.
41
- * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
42
- */
43
- protected override _swap(srcNode: N, destNode: N): N {
44
- const {key, val, height} = destNode;
45
- const tempNode = this.createNode(key, val);
46
-
47
- if (tempNode) {
48
- tempNode.height = height;
49
-
50
- destNode.key = srcNode.key;
51
- destNode.val = srcNode.val;
52
- destNode.height = srcNode.height;
53
-
54
- srcNode.key = tempNode.key;
55
- srcNode.val = tempNode.val;
56
- srcNode.height = tempNode.height;
57
- }
58
-
59
- return destNode;
60
- }
61
-
62
35
  /**
63
36
  * The function creates a new AVL tree node with the specified key and value.
64
37
  * @param {BinaryTreeNodeKey} key - The key parameter is the key value that will be associated with
@@ -105,6 +78,33 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
105
78
  return deletedResults;
106
79
  }
107
80
 
81
+ /**
82
+ * The function swaps the key, value, and height properties between two nodes in a binary tree.
83
+ * @param {N} srcNode - The `srcNode` parameter represents the source node that needs to be swapped
84
+ * with the `destNode`.
85
+ * @param {N} destNode - The `destNode` parameter represents the destination node where the values
86
+ * from the source node (`srcNode`) will be swapped to.
87
+ * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
88
+ */
89
+ protected override _swap(srcNode: N, destNode: N): N {
90
+ const {key, val, height} = destNode;
91
+ const tempNode = this.createNode(key, val);
92
+
93
+ if (tempNode) {
94
+ tempNode.height = height;
95
+
96
+ destNode.key = srcNode.key;
97
+ destNode.val = srcNode.val;
98
+ destNode.height = srcNode.height;
99
+
100
+ srcNode.key = tempNode.key;
101
+ srcNode.val = tempNode.val;
102
+ srcNode.height = tempNode.height;
103
+ }
104
+
105
+ return destNode;
106
+ }
107
+
108
108
  /**
109
109
  * The function calculates the balance factor of a node in a binary tree.
110
110
  * @param {N} node - The parameter "node" represents a node in a binary tree data structure.
@@ -26,16 +26,6 @@ import {Queue} from '../queue';
26
26
  * @template FAMILY - The type of the family relationship in the binary tree.
27
27
  */
28
28
  export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> = BinaryTreeNodeNested<V>> {
29
- /**
30
- * Creates a new instance of BinaryTreeNode.
31
- * @param {BinaryTreeNodeKey} key - The key associated with the node.
32
- * @param {V} val - The value stored in the node.
33
- */
34
- constructor(key: BinaryTreeNodeKey, val?: V) {
35
- this.key = key;
36
- this.val = val;
37
- }
38
-
39
29
  /**
40
30
  * The key associated with the node.
41
31
  */
@@ -46,6 +36,21 @@ export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> =
46
36
  */
47
37
  val: V | undefined;
48
38
 
39
+ /**
40
+ * The parent node of the current node.
41
+ */
42
+ parent: FAMILY | null | undefined;
43
+
44
+ /**
45
+ * Creates a new instance of BinaryTreeNode.
46
+ * @param {BinaryTreeNodeKey} key - The key associated with the node.
47
+ * @param {V} val - The value stored in the node.
48
+ */
49
+ constructor(key: BinaryTreeNodeKey, val?: V) {
50
+ this.key = key;
51
+ this.val = val;
52
+ }
53
+
49
54
  private _left: FAMILY | null | undefined;
50
55
 
51
56
  /**
@@ -86,11 +91,6 @@ export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> =
86
91
  this._right = v;
87
92
  }
88
93
 
89
- /**
90
- * The parent node of the current node.
91
- */
92
- parent: FAMILY | null | undefined;
93
-
94
94
  /**
95
95
  * Get the position of the node within its family.
96
96
  * @returns {FamilyPosition} - The family position of the node.
@@ -128,6 +128,8 @@ export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> =
128
128
  * @template N - The type of the binary tree's nodes.
129
129
  */
130
130
  export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode> implements IBinaryTree<N> {
131
+ private _loopType: IterationType = IterationType.ITERATIVE;
132
+
131
133
  /**
132
134
  * Creates a new instance of BinaryTree.
133
135
  * @param {BinaryTreeOptions} [options] - The options for the binary tree.
@@ -139,16 +141,6 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
139
141
  }
140
142
  }
141
143
 
142
- /**
143
- * Creates a new instance of BinaryTreeNode with the given key and value.
144
- * @param {BinaryTreeNodeKey} key - The key for the new node.
145
- * @param {N['val']} val - The value for the new node.
146
- * @returns {N} - The newly created BinaryTreeNode.
147
- */
148
- createNode(key: BinaryTreeNodeKey, val?: N['val']): N {
149
- return new BinaryTreeNode<N['val'], N>(key, val) as N;
150
- }
151
-
152
144
  private _root: N | null = null;
153
145
 
154
146
  /**
@@ -167,8 +159,6 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
167
159
  return this._size;
168
160
  }
169
161
 
170
- private _loopType: IterationType = IterationType.ITERATIVE;
171
-
172
162
  /**
173
163
  * Get the iteration type used in the binary tree.
174
164
  */
@@ -185,24 +175,13 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
185
175
  }
186
176
 
187
177
  /**
188
- * Swap the data of two nodes in the binary tree.
189
- * @param {N} srcNode - The source node to swap.
190
- * @param {N} destNode - The destination node to swap.
191
- * @returns {N} - The destination node after the swap.
178
+ * Creates a new instance of BinaryTreeNode with the given key and value.
179
+ * @param {BinaryTreeNodeKey} key - The key for the new node.
180
+ * @param {N['val']} val - The value for the new node.
181
+ * @returns {N} - The newly created BinaryTreeNode.
192
182
  */
193
- protected _swap(srcNode: N, destNode: N): N {
194
- const {key, val} = destNode;
195
- const tempNode = this.createNode(key, val);
196
-
197
- if (tempNode) {
198
- destNode.key = srcNode.key;
199
- destNode.val = srcNode.val;
200
-
201
- srcNode.key = tempNode.key;
202
- srcNode.val = tempNode.val;
203
- }
204
-
205
- return destNode;
183
+ createNode(key: BinaryTreeNodeKey, val?: N['val']): N {
184
+ return new BinaryTreeNode<N['val'], N>(key, val) as N;
206
185
  }
207
186
 
208
187
  /**
@@ -449,8 +428,6 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
449
428
  }
450
429
  }
451
430
 
452
- protected _defaultCallbackByKey: MapCallback<N> = node => node.key;
453
-
454
431
  /**
455
432
  * The `getMinHeight` function calculates the minimum height of a binary tree using either a
456
433
  * recursive or iterative approach.
@@ -902,8 +879,6 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
902
879
  return ans;
903
880
  }
904
881
 
905
- // --- start additional methods ---
906
-
907
882
  /**
908
883
  * The bfs function performs a breadth-first search traversal on a binary tree, executing a callback
909
884
  * function on each node.
@@ -973,13 +948,7 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
973
948
  }
974
949
  }
975
950
 
976
- /**
977
- * Time complexity is O(n)
978
- * Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
979
- * The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
980
- * the tree's structure should be restored to its original state to maintain the tree's integrity.
981
- * This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
982
- */
951
+ // --- start additional methods ---
983
952
 
984
953
  /**
985
954
  * The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
@@ -1080,6 +1049,37 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
1080
1049
  return ans;
1081
1050
  }
1082
1051
 
1052
+ /**
1053
+ * Swap the data of two nodes in the binary tree.
1054
+ * @param {N} srcNode - The source node to swap.
1055
+ * @param {N} destNode - The destination node to swap.
1056
+ * @returns {N} - The destination node after the swap.
1057
+ */
1058
+ protected _swap(srcNode: N, destNode: N): N {
1059
+ const {key, val} = destNode;
1060
+ const tempNode = this.createNode(key, val);
1061
+
1062
+ if (tempNode) {
1063
+ destNode.key = srcNode.key;
1064
+ destNode.val = srcNode.val;
1065
+
1066
+ srcNode.key = tempNode.key;
1067
+ srcNode.val = tempNode.val;
1068
+ }
1069
+
1070
+ return destNode;
1071
+ }
1072
+
1073
+ /**
1074
+ * Time complexity is O(n)
1075
+ * Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
1076
+ * The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
1077
+ * the tree's structure should be restored to its original state to maintain the tree's integrity.
1078
+ * This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
1079
+ */
1080
+
1081
+ protected _defaultCallbackByKey: MapCallback<N> = node => node.key;
1082
+
1083
1083
  /**
1084
1084
  * The function `_addTo` adds a new node to a binary tree if there is an available position.
1085
1085
  * @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to
@@ -153,22 +153,26 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
153
153
  function hasNoNull(arr: (BinaryTreeNodeKey | null)[] | (N | null)[]): arr is BinaryTreeNodeKey[] | N[] {
154
154
  return arr.indexOf(null) === -1;
155
155
  }
156
+
156
157
  if (!isBalanceAdd || !hasNoNull(keysOrNodes)) {
157
158
  return super.addMany(keysOrNodes, data);
158
159
  }
159
160
  const inserted: (N | null | undefined)[] = [];
160
161
  const combinedArr: [BinaryTreeNodeKey | N, N['val']][] = keysOrNodes.map((value, index) => [value, data?.[index]]);
161
162
  let sorted = [];
163
+
162
164
  function isNodeOrNullTuple(arr: [BinaryTreeNodeKey | N, N['val']][]): arr is [N, N['val']][] {
163
165
  for (const [keyOrNode] of arr) if (keyOrNode instanceof BSTNode) return true;
164
166
  return false;
165
167
  }
168
+
166
169
  function isBinaryTreeKeyOrNullTuple(
167
170
  arr: [BinaryTreeNodeKey | N, N['val']][]
168
171
  ): arr is [BinaryTreeNodeKey, N['val']][] {
169
172
  for (const [keyOrNode] of arr) if (typeof keyOrNode === 'number') return true;
170
173
  return false;
171
174
  }
175
+
172
176
  let sortedKeysOrNodes: (number | N | null)[] = [],
173
177
  sortedData: (N['val'] | undefined)[] | undefined = [];
174
178
 
@@ -6,13 +6,13 @@ export class RBTreeNode<V = any, FAMILY extends RBTreeNode<V, FAMILY> = RBTreeNo
6
6
  V,
7
7
  FAMILY
8
8
  > {
9
- private _color: RBColor;
10
-
11
9
  constructor(key: BinaryTreeNodeKey, val?: V) {
12
10
  super(key, val);
13
11
  this._color = RBColor.RED;
14
12
  }
15
13
 
14
+ private _color: RBColor;
15
+
16
16
  get color(): RBColor {
17
17
  return this._color;
18
18
  }
@@ -14,6 +14,8 @@ export class TreeMultisetNode<
14
14
  V = any,
15
15
  FAMILY extends TreeMultisetNode<V, FAMILY> = TreeMultisetNodeNested<V>
16
16
  > extends AVLTreeNode<V, FAMILY> {
17
+ count: number;
18
+
17
19
  /**
18
20
  * The constructor function initializes a BinaryTreeNode object with a key, value, and count.
19
21
  * @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier
@@ -28,8 +30,6 @@ export class TreeMultisetNode<
28
30
  super(key, val);
29
31
  this.count = count;
30
32
  }
31
-
32
- count: number;
33
33
  }
34
34
 
35
35
  /**
@@ -68,33 +68,6 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
68
68
  return new TreeMultisetNode(key, val, count) as N;
69
69
  }
70
70
 
71
- /**
72
- * The function swaps the values of two nodes in a binary tree.
73
- * @param {N} srcNode - The source node that needs to be swapped with the destination node.
74
- * @param {N} destNode - The `destNode` parameter represents the destination node where the values
75
- * from `srcNode` will be swapped into.
76
- * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
77
- */
78
- protected override _swap(srcNode: N, destNode: N): N {
79
- const {key, val, count, height} = destNode;
80
- const tempNode = this.createNode(key, val, count);
81
- if (tempNode) {
82
- tempNode.height = height;
83
-
84
- destNode.key = srcNode.key;
85
- destNode.val = srcNode.val;
86
- destNode.count = srcNode.count;
87
- destNode.height = srcNode.height;
88
-
89
- srcNode.key = tempNode.key;
90
- srcNode.val = tempNode.val;
91
- srcNode.count = tempNode.count;
92
- srcNode.height = tempNode.height;
93
- }
94
-
95
- return destNode;
96
- }
97
-
98
71
  /**
99
72
  * The `add` function adds a new node to a binary search tree, updating the count if the key already
100
73
  * exists, and balancing the tree if necessary.
@@ -365,6 +338,33 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
365
338
  this._setCount(0);
366
339
  }
367
340
 
341
+ /**
342
+ * The function swaps the values of two nodes in a binary tree.
343
+ * @param {N} srcNode - The source node that needs to be swapped with the destination node.
344
+ * @param {N} destNode - The `destNode` parameter represents the destination node where the values
345
+ * from `srcNode` will be swapped into.
346
+ * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
347
+ */
348
+ protected override _swap(srcNode: N, destNode: N): N {
349
+ const {key, val, count, height} = destNode;
350
+ const tempNode = this.createNode(key, val, count);
351
+ if (tempNode) {
352
+ tempNode.height = height;
353
+
354
+ destNode.key = srcNode.key;
355
+ destNode.val = srcNode.val;
356
+ destNode.count = srcNode.count;
357
+ destNode.height = srcNode.height;
358
+
359
+ srcNode.key = tempNode.key;
360
+ srcNode.val = tempNode.val;
361
+ srcNode.count = tempNode.count;
362
+ srcNode.height = tempNode.height;
363
+ }
364
+
365
+ return destNode;
366
+ }
367
+
368
368
  /**
369
369
  * The function sets the value of the "_count" property.
370
370
  * @param {number} v - number
@@ -8,28 +8,47 @@ import {HashFunction} from '../../types';
8
8
  * @license MIT License
9
9
  */
10
10
  export class HashMap<K, V> {
11
- get hashFn(): HashFunction<K> {
12
- return this._hashFn;
11
+ /**
12
+ * The constructor initializes the properties of a hash table, including the initial capacity, load factor, capacity
13
+ * multiplier, size, table array, and hash function.
14
+ * @param [initialCapacity=16] - The initial capacity is the initial size of the hash table. It determines the number of
15
+ * buckets or slots available for storing key-value pairs. The default value is 16.
16
+ * @param [loadFactor=0.75] - The load factor is a measure of how full the hash table can be before it is resized. It is
17
+ * a value between 0 and 1, where 1 means the hash table is completely full and 0 means it is completely empty. When the
18
+ * load factor is reached, the hash table will
19
+ * @param [hashFn] - The `hashFn` parameter is an optional parameter that represents the hash function used to calculate
20
+ * the index of a key in the hash table. If a custom hash function is not provided, a default hash function is used. The
21
+ * default hash function converts the key to a string, calculates the sum of the
22
+ */
23
+ constructor(initialCapacity = 16, loadFactor = 0.75, hashFn?: HashFunction<K>) {
24
+ this._initialCapacity = initialCapacity;
25
+ this._loadFactor = loadFactor;
26
+ this._capacityMultiplier = 2;
27
+ this._size = 0;
28
+ this._table = new Array(initialCapacity);
29
+ this._hashFn =
30
+ hashFn ||
31
+ ((key: K) => {
32
+ const strKey = String(key);
33
+ let hash = 0;
34
+ for (let i = 0; i < strKey.length; i++) {
35
+ hash += strKey.charCodeAt(i);
36
+ }
37
+ return hash % this.table.length;
38
+ });
13
39
  }
14
40
 
15
- set hashFn(value: HashFunction<K>) {
16
- this._hashFn = value;
17
- }
18
- get table(): Array<Array<[K, V]>> {
19
- return this._table;
20
- }
41
+ private _initialCapacity: number;
21
42
 
22
- set table(value: Array<Array<[K, V]>>) {
23
- this._table = value;
43
+ get initialCapacity(): number {
44
+ return this._initialCapacity;
24
45
  }
25
46
 
26
- get capacityMultiplier(): number {
27
- return this._capacityMultiplier;
47
+ set initialCapacity(value: number) {
48
+ this._initialCapacity = value;
28
49
  }
29
50
 
30
- set capacityMultiplier(value: number) {
31
- this._capacityMultiplier = value;
32
- }
51
+ private _loadFactor: number;
33
52
 
34
53
  get loadFactor(): number {
35
54
  return this._loadFactor;
@@ -39,14 +58,18 @@ export class HashMap<K, V> {
39
58
  this._loadFactor = value;
40
59
  }
41
60
 
42
- get initialCapacity(): number {
43
- return this._initialCapacity;
61
+ private _capacityMultiplier: number;
62
+
63
+ get capacityMultiplier(): number {
64
+ return this._capacityMultiplier;
44
65
  }
45
66
 
46
- set initialCapacity(value: number) {
47
- this._initialCapacity = value;
67
+ set capacityMultiplier(value: number) {
68
+ this._capacityMultiplier = value;
48
69
  }
49
70
 
71
+ private _size: number;
72
+
50
73
  get size(): number {
51
74
  return this._size;
52
75
  }
@@ -55,68 +78,24 @@ export class HashMap<K, V> {
55
78
  this._size = value;
56
79
  }
57
80
 
58
- private _initialCapacity: number;
59
- private _loadFactor: number;
60
- private _capacityMultiplier: number;
61
- private _size: number;
62
81
  private _table: Array<Array<[K, V]>>;
63
- private _hashFn: HashFunction<K>;
64
82
 
65
- /**
66
- * The constructor initializes the properties of a hash table, including the initial capacity, load factor, capacity
67
- * multiplier, size, table array, and hash function.
68
- * @param [initialCapacity=16] - The initial capacity is the initial size of the hash table. It determines the number of
69
- * buckets or slots available for storing key-value pairs. The default value is 16.
70
- * @param [loadFactor=0.75] - The load factor is a measure of how full the hash table can be before it is resized. It is
71
- * a value between 0 and 1, where 1 means the hash table is completely full and 0 means it is completely empty. When the
72
- * load factor is reached, the hash table will
73
- * @param [hashFn] - The `hashFn` parameter is an optional parameter that represents the hash function used to calculate
74
- * the index of a key in the hash table. If a custom hash function is not provided, a default hash function is used. The
75
- * default hash function converts the key to a string, calculates the sum of the
76
- */
77
- constructor(initialCapacity = 16, loadFactor = 0.75, hashFn?: HashFunction<K>) {
78
- this._initialCapacity = initialCapacity;
79
- this._loadFactor = loadFactor;
80
- this._capacityMultiplier = 2;
81
- this._size = 0;
82
- this._table = new Array(initialCapacity);
83
- this._hashFn =
84
- hashFn ||
85
- ((key: K) => {
86
- const strKey = String(key);
87
- let hash = 0;
88
- for (let i = 0; i < strKey.length; i++) {
89
- hash += strKey.charCodeAt(i);
90
- }
91
- return hash % this.table.length;
92
- });
83
+ get table(): Array<Array<[K, V]>> {
84
+ return this._table;
93
85
  }
94
86
 
95
- private _hash(key: K): number {
96
- return this._hashFn(key);
87
+ set table(value: Array<Array<[K, V]>>) {
88
+ this._table = value;
97
89
  }
98
90
 
99
- /**
100
- * The `resizeTable` function resizes the table used in a hash map by creating a new table with a specified capacity and
101
- * rehashing the key-value pairs from the old table into the new table.
102
- * @param {number} newCapacity - The newCapacity parameter is the desired capacity for the resized table. It represents
103
- * the number of buckets that the new table should have.
104
- */
105
- private resizeTable(newCapacity: number): void {
106
- const newTable = new Array(newCapacity);
107
- for (const bucket of this._table) {
108
- // Note that this is this._table
109
- if (bucket) {
110
- for (const [key, value] of bucket) {
111
- const newIndex = this._hash(key) % newCapacity;
112
- if (!newTable[newIndex]) {
113
- newTable[newIndex] = [];
114
- }
115
- newTable[newIndex].push([key, value]);
116
- }
117
- }
118
- }
119
- this._table = newTable; // Again, here is this._table
91
+ private _hashFn: HashFunction<K>;
92
+
93
+ get hashFn(): HashFunction<K> {
94
+ return this._hashFn;
95
+ }
96
+
97
+ set hashFn(value: HashFunction<K>) {
98
+ this._hashFn = value;
120
99
  }
121
100
 
122
101
  set(key: K, value: V): void {
@@ -200,4 +179,31 @@ export class HashMap<K, V> {
200
179
  isEmpty(): boolean {
201
180
  return this.size === 0;
202
181
  }
182
+
183
+ private _hash(key: K): number {
184
+ return this._hashFn(key);
185
+ }
186
+
187
+ /**
188
+ * The `resizeTable` function resizes the table used in a hash map by creating a new table with a specified capacity and
189
+ * rehashing the key-value pairs from the old table into the new table.
190
+ * @param {number} newCapacity - The newCapacity parameter is the desired capacity for the resized table. It represents
191
+ * the number of buckets that the new table should have.
192
+ */
193
+ private resizeTable(newCapacity: number): void {
194
+ const newTable = new Array(newCapacity);
195
+ for (const bucket of this._table) {
196
+ // Note that this is this._table
197
+ if (bucket) {
198
+ for (const [key, value] of bucket) {
199
+ const newIndex = this._hash(key) % newCapacity;
200
+ if (!newTable[newIndex]) {
201
+ newTable[newIndex] = [];
202
+ }
203
+ newTable[newIndex].push([key, value]);
204
+ }
205
+ }
206
+ }
207
+ this._table = newTable; // Again, here is this._table
208
+ }
203
209
  }