data-structure-typed 1.49.5 → 1.49.7
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.
- package/CHANGELOG.md +1 -1
- package/README.md +17 -23
- package/README_zh-CN.md +2 -0
- package/benchmark/report.html +14 -23
- package/benchmark/report.json +158 -251
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +53 -48
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +153 -130
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +194 -153
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/cjs/data-structures/binary-tree/bst.js +114 -91
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +46 -39
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +58 -51
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +24 -27
- package/dist/cjs/data-structures/hash/hash-map.js +35 -35
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/index.d.ts +0 -1
- package/dist/cjs/data-structures/hash/index.js +0 -1
- package/dist/cjs/data-structures/hash/index.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +2 -1
- package/dist/cjs/data-structures/heap/heap.js +13 -13
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.js +1 -1
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.js +1 -1
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +1 -3
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +2 -8
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +15 -18
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.d.ts +2 -7
- package/dist/cjs/data-structures/matrix/matrix.js +0 -7
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +2 -11
- package/dist/cjs/data-structures/queue/deque.js +9 -13
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +13 -13
- package/dist/cjs/data-structures/queue/queue.js +29 -25
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.js +2 -3
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +2 -2
- package/dist/cjs/data-structures/trie/trie.js +9 -5
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/cjs/types/common.d.ts +3 -3
- package/dist/cjs/types/common.js +2 -2
- package/dist/cjs/types/common.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +5 -2
- package/dist/cjs/types/data-structures/hash/index.d.ts +0 -1
- package/dist/cjs/types/data-structures/hash/index.js +0 -1
- package/dist/cjs/types/data-structures/hash/index.js.map +1 -1
- package/dist/cjs/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/cjs/types/data-structures/linked-list/index.d.ts +1 -0
- package/dist/cjs/types/data-structures/linked-list/index.js +1 -0
- package/dist/cjs/types/data-structures/linked-list/index.js.map +1 -1
- package/dist/cjs/types/data-structures/linked-list/skip-linked-list.d.ts +4 -1
- package/dist/cjs/types/data-structures/matrix/index.d.ts +1 -0
- package/dist/cjs/types/data-structures/matrix/index.js +1 -0
- package/dist/cjs/types/data-structures/matrix/index.js.map +1 -1
- package/dist/cjs/types/data-structures/matrix/matrix.d.ts +7 -1
- package/dist/cjs/types/data-structures/queue/deque.d.ts +3 -1
- package/dist/cjs/types/data-structures/trie/trie.d.ts +3 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +53 -48
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +153 -130
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +194 -153
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/mjs/data-structures/binary-tree/bst.js +114 -91
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +46 -39
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +58 -51
- package/dist/mjs/data-structures/hash/hash-map.d.ts +24 -27
- package/dist/mjs/data-structures/hash/hash-map.js +35 -37
- package/dist/mjs/data-structures/hash/index.d.ts +0 -1
- package/dist/mjs/data-structures/hash/index.js +0 -1
- package/dist/mjs/data-structures/heap/heap.d.ts +2 -1
- package/dist/mjs/data-structures/heap/heap.js +19 -20
- package/dist/mjs/data-structures/heap/max-heap.js +1 -1
- package/dist/mjs/data-structures/heap/min-heap.js +1 -1
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +1 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +2 -5
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +2 -8
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +16 -23
- package/dist/mjs/data-structures/matrix/matrix.d.ts +2 -7
- package/dist/mjs/data-structures/matrix/matrix.js +0 -7
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +1 -1
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +1 -1
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +1 -1
- package/dist/mjs/data-structures/queue/deque.d.ts +2 -11
- package/dist/mjs/data-structures/queue/deque.js +9 -14
- package/dist/mjs/data-structures/queue/queue.d.ts +13 -13
- package/dist/mjs/data-structures/queue/queue.js +30 -28
- package/dist/mjs/data-structures/stack/stack.js +3 -5
- package/dist/mjs/data-structures/trie/trie.d.ts +2 -2
- package/dist/mjs/data-structures/trie/trie.js +10 -9
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/mjs/types/common.d.ts +3 -3
- package/dist/mjs/types/common.js +2 -2
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/mjs/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +5 -2
- package/dist/mjs/types/data-structures/hash/index.d.ts +0 -1
- package/dist/mjs/types/data-structures/hash/index.js +0 -1
- package/dist/mjs/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/mjs/types/data-structures/linked-list/index.d.ts +1 -0
- package/dist/mjs/types/data-structures/linked-list/index.js +1 -0
- package/dist/mjs/types/data-structures/linked-list/skip-linked-list.d.ts +4 -1
- package/dist/mjs/types/data-structures/matrix/index.d.ts +1 -0
- package/dist/mjs/types/data-structures/matrix/index.js +1 -0
- package/dist/mjs/types/data-structures/matrix/matrix.d.ts +7 -1
- package/dist/mjs/types/data-structures/queue/deque.d.ts +3 -1
- package/dist/mjs/types/data-structures/trie/trie.d.ts +3 -1
- package/dist/umd/data-structure-typed.js +620 -823
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +58 -53
- package/src/data-structures/binary-tree/binary-tree.ts +255 -211
- package/src/data-structures/binary-tree/bst.ts +126 -107
- package/src/data-structures/binary-tree/rb-tree.ts +66 -64
- package/src/data-structures/binary-tree/tree-multimap.ts +62 -56
- package/src/data-structures/hash/hash-map.ts +46 -50
- package/src/data-structures/hash/index.ts +0 -1
- package/src/data-structures/heap/heap.ts +20 -19
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -5
- package/src/data-structures/linked-list/skip-linked-list.ts +15 -16
- package/src/data-structures/matrix/matrix.ts +2 -10
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +11 -15
- package/src/data-structures/queue/queue.ts +29 -28
- package/src/data-structures/stack/stack.ts +3 -6
- package/src/data-structures/trie/trie.ts +10 -11
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +6 -2
- package/src/types/data-structures/hash/index.ts +0 -1
- package/src/types/data-structures/heap/heap.ts +1 -1
- package/src/types/data-structures/linked-list/index.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -1
- package/src/types/data-structures/matrix/index.ts +1 -0
- package/src/types/data-structures/matrix/matrix.ts +7 -1
- package/src/types/data-structures/queue/deque.ts +1 -1
- package/src/types/data-structures/trie/trie.ts +1 -1
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +4 -12
- package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +37 -0
- package/test/performance/data-structures/binary-tree/binary-tree.test.ts +6 -16
- package/test/performance/data-structures/binary-tree/bst.test.ts +5 -13
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +5 -15
- package/test/performance/data-structures/comparison/comparison.test.ts +13 -36
- package/test/performance/data-structures/graph/directed-graph.test.ts +3 -14
- package/test/performance/data-structures/hash/hash-map.test.ts +11 -34
- package/test/performance/data-structures/heap/heap.test.ts +5 -18
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +0 -1
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +0 -2
- package/test/performance/data-structures/priority-queue/max-priority-queue.test.ts +2 -4
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +4 -14
- package/test/performance/data-structures/queue/queue.test.ts +8 -25
- package/test/performance/data-structures/stack/stack.test.ts +6 -18
- package/test/performance/data-structures/trie/trie.test.ts +2 -6
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +6 -5
- package/test/unit/data-structures/binary-tree/bst.test.ts +17 -1
- package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
- package/test/unit/data-structures/queue/deque.test.ts +5 -5
- package/test/unit/data-structures/trie/trie.test.ts +1 -1
- package/dist/cjs/data-structures/hash/hash-table.d.ts +0 -108
- package/dist/cjs/data-structures/hash/hash-table.js +0 -282
- package/dist/cjs/data-structures/hash/hash-table.js.map +0 -1
- package/dist/cjs/types/data-structures/hash/hash-table.d.ts +0 -1
- package/dist/cjs/types/data-structures/hash/hash-table.js +0 -3
- package/dist/cjs/types/data-structures/hash/hash-table.js.map +0 -1
- package/dist/cjs/types/data-structures/matrix/matrix2d.d.ts +0 -1
- package/dist/cjs/types/data-structures/matrix/matrix2d.js +0 -3
- package/dist/cjs/types/data-structures/matrix/matrix2d.js.map +0 -1
- package/dist/cjs/types/data-structures/matrix/vector2d.d.ts +0 -1
- package/dist/cjs/types/data-structures/matrix/vector2d.js +0 -3
- package/dist/cjs/types/data-structures/matrix/vector2d.js.map +0 -1
- package/dist/mjs/data-structures/hash/hash-table.d.ts +0 -108
- package/dist/mjs/data-structures/hash/hash-table.js +0 -283
- package/dist/mjs/types/data-structures/hash/hash-table.d.ts +0 -1
- package/dist/mjs/types/data-structures/hash/hash-table.js +0 -1
- package/dist/mjs/types/data-structures/matrix/matrix2d.d.ts +0 -1
- package/dist/mjs/types/data-structures/matrix/matrix2d.js +0 -1
- package/dist/mjs/types/data-structures/matrix/vector2d.d.ts +0 -1
- package/dist/mjs/types/data-structures/matrix/vector2d.js +0 -1
- package/src/data-structures/hash/hash-table.ts +0 -318
- package/src/types/data-structures/hash/hash-table.ts +0 -1
- package/src/types/data-structures/matrix/matrix2d.ts +0 -1
- package/src/types/data-structures/matrix/vector2d.ts +0 -1
- package/test/performance/data-structures/binary-tree/overall.test.ts +0 -0
- package/test/performance/data-structures/matrix/matrix2d.test.ts +0 -0
- package/test/performance/data-structures/matrix/vector2d.test.ts +0 -0
- package/test/unit/data-structures/hash/hash-table.test.ts +0 -238
|
@@ -21,10 +21,10 @@ export class TreeMultimapNode extends AVLTreeNode {
|
|
|
21
21
|
* The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
22
22
|
*/
|
|
23
23
|
export class TreeMultimap extends AVLTree {
|
|
24
|
-
constructor(
|
|
24
|
+
constructor(keysOrNodesOrEntries = [], options) {
|
|
25
25
|
super([], options);
|
|
26
|
-
if (
|
|
27
|
-
this.addMany(
|
|
26
|
+
if (keysOrNodesOrEntries)
|
|
27
|
+
this.addMany(keysOrNodesOrEntries);
|
|
28
28
|
}
|
|
29
29
|
_count = 0;
|
|
30
30
|
// TODO the _count is not accurate after nodes count modified
|
|
@@ -53,26 +53,8 @@ export class TreeMultimap extends AVLTree {
|
|
|
53
53
|
});
|
|
54
54
|
}
|
|
55
55
|
/**
|
|
56
|
-
* The function
|
|
57
|
-
* @param
|
|
58
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
|
|
59
|
-
* class.
|
|
60
|
-
*/
|
|
61
|
-
isNode(exemplar) {
|
|
62
|
-
return exemplar instanceof TreeMultimapNode;
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
66
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
67
|
-
* data type.
|
|
68
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
69
|
-
*/
|
|
70
|
-
isNotNodeInstance(potentialKey) {
|
|
71
|
-
return !(potentialKey instanceof TreeMultimapNode);
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
75
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, which means it
|
|
56
|
+
* The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
|
|
57
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, which means it
|
|
76
58
|
* can be one of the following:
|
|
77
59
|
* @param {V} [value] - The `value` parameter is an optional argument that represents the value
|
|
78
60
|
* associated with the node. It is of type `V`, which can be any data type. If no value is provided,
|
|
@@ -81,16 +63,16 @@ export class TreeMultimap extends AVLTree {
|
|
|
81
63
|
* times the value should be added to the node. If not provided, it defaults to 1.
|
|
82
64
|
* @returns a node of type `N` or `undefined`.
|
|
83
65
|
*/
|
|
84
|
-
exemplarToNode(
|
|
66
|
+
exemplarToNode(keyOrNodeOrEntry, value, count = 1) {
|
|
85
67
|
let node;
|
|
86
|
-
if (
|
|
68
|
+
if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
|
|
87
69
|
return;
|
|
88
70
|
}
|
|
89
|
-
else if (this.isNode(
|
|
90
|
-
node =
|
|
71
|
+
else if (this.isNode(keyOrNodeOrEntry)) {
|
|
72
|
+
node = keyOrNodeOrEntry;
|
|
91
73
|
}
|
|
92
|
-
else if (this.isEntry(
|
|
93
|
-
const [key, value] =
|
|
74
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
75
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
94
76
|
if (key === undefined || key === null) {
|
|
95
77
|
return;
|
|
96
78
|
}
|
|
@@ -98,8 +80,8 @@ export class TreeMultimap extends AVLTree {
|
|
|
98
80
|
node = this.createNode(key, value, count);
|
|
99
81
|
}
|
|
100
82
|
}
|
|
101
|
-
else if (this.isNotNodeInstance(
|
|
102
|
-
node = this.createNode(
|
|
83
|
+
else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
84
|
+
node = this.createNode(keyOrNodeOrEntry, value, count);
|
|
103
85
|
}
|
|
104
86
|
else {
|
|
105
87
|
return;
|
|
@@ -107,12 +89,31 @@ export class TreeMultimap extends AVLTree {
|
|
|
107
89
|
return node;
|
|
108
90
|
}
|
|
109
91
|
/**
|
|
110
|
-
*
|
|
111
|
-
*
|
|
92
|
+
* The function checks if an keyOrNodeOrEntry is an instance of the TreeMultimapNode class.
|
|
93
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
94
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the TreeMultimapNode
|
|
95
|
+
* class.
|
|
112
96
|
*/
|
|
97
|
+
isNode(keyOrNodeOrEntry) {
|
|
98
|
+
return keyOrNodeOrEntry instanceof TreeMultimapNode;
|
|
99
|
+
}
|
|
113
100
|
/**
|
|
114
|
-
*
|
|
115
|
-
*
|
|
101
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
102
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
103
|
+
* data type.
|
|
104
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
105
|
+
*/
|
|
106
|
+
isNotNodeInstance(potentialKey) {
|
|
107
|
+
return !(potentialKey instanceof TreeMultimapNode);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Time Complexity: O(log n)
|
|
111
|
+
* Space Complexity: O(1)
|
|
112
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
113
|
+
*/
|
|
114
|
+
/**
|
|
115
|
+
* Time Complexity: O(log n)
|
|
116
|
+
* Space Complexity: O(1)
|
|
116
117
|
*
|
|
117
118
|
* The function overrides the add method of a binary tree node and adds a new node to the tree.
|
|
118
119
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
@@ -129,21 +130,22 @@ export class TreeMultimap extends AVLTree {
|
|
|
129
130
|
add(keyOrNodeOrEntry, value, count = 1) {
|
|
130
131
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value, count);
|
|
131
132
|
if (newNode === undefined)
|
|
132
|
-
return;
|
|
133
|
+
return false;
|
|
133
134
|
const orgNodeCount = newNode?.count || 0;
|
|
134
135
|
const inserted = super.add(newNode);
|
|
135
136
|
if (inserted) {
|
|
136
137
|
this._count += orgNodeCount;
|
|
137
138
|
}
|
|
138
|
-
return
|
|
139
|
+
return true;
|
|
139
140
|
}
|
|
140
141
|
/**
|
|
141
|
-
* Time Complexity: O(k log n)
|
|
142
|
-
* Space Complexity: O(1)
|
|
142
|
+
* Time Complexity: O(k log n)
|
|
143
|
+
* Space Complexity: O(1)
|
|
144
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
143
145
|
*/
|
|
144
146
|
/**
|
|
145
|
-
* Time Complexity: O(k log n)
|
|
146
|
-
* Space Complexity: O(1)
|
|
147
|
+
* Time Complexity: O(k log n)
|
|
148
|
+
* Space Complexity: O(1)
|
|
147
149
|
*
|
|
148
150
|
* The function overrides the addMany method to add multiple keys, nodes, or entries to a data
|
|
149
151
|
* structure.
|
|
@@ -155,12 +157,13 @@ export class TreeMultimap extends AVLTree {
|
|
|
155
157
|
return super.addMany(keysOrNodesOrEntries);
|
|
156
158
|
}
|
|
157
159
|
/**
|
|
158
|
-
* Time Complexity: O(
|
|
159
|
-
* Space Complexity: O(
|
|
160
|
+
* Time Complexity: O(n log n)
|
|
161
|
+
* Space Complexity: O(n)
|
|
162
|
+
* logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node. linear space, as it creates an array to store the sorted nodes.
|
|
160
163
|
*/
|
|
161
164
|
/**
|
|
162
|
-
* Time Complexity: O(n log n)
|
|
163
|
-
* Space Complexity: O(n)
|
|
165
|
+
* Time Complexity: O(n log n)
|
|
166
|
+
* Space Complexity: O(n)
|
|
164
167
|
*
|
|
165
168
|
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
166
169
|
* tree using either a recursive or iterative approach.
|
|
@@ -206,12 +209,13 @@ export class TreeMultimap extends AVLTree {
|
|
|
206
209
|
}
|
|
207
210
|
}
|
|
208
211
|
/**
|
|
209
|
-
* Time Complexity: O(k log n)
|
|
210
|
-
* Space Complexity: O(1)
|
|
212
|
+
* Time Complexity: O(k log n)
|
|
213
|
+
* Space Complexity: O(1)
|
|
214
|
+
* logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each. constant space, as it doesn't use additional data structures that scale with input size.
|
|
211
215
|
*/
|
|
212
216
|
/**
|
|
213
|
-
* Time Complexity: O(log n)
|
|
214
|
-
* Space Complexity: O(1)
|
|
217
|
+
* Time Complexity: O(k log n)
|
|
218
|
+
* Space Complexity: O(1)
|
|
215
219
|
*
|
|
216
220
|
* The `delete` function in TypeScript is used to remove a node from a binary tree, taking into
|
|
217
221
|
* account the count of the node and balancing the tree if necessary.
|
|
@@ -286,10 +290,13 @@ export class TreeMultimap extends AVLTree {
|
|
|
286
290
|
return deletedResult;
|
|
287
291
|
}
|
|
288
292
|
/**
|
|
289
|
-
* Time Complexity: O(
|
|
290
|
-
* Space Complexity: O(
|
|
293
|
+
* Time Complexity: O(1)
|
|
294
|
+
* Space Complexity: O(1)
|
|
291
295
|
*/
|
|
292
296
|
/**
|
|
297
|
+
* Time Complexity: O(1)
|
|
298
|
+
* Space Complexity: O(1)
|
|
299
|
+
*
|
|
293
300
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
294
301
|
*/
|
|
295
302
|
clear() {
|
|
@@ -5,13 +5,13 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types';
|
|
8
|
+
import type { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem, LinkedHashMapOptions } from '../../types';
|
|
9
9
|
import { IterableEntryBase } from '../base';
|
|
10
10
|
/**
|
|
11
11
|
* 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key maps to a value.
|
|
12
|
-
* 2. Fast Lookup: It's used when you need to quickly find, insert, or delete
|
|
12
|
+
* 2. Fast Lookup: It's used when you need to quickly find, insert, or delete entries based on a key.
|
|
13
13
|
* 3. Unique Keys: Keys are unique. If you try to insert another entry with the same key, the old entry will be replaced by the new one.
|
|
14
|
-
* 4. Unordered Collection: HashMap does not guarantee the order of
|
|
14
|
+
* 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
|
|
15
15
|
*/
|
|
16
16
|
export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
17
17
|
protected _store: {
|
|
@@ -19,16 +19,14 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
19
19
|
};
|
|
20
20
|
protected _objMap: Map<object, V>;
|
|
21
21
|
/**
|
|
22
|
-
* The constructor function initializes a new instance of a class with optional
|
|
23
|
-
* @param
|
|
22
|
+
* The constructor function initializes a new instance of a class with optional entries and options.
|
|
23
|
+
* @param entries - The `entries` parameter is an iterable containing key-value pairs `[K, V]`. It
|
|
24
24
|
* is optional and defaults to an empty array `[]`. This parameter is used to initialize the map with
|
|
25
25
|
* key-value pairs.
|
|
26
26
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
27
27
|
* configuration options for the constructor. In this case, it has one property:
|
|
28
28
|
*/
|
|
29
|
-
constructor(
|
|
30
|
-
hashFn: (key: K) => string;
|
|
31
|
-
});
|
|
29
|
+
constructor(entries?: Iterable<[K, V]>, options?: HashMapOptions<K>);
|
|
32
30
|
protected _size: number;
|
|
33
31
|
get size(): number;
|
|
34
32
|
isEmpty(): boolean;
|
|
@@ -45,10 +43,10 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
45
43
|
set(key: K, value: V): boolean;
|
|
46
44
|
/**
|
|
47
45
|
* The function "setMany" sets multiple key-value pairs in a map.
|
|
48
|
-
* @param
|
|
49
|
-
* key-value pair is represented as an array with two
|
|
46
|
+
* @param entries - The `entries` parameter is an iterable containing key-value pairs. Each
|
|
47
|
+
* key-value pair is represented as an array with two entries: the key and the value.
|
|
50
48
|
*/
|
|
51
|
-
setMany(
|
|
49
|
+
setMany(entries: Iterable<[K, V]>): boolean[];
|
|
52
50
|
/**
|
|
53
51
|
* The `get` function retrieves a value from a map based on a given key, either from an object map or
|
|
54
52
|
* a string map.
|
|
@@ -113,7 +111,6 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
113
111
|
* from the original `HashMap` that pass the provided `predicate` function.
|
|
114
112
|
*/
|
|
115
113
|
filter(predicate: EntryCallback<K, V, boolean>, thisArg?: any): HashMap<K, V>;
|
|
116
|
-
print(): void;
|
|
117
114
|
put(key: K, value: V): boolean;
|
|
118
115
|
/**
|
|
119
116
|
* The function returns an iterator that yields key-value pairs from both an object store and an
|
|
@@ -125,8 +122,8 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
125
122
|
protected _getNoObjKey(key: K): string;
|
|
126
123
|
}
|
|
127
124
|
/**
|
|
128
|
-
* 1. Maintaining the Order of Element Insertion: Unlike HashMap, LinkedHashMap maintains the order in which
|
|
129
|
-
* 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of
|
|
125
|
+
* 1. Maintaining the Order of Element Insertion: Unlike HashMap, LinkedHashMap maintains the order in which entries are inserted. Therefore, when you traverse it, entries will be returned in the order they were inserted into the map.
|
|
126
|
+
* 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of entries through the linked list.
|
|
130
127
|
* 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases.
|
|
131
128
|
*/
|
|
132
129
|
export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
@@ -135,9 +132,7 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
|
|
|
135
132
|
protected _head: HashMapLinkedNode<K, V | undefined>;
|
|
136
133
|
protected _tail: HashMapLinkedNode<K, V | undefined>;
|
|
137
134
|
protected readonly _sentinel: HashMapLinkedNode<K, V | undefined>;
|
|
138
|
-
|
|
139
|
-
protected _objHashFn: (key: K) => object;
|
|
140
|
-
constructor(elements?: Iterable<[K, V]>, options?: HashMapOptions<K>);
|
|
135
|
+
constructor(entries?: Iterable<[K, V]>, options?: LinkedHashMapOptions<K>);
|
|
141
136
|
protected _size: number;
|
|
142
137
|
get size(): number;
|
|
143
138
|
/**
|
|
@@ -242,14 +237,10 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
|
|
|
242
237
|
* Time Complexity: O(1)
|
|
243
238
|
* Space Complexity: O(1)
|
|
244
239
|
*
|
|
245
|
-
* The `clear` function clears all the
|
|
240
|
+
* The `clear` function clears all the entries in a data structure and resets its properties.
|
|
246
241
|
*/
|
|
247
242
|
clear(): void;
|
|
248
243
|
clone(): LinkedHashMap<K, V>;
|
|
249
|
-
/**
|
|
250
|
-
* Time Complexity: O(n)
|
|
251
|
-
* Space Complexity: O(n)
|
|
252
|
-
*/
|
|
253
244
|
/**
|
|
254
245
|
* Time Complexity: O(n)
|
|
255
246
|
* Space Complexity: O(n)
|
|
@@ -266,10 +257,6 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
|
|
|
266
257
|
* `LinkedHashMap` object that satisfy the given predicate function.
|
|
267
258
|
*/
|
|
268
259
|
filter(predicate: EntryCallback<K, V, boolean>, thisArg?: any): LinkedHashMap<K, V>;
|
|
269
|
-
/**
|
|
270
|
-
* Time Complexity: O(n)
|
|
271
|
-
* Space Complexity: O(n)
|
|
272
|
-
*/
|
|
273
260
|
/**
|
|
274
261
|
* Time Complexity: O(n)
|
|
275
262
|
* Space Complexity: O(n)
|
|
@@ -288,9 +275,19 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
|
|
|
288
275
|
* function.
|
|
289
276
|
*/
|
|
290
277
|
map<NV>(callback: EntryCallback<K, V, NV>, thisArg?: any): LinkedHashMap<K, NV>;
|
|
278
|
+
/**
|
|
279
|
+
* Time Complexity: O(n)
|
|
280
|
+
* Space Complexity: O(n)
|
|
281
|
+
*/
|
|
291
282
|
put(key: K, value: V): boolean;
|
|
292
283
|
/**
|
|
293
|
-
* Time Complexity: O(n)
|
|
284
|
+
* Time Complexity: O(n)
|
|
285
|
+
* Space Complexity: O(n)
|
|
286
|
+
*/
|
|
287
|
+
protected _hashFn: (key: K) => string;
|
|
288
|
+
protected _objHashFn: (key: K) => object;
|
|
289
|
+
/**
|
|
290
|
+
* Time Complexity: O(n), where n is the number of entries in the LinkedHashMap.
|
|
294
291
|
* Space Complexity: O(1)
|
|
295
292
|
*
|
|
296
293
|
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
@@ -2,22 +2,22 @@ import { IterableEntryBase } from '../base';
|
|
|
2
2
|
import { isWeakKey, rangeCheck } from '../../utils';
|
|
3
3
|
/**
|
|
4
4
|
* 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key maps to a value.
|
|
5
|
-
* 2. Fast Lookup: It's used when you need to quickly find, insert, or delete
|
|
5
|
+
* 2. Fast Lookup: It's used when you need to quickly find, insert, or delete entries based on a key.
|
|
6
6
|
* 3. Unique Keys: Keys are unique. If you try to insert another entry with the same key, the old entry will be replaced by the new one.
|
|
7
|
-
* 4. Unordered Collection: HashMap does not guarantee the order of
|
|
7
|
+
* 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
|
|
8
8
|
*/
|
|
9
9
|
export class HashMap extends IterableEntryBase {
|
|
10
10
|
_store = {};
|
|
11
11
|
_objMap = new Map();
|
|
12
12
|
/**
|
|
13
|
-
* The constructor function initializes a new instance of a class with optional
|
|
14
|
-
* @param
|
|
13
|
+
* The constructor function initializes a new instance of a class with optional entries and options.
|
|
14
|
+
* @param entries - The `entries` parameter is an iterable containing key-value pairs `[K, V]`. It
|
|
15
15
|
* is optional and defaults to an empty array `[]`. This parameter is used to initialize the map with
|
|
16
16
|
* key-value pairs.
|
|
17
17
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
18
18
|
* configuration options for the constructor. In this case, it has one property:
|
|
19
19
|
*/
|
|
20
|
-
constructor(
|
|
20
|
+
constructor(entries = [], options) {
|
|
21
21
|
super();
|
|
22
22
|
if (options) {
|
|
23
23
|
const { hashFn } = options;
|
|
@@ -25,8 +25,8 @@ export class HashMap extends IterableEntryBase {
|
|
|
25
25
|
this._hashFn = hashFn;
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
|
-
if (
|
|
29
|
-
this.setMany(
|
|
28
|
+
if (entries) {
|
|
29
|
+
this.setMany(entries);
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
_size = 0;
|
|
@@ -68,12 +68,12 @@ export class HashMap extends IterableEntryBase {
|
|
|
68
68
|
}
|
|
69
69
|
/**
|
|
70
70
|
* The function "setMany" sets multiple key-value pairs in a map.
|
|
71
|
-
* @param
|
|
72
|
-
* key-value pair is represented as an array with two
|
|
71
|
+
* @param entries - The `entries` parameter is an iterable containing key-value pairs. Each
|
|
72
|
+
* key-value pair is represented as an array with two entries: the key and the value.
|
|
73
73
|
*/
|
|
74
|
-
setMany(
|
|
74
|
+
setMany(entries) {
|
|
75
75
|
const results = [];
|
|
76
|
-
for (const [key, value] of
|
|
76
|
+
for (const [key, value] of entries)
|
|
77
77
|
results.push(this.set(key, value));
|
|
78
78
|
return results;
|
|
79
79
|
}
|
|
@@ -189,9 +189,6 @@ export class HashMap extends IterableEntryBase {
|
|
|
189
189
|
}
|
|
190
190
|
return filteredMap;
|
|
191
191
|
}
|
|
192
|
-
print() {
|
|
193
|
-
console.log([...this.entries()]);
|
|
194
|
-
}
|
|
195
192
|
put(key, value) {
|
|
196
193
|
return this.set(key, value);
|
|
197
194
|
}
|
|
@@ -231,8 +228,8 @@ export class HashMap extends IterableEntryBase {
|
|
|
231
228
|
}
|
|
232
229
|
}
|
|
233
230
|
/**
|
|
234
|
-
* 1. Maintaining the Order of Element Insertion: Unlike HashMap, LinkedHashMap maintains the order in which
|
|
235
|
-
* 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of
|
|
231
|
+
* 1. Maintaining the Order of Element Insertion: Unlike HashMap, LinkedHashMap maintains the order in which entries are inserted. Therefore, when you traverse it, entries will be returned in the order they were inserted into the map.
|
|
232
|
+
* 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of entries through the linked list.
|
|
236
233
|
* 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases.
|
|
237
234
|
*/
|
|
238
235
|
export class LinkedHashMap extends IterableEntryBase {
|
|
@@ -241,20 +238,19 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
241
238
|
_head;
|
|
242
239
|
_tail;
|
|
243
240
|
_sentinel;
|
|
244
|
-
|
|
245
|
-
_objHashFn;
|
|
246
|
-
constructor(elements, options = {
|
|
247
|
-
hashFn: (key) => String(key),
|
|
248
|
-
objHashFn: (key) => key
|
|
249
|
-
}) {
|
|
241
|
+
constructor(entries, options) {
|
|
250
242
|
super();
|
|
251
243
|
this._sentinel = {};
|
|
252
244
|
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
245
|
+
if (options) {
|
|
246
|
+
const { hashFn, objHashFn } = options;
|
|
247
|
+
if (hashFn)
|
|
248
|
+
this._hashFn = hashFn;
|
|
249
|
+
if (objHashFn)
|
|
250
|
+
this._objHashFn = objHashFn;
|
|
251
|
+
}
|
|
252
|
+
if (entries) {
|
|
253
|
+
for (const el of entries) {
|
|
258
254
|
this.set(el[0], el[1]);
|
|
259
255
|
}
|
|
260
256
|
}
|
|
@@ -495,7 +491,7 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
495
491
|
* Time Complexity: O(1)
|
|
496
492
|
* Space Complexity: O(1)
|
|
497
493
|
*
|
|
498
|
-
* The `clear` function clears all the
|
|
494
|
+
* The `clear` function clears all the entries in a data structure and resets its properties.
|
|
499
495
|
*/
|
|
500
496
|
clear() {
|
|
501
497
|
this._noObjMap = {};
|
|
@@ -510,10 +506,6 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
510
506
|
}
|
|
511
507
|
return cloned;
|
|
512
508
|
}
|
|
513
|
-
/**
|
|
514
|
-
* Time Complexity: O(n)
|
|
515
|
-
* Space Complexity: O(n)
|
|
516
|
-
*/
|
|
517
509
|
/**
|
|
518
510
|
* Time Complexity: O(n)
|
|
519
511
|
* Space Complexity: O(n)
|
|
@@ -540,10 +532,6 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
540
532
|
}
|
|
541
533
|
return filteredMap;
|
|
542
534
|
}
|
|
543
|
-
/**
|
|
544
|
-
* Time Complexity: O(n)
|
|
545
|
-
* Space Complexity: O(n)
|
|
546
|
-
*/
|
|
547
535
|
/**
|
|
548
536
|
* Time Complexity: O(n)
|
|
549
537
|
* Space Complexity: O(n)
|
|
@@ -571,11 +559,21 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
571
559
|
}
|
|
572
560
|
return mappedMap;
|
|
573
561
|
}
|
|
562
|
+
/**
|
|
563
|
+
* Time Complexity: O(n)
|
|
564
|
+
* Space Complexity: O(n)
|
|
565
|
+
*/
|
|
574
566
|
put(key, value) {
|
|
575
567
|
return this.set(key, value);
|
|
576
568
|
}
|
|
577
569
|
/**
|
|
578
|
-
* Time Complexity: O(n)
|
|
570
|
+
* Time Complexity: O(n)
|
|
571
|
+
* Space Complexity: O(n)
|
|
572
|
+
*/
|
|
573
|
+
_hashFn = (key) => String(key);
|
|
574
|
+
_objHashFn = (key) => key;
|
|
575
|
+
/**
|
|
576
|
+
* Time Complexity: O(n), where n is the number of entries in the LinkedHashMap.
|
|
579
577
|
* Space Complexity: O(1)
|
|
580
578
|
*
|
|
581
579
|
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
@@ -19,8 +19,9 @@ import { IterableElementBase } from '../base';
|
|
|
19
19
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
|
20
20
|
*/
|
|
21
21
|
export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
22
|
-
options: HeapOptions<E>;
|
|
23
22
|
constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
|
|
23
|
+
protected _comparator: (a: E, b: E) => number;
|
|
24
|
+
get comparator(): (a: E, b: E) => number;
|
|
24
25
|
protected _elements: E[];
|
|
25
26
|
get elements(): E[];
|
|
26
27
|
/**
|
|
@@ -18,24 +18,12 @@ import { IterableElementBase } from '../base';
|
|
|
18
18
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
|
19
19
|
*/
|
|
20
20
|
export class Heap extends IterableElementBase {
|
|
21
|
-
options
|
|
22
|
-
constructor(elements, options) {
|
|
21
|
+
constructor(elements = [], options) {
|
|
23
22
|
super();
|
|
24
|
-
const defaultComparator = (a, b) => {
|
|
25
|
-
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
26
|
-
throw new Error('The a, b params of compare function must be number');
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
29
|
-
return a - b;
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
23
|
if (options) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
this.options = {
|
|
37
|
-
comparator: defaultComparator
|
|
38
|
-
};
|
|
24
|
+
const { comparator } = options;
|
|
25
|
+
if (comparator)
|
|
26
|
+
this._comparator = comparator;
|
|
39
27
|
}
|
|
40
28
|
if (elements) {
|
|
41
29
|
for (const el of elements) {
|
|
@@ -44,6 +32,17 @@ export class Heap extends IterableElementBase {
|
|
|
44
32
|
// this.fix();
|
|
45
33
|
}
|
|
46
34
|
}
|
|
35
|
+
_comparator = (a, b) => {
|
|
36
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
37
|
+
throw new Error('The a, b params of compare function must be number');
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
return a - b;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
get comparator() {
|
|
44
|
+
return this._comparator;
|
|
45
|
+
}
|
|
47
46
|
_elements = [];
|
|
48
47
|
get elements() {
|
|
49
48
|
return this._elements;
|
|
@@ -253,7 +252,7 @@ export class Heap extends IterableElementBase {
|
|
|
253
252
|
* @returns A new Heap instance containing the same elements.
|
|
254
253
|
*/
|
|
255
254
|
clone() {
|
|
256
|
-
const clonedHeap = new Heap([], this.
|
|
255
|
+
const clonedHeap = new Heap([], { comparator: this.comparator });
|
|
257
256
|
clonedHeap._elements = [...this.elements];
|
|
258
257
|
return clonedHeap;
|
|
259
258
|
}
|
|
@@ -379,7 +378,7 @@ export class Heap extends IterableElementBase {
|
|
|
379
378
|
while (index > 0) {
|
|
380
379
|
const parent = (index - 1) >> 1;
|
|
381
380
|
const parentItem = this.elements[parent];
|
|
382
|
-
if (this.
|
|
381
|
+
if (this.comparator(parentItem, element) <= 0)
|
|
383
382
|
break;
|
|
384
383
|
this.elements[index] = parentItem;
|
|
385
384
|
index = parent;
|
|
@@ -401,11 +400,11 @@ export class Heap extends IterableElementBase {
|
|
|
401
400
|
let left = (index << 1) | 1;
|
|
402
401
|
const right = left + 1;
|
|
403
402
|
let minItem = this.elements[left];
|
|
404
|
-
if (right < this.elements.length && this.
|
|
403
|
+
if (right < this.elements.length && this.comparator(minItem, this.elements[right]) > 0) {
|
|
405
404
|
left = right;
|
|
406
405
|
minItem = this.elements[right];
|
|
407
406
|
}
|
|
408
|
-
if (this.
|
|
407
|
+
if (this.comparator(minItem, element) >= 0)
|
|
409
408
|
break;
|
|
410
409
|
this.elements[index] = minItem;
|
|
411
410
|
index = left;
|
|
@@ -10,7 +10,7 @@ import { Heap } from './heap';
|
|
|
10
10
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
|
11
11
|
*/
|
|
12
12
|
export class MaxHeap extends Heap {
|
|
13
|
-
constructor(elements, options = {
|
|
13
|
+
constructor(elements = [], options = {
|
|
14
14
|
comparator: (a, b) => {
|
|
15
15
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
16
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -10,7 +10,7 @@ import { Heap } from './heap';
|
|
|
10
10
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
|
11
11
|
*/
|
|
12
12
|
export class MinHeap extends Heap {
|
|
13
|
-
constructor(elements, options = {
|
|
13
|
+
constructor(elements = [], options = {
|
|
14
14
|
comparator: (a, b) => {
|
|
15
15
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
16
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -24,7 +24,7 @@ export class DoublyLinkedList extends IterableElementBase {
|
|
|
24
24
|
/**
|
|
25
25
|
* The constructor initializes the linked list with an empty head, tail, and size.
|
|
26
26
|
*/
|
|
27
|
-
constructor(elements) {
|
|
27
|
+
constructor(elements = []) {
|
|
28
28
|
super();
|
|
29
29
|
this._head = undefined;
|
|
30
30
|
this._tail = undefined;
|
|
@@ -16,11 +16,8 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
16
16
|
/**
|
|
17
17
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
18
18
|
*/
|
|
19
|
-
constructor(elements) {
|
|
19
|
+
constructor(elements = []) {
|
|
20
20
|
super();
|
|
21
|
-
this._head = undefined;
|
|
22
|
-
this._tail = undefined;
|
|
23
|
-
this._size = 0;
|
|
24
21
|
if (elements) {
|
|
25
22
|
for (const el of elements)
|
|
26
23
|
this.push(el);
|
|
@@ -34,7 +31,7 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
34
31
|
get tail() {
|
|
35
32
|
return this._tail;
|
|
36
33
|
}
|
|
37
|
-
_size;
|
|
34
|
+
_size = 0;
|
|
38
35
|
get size() {
|
|
39
36
|
return this._size;
|
|
40
37
|
}
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
+
import type { SkipLinkedListOptions } from '../../types';
|
|
8
9
|
export declare class SkipListNode<K, V> {
|
|
9
10
|
key: K;
|
|
10
11
|
value: V;
|
|
@@ -12,14 +13,7 @@ export declare class SkipListNode<K, V> {
|
|
|
12
13
|
constructor(key: K, value: V, level: number);
|
|
13
14
|
}
|
|
14
15
|
export declare class SkipList<K, V> {
|
|
15
|
-
|
|
16
|
-
* The constructor initializes a SkipList with a specified maximum level and probability.
|
|
17
|
-
* @param [maxLevel=16] - The `maxLevel` parameter represents the maximum level that a skip list can have. It determines
|
|
18
|
-
* the maximum number of levels that can be created in the skip list.
|
|
19
|
-
* @param [probability=0.5] - The probability parameter represents the probability of a node being promoted to a higher
|
|
20
|
-
* level in the skip list. It is used to determine the height of each node in the skip list.
|
|
21
|
-
*/
|
|
22
|
-
constructor(maxLevel?: number, probability?: number);
|
|
16
|
+
constructor(elements?: Iterable<[K, V]>, options?: SkipLinkedListOptions);
|
|
23
17
|
protected _head: SkipListNode<K, V>;
|
|
24
18
|
get head(): SkipListNode<K, V>;
|
|
25
19
|
protected _level: number;
|