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
@@ -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
@@ -102,24 +102,22 @@ export class BinaryTree {
102
102
  * @param {BinaryTreeOptions} [options] - The options for the binary tree.
103
103
  */
104
104
  constructor(options) {
105
+ this._loopType = IterationType.ITERATIVE;
105
106
  this._root = null;
106
107
  this._size = 0;
107
- this._loopType = IterationType.ITERATIVE;
108
+ /**
109
+ * Time complexity is O(n)
110
+ * Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
111
+ * The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
112
+ * the tree's structure should be restored to its original state to maintain the tree's integrity.
113
+ * This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
114
+ */
108
115
  this._defaultCallbackByKey = node => node.key;
109
116
  if (options !== undefined) {
110
117
  const { iterationType = IterationType.ITERATIVE } = options;
111
118
  this._loopType = iterationType;
112
119
  }
113
120
  }
114
- /**
115
- * Creates a new instance of BinaryTreeNode with the given key and value.
116
- * @param {BinaryTreeNodeKey} key - The key for the new node.
117
- * @param {N['val']} val - The value for the new node.
118
- * @returns {N} - The newly created BinaryTreeNode.
119
- */
120
- createNode(key, val) {
121
- return new BinaryTreeNode(key, val);
122
- }
123
121
  /**
124
122
  * Get the root node of the binary tree.
125
123
  */
@@ -146,21 +144,13 @@ export class BinaryTree {
146
144
  this._loopType = v;
147
145
  }
148
146
  /**
149
- * Swap the data of two nodes in the binary tree.
150
- * @param {N} srcNode - The source node to swap.
151
- * @param {N} destNode - The destination node to swap.
152
- * @returns {N} - The destination node after the swap.
147
+ * Creates a new instance of BinaryTreeNode with the given key and value.
148
+ * @param {BinaryTreeNodeKey} key - The key for the new node.
149
+ * @param {N['val']} val - The value for the new node.
150
+ * @returns {N} - The newly created BinaryTreeNode.
153
151
  */
154
- _swap(srcNode, destNode) {
155
- const { key, val } = destNode;
156
- const tempNode = this.createNode(key, val);
157
- if (tempNode) {
158
- destNode.key = srcNode.key;
159
- destNode.val = srcNode.val;
160
- srcNode.key = tempNode.key;
161
- srcNode.val = tempNode.val;
162
- }
163
- return destNode;
152
+ createNode(key, val) {
153
+ return new BinaryTreeNode(key, val);
164
154
  }
165
155
  /**
166
156
  * Clear the binary tree, removing all nodes.
@@ -830,7 +820,6 @@ export class BinaryTree {
830
820
  }
831
821
  return ans;
832
822
  }
833
- // --- start additional methods ---
834
823
  /**
835
824
  * The bfs function performs a breadth-first search traversal on a binary tree, executing a callback
836
825
  * function on each node.
@@ -895,13 +884,7 @@ export class BinaryTree {
895
884
  return node;
896
885
  }
897
886
  }
898
- /**
899
- * Time complexity is O(n)
900
- * Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
901
- * The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
902
- * the tree's structure should be restored to its original state to maintain the tree's integrity.
903
- * This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
904
- */
887
+ // --- start additional methods ---
905
888
  /**
906
889
  * The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
907
890
  * algorithm and returns an array of values obtained by applying a callback function to each node.
@@ -1000,6 +983,23 @@ export class BinaryTree {
1000
983
  }
1001
984
  return ans;
1002
985
  }
986
+ /**
987
+ * Swap the data of two nodes in the binary tree.
988
+ * @param {N} srcNode - The source node to swap.
989
+ * @param {N} destNode - The destination node to swap.
990
+ * @returns {N} - The destination node after the swap.
991
+ */
992
+ _swap(srcNode, destNode) {
993
+ const { key, val } = destNode;
994
+ const tempNode = this.createNode(key, val);
995
+ if (tempNode) {
996
+ destNode.key = srcNode.key;
997
+ destNode.val = srcNode.val;
998
+ srcNode.key = tempNode.key;
999
+ srcNode.val = tempNode.val;
1000
+ }
1001
+ return destNode;
1002
+ }
1003
1003
  /**
1004
1004
  * The function `_addTo` adds a new node to a binary tree if there is an available position.
1005
1005
  * @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
@@ -45,29 +45,6 @@ export class TreeMultiset extends AVLTree {
45
45
  createNode(key, val, count) {
46
46
  return new TreeMultisetNode(key, val, count);
47
47
  }
48
- /**
49
- * The function swaps the values of two nodes in a binary tree.
50
- * @param {N} srcNode - The source node that needs to be swapped with the destination node.
51
- * @param {N} destNode - The `destNode` parameter represents the destination node where the values
52
- * from `srcNode` will be swapped into.
53
- * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
54
- */
55
- _swap(srcNode, destNode) {
56
- const { key, val, count, height } = destNode;
57
- const tempNode = this.createNode(key, val, count);
58
- if (tempNode) {
59
- tempNode.height = height;
60
- destNode.key = srcNode.key;
61
- destNode.val = srcNode.val;
62
- destNode.count = srcNode.count;
63
- destNode.height = srcNode.height;
64
- srcNode.key = tempNode.key;
65
- srcNode.val = tempNode.val;
66
- srcNode.count = tempNode.count;
67
- srcNode.height = tempNode.height;
68
- }
69
- return destNode;
70
- }
71
48
  /**
72
49
  * The `add` function adds a new node to a binary search tree, updating the count if the key already
73
50
  * exists, and balancing the tree if necessary.
@@ -336,6 +313,29 @@ export class TreeMultiset extends AVLTree {
336
313
  super.clear();
337
314
  this._setCount(0);
338
315
  }
316
+ /**
317
+ * The function swaps the values of two nodes in a binary tree.
318
+ * @param {N} srcNode - The source node that needs to be swapped with the destination node.
319
+ * @param {N} destNode - The `destNode` parameter represents the destination node where the values
320
+ * from `srcNode` will be swapped into.
321
+ * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
322
+ */
323
+ _swap(srcNode, destNode) {
324
+ const { key, val, count, height } = destNode;
325
+ const tempNode = this.createNode(key, val, count);
326
+ if (tempNode) {
327
+ tempNode.height = height;
328
+ destNode.key = srcNode.key;
329
+ destNode.val = srcNode.val;
330
+ destNode.count = srcNode.count;
331
+ destNode.height = srcNode.height;
332
+ srcNode.key = tempNode.key;
333
+ srcNode.val = tempNode.val;
334
+ srcNode.count = tempNode.count;
335
+ srcNode.height = tempNode.height;
336
+ }
337
+ return destNode;
338
+ }
339
339
  /**
340
340
  * The function sets the value of the "_count" property.
341
341
  * @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
  }
@@ -6,42 +6,6 @@
6
6
  * @license MIT License
7
7
  */
8
8
  export class HashMap {
9
- get hashFn() {
10
- return this._hashFn;
11
- }
12
- set hashFn(value) {
13
- this._hashFn = value;
14
- }
15
- get table() {
16
- return this._table;
17
- }
18
- set table(value) {
19
- this._table = value;
20
- }
21
- get capacityMultiplier() {
22
- return this._capacityMultiplier;
23
- }
24
- set capacityMultiplier(value) {
25
- this._capacityMultiplier = value;
26
- }
27
- get loadFactor() {
28
- return this._loadFactor;
29
- }
30
- set loadFactor(value) {
31
- this._loadFactor = value;
32
- }
33
- get initialCapacity() {
34
- return this._initialCapacity;
35
- }
36
- set initialCapacity(value) {
37
- this._initialCapacity = value;
38
- }
39
- get size() {
40
- return this._size;
41
- }
42
- set size(value) {
43
- this._size = value;
44
- }
45
9
  /**
46
10
  * The constructor initializes the properties of a hash table, including the initial capacity, load factor, capacity
47
11
  * multiplier, size, table array, and hash function.
@@ -71,30 +35,41 @@ export class HashMap {
71
35
  return hash % this.table.length;
72
36
  });
73
37
  }
74
- _hash(key) {
75
- return this._hashFn(key);
38
+ get initialCapacity() {
39
+ return this._initialCapacity;
76
40
  }
77
- /**
78
- * The `resizeTable` function resizes the table used in a hash map by creating a new table with a specified capacity and
79
- * rehashing the key-value pairs from the old table into the new table.
80
- * @param {number} newCapacity - The newCapacity parameter is the desired capacity for the resized table. It represents
81
- * the number of buckets that the new table should have.
82
- */
83
- resizeTable(newCapacity) {
84
- const newTable = new Array(newCapacity);
85
- for (const bucket of this._table) {
86
- // Note that this is this._table
87
- if (bucket) {
88
- for (const [key, value] of bucket) {
89
- const newIndex = this._hash(key) % newCapacity;
90
- if (!newTable[newIndex]) {
91
- newTable[newIndex] = [];
92
- }
93
- newTable[newIndex].push([key, value]);
94
- }
95
- }
96
- }
97
- this._table = newTable; // Again, here is this._table
41
+ set initialCapacity(value) {
42
+ this._initialCapacity = value;
43
+ }
44
+ get loadFactor() {
45
+ return this._loadFactor;
46
+ }
47
+ set loadFactor(value) {
48
+ this._loadFactor = value;
49
+ }
50
+ get capacityMultiplier() {
51
+ return this._capacityMultiplier;
52
+ }
53
+ set capacityMultiplier(value) {
54
+ this._capacityMultiplier = value;
55
+ }
56
+ get size() {
57
+ return this._size;
58
+ }
59
+ set size(value) {
60
+ this._size = value;
61
+ }
62
+ get table() {
63
+ return this._table;
64
+ }
65
+ set table(value) {
66
+ this._table = value;
67
+ }
68
+ get hashFn() {
69
+ return this._hashFn;
70
+ }
71
+ set hashFn(value) {
72
+ this._hashFn = value;
98
73
  }
99
74
  set(key, value) {
100
75
  const loadFactor = this.size / this.table.length;
@@ -164,4 +139,29 @@ export class HashMap {
164
139
  isEmpty() {
165
140
  return this.size === 0;
166
141
  }
142
+ _hash(key) {
143
+ return this._hashFn(key);
144
+ }
145
+ /**
146
+ * The `resizeTable` function resizes the table used in a hash map by creating a new table with a specified capacity and
147
+ * rehashing the key-value pairs from the old table into the new table.
148
+ * @param {number} newCapacity - The newCapacity parameter is the desired capacity for the resized table. It represents
149
+ * the number of buckets that the new table should have.
150
+ */
151
+ resizeTable(newCapacity) {
152
+ const newTable = new Array(newCapacity);
153
+ for (const bucket of this._table) {
154
+ // Note that this is this._table
155
+ if (bucket) {
156
+ for (const [key, value] of bucket) {
157
+ const newIndex = this._hash(key) % newCapacity;
158
+ if (!newTable[newIndex]) {
159
+ newTable[newIndex] = [];
160
+ }
161
+ newTable[newIndex].push([key, value]);
162
+ }
163
+ }
164
+ }
165
+ this._table = newTable; // Again, here is this._table
166
+ }
167
167
  }
@@ -13,19 +13,46 @@ export declare class HashTableNode<K, V> {
13
13
  }
14
14
  import { HashFunction } from '../../types';
15
15
  export declare class HashTable<K, V> {
16
- get hashFn(): HashFunction<K>;
17
- set hashFn(value: HashFunction<K>);
18
- get buckets(): Array<HashTableNode<K, V> | null>;
19
- set buckets(value: Array<HashTableNode<K, V> | null>);
20
- get capacity(): number;
21
- set capacity(value: number);
22
16
  private static readonly DEFAULT_CAPACITY;
23
17
  private static readonly LOAD_FACTOR;
18
+ constructor(capacity?: number, hashFn?: HashFunction<K>);
24
19
  private _capacity;
20
+ get capacity(): number;
21
+ set capacity(value: number);
25
22
  private _size;
23
+ get size(): number;
26
24
  private _buckets;
25
+ get buckets(): Array<HashTableNode<K, V> | null>;
26
+ set buckets(value: Array<HashTableNode<K, V> | null>);
27
27
  private _hashFn;
28
- constructor(capacity?: number, hashFn?: HashFunction<K>);
28
+ get hashFn(): HashFunction<K>;
29
+ set hashFn(value: HashFunction<K>);
30
+ /**
31
+ * The set function adds a key-value pair to the hash table, handling collisions and resizing if necessary.
32
+ * @param {K} key - The key parameter represents the key of the key-value pair that you want to insert into the hash
33
+ * table. It is of type K, which is a generic type representing the key's data type.
34
+ * @param {V} val - The parameter `val` represents the value that you want to associate with the given key in the hash
35
+ * table.
36
+ * @returns Nothing is being returned. The return type of the `put` method is `void`, which means it does not return any
37
+ * value.
38
+ */
39
+ set(key: K, val: V): void;
40
+ /**
41
+ * The `get` function retrieves the value associated with a given key from a hash table.
42
+ * @param {K} key - The `key` parameter represents the key of the element that we want to retrieve from the data
43
+ * structure.
44
+ * @returns The method is returning the value associated with the given key if it exists in the hash table. If the key is
45
+ * not found, it returns `undefined`.
46
+ */
47
+ get(key: K): V | undefined;
48
+ /**
49
+ * The delete function removes a key-value pair from a hash table.
50
+ * @param {K} key - The `key` parameter represents the key of the key-value pair that needs to be removed from the hash
51
+ * table.
52
+ * @returns Nothing is being returned. The `delete` method has a return type of `void`, which means it does not return
53
+ * any value.
54
+ */
55
+ delete(key: K): void;
29
56
  /**
30
57
  * The function `_defaultHashFn` calculates the hash value of a given key and returns the remainder when divided by the
31
58
  * capacity of the data structure.
@@ -71,36 +98,9 @@ export declare class HashTable<K, V> {
71
98
  * @returns a number, which is the hash value of the key.
72
99
  */
73
100
  protected _objectHash(key: K): number;
74
- /**
75
- * The set function adds a key-value pair to the hash table, handling collisions and resizing if necessary.
76
- * @param {K} key - The key parameter represents the key of the key-value pair that you want to insert into the hash
77
- * table. It is of type K, which is a generic type representing the key's data type.
78
- * @param {V} val - The parameter `val` represents the value that you want to associate with the given key in the hash
79
- * table.
80
- * @returns Nothing is being returned. The return type of the `put` method is `void`, which means it does not return any
81
- * value.
82
- */
83
- set(key: K, val: V): void;
84
- /**
85
- * The `get` function retrieves the value associated with a given key from a hash table.
86
- * @param {K} key - The `key` parameter represents the key of the element that we want to retrieve from the data
87
- * structure.
88
- * @returns The method is returning the value associated with the given key if it exists in the hash table. If the key is
89
- * not found, it returns `undefined`.
90
- */
91
- get(key: K): V | undefined;
92
- /**
93
- * The delete function removes a key-value pair from a hash table.
94
- * @param {K} key - The `key` parameter represents the key of the key-value pair that needs to be removed from the hash
95
- * table.
96
- * @returns Nothing is being returned. The `delete` method has a return type of `void`, which means it does not return
97
- * any value.
98
- */
99
- delete(key: K): void;
100
101
  /**
101
102
  * The `expand` function increases the capacity of a hash table by creating a new array of buckets with double the
102
103
  * capacity and rehashing all the existing key-value pairs into the new buckets.
103
104
  */
104
105
  protected _expand(): void;
105
- get size(): number;
106
106
  }