heap-typed 1.38.0 → 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 (38) 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/binary-tree.d.ts +31 -31
  4. package/dist/data-structures/binary-tree/binary-tree.js +32 -32
  5. package/dist/data-structures/binary-tree/rb-tree.d.ts +1 -1
  6. package/dist/data-structures/binary-tree/tree-multiset.d.ts +9 -9
  7. package/dist/data-structures/binary-tree/tree-multiset.js +23 -23
  8. package/dist/data-structures/hash/hash-map.d.ts +25 -25
  9. package/dist/data-structures/hash/hash-map.js +59 -59
  10. package/dist/data-structures/hash/hash-table.d.ts +34 -34
  11. package/dist/data-structures/hash/hash-table.js +99 -99
  12. package/dist/data-structures/heap/heap.d.ts +66 -66
  13. package/dist/data-structures/heap/heap.js +167 -167
  14. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +1 -1
  15. package/dist/data-structures/linked-list/doubly-linked-list.js +3 -3
  16. package/dist/data-structures/linked-list/skip-linked-list.d.ts +17 -17
  17. package/dist/data-structures/linked-list/skip-linked-list.js +34 -34
  18. package/dist/data-structures/matrix/matrix2d.d.ts +7 -7
  19. package/dist/data-structures/matrix/matrix2d.js +9 -9
  20. package/dist/data-structures/trie/trie.d.ts +2 -2
  21. package/dist/data-structures/trie/trie.js +6 -6
  22. package/dist/index.d.ts +3 -3
  23. package/dist/index.js +3 -3
  24. package/package.json +1 -4
  25. package/src/data-structures/binary-tree/avl-tree.ts +27 -27
  26. package/src/data-structures/binary-tree/binary-tree.ts +55 -55
  27. package/src/data-structures/binary-tree/bst.ts +4 -0
  28. package/src/data-structures/binary-tree/rb-tree.ts +2 -2
  29. package/src/data-structures/binary-tree/tree-multiset.ts +29 -29
  30. package/src/data-structures/hash/hash-map.ts +81 -75
  31. package/src/data-structures/hash/hash-table.ts +112 -109
  32. package/src/data-structures/heap/heap.ts +182 -181
  33. package/src/data-structures/linked-list/doubly-linked-list.ts +4 -4
  34. package/src/data-structures/linked-list/skip-linked-list.ts +45 -38
  35. package/src/data-structures/matrix/matrix2d.ts +10 -10
  36. package/src/data-structures/trie/trie.ts +9 -9
  37. package/src/index.ts +3 -3
  38. package/src/types/helpers.ts +5 -1
@@ -20,15 +20,6 @@ export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> e
20
20
  * options.
21
21
  */
22
22
  constructor(options?: AVLTreeOptions);
23
- /**
24
- * The function swaps the key, value, and height properties between two nodes in a binary tree.
25
- * @param {N} srcNode - The `srcNode` parameter represents the source node that needs to be swapped
26
- * with the `destNode`.
27
- * @param {N} destNode - The `destNode` parameter represents the destination node where the values
28
- * from the source node (`srcNode`) will be swapped to.
29
- * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
30
- */
31
- protected _swap(srcNode: N, destNode: N): N;
32
23
  /**
33
24
  * The function creates a new AVL tree node with the specified key and value.
34
25
  * @param {BinaryTreeNodeKey} key - The key parameter is the key value that will be associated with
@@ -57,6 +48,15 @@ export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> e
57
48
  * @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
58
49
  */
59
50
  delete(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[];
51
+ /**
52
+ * The function swaps the key, value, and height properties between two nodes in a binary tree.
53
+ * @param {N} srcNode - The `srcNode` parameter represents the source node that needs to be swapped
54
+ * with the `destNode`.
55
+ * @param {N} destNode - The `destNode` parameter represents the destination node where the values
56
+ * from the source node (`srcNode`) will be swapped to.
57
+ * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
58
+ */
59
+ protected _swap(srcNode: N, destNode: N): N;
60
60
  /**
61
61
  * The function calculates the balance factor of a node in a binary tree.
62
62
  * @param {N} node - The parameter "node" represents a node in a binary tree data structure.
@@ -26,28 +26,6 @@ class AVLTree extends bst_1.BST {
26
26
  constructor(options) {
27
27
  super(options);
28
28
  }
29
- /**
30
- * The function swaps the key, value, and height properties between two nodes in a binary tree.
31
- * @param {N} srcNode - The `srcNode` parameter represents the source node that needs to be swapped
32
- * with the `destNode`.
33
- * @param {N} destNode - The `destNode` parameter represents the destination node where the values
34
- * from the source node (`srcNode`) will be swapped to.
35
- * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
36
- */
37
- _swap(srcNode, destNode) {
38
- const { key, val, height } = destNode;
39
- const tempNode = this.createNode(key, val);
40
- if (tempNode) {
41
- tempNode.height = height;
42
- destNode.key = srcNode.key;
43
- destNode.val = srcNode.val;
44
- destNode.height = srcNode.height;
45
- srcNode.key = tempNode.key;
46
- srcNode.val = tempNode.val;
47
- srcNode.height = tempNode.height;
48
- }
49
- return destNode;
50
- }
51
29
  /**
52
30
  * The function creates a new AVL tree node with the specified key and value.
53
31
  * @param {BinaryTreeNodeKey} key - The key parameter is the key value that will be associated with
@@ -92,6 +70,28 @@ class AVLTree extends bst_1.BST {
92
70
  }
93
71
  return deletedResults;
94
72
  }
73
+ /**
74
+ * The function swaps the key, value, and height properties between two nodes in a binary tree.
75
+ * @param {N} srcNode - The `srcNode` parameter represents the source node that needs to be swapped
76
+ * with the `destNode`.
77
+ * @param {N} destNode - The `destNode` parameter represents the destination node where the values
78
+ * from the source node (`srcNode`) will be swapped to.
79
+ * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
80
+ */
81
+ _swap(srcNode, destNode) {
82
+ const { key, val, height } = destNode;
83
+ const tempNode = this.createNode(key, val);
84
+ if (tempNode) {
85
+ tempNode.height = height;
86
+ destNode.key = srcNode.key;
87
+ destNode.val = srcNode.val;
88
+ destNode.height = srcNode.height;
89
+ srcNode.key = tempNode.key;
90
+ srcNode.val = tempNode.val;
91
+ srcNode.height = tempNode.height;
92
+ }
93
+ return destNode;
94
+ }
95
95
  /**
96
96
  * The function calculates the balance factor of a node in a binary tree.
97
97
  * @param {N} node - The parameter "node" represents a node in a binary tree data structure.
@@ -14,12 +14,6 @@ import { IBinaryTree } from '../../interfaces';
14
14
  * @template FAMILY - The type of the family relationship in the binary tree.
15
15
  */
16
16
  export declare class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> = BinaryTreeNodeNested<V>> {
17
- /**
18
- * Creates a new instance of BinaryTreeNode.
19
- * @param {BinaryTreeNodeKey} key - The key associated with the node.
20
- * @param {V} val - The value stored in the node.
21
- */
22
- constructor(key: BinaryTreeNodeKey, val?: V);
23
17
  /**
24
18
  * The key associated with the node.
25
19
  */
@@ -28,6 +22,16 @@ export declare class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FA
28
22
  * The value stored in the node.
29
23
  */
30
24
  val: V | undefined;
25
+ /**
26
+ * The parent node of the current node.
27
+ */
28
+ parent: FAMILY | null | undefined;
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);
31
35
  private _left;
32
36
  /**
33
37
  * Get the left child node.
@@ -48,10 +52,6 @@ export declare class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FA
48
52
  * @param {FAMILY | null | undefined} v - The right child node.
49
53
  */
50
54
  set right(v: FAMILY | null | undefined);
51
- /**
52
- * The parent node of the current node.
53
- */
54
- parent: FAMILY | null | undefined;
55
55
  /**
56
56
  * Get the position of the node within its family.
57
57
  * @returns {FamilyPosition} - The family position of the node.
@@ -63,18 +63,12 @@ export declare class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FA
63
63
  * @template N - The type of the binary tree's nodes.
64
64
  */
65
65
  export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode> implements IBinaryTree<N> {
66
+ private _loopType;
66
67
  /**
67
68
  * Creates a new instance of BinaryTree.
68
69
  * @param {BinaryTreeOptions} [options] - The options for the binary tree.
69
70
  */
70
71
  constructor(options?: BinaryTreeOptions);
71
- /**
72
- * Creates a new instance of BinaryTreeNode with the given key and value.
73
- * @param {BinaryTreeNodeKey} key - The key for the new node.
74
- * @param {N['val']} val - The value for the new node.
75
- * @returns {N} - The newly created BinaryTreeNode.
76
- */
77
- createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
78
72
  private _root;
79
73
  /**
80
74
  * Get the root node of the binary tree.
@@ -85,7 +79,6 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
85
79
  * Get the number of nodes in the binary tree.
86
80
  */
87
81
  get size(): number;
88
- private _loopType;
89
82
  /**
90
83
  * Get the iteration type used in the binary tree.
91
84
  */
@@ -96,12 +89,12 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
96
89
  */
97
90
  set iterationType(v: IterationType);
98
91
  /**
99
- * Swap the data of two nodes in the binary tree.
100
- * @param {N} srcNode - The source node to swap.
101
- * @param {N} destNode - The destination node to swap.
102
- * @returns {N} - The destination node after the swap.
92
+ * Creates a new instance of BinaryTreeNode with the given key and value.
93
+ * @param {BinaryTreeNodeKey} key - The key for the new node.
94
+ * @param {N['val']} val - The value for the new node.
95
+ * @returns {N} - The newly created BinaryTreeNode.
103
96
  */
104
- protected _swap(srcNode: N, destNode: N): N;
97
+ createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
105
98
  /**
106
99
  * Clear the binary tree, removing all nodes.
107
100
  */
@@ -174,7 +167,6 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
174
167
  * @returns the height of the binary tree.
175
168
  */
176
169
  getHeight(beginRoot?: N | BinaryTreeNodeKey | null, iterationType?: IterationType): number;
177
- protected _defaultCallbackByKey: MapCallback<N>;
178
170
  /**
179
171
  * The `getMinHeight` function calculates the minimum height of a binary tree using either a
180
172
  * recursive or iterative approach.
@@ -359,13 +351,6 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
359
351
  * @returns The function `getPredecessor` returns the predecessor node of the given node `node`.
360
352
  */
361
353
  getPredecessor(node: N): N;
362
- /**
363
- * Time complexity is O(n)
364
- * Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
365
- * The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
366
- * the tree's structure should be restored to its original state to maintain the tree's integrity.
367
- * This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
368
- */
369
354
  /**
370
355
  * The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
371
356
  * algorithm and returns an array of values obtained by applying a callback function to each node.
@@ -381,6 +366,21 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
381
366
  * @returns The `morris` function returns an array of `MapCallbackReturn<N>` values.
382
367
  */
383
368
  morris(callback?: MapCallback<N>, pattern?: DFSOrderPattern, beginRoot?: N | null): MapCallbackReturn<N>[];
369
+ /**
370
+ * Swap the data of two nodes in the binary tree.
371
+ * @param {N} srcNode - The source node to swap.
372
+ * @param {N} destNode - The destination node to swap.
373
+ * @returns {N} - The destination node after the swap.
374
+ */
375
+ protected _swap(srcNode: N, destNode: N): N;
376
+ /**
377
+ * Time complexity is O(n)
378
+ * Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
379
+ * The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
380
+ * the tree's structure should be restored to its original state to maintain the tree's integrity.
381
+ * This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
382
+ */
383
+ protected _defaultCallbackByKey: MapCallback<N>;
384
384
  /**
385
385
  * The function `_addTo` adds a new node to a binary tree if there is an available position.
386
386
  * @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to
@@ -106,24 +106,22 @@ class BinaryTree {
106
106
  * @param {BinaryTreeOptions} [options] - The options for the binary tree.
107
107
  */
108
108
  constructor(options) {
109
+ this._loopType = types_1.IterationType.ITERATIVE;
109
110
  this._root = null;
110
111
  this._size = 0;
111
- this._loopType = types_1.IterationType.ITERATIVE;
112
+ /**
113
+ * Time complexity is O(n)
114
+ * Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
115
+ * The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
116
+ * the tree's structure should be restored to its original state to maintain the tree's integrity.
117
+ * This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
118
+ */
112
119
  this._defaultCallbackByKey = node => node.key;
113
120
  if (options !== undefined) {
114
121
  const { iterationType = types_1.IterationType.ITERATIVE } = options;
115
122
  this._loopType = iterationType;
116
123
  }
117
124
  }
118
- /**
119
- * Creates a new instance of BinaryTreeNode with the given key and value.
120
- * @param {BinaryTreeNodeKey} key - The key for the new node.
121
- * @param {N['val']} val - The value for the new node.
122
- * @returns {N} - The newly created BinaryTreeNode.
123
- */
124
- createNode(key, val) {
125
- return new BinaryTreeNode(key, val);
126
- }
127
125
  /**
128
126
  * Get the root node of the binary tree.
129
127
  */
@@ -150,21 +148,13 @@ class BinaryTree {
150
148
  this._loopType = v;
151
149
  }
152
150
  /**
153
- * Swap the data of two nodes in the binary tree.
154
- * @param {N} srcNode - The source node to swap.
155
- * @param {N} destNode - The destination node to swap.
156
- * @returns {N} - The destination node after the swap.
151
+ * Creates a new instance of BinaryTreeNode with the given key and value.
152
+ * @param {BinaryTreeNodeKey} key - The key for the new node.
153
+ * @param {N['val']} val - The value for the new node.
154
+ * @returns {N} - The newly created BinaryTreeNode.
157
155
  */
158
- _swap(srcNode, destNode) {
159
- const { key, val } = destNode;
160
- const tempNode = this.createNode(key, val);
161
- if (tempNode) {
162
- destNode.key = srcNode.key;
163
- destNode.val = srcNode.val;
164
- srcNode.key = tempNode.key;
165
- srcNode.val = tempNode.val;
166
- }
167
- return destNode;
156
+ createNode(key, val) {
157
+ return new BinaryTreeNode(key, val);
168
158
  }
169
159
  /**
170
160
  * Clear the binary tree, removing all nodes.
@@ -834,7 +824,6 @@ class BinaryTree {
834
824
  }
835
825
  return ans;
836
826
  }
837
- // --- start additional methods ---
838
827
  /**
839
828
  * The bfs function performs a breadth-first search traversal on a binary tree, executing a callback
840
829
  * function on each node.
@@ -899,13 +888,7 @@ class BinaryTree {
899
888
  return node;
900
889
  }
901
890
  }
902
- /**
903
- * Time complexity is O(n)
904
- * Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
905
- * The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
906
- * the tree's structure should be restored to its original state to maintain the tree's integrity.
907
- * This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
908
- */
891
+ // --- start additional methods ---
909
892
  /**
910
893
  * The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
911
894
  * algorithm and returns an array of values obtained by applying a callback function to each node.
@@ -1004,6 +987,23 @@ class BinaryTree {
1004
987
  }
1005
988
  return ans;
1006
989
  }
990
+ /**
991
+ * Swap the data of two nodes in the binary tree.
992
+ * @param {N} srcNode - The source node to swap.
993
+ * @param {N} destNode - The destination node to swap.
994
+ * @returns {N} - The destination node after the swap.
995
+ */
996
+ _swap(srcNode, destNode) {
997
+ const { key, val } = destNode;
998
+ const tempNode = this.createNode(key, val);
999
+ if (tempNode) {
1000
+ destNode.key = srcNode.key;
1001
+ destNode.val = srcNode.val;
1002
+ srcNode.key = tempNode.key;
1003
+ srcNode.val = tempNode.val;
1004
+ }
1005
+ return destNode;
1006
+ }
1007
1007
  /**
1008
1008
  * The function `_addTo` adds a new node to a binary tree if there is an available position.
1009
1009
  * @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to
@@ -2,8 +2,8 @@ import { BinaryTreeNodeKey, RBColor, RBTreeNodeNested, RBTreeOptions } from '../
2
2
  import { IBinaryTree } from '../../interfaces';
3
3
  import { BST, BSTNode } from './bst';
4
4
  export declare class RBTreeNode<V = any, FAMILY extends RBTreeNode<V, FAMILY> = RBTreeNodeNested<V>> extends BSTNode<V, FAMILY> {
5
- private _color;
6
5
  constructor(key: BinaryTreeNodeKey, val?: V);
6
+ private _color;
7
7
  get color(): RBColor;
8
8
  set color(value: RBColor);
9
9
  }
@@ -10,6 +10,7 @@ import { BinaryTreeDeletedResult, IterationType } from '../../types';
10
10
  import { IBinaryTree } from '../../interfaces';
11
11
  import { AVLTree, AVLTreeNode } from './avl-tree';
12
12
  export declare class TreeMultisetNode<V = any, FAMILY extends TreeMultisetNode<V, FAMILY> = TreeMultisetNodeNested<V>> extends AVLTreeNode<V, FAMILY> {
13
+ count: number;
13
14
  /**
14
15
  * The constructor function initializes a BinaryTreeNode object with a key, value, and count.
15
16
  * @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier
@@ -21,7 +22,6 @@ export declare class TreeMultisetNode<V = any, FAMILY extends TreeMultisetNode<V
21
22
  * parameter when creating a new instance of the `BinaryTreeNode` class.
22
23
  */
23
24
  constructor(key: BinaryTreeNodeKey, val?: V, count?: number);
24
- count: number;
25
25
  }
26
26
  /**
27
27
  * The only distinction between a TreeMultiset and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
@@ -46,14 +46,6 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
46
46
  * @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
47
47
  */
48
48
  createNode(key: BinaryTreeNodeKey, val?: N['val'], count?: number): N;
49
- /**
50
- * The function swaps the values of two nodes in a binary tree.
51
- * @param {N} srcNode - The source node that needs to be swapped with the destination node.
52
- * @param {N} destNode - The `destNode` parameter represents the destination node where the values
53
- * from `srcNode` will be swapped into.
54
- * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
55
- */
56
- protected _swap(srcNode: N, destNode: N): N;
57
49
  /**
58
50
  * The `add` function adds a new node to a binary search tree, updating the count if the key already
59
51
  * exists, and balancing the tree if necessary.
@@ -114,6 +106,14 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
114
106
  * The clear() function clears the contents of a data structure and sets the count to zero.
115
107
  */
116
108
  clear(): void;
109
+ /**
110
+ * The function swaps the values of two nodes in a binary tree.
111
+ * @param {N} srcNode - The source node that needs to be swapped with the destination node.
112
+ * @param {N} destNode - The `destNode` parameter represents the destination node where the values
113
+ * from `srcNode` will be swapped into.
114
+ * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
115
+ */
116
+ protected _swap(srcNode: N, destNode: N): N;
117
117
  /**
118
118
  * The function sets the value of the "_count" property.
119
119
  * @param {number} v - number
@@ -49,29 +49,6 @@ class TreeMultiset extends avl_tree_1.AVLTree {
49
49
  createNode(key, val, count) {
50
50
  return new TreeMultisetNode(key, val, count);
51
51
  }
52
- /**
53
- * The function swaps the values of two nodes in a binary tree.
54
- * @param {N} srcNode - The source node that needs to be swapped with the destination node.
55
- * @param {N} destNode - The `destNode` parameter represents the destination node where the values
56
- * from `srcNode` will be swapped into.
57
- * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
58
- */
59
- _swap(srcNode, destNode) {
60
- const { key, val, count, height } = destNode;
61
- const tempNode = this.createNode(key, val, count);
62
- if (tempNode) {
63
- tempNode.height = height;
64
- destNode.key = srcNode.key;
65
- destNode.val = srcNode.val;
66
- destNode.count = srcNode.count;
67
- destNode.height = srcNode.height;
68
- srcNode.key = tempNode.key;
69
- srcNode.val = tempNode.val;
70
- srcNode.count = tempNode.count;
71
- srcNode.height = tempNode.height;
72
- }
73
- return destNode;
74
- }
75
52
  /**
76
53
  * The `add` function adds a new node to a binary search tree, updating the count if the key already
77
54
  * exists, and balancing the tree if necessary.
@@ -340,6 +317,29 @@ class TreeMultiset extends avl_tree_1.AVLTree {
340
317
  super.clear();
341
318
  this._setCount(0);
342
319
  }
320
+ /**
321
+ * The function swaps the values of two nodes in a binary tree.
322
+ * @param {N} srcNode - The source node that needs to be swapped with the destination node.
323
+ * @param {N} destNode - The `destNode` parameter represents the destination node where the values
324
+ * from `srcNode` will be swapped into.
325
+ * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
326
+ */
327
+ _swap(srcNode, destNode) {
328
+ const { key, val, count, height } = destNode;
329
+ const tempNode = this.createNode(key, val, count);
330
+ if (tempNode) {
331
+ tempNode.height = height;
332
+ destNode.key = srcNode.key;
333
+ destNode.val = srcNode.val;
334
+ destNode.count = srcNode.count;
335
+ destNode.height = srcNode.height;
336
+ srcNode.key = tempNode.key;
337
+ srcNode.val = tempNode.val;
338
+ srcNode.count = tempNode.count;
339
+ srcNode.height = tempNode.height;
340
+ }
341
+ return destNode;
342
+ }
343
343
  /**
344
344
  * The function sets the value of the "_count" property.
345
345
  * @param {number} v - number
@@ -7,24 +7,6 @@ import { HashFunction } from '../../types';
7
7
  * @license MIT License
8
8
  */
9
9
  export declare class HashMap<K, V> {
10
- get hashFn(): HashFunction<K>;
11
- set hashFn(value: HashFunction<K>);
12
- get table(): Array<Array<[K, V]>>;
13
- set table(value: Array<Array<[K, V]>>);
14
- get capacityMultiplier(): number;
15
- set capacityMultiplier(value: number);
16
- get loadFactor(): number;
17
- set loadFactor(value: number);
18
- get initialCapacity(): number;
19
- set initialCapacity(value: number);
20
- get size(): number;
21
- set size(value: number);
22
- private _initialCapacity;
23
- private _loadFactor;
24
- private _capacityMultiplier;
25
- private _size;
26
- private _table;
27
- private _hashFn;
28
10
  /**
29
11
  * The constructor initializes the properties of a hash table, including the initial capacity, load factor, capacity
30
12
  * multiplier, size, table array, and hash function.
@@ -38,6 +20,31 @@ export declare class HashMap<K, V> {
38
20
  * default hash function converts the key to a string, calculates the sum of the
39
21
  */
40
22
  constructor(initialCapacity?: number, loadFactor?: number, hashFn?: HashFunction<K>);
23
+ private _initialCapacity;
24
+ get initialCapacity(): number;
25
+ set initialCapacity(value: number);
26
+ private _loadFactor;
27
+ get loadFactor(): number;
28
+ set loadFactor(value: number);
29
+ private _capacityMultiplier;
30
+ get capacityMultiplier(): number;
31
+ set capacityMultiplier(value: number);
32
+ private _size;
33
+ get size(): number;
34
+ set size(value: number);
35
+ private _table;
36
+ get table(): Array<Array<[K, V]>>;
37
+ set table(value: Array<Array<[K, V]>>);
38
+ private _hashFn;
39
+ get hashFn(): HashFunction<K>;
40
+ set hashFn(value: HashFunction<K>);
41
+ set(key: K, value: V): void;
42
+ get(key: K): V | undefined;
43
+ delete(key: K): void;
44
+ entries(): IterableIterator<[K, V]>;
45
+ [Symbol.iterator](): IterableIterator<[K, V]>;
46
+ clear(): void;
47
+ isEmpty(): boolean;
41
48
  private _hash;
42
49
  /**
43
50
  * The `resizeTable` function resizes the table used in a hash map by creating a new table with a specified capacity and
@@ -46,11 +53,4 @@ export declare class HashMap<K, V> {
46
53
  * the number of buckets that the new table should have.
47
54
  */
48
55
  private resizeTable;
49
- set(key: K, value: V): void;
50
- get(key: K): V | undefined;
51
- delete(key: K): void;
52
- entries(): IterableIterator<[K, V]>;
53
- [Symbol.iterator](): IterableIterator<[K, V]>;
54
- clear(): void;
55
- isEmpty(): boolean;
56
56
  }
@@ -9,42 +9,6 @@ exports.HashMap = void 0;
9
9
  * @license MIT License
10
10
  */
11
11
  class HashMap {
12
- get hashFn() {
13
- return this._hashFn;
14
- }
15
- set hashFn(value) {
16
- this._hashFn = value;
17
- }
18
- get table() {
19
- return this._table;
20
- }
21
- set table(value) {
22
- this._table = value;
23
- }
24
- get capacityMultiplier() {
25
- return this._capacityMultiplier;
26
- }
27
- set capacityMultiplier(value) {
28
- this._capacityMultiplier = value;
29
- }
30
- get loadFactor() {
31
- return this._loadFactor;
32
- }
33
- set loadFactor(value) {
34
- this._loadFactor = value;
35
- }
36
- get initialCapacity() {
37
- return this._initialCapacity;
38
- }
39
- set initialCapacity(value) {
40
- this._initialCapacity = value;
41
- }
42
- get size() {
43
- return this._size;
44
- }
45
- set size(value) {
46
- this._size = value;
47
- }
48
12
  /**
49
13
  * The constructor initializes the properties of a hash table, including the initial capacity, load factor, capacity
50
14
  * multiplier, size, table array, and hash function.
@@ -74,30 +38,41 @@ class HashMap {
74
38
  return hash % this.table.length;
75
39
  });
76
40
  }
77
- _hash(key) {
78
- return this._hashFn(key);
41
+ get initialCapacity() {
42
+ return this._initialCapacity;
79
43
  }
80
- /**
81
- * The `resizeTable` function resizes the table used in a hash map by creating a new table with a specified capacity and
82
- * rehashing the key-value pairs from the old table into the new table.
83
- * @param {number} newCapacity - The newCapacity parameter is the desired capacity for the resized table. It represents
84
- * the number of buckets that the new table should have.
85
- */
86
- resizeTable(newCapacity) {
87
- const newTable = new Array(newCapacity);
88
- for (const bucket of this._table) {
89
- // Note that this is this._table
90
- if (bucket) {
91
- for (const [key, value] of bucket) {
92
- const newIndex = this._hash(key) % newCapacity;
93
- if (!newTable[newIndex]) {
94
- newTable[newIndex] = [];
95
- }
96
- newTable[newIndex].push([key, value]);
97
- }
98
- }
99
- }
100
- this._table = newTable; // Again, here is this._table
44
+ set initialCapacity(value) {
45
+ this._initialCapacity = value;
46
+ }
47
+ get loadFactor() {
48
+ return this._loadFactor;
49
+ }
50
+ set loadFactor(value) {
51
+ this._loadFactor = value;
52
+ }
53
+ get capacityMultiplier() {
54
+ return this._capacityMultiplier;
55
+ }
56
+ set capacityMultiplier(value) {
57
+ this._capacityMultiplier = value;
58
+ }
59
+ get size() {
60
+ return this._size;
61
+ }
62
+ set size(value) {
63
+ this._size = value;
64
+ }
65
+ get table() {
66
+ return this._table;
67
+ }
68
+ set table(value) {
69
+ this._table = value;
70
+ }
71
+ get hashFn() {
72
+ return this._hashFn;
73
+ }
74
+ set hashFn(value) {
75
+ this._hashFn = value;
101
76
  }
102
77
  set(key, value) {
103
78
  const loadFactor = this.size / this.table.length;
@@ -167,5 +142,30 @@ class HashMap {
167
142
  isEmpty() {
168
143
  return this.size === 0;
169
144
  }
145
+ _hash(key) {
146
+ return this._hashFn(key);
147
+ }
148
+ /**
149
+ * The `resizeTable` function resizes the table used in a hash map by creating a new table with a specified capacity and
150
+ * rehashing the key-value pairs from the old table into the new table.
151
+ * @param {number} newCapacity - The newCapacity parameter is the desired capacity for the resized table. It represents
152
+ * the number of buckets that the new table should have.
153
+ */
154
+ resizeTable(newCapacity) {
155
+ const newTable = new Array(newCapacity);
156
+ for (const bucket of this._table) {
157
+ // Note that this is this._table
158
+ if (bucket) {
159
+ for (const [key, value] of bucket) {
160
+ const newIndex = this._hash(key) % newCapacity;
161
+ if (!newTable[newIndex]) {
162
+ newTable[newIndex] = [];
163
+ }
164
+ newTable[newIndex].push([key, value]);
165
+ }
166
+ }
167
+ }
168
+ this._table = newTable; // Again, here is this._table
169
+ }
170
170
  }
171
171
  exports.HashMap = HashMap;