bst-typed 1.53.6 → 1.53.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +61 -0
- package/dist/common/index.d.ts +12 -0
- package/dist/common/index.js +28 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +9 -12
- package/dist/data-structures/binary-tree/avl-tree.js +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +55 -20
- package/dist/data-structures/binary-tree/binary-tree.js +102 -68
- package/dist/data-structures/binary-tree/bst.d.ts +131 -37
- package/dist/data-structures/binary-tree/bst.js +222 -69
- package/dist/data-structures/binary-tree/index.d.ts +1 -1
- package/dist/data-structures/binary-tree/index.js +1 -1
- package/dist/data-structures/binary-tree/{rb-tree.d.ts → red-black-tree.d.ts} +53 -0
- package/dist/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +56 -3
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
- package/dist/data-structures/binary-tree/tree-multi-map.js +7 -7
- package/dist/data-structures/hash/hash-map.d.ts +30 -0
- package/dist/data-structures/hash/hash-map.js +30 -0
- package/dist/data-structures/heap/heap.d.ts +26 -9
- package/dist/data-structures/heap/heap.js +37 -17
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +54 -9
- package/dist/data-structures/linked-list/doubly-linked-list.js +80 -19
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +35 -2
- package/dist/data-structures/linked-list/singly-linked-list.js +55 -11
- package/dist/data-structures/queue/deque.d.ts +37 -8
- package/dist/data-structures/queue/deque.js +73 -29
- package/dist/data-structures/queue/queue.d.ts +41 -1
- package/dist/data-structures/queue/queue.js +51 -9
- package/dist/data-structures/stack/stack.d.ts +27 -10
- package/dist/data-structures/stack/stack.js +39 -20
- package/dist/data-structures/trie/trie.d.ts +111 -6
- package/dist/data-structures/trie/trie.js +123 -14
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/types/utils/utils.d.ts +10 -6
- package/dist/utils/utils.js +4 -2
- package/package.json +2 -2
- package/src/common/index.ts +25 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +9 -11
- package/src/data-structures/binary-tree/avl-tree.ts +3 -2
- package/src/data-structures/binary-tree/binary-tree.ts +110 -66
- package/src/data-structures/binary-tree/bst.ts +232 -72
- package/src/data-structures/binary-tree/index.ts +1 -1
- package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +56 -3
- package/src/data-structures/binary-tree/tree-multi-map.ts +6 -6
- package/src/data-structures/hash/hash-map.ts +30 -0
- package/src/data-structures/heap/heap.ts +72 -49
- package/src/data-structures/linked-list/doubly-linked-list.ts +173 -105
- package/src/data-structures/linked-list/singly-linked-list.ts +61 -11
- package/src/data-structures/queue/deque.ts +72 -28
- package/src/data-structures/queue/queue.ts +50 -7
- package/src/data-structures/stack/stack.ts +39 -20
- package/src/data-structures/trie/trie.ts +123 -13
- package/src/index.ts +2 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +3 -2
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
- package/src/types/utils/utils.ts +16 -10
- package/src/utils/utils.ts +4 -2
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TreeMultiMap = exports.TreeMultiMapNode = void 0;
|
|
4
|
-
const
|
|
5
|
-
class TreeMultiMapNode extends
|
|
4
|
+
const red_black_tree_1 = require("./red-black-tree");
|
|
5
|
+
class TreeMultiMapNode extends red_black_tree_1.RedBlackTreeNode {
|
|
6
6
|
/**
|
|
7
7
|
* The constructor function initializes a Red-Black Tree node with a key, value, count, and color.
|
|
8
8
|
* @param {K} key - The key parameter represents the key of the node in the Red-Black Tree. It is
|
|
@@ -37,7 +37,7 @@ class TreeMultiMapNode extends rb_tree_1.RedBlackTreeNode {
|
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
exports.TreeMultiMapNode = TreeMultiMapNode;
|
|
40
|
-
class TreeMultiMap extends
|
|
40
|
+
class TreeMultiMap extends red_black_tree_1.RedBlackTree {
|
|
41
41
|
/**
|
|
42
42
|
* The constructor function initializes a TreeMultiMap object with optional initial data.
|
|
43
43
|
* @param keysNodesEntriesOrRaws - The parameter `keysNodesEntriesOrRaws` is an
|
|
@@ -99,7 +99,7 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
99
99
|
* existing `iterationType` property. The returned value is casted as `TREE`.
|
|
100
100
|
*/
|
|
101
101
|
createTree(options) {
|
|
102
|
-
return new TreeMultiMap([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode,
|
|
102
|
+
return new TreeMultiMap([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, extractComparable: this._extractComparable, toEntryFn: this._toEntryFn }, options));
|
|
103
103
|
}
|
|
104
104
|
/**
|
|
105
105
|
* The function `keyValueNodeEntryRawToNodeAndValue` takes in a key, value, and count and returns a
|
|
@@ -113,7 +113,7 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
113
113
|
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
114
114
|
* @returns either a NODE object or undefined.
|
|
115
115
|
*/
|
|
116
|
-
|
|
116
|
+
_keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count = 1) {
|
|
117
117
|
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null)
|
|
118
118
|
return [undefined, undefined];
|
|
119
119
|
if (this.isNode(keyNodeEntryOrRaw))
|
|
@@ -126,7 +126,7 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
126
126
|
if (this.isKey(key))
|
|
127
127
|
return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
|
|
128
128
|
}
|
|
129
|
-
if (this.
|
|
129
|
+
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
130
130
|
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
131
131
|
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
132
132
|
if (this.isKey(key))
|
|
@@ -163,7 +163,7 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
163
163
|
* was successful, and false otherwise.
|
|
164
164
|
*/
|
|
165
165
|
add(keyNodeEntryOrRaw, value, count = 1) {
|
|
166
|
-
const [newNode, newValue] = this.
|
|
166
|
+
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
|
|
167
167
|
const orgCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0;
|
|
168
168
|
const isSuccessAdded = super.add(newNode, newValue);
|
|
169
169
|
if (isSuccessAdded) {
|
|
@@ -62,6 +62,9 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
|
|
|
62
62
|
*/
|
|
63
63
|
get hashFn(): (key: K) => string;
|
|
64
64
|
/**
|
|
65
|
+
* Time Complexity: O(1)
|
|
66
|
+
* Space Complexity: O(1)
|
|
67
|
+
*
|
|
65
68
|
* The function checks if a given element is an array with exactly two elements.
|
|
66
69
|
* @param {any} rawElement - The `rawElement` parameter is of type `any`, which means it can be any
|
|
67
70
|
* data type.
|
|
@@ -69,16 +72,25 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
|
|
|
69
72
|
*/
|
|
70
73
|
isEntry(rawElement: any): rawElement is [K, V];
|
|
71
74
|
/**
|
|
75
|
+
* Time Complexity: O(1)
|
|
76
|
+
* Space Complexity: O(1)
|
|
77
|
+
*
|
|
72
78
|
* The function checks if the size of an object is equal to zero and returns a boolean value.
|
|
73
79
|
* @returns A boolean value indicating whether the size of the object is 0 or not.
|
|
74
80
|
*/
|
|
75
81
|
isEmpty(): boolean;
|
|
76
82
|
/**
|
|
83
|
+
* Time Complexity: O(1)
|
|
84
|
+
* Space Complexity: O(1)
|
|
85
|
+
*
|
|
77
86
|
* The clear() function resets the state of an object by clearing its internal store, object map, and
|
|
78
87
|
* size.
|
|
79
88
|
*/
|
|
80
89
|
clear(): void;
|
|
81
90
|
/**
|
|
91
|
+
* Time Complexity: O(1)
|
|
92
|
+
* Space Complexity: O(1)
|
|
93
|
+
*
|
|
82
94
|
* The `set` function adds a key-value pair to a map-like data structure, incrementing the size if
|
|
83
95
|
* the key is not already present.
|
|
84
96
|
* @param {K} key - The key parameter is the key used to identify the value in the data structure. It
|
|
@@ -89,6 +101,9 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
|
|
|
89
101
|
*/
|
|
90
102
|
set(key: K, value: V): boolean;
|
|
91
103
|
/**
|
|
104
|
+
* Time Complexity: O(k)
|
|
105
|
+
* Space Complexity: O(k)
|
|
106
|
+
*
|
|
92
107
|
* The function `setMany` takes an iterable collection of objects, maps each object to a key-value
|
|
93
108
|
* pair using a mapping function, and sets each key-value pair in the current object.
|
|
94
109
|
* @param entryOrRawElements - The `entryOrRawElements` parameter is an iterable collection of elements of a type
|
|
@@ -97,6 +112,9 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
|
|
|
97
112
|
*/
|
|
98
113
|
setMany(entryOrRawElements: Iterable<R | [K, V]>): boolean[];
|
|
99
114
|
/**
|
|
115
|
+
* Time Complexity: O(1)
|
|
116
|
+
* Space Complexity: O(1)
|
|
117
|
+
*
|
|
100
118
|
* The `get` function retrieves a value from a map based on a given key, either from an object map or
|
|
101
119
|
* a string map.
|
|
102
120
|
* @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be
|
|
@@ -106,6 +124,9 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
|
|
|
106
124
|
*/
|
|
107
125
|
get(key: K): V | undefined;
|
|
108
126
|
/**
|
|
127
|
+
* Time Complexity: O(1)
|
|
128
|
+
* Space Complexity: O(1)
|
|
129
|
+
*
|
|
109
130
|
* The `has` function checks if a given key exists in the `_objMap` or `_store` based on whether it
|
|
110
131
|
* is an object key or not.
|
|
111
132
|
* @param {K} key - The parameter "key" is of type K, which means it can be any type.
|
|
@@ -113,6 +134,9 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
|
|
|
113
134
|
*/
|
|
114
135
|
has(key: K): boolean;
|
|
115
136
|
/**
|
|
137
|
+
* Time Complexity: O(1)
|
|
138
|
+
* Space Complexity: O(1)
|
|
139
|
+
*
|
|
116
140
|
* The `delete` function removes an element from a map-like data structure based on the provided key.
|
|
117
141
|
* @param {K} key - The `key` parameter is the key of the element that you want to delete from the
|
|
118
142
|
* data structure.
|
|
@@ -292,6 +316,9 @@ export declare class LinkedHashMap<K = any, V = any, R = [K, V]> extends Iterabl
|
|
|
292
316
|
*/
|
|
293
317
|
set(key: K, value?: V): boolean;
|
|
294
318
|
/**
|
|
319
|
+
* Time Complexity: O(k)
|
|
320
|
+
* Space Complexity: O(k)
|
|
321
|
+
*
|
|
295
322
|
* The function `setMany` takes an iterable collection, converts each element into a key-value pair
|
|
296
323
|
* using a provided function, and sets each key-value pair in the current object, returning an array
|
|
297
324
|
* of booleans indicating the success of each set operation.
|
|
@@ -301,6 +328,9 @@ export declare class LinkedHashMap<K = any, V = any, R = [K, V]> extends Iterabl
|
|
|
301
328
|
*/
|
|
302
329
|
setMany(entryOrRawElements: Iterable<R | [K, V]>): boolean[];
|
|
303
330
|
/**
|
|
331
|
+
* Time Complexity: O(1)
|
|
332
|
+
* Space Complexity: O(1)
|
|
333
|
+
*
|
|
304
334
|
* The function checks if a given key exists in a map, using different logic depending on whether the
|
|
305
335
|
* key is a weak key or not.
|
|
306
336
|
* @param {K} key - The `key` parameter is the key that is being checked for existence in the map.
|
|
@@ -75,6 +75,9 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
75
75
|
return this._hashFn;
|
|
76
76
|
}
|
|
77
77
|
/**
|
|
78
|
+
* Time Complexity: O(1)
|
|
79
|
+
* Space Complexity: O(1)
|
|
80
|
+
*
|
|
78
81
|
* The function checks if a given element is an array with exactly two elements.
|
|
79
82
|
* @param {any} rawElement - The `rawElement` parameter is of type `any`, which means it can be any
|
|
80
83
|
* data type.
|
|
@@ -84,6 +87,9 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
84
87
|
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
85
88
|
}
|
|
86
89
|
/**
|
|
90
|
+
* Time Complexity: O(1)
|
|
91
|
+
* Space Complexity: O(1)
|
|
92
|
+
*
|
|
87
93
|
* The function checks if the size of an object is equal to zero and returns a boolean value.
|
|
88
94
|
* @returns A boolean value indicating whether the size of the object is 0 or not.
|
|
89
95
|
*/
|
|
@@ -91,6 +97,9 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
91
97
|
return this._size === 0;
|
|
92
98
|
}
|
|
93
99
|
/**
|
|
100
|
+
* Time Complexity: O(1)
|
|
101
|
+
* Space Complexity: O(1)
|
|
102
|
+
*
|
|
94
103
|
* The clear() function resets the state of an object by clearing its internal store, object map, and
|
|
95
104
|
* size.
|
|
96
105
|
*/
|
|
@@ -100,6 +109,9 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
100
109
|
this._size = 0;
|
|
101
110
|
}
|
|
102
111
|
/**
|
|
112
|
+
* Time Complexity: O(1)
|
|
113
|
+
* Space Complexity: O(1)
|
|
114
|
+
*
|
|
103
115
|
* The `set` function adds a key-value pair to a map-like data structure, incrementing the size if
|
|
104
116
|
* the key is not already present.
|
|
105
117
|
* @param {K} key - The key parameter is the key used to identify the value in the data structure. It
|
|
@@ -125,6 +137,9 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
125
137
|
return true;
|
|
126
138
|
}
|
|
127
139
|
/**
|
|
140
|
+
* Time Complexity: O(k)
|
|
141
|
+
* Space Complexity: O(k)
|
|
142
|
+
*
|
|
128
143
|
* The function `setMany` takes an iterable collection of objects, maps each object to a key-value
|
|
129
144
|
* pair using a mapping function, and sets each key-value pair in the current object.
|
|
130
145
|
* @param entryOrRawElements - The `entryOrRawElements` parameter is an iterable collection of elements of a type
|
|
@@ -150,6 +165,9 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
150
165
|
return results;
|
|
151
166
|
}
|
|
152
167
|
/**
|
|
168
|
+
* Time Complexity: O(1)
|
|
169
|
+
* Space Complexity: O(1)
|
|
170
|
+
*
|
|
153
171
|
* The `get` function retrieves a value from a map based on a given key, either from an object map or
|
|
154
172
|
* a string map.
|
|
155
173
|
* @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be
|
|
@@ -168,6 +186,9 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
168
186
|
}
|
|
169
187
|
}
|
|
170
188
|
/**
|
|
189
|
+
* Time Complexity: O(1)
|
|
190
|
+
* Space Complexity: O(1)
|
|
191
|
+
*
|
|
171
192
|
* The `has` function checks if a given key exists in the `_objMap` or `_store` based on whether it
|
|
172
193
|
* is an object key or not.
|
|
173
194
|
* @param {K} key - The parameter "key" is of type K, which means it can be any type.
|
|
@@ -183,6 +204,9 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
183
204
|
}
|
|
184
205
|
}
|
|
185
206
|
/**
|
|
207
|
+
* Time Complexity: O(1)
|
|
208
|
+
* Space Complexity: O(1)
|
|
209
|
+
*
|
|
186
210
|
* The `delete` function removes an element from a map-like data structure based on the provided key.
|
|
187
211
|
* @param {K} key - The `key` parameter is the key of the element that you want to delete from the
|
|
188
212
|
* data structure.
|
|
@@ -523,6 +547,9 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
523
547
|
return true;
|
|
524
548
|
}
|
|
525
549
|
/**
|
|
550
|
+
* Time Complexity: O(k)
|
|
551
|
+
* Space Complexity: O(k)
|
|
552
|
+
*
|
|
526
553
|
* The function `setMany` takes an iterable collection, converts each element into a key-value pair
|
|
527
554
|
* using a provided function, and sets each key-value pair in the current object, returning an array
|
|
528
555
|
* of booleans indicating the success of each set operation.
|
|
@@ -549,6 +576,9 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
549
576
|
return results;
|
|
550
577
|
}
|
|
551
578
|
/**
|
|
579
|
+
* Time Complexity: O(1)
|
|
580
|
+
* Space Complexity: O(1)
|
|
581
|
+
*
|
|
552
582
|
* The function checks if a given key exists in a map, using different logic depending on whether the
|
|
553
583
|
* key is a weak key or not.
|
|
554
584
|
* @param {K} key - The `key` parameter is the key that is being checked for existence in the map.
|
|
@@ -19,7 +19,7 @@ import { IterableElementBase } from '../base';
|
|
|
19
19
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prime's minimum-spanning tree algorithm, which use heaps to improve performance.
|
|
20
20
|
* @example
|
|
21
21
|
* // Use Heap to sort an array
|
|
22
|
-
*
|
|
22
|
+
* function heapSort(arr: number[]): number[] {
|
|
23
23
|
* const heap = new Heap<number>(arr, { comparator: (a, b) => a - b });
|
|
24
24
|
* const sorted: number[] = [];
|
|
25
25
|
* while (!heap.isEmpty()) {
|
|
@@ -32,7 +32,7 @@ import { IterableElementBase } from '../base';
|
|
|
32
32
|
* console.log(heapSort(array)); // [1, 2, 3, 4, 5, 8]
|
|
33
33
|
* @example
|
|
34
34
|
* // Use Heap to solve top k problems
|
|
35
|
-
*
|
|
35
|
+
* function topKElements(arr: number[], k: number): number[] {
|
|
36
36
|
* const heap = new Heap<number>([], { comparator: (a, b) => b - a }); // Max heap
|
|
37
37
|
* arr.forEach(num => {
|
|
38
38
|
* heap.add(num);
|
|
@@ -45,7 +45,7 @@ import { IterableElementBase } from '../base';
|
|
|
45
45
|
* console.log(topKElements(numbers, 3)); // [15, 10, 5]
|
|
46
46
|
* @example
|
|
47
47
|
* // Use Heap to merge sorted sequences
|
|
48
|
-
*
|
|
48
|
+
* function mergeSortedSequences(sequences: number[][]): number[] {
|
|
49
49
|
* const heap = new Heap<{ value: number; seqIndex: number; itemIndex: number }>([], {
|
|
50
50
|
* comparator: (a, b) => a.value - b.value // Min heap
|
|
51
51
|
* });
|
|
@@ -82,7 +82,7 @@ import { IterableElementBase } from '../base';
|
|
|
82
82
|
* console.log(mergeSortedSequences(sequences)); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
83
83
|
* @example
|
|
84
84
|
* // Use Heap to dynamically maintain the median
|
|
85
|
-
*
|
|
85
|
+
* class MedianFinder {
|
|
86
86
|
* private low: MaxHeap<number>; // Max heap, stores the smaller half
|
|
87
87
|
* private high: MinHeap<number>; // Min heap, stores the larger half
|
|
88
88
|
*
|
|
@@ -119,7 +119,7 @@ import { IterableElementBase } from '../base';
|
|
|
119
119
|
* console.log(medianFinder.findMedian()); // 30
|
|
120
120
|
* @example
|
|
121
121
|
* // Use Heap for load balancing
|
|
122
|
-
*
|
|
122
|
+
* function loadBalance(requests: number[], servers: number): number[] {
|
|
123
123
|
* const serverHeap = new Heap<{ id: number; load: number }>([], { comparator: (a, b) => a.load - b.load }); // min heap
|
|
124
124
|
* const serverLoads = new Array(servers).fill(0);
|
|
125
125
|
*
|
|
@@ -141,7 +141,7 @@ import { IterableElementBase } from '../base';
|
|
|
141
141
|
* console.log(loadBalance(requests, 3)); // [12, 8, 5]
|
|
142
142
|
* @example
|
|
143
143
|
* // Use Heap to schedule tasks
|
|
144
|
-
*
|
|
144
|
+
* type Task = [string, number];
|
|
145
145
|
*
|
|
146
146
|
* function scheduleTasks(tasks: Task[], machines: number): Map<number, Task[]> {
|
|
147
147
|
* const machineHeap = new Heap<{ id: number; load: number }>([], { comparator: (a, b) => a.load - b.load }); // Min heap
|
|
@@ -224,10 +224,27 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R, He
|
|
|
224
224
|
* Time Complexity: O(log n)
|
|
225
225
|
* Space Complexity: O(1)
|
|
226
226
|
*
|
|
227
|
-
*
|
|
228
|
-
* @param element - The element to
|
|
227
|
+
* The add function pushes an element into an array and then triggers a bubble-up operation.
|
|
228
|
+
* @param {E} element - The `element` parameter represents the element that you want to add to the
|
|
229
|
+
* data structure.
|
|
230
|
+
* @returns The `add` method is returning a boolean value, which is the result of calling the
|
|
231
|
+
* `_bubbleUp` method with the index `this.elements.length - 1` as an argument.
|
|
229
232
|
*/
|
|
230
233
|
add(element: E): boolean;
|
|
234
|
+
/**
|
|
235
|
+
* Time Complexity: O(k log n)
|
|
236
|
+
* Space Complexity: O(1)
|
|
237
|
+
*
|
|
238
|
+
* The `addMany` function iterates over elements and adds them to a collection, returning an array of
|
|
239
|
+
* boolean values indicating success or failure.
|
|
240
|
+
* @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `addMany` method is
|
|
241
|
+
* an iterable containing elements of type `E` or `R`. The method iterates over each element in the
|
|
242
|
+
* iterable and adds them to the data structure. If a transformation function `_toElementFn` is
|
|
243
|
+
* provided, it transforms the element
|
|
244
|
+
* @returns The `addMany` method returns an array of boolean values indicating whether each element
|
|
245
|
+
* in the input iterable was successfully added to the data structure.
|
|
246
|
+
*/
|
|
247
|
+
addMany(elements: Iterable<E> | Iterable<R>): boolean[];
|
|
231
248
|
/**
|
|
232
249
|
* Time Complexity: O(log n)
|
|
233
250
|
* Space Complexity: O(1)
|
|
@@ -340,7 +357,7 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R, He
|
|
|
340
357
|
*/
|
|
341
358
|
filter(callback: ElementCallback<E, R, boolean, Heap<E, R>>, thisArg?: any): Heap<E, R>;
|
|
342
359
|
/**
|
|
343
|
-
* Time Complexity: O(n
|
|
360
|
+
* Time Complexity: O(n)
|
|
344
361
|
* Space Complexity: O(n)
|
|
345
362
|
*
|
|
346
363
|
* The `map` function creates a new heap by applying a callback function to each element of the
|
|
@@ -21,7 +21,7 @@ const base_1 = require("../base");
|
|
|
21
21
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prime's minimum-spanning tree algorithm, which use heaps to improve performance.
|
|
22
22
|
* @example
|
|
23
23
|
* // Use Heap to sort an array
|
|
24
|
-
*
|
|
24
|
+
* function heapSort(arr: number[]): number[] {
|
|
25
25
|
* const heap = new Heap<number>(arr, { comparator: (a, b) => a - b });
|
|
26
26
|
* const sorted: number[] = [];
|
|
27
27
|
* while (!heap.isEmpty()) {
|
|
@@ -34,7 +34,7 @@ const base_1 = require("../base");
|
|
|
34
34
|
* console.log(heapSort(array)); // [1, 2, 3, 4, 5, 8]
|
|
35
35
|
* @example
|
|
36
36
|
* // Use Heap to solve top k problems
|
|
37
|
-
*
|
|
37
|
+
* function topKElements(arr: number[], k: number): number[] {
|
|
38
38
|
* const heap = new Heap<number>([], { comparator: (a, b) => b - a }); // Max heap
|
|
39
39
|
* arr.forEach(num => {
|
|
40
40
|
* heap.add(num);
|
|
@@ -47,7 +47,7 @@ const base_1 = require("../base");
|
|
|
47
47
|
* console.log(topKElements(numbers, 3)); // [15, 10, 5]
|
|
48
48
|
* @example
|
|
49
49
|
* // Use Heap to merge sorted sequences
|
|
50
|
-
*
|
|
50
|
+
* function mergeSortedSequences(sequences: number[][]): number[] {
|
|
51
51
|
* const heap = new Heap<{ value: number; seqIndex: number; itemIndex: number }>([], {
|
|
52
52
|
* comparator: (a, b) => a.value - b.value // Min heap
|
|
53
53
|
* });
|
|
@@ -84,7 +84,7 @@ const base_1 = require("../base");
|
|
|
84
84
|
* console.log(mergeSortedSequences(sequences)); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
85
85
|
* @example
|
|
86
86
|
* // Use Heap to dynamically maintain the median
|
|
87
|
-
*
|
|
87
|
+
* class MedianFinder {
|
|
88
88
|
* private low: MaxHeap<number>; // Max heap, stores the smaller half
|
|
89
89
|
* private high: MinHeap<number>; // Min heap, stores the larger half
|
|
90
90
|
*
|
|
@@ -121,7 +121,7 @@ const base_1 = require("../base");
|
|
|
121
121
|
* console.log(medianFinder.findMedian()); // 30
|
|
122
122
|
* @example
|
|
123
123
|
* // Use Heap for load balancing
|
|
124
|
-
*
|
|
124
|
+
* function loadBalance(requests: number[], servers: number): number[] {
|
|
125
125
|
* const serverHeap = new Heap<{ id: number; load: number }>([], { comparator: (a, b) => a.load - b.load }); // min heap
|
|
126
126
|
* const serverLoads = new Array(servers).fill(0);
|
|
127
127
|
*
|
|
@@ -143,7 +143,7 @@ const base_1 = require("../base");
|
|
|
143
143
|
* console.log(loadBalance(requests, 3)); // [12, 8, 5]
|
|
144
144
|
* @example
|
|
145
145
|
* // Use Heap to schedule tasks
|
|
146
|
-
*
|
|
146
|
+
* type Task = [string, number];
|
|
147
147
|
*
|
|
148
148
|
* function scheduleTasks(tasks: Task[], machines: number): Map<number, Task[]> {
|
|
149
149
|
* const machineHeap = new Heap<{ id: number; load: number }>([], { comparator: (a, b) => a.load - b.load }); // Min heap
|
|
@@ -218,14 +218,7 @@ class Heap extends base_1.IterableElementBase {
|
|
|
218
218
|
if (comparator)
|
|
219
219
|
this._comparator = comparator;
|
|
220
220
|
}
|
|
221
|
-
|
|
222
|
-
for (const el of elements) {
|
|
223
|
-
if (this.toElementFn)
|
|
224
|
-
this.add(this.toElementFn(el));
|
|
225
|
-
else
|
|
226
|
-
this.add(el);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
221
|
+
this.addMany(elements);
|
|
229
222
|
}
|
|
230
223
|
/**
|
|
231
224
|
* The function returns an array of elements.
|
|
@@ -261,13 +254,40 @@ class Heap extends base_1.IterableElementBase {
|
|
|
261
254
|
* Time Complexity: O(log n)
|
|
262
255
|
* Space Complexity: O(1)
|
|
263
256
|
*
|
|
264
|
-
*
|
|
265
|
-
* @param element - The element to
|
|
257
|
+
* The add function pushes an element into an array and then triggers a bubble-up operation.
|
|
258
|
+
* @param {E} element - The `element` parameter represents the element that you want to add to the
|
|
259
|
+
* data structure.
|
|
260
|
+
* @returns The `add` method is returning a boolean value, which is the result of calling the
|
|
261
|
+
* `_bubbleUp` method with the index `this.elements.length - 1` as an argument.
|
|
266
262
|
*/
|
|
267
263
|
add(element) {
|
|
268
264
|
this._elements.push(element);
|
|
269
265
|
return this._bubbleUp(this.elements.length - 1);
|
|
270
266
|
}
|
|
267
|
+
/**
|
|
268
|
+
* Time Complexity: O(k log n)
|
|
269
|
+
* Space Complexity: O(1)
|
|
270
|
+
*
|
|
271
|
+
* The `addMany` function iterates over elements and adds them to a collection, returning an array of
|
|
272
|
+
* boolean values indicating success or failure.
|
|
273
|
+
* @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `addMany` method is
|
|
274
|
+
* an iterable containing elements of type `E` or `R`. The method iterates over each element in the
|
|
275
|
+
* iterable and adds them to the data structure. If a transformation function `_toElementFn` is
|
|
276
|
+
* provided, it transforms the element
|
|
277
|
+
* @returns The `addMany` method returns an array of boolean values indicating whether each element
|
|
278
|
+
* in the input iterable was successfully added to the data structure.
|
|
279
|
+
*/
|
|
280
|
+
addMany(elements) {
|
|
281
|
+
const ans = [];
|
|
282
|
+
for (const el of elements) {
|
|
283
|
+
if (this._toElementFn) {
|
|
284
|
+
ans.push(this.add(this._toElementFn(el)));
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
ans.push(this.add(el));
|
|
288
|
+
}
|
|
289
|
+
return ans;
|
|
290
|
+
}
|
|
271
291
|
/**
|
|
272
292
|
* Time Complexity: O(log n)
|
|
273
293
|
* Space Complexity: O(1)
|
|
@@ -470,7 +490,7 @@ class Heap extends base_1.IterableElementBase {
|
|
|
470
490
|
return filteredList;
|
|
471
491
|
}
|
|
472
492
|
/**
|
|
473
|
-
* Time Complexity: O(n
|
|
493
|
+
* Time Complexity: O(n)
|
|
474
494
|
* Space Complexity: O(n)
|
|
475
495
|
*
|
|
476
496
|
* The `map` function creates a new heap by applying a callback function to each element of the
|
|
@@ -55,13 +55,13 @@ export declare class DoublyLinkedListNode<E = any> {
|
|
|
55
55
|
set prev(value: DoublyLinkedListNode<E> | undefined);
|
|
56
56
|
}
|
|
57
57
|
/**
|
|
58
|
-
*
|
|
58
|
+
*1. Node Structure: Each node contains three parts: a data field, a pointer (or reference) to the previous node, and a pointer to the next node. This structure allows traversal of the linked list in both directions.
|
|
59
59
|
* 2. Bidirectional Traversal: Unlike singly linked lists, doubly linked lists can be easily traversed forwards or backwards. This makes insertions and deletions in the list more flexible and efficient.
|
|
60
60
|
* 3. No Centralized Index: Unlike arrays, elements in a linked list are not stored contiguously, so there is no centralized index. Accessing elements in a linked list typically requires traversing from the head or tail node.
|
|
61
61
|
* 4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays.
|
|
62
62
|
* @example
|
|
63
63
|
* // text editor operation history
|
|
64
|
-
*
|
|
64
|
+
* const actions = [
|
|
65
65
|
* { type: 'insert', content: 'first line of text' },
|
|
66
66
|
* { type: 'insert', content: 'second line of text' },
|
|
67
67
|
* { type: 'delete', content: 'delete the first line' }
|
|
@@ -73,7 +73,7 @@ export declare class DoublyLinkedListNode<E = any> {
|
|
|
73
73
|
* console.log(editorHistory.last?.type); // 'insert'
|
|
74
74
|
* @example
|
|
75
75
|
* // Browser history
|
|
76
|
-
*
|
|
76
|
+
* const browserHistory = new DoublyLinkedList<string>();
|
|
77
77
|
*
|
|
78
78
|
* browserHistory.push('home page');
|
|
79
79
|
* browserHistory.push('search page');
|
|
@@ -84,7 +84,7 @@ export declare class DoublyLinkedListNode<E = any> {
|
|
|
84
84
|
* console.log(browserHistory.last); // 'search page'
|
|
85
85
|
* @example
|
|
86
86
|
* // Use DoublyLinkedList to implement music player
|
|
87
|
-
*
|
|
87
|
+
* // Define the Song interface
|
|
88
88
|
* interface Song {
|
|
89
89
|
* title: string;
|
|
90
90
|
* artist: string;
|
|
@@ -209,7 +209,7 @@ export declare class DoublyLinkedListNode<E = any> {
|
|
|
209
209
|
* // ]
|
|
210
210
|
* @example
|
|
211
211
|
* // Use DoublyLinkedList to implement LRU cache
|
|
212
|
-
*
|
|
212
|
+
* interface CacheEntry<K, V> {
|
|
213
213
|
* key: K;
|
|
214
214
|
* value: V;
|
|
215
215
|
* }
|
|
@@ -371,7 +371,7 @@ export declare class DoublyLinkedListNode<E = any> {
|
|
|
371
371
|
* console.log(cache.isEmpty); // true
|
|
372
372
|
* @example
|
|
373
373
|
* // finding lyrics by timestamp in Coldplay's "Fix You"
|
|
374
|
-
*
|
|
374
|
+
* // Create a DoublyLinkedList to store song lyrics with timestamps
|
|
375
375
|
* const lyricsList = new DoublyLinkedList<{ time: number; text: string }>();
|
|
376
376
|
*
|
|
377
377
|
* // Detailed lyrics with precise timestamps (in milliseconds)
|
|
@@ -411,7 +411,7 @@ export declare class DoublyLinkedListNode<E = any> {
|
|
|
411
411
|
* console.log(lateTimeLyric?.text); // 'And I will try to fix you'
|
|
412
412
|
* @example
|
|
413
413
|
* // cpu process schedules
|
|
414
|
-
*
|
|
414
|
+
* class Process {
|
|
415
415
|
* constructor(
|
|
416
416
|
* public id: number,
|
|
417
417
|
* public priority: number
|
|
@@ -487,7 +487,17 @@ export declare class DoublyLinkedListNode<E = any> {
|
|
|
487
487
|
* console.log(scheduler.listProcesses()); // []
|
|
488
488
|
*/
|
|
489
489
|
export declare class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R, DoublyLinkedList<E, R>> {
|
|
490
|
-
|
|
490
|
+
/**
|
|
491
|
+
* This TypeScript constructor initializes a DoublyLinkedList with optional elements and options.
|
|
492
|
+
* @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the constructor is an
|
|
493
|
+
* iterable collection of elements of type `E` or `R`. It is used to initialize the DoublyLinkedList
|
|
494
|
+
* with the elements provided in the iterable. If no elements are provided, the default value is an
|
|
495
|
+
* empty iterable.
|
|
496
|
+
* @param [options] - The `options` parameter in the constructor is of type
|
|
497
|
+
* `DoublyLinkedListOptions<E, R>`. It is an optional parameter that allows you to pass additional
|
|
498
|
+
* configuration options to customize the behavior of the DoublyLinkedList.
|
|
499
|
+
*/
|
|
500
|
+
constructor(elements?: Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>, options?: DoublyLinkedListOptions<E, R>);
|
|
491
501
|
protected _head: DoublyLinkedListNode<E> | undefined;
|
|
492
502
|
/**
|
|
493
503
|
* The `head` function returns the first node of a doubly linked list.
|
|
@@ -575,6 +585,35 @@ export declare class DoublyLinkedList<E = any, R = any> extends IterableElementB
|
|
|
575
585
|
* @returns The `unshift` method is returning a boolean value, specifically `true`.
|
|
576
586
|
*/
|
|
577
587
|
unshift(elementOrNode: E | DoublyLinkedListNode<E>): boolean;
|
|
588
|
+
/**
|
|
589
|
+
* Time Complexity: O(k)
|
|
590
|
+
* Space Complexity: O(k)
|
|
591
|
+
*
|
|
592
|
+
* The function `pushMany` iterates over elements and pushes them into a data structure, applying a
|
|
593
|
+
* transformation function if provided.
|
|
594
|
+
* @param {Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>} elements - The `elements`
|
|
595
|
+
* parameter in the `pushMany` function can accept an iterable containing elements of type `E`, `R`,
|
|
596
|
+
* or `DoublyLinkedListNode<E>`. The function iterates over each element in the iterable and pushes
|
|
597
|
+
* it onto the linked list. If a transformation function `to
|
|
598
|
+
* @returns The `pushMany` function is returning an array of boolean values (`ans`) which indicate
|
|
599
|
+
* the success or failure of pushing each element into the data structure.
|
|
600
|
+
*/
|
|
601
|
+
pushMany(elements: Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>): boolean[];
|
|
602
|
+
/**
|
|
603
|
+
* Time Complexity: O(k)
|
|
604
|
+
* Space Complexity: O(k)
|
|
605
|
+
*
|
|
606
|
+
* The function `unshiftMany` iterates through a collection of elements and adds them to the
|
|
607
|
+
* beginning of a Doubly Linked List, returning an array of boolean values indicating the success of
|
|
608
|
+
* each insertion.
|
|
609
|
+
* @param {Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>} elements - The `elements`
|
|
610
|
+
* parameter in the `unshiftMany` function can accept an iterable containing elements of type `E`,
|
|
611
|
+
* `R`, or `DoublyLinkedListNode<E>`. The function iterates over each element in the iterable and
|
|
612
|
+
* performs an `unshift` operation on the doubly linked list
|
|
613
|
+
* @returns The `unshiftMany` function returns an array of boolean values indicating the success of
|
|
614
|
+
* each unshift operation performed on the elements passed as input.
|
|
615
|
+
*/
|
|
616
|
+
unshiftMany(elements: Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>): boolean[];
|
|
578
617
|
/**
|
|
579
618
|
* Time Complexity: O(n)
|
|
580
619
|
* Space Complexity: O(1)
|
|
@@ -729,7 +768,7 @@ export declare class DoublyLinkedList<E = any, R = any> extends IterableElementB
|
|
|
729
768
|
* @returns The `get` method returns the value of the first node in the doubly linked list that
|
|
730
769
|
* satisfies the provided predicate function. If no such node is found, it returns `undefined`.
|
|
731
770
|
*/
|
|
732
|
-
|
|
771
|
+
search(elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): E | undefined;
|
|
733
772
|
/**
|
|
734
773
|
* Time Complexity: O(n)
|
|
735
774
|
* Space Complexity: O(1)
|
|
@@ -820,6 +859,12 @@ export declare class DoublyLinkedList<E = any, R = any> extends IterableElementB
|
|
|
820
859
|
* Time Complexity: O(n)
|
|
821
860
|
* Space Complexity: O(1)
|
|
822
861
|
*
|
|
862
|
+
* The function `countOccurrences` iterates through a doubly linked list and counts the occurrences
|
|
863
|
+
* of a specified element or nodes that satisfy a given predicate.
|
|
864
|
+
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementOrNode
|
|
865
|
+
* - The `elementOrNode` parameter in the `countOccurrences` method can accept three types of values:
|
|
866
|
+
* @returns The `countOccurrences` method returns the number of occurrences of the specified element,
|
|
867
|
+
* node, or predicate function in the doubly linked list.
|
|
823
868
|
*/
|
|
824
869
|
countOccurrences(elementOrNode: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): number;
|
|
825
870
|
/**
|