undirected-graph-typed 1.54.3 → 2.0.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.
- package/dist/data-structures/base/iterable-element-base.d.ts +14 -40
- package/dist/data-structures/base/iterable-element-base.js +14 -11
- package/dist/data-structures/base/linear-base.d.ts +277 -0
- package/dist/data-structures/base/linear-base.js +552 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +12 -8
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +50 -37
- package/dist/data-structures/binary-tree/avl-tree.d.ts +64 -0
- package/dist/data-structures/binary-tree/avl-tree.js +64 -0
- package/dist/data-structures/binary-tree/binary-tree.js +5 -5
- package/dist/data-structures/binary-tree/bst.js +11 -11
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +175 -14
- package/dist/data-structures/binary-tree/tree-multi-map.js +210 -40
- package/dist/data-structures/graph/abstract-graph.js +16 -16
- package/dist/data-structures/hash/hash-map.d.ts +46 -0
- package/dist/data-structures/hash/hash-map.js +46 -0
- package/dist/data-structures/heap/heap.d.ts +3 -11
- package/dist/data-structures/heap/heap.js +0 -10
- package/dist/data-structures/heap/max-heap.d.ts +2 -2
- package/dist/data-structures/heap/min-heap.d.ts +2 -2
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
- package/dist/data-structures/linked-list/doubly-linked-list.js +131 -146
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +145 -75
- package/dist/data-structures/linked-list/singly-linked-list.js +283 -169
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
- package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -2
- package/dist/data-structures/queue/deque.d.ts +130 -91
- package/dist/data-structures/queue/deque.js +269 -169
- package/dist/data-structures/queue/queue.d.ts +131 -40
- package/dist/data-structures/queue/queue.js +181 -50
- package/dist/data-structures/stack/stack.d.ts +124 -11
- package/dist/data-structures/stack/stack.js +121 -10
- package/dist/data-structures/trie/trie.d.ts +4 -3
- package/dist/data-structures/trie/trie.js +3 -0
- package/dist/types/data-structures/base/base.d.ts +9 -4
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/types/data-structures/queue/deque.d.ts +2 -3
- package/dist/types/data-structures/queue/queue.d.ts +2 -2
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +29 -20
- package/src/data-structures/base/linear-base.ts +649 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +51 -36
- package/src/data-structures/binary-tree/avl-tree.ts +64 -0
- package/src/data-structures/binary-tree/binary-tree.ts +5 -5
- package/src/data-structures/binary-tree/bst.ts +9 -9
- package/src/data-structures/binary-tree/tree-multi-map.ts +214 -40
- package/src/data-structures/graph/abstract-graph.ts +16 -16
- package/src/data-structures/hash/hash-map.ts +46 -0
- package/src/data-structures/heap/heap.ts +3 -14
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/heap/min-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +144 -160
- package/src/data-structures/linked-list/singly-linked-list.ts +307 -185
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -5
- package/src/data-structures/priority-queue/min-priority-queue.ts +2 -5
- package/src/data-structures/priority-queue/priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +286 -183
- package/src/data-structures/queue/queue.ts +196 -63
- package/src/data-structures/stack/stack.ts +124 -18
- package/src/data-structures/trie/trie.ts +7 -3
- package/src/types/data-structures/base/base.ts +17 -8
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +2 -2
- package/src/types/data-structures/linked-list/singly-linked-list.ts +2 -2
- package/src/types/data-structures/queue/deque.ts +2 -3
- package/src/types/data-structures/queue/queue.ts +2 -2
|
@@ -14,8 +14,129 @@ import { IterableElementBase } from '../base';
|
|
|
14
14
|
* 4. Function Calls: In most modern programming languages, the records of function calls are managed through a stack. When a function is called, its record (including parameters, local variables, and return address) is 'pushed' into the stack. When the function returns, its record is 'popped' from the stack.
|
|
15
15
|
* 5. Expression Evaluation: Used for the evaluation of arithmetic or logical expressions, especially when dealing with parenthesis matching and operator precedence.
|
|
16
16
|
* 6. Backtracking Algorithms: In problems where multiple branches need to be explored but only one branch can be explored at a time, stacks can be used to save the state at each branching point.
|
|
17
|
+
* @example
|
|
18
|
+
* // Balanced Parentheses or Brackets
|
|
19
|
+
* type ValidCharacters = ')' | '(' | ']' | '[' | '}' | '{';
|
|
20
|
+
*
|
|
21
|
+
* const stack = new Stack<string>();
|
|
22
|
+
* const input: ValidCharacters[] = '[({})]'.split('') as ValidCharacters[];
|
|
23
|
+
* const matches: { [key in ValidCharacters]?: ValidCharacters } = { ')': '(', ']': '[', '}': '{' };
|
|
24
|
+
* for (const char of input) {
|
|
25
|
+
* if ('([{'.includes(char)) {
|
|
26
|
+
* stack.push(char);
|
|
27
|
+
* } else if (')]}'.includes(char)) {
|
|
28
|
+
* if (stack.pop() !== matches[char]) {
|
|
29
|
+
* fail('Parentheses are not balanced');
|
|
30
|
+
* }
|
|
31
|
+
* }
|
|
32
|
+
* }
|
|
33
|
+
* console.log(stack.isEmpty()); // true
|
|
34
|
+
* @example
|
|
35
|
+
* // Expression Evaluation and Conversion
|
|
36
|
+
* const stack = new Stack<number>();
|
|
37
|
+
* const expression = [5, 3, '+']; // Equivalent to 5 + 3
|
|
38
|
+
* expression.forEach(token => {
|
|
39
|
+
* if (typeof token === 'number') {
|
|
40
|
+
* stack.push(token);
|
|
41
|
+
* } else {
|
|
42
|
+
* const b = stack.pop()!;
|
|
43
|
+
* const a = stack.pop()!;
|
|
44
|
+
* stack.push(token === '+' ? a + b : 0); // Only handling '+' here
|
|
45
|
+
* }
|
|
46
|
+
* });
|
|
47
|
+
* console.log(stack.pop()); // 8
|
|
48
|
+
* @example
|
|
49
|
+
* // Depth-First Search (DFS)
|
|
50
|
+
* const stack = new Stack<number>();
|
|
51
|
+
* const graph: { [key in number]: number[] } = { 1: [2, 3], 2: [4], 3: [5], 4: [], 5: [] };
|
|
52
|
+
* const visited: number[] = [];
|
|
53
|
+
* stack.push(1);
|
|
54
|
+
* while (!stack.isEmpty()) {
|
|
55
|
+
* const node = stack.pop()!;
|
|
56
|
+
* if (!visited.includes(node)) {
|
|
57
|
+
* visited.push(node);
|
|
58
|
+
* graph[node].forEach(neighbor => stack.push(neighbor));
|
|
59
|
+
* }
|
|
60
|
+
* }
|
|
61
|
+
* console.log(visited); // [1, 3, 5, 2, 4]
|
|
62
|
+
* @example
|
|
63
|
+
* // Backtracking Algorithms
|
|
64
|
+
* const stack = new Stack<[number, number]>();
|
|
65
|
+
* const maze = [
|
|
66
|
+
* ['S', ' ', 'X'],
|
|
67
|
+
* ['X', ' ', 'X'],
|
|
68
|
+
* [' ', ' ', 'E']
|
|
69
|
+
* ];
|
|
70
|
+
* const start: [number, number] = [0, 0];
|
|
71
|
+
* const end = [2, 2];
|
|
72
|
+
* const directions = [
|
|
73
|
+
* [0, 1], // To the right
|
|
74
|
+
* [1, 0], // down
|
|
75
|
+
* [0, -1], // left
|
|
76
|
+
* [-1, 0] // up
|
|
77
|
+
* ];
|
|
78
|
+
*
|
|
79
|
+
* const visited = new Set<string>(); // Used to record visited nodes
|
|
80
|
+
* stack.push(start);
|
|
81
|
+
* const path: number[][] = [];
|
|
82
|
+
*
|
|
83
|
+
* while (!stack.isEmpty()) {
|
|
84
|
+
* const [x, y] = stack.pop()!;
|
|
85
|
+
* if (visited.has(`${x},${y}`)) continue; // Skip already visited nodes
|
|
86
|
+
* visited.add(`${x},${y}`);
|
|
87
|
+
*
|
|
88
|
+
* path.push([x, y]);
|
|
89
|
+
*
|
|
90
|
+
* if (x === end[0] && y === end[1]) {
|
|
91
|
+
* break; // Find the end point and exit
|
|
92
|
+
* }
|
|
93
|
+
*
|
|
94
|
+
* for (const [dx, dy] of directions) {
|
|
95
|
+
* const nx = x + dx;
|
|
96
|
+
* const ny = y + dy;
|
|
97
|
+
* if (
|
|
98
|
+
* maze[nx]?.[ny] === ' ' || // feasible path
|
|
99
|
+
* maze[nx]?.[ny] === 'E' // destination
|
|
100
|
+
* ) {
|
|
101
|
+
* stack.push([nx, ny]);
|
|
102
|
+
* }
|
|
103
|
+
* }
|
|
104
|
+
* }
|
|
105
|
+
*
|
|
106
|
+
* expect(path).toContainEqual(end);
|
|
107
|
+
* @example
|
|
108
|
+
* // Function Call Stack
|
|
109
|
+
* const functionStack = new Stack<string>();
|
|
110
|
+
* functionStack.push('main');
|
|
111
|
+
* functionStack.push('foo');
|
|
112
|
+
* functionStack.push('bar');
|
|
113
|
+
* console.log(functionStack.pop()); // 'bar'
|
|
114
|
+
* console.log(functionStack.pop()); // 'foo'
|
|
115
|
+
* console.log(functionStack.pop()); // 'main'
|
|
116
|
+
* @example
|
|
117
|
+
* // Simplify File Paths
|
|
118
|
+
* const stack = new Stack<string>();
|
|
119
|
+
* const path = '/a/./b/../../c';
|
|
120
|
+
* path.split('/').forEach(segment => {
|
|
121
|
+
* if (segment === '..') stack.pop();
|
|
122
|
+
* else if (segment && segment !== '.') stack.push(segment);
|
|
123
|
+
* });
|
|
124
|
+
* console.log(stack.elements.join('/')); // 'c'
|
|
125
|
+
* @example
|
|
126
|
+
* // Stock Span Problem
|
|
127
|
+
* const stack = new Stack<number>();
|
|
128
|
+
* const prices = [100, 80, 60, 70, 60, 75, 85];
|
|
129
|
+
* const spans: number[] = [];
|
|
130
|
+
* prices.forEach((price, i) => {
|
|
131
|
+
* while (!stack.isEmpty() && prices[stack.peek()!] <= price) {
|
|
132
|
+
* stack.pop();
|
|
133
|
+
* }
|
|
134
|
+
* spans.push(stack.isEmpty() ? i + 1 : i - stack.peek()!);
|
|
135
|
+
* stack.push(i);
|
|
136
|
+
* });
|
|
137
|
+
* console.log(spans); // [1, 1, 1, 2, 1, 4, 6]
|
|
17
138
|
*/
|
|
18
|
-
export declare class Stack<E = any, R = any> extends IterableElementBase<E, R
|
|
139
|
+
export declare class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
19
140
|
constructor(elements?: Iterable<E> | Iterable<R>, options?: StackOptions<E, R>);
|
|
20
141
|
protected _elements: E[];
|
|
21
142
|
/**
|
|
@@ -102,14 +223,6 @@ export declare class Stack<E = any, R = any> extends IterableElementBase<E, R, S
|
|
|
102
223
|
* @returns An array of type E.
|
|
103
224
|
*/
|
|
104
225
|
deleteAt(index: number): boolean;
|
|
105
|
-
/**
|
|
106
|
-
* Time Complexity: O(n)
|
|
107
|
-
* Space Complexity: O(n)
|
|
108
|
-
*
|
|
109
|
-
* The toArray function returns a copy of the elements in an array.
|
|
110
|
-
* @returns An array of type E.
|
|
111
|
-
*/
|
|
112
|
-
toArray(): E[];
|
|
113
226
|
/**
|
|
114
227
|
* Time Complexity: O(1)
|
|
115
228
|
* Space Complexity: O(1)
|
|
@@ -141,7 +254,7 @@ export declare class Stack<E = any, R = any> extends IterableElementBase<E, R, S
|
|
|
141
254
|
* @returns The `filter` method is returning a new `Stack` object that contains the elements that
|
|
142
255
|
* satisfy the given predicate function.
|
|
143
256
|
*/
|
|
144
|
-
filter(predicate: ElementCallback<E, R, boolean
|
|
257
|
+
filter(predicate: ElementCallback<E, R, boolean>, thisArg?: any): Stack<E, R>;
|
|
145
258
|
/**
|
|
146
259
|
* Time Complexity: O(n)
|
|
147
260
|
* Space Complexity: O(n)
|
|
@@ -159,7 +272,7 @@ export declare class Stack<E = any, R = any> extends IterableElementBase<E, R, S
|
|
|
159
272
|
* value of
|
|
160
273
|
* @returns a new Stack object with elements of type EM and raw elements of type RM.
|
|
161
274
|
*/
|
|
162
|
-
map<EM, RM>(callback: ElementCallback<E, R, EM
|
|
275
|
+
map<EM, RM>(callback: ElementCallback<E, R, EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): Stack<EM, RM>;
|
|
163
276
|
/**
|
|
164
277
|
* Time Complexity: O(n)
|
|
165
278
|
* Space Complexity: O(n)
|
|
@@ -9,6 +9,127 @@ const base_1 = require("../base");
|
|
|
9
9
|
* 4. Function Calls: In most modern programming languages, the records of function calls are managed through a stack. When a function is called, its record (including parameters, local variables, and return address) is 'pushed' into the stack. When the function returns, its record is 'popped' from the stack.
|
|
10
10
|
* 5. Expression Evaluation: Used for the evaluation of arithmetic or logical expressions, especially when dealing with parenthesis matching and operator precedence.
|
|
11
11
|
* 6. Backtracking Algorithms: In problems where multiple branches need to be explored but only one branch can be explored at a time, stacks can be used to save the state at each branching point.
|
|
12
|
+
* @example
|
|
13
|
+
* // Balanced Parentheses or Brackets
|
|
14
|
+
* type ValidCharacters = ')' | '(' | ']' | '[' | '}' | '{';
|
|
15
|
+
*
|
|
16
|
+
* const stack = new Stack<string>();
|
|
17
|
+
* const input: ValidCharacters[] = '[({})]'.split('') as ValidCharacters[];
|
|
18
|
+
* const matches: { [key in ValidCharacters]?: ValidCharacters } = { ')': '(', ']': '[', '}': '{' };
|
|
19
|
+
* for (const char of input) {
|
|
20
|
+
* if ('([{'.includes(char)) {
|
|
21
|
+
* stack.push(char);
|
|
22
|
+
* } else if (')]}'.includes(char)) {
|
|
23
|
+
* if (stack.pop() !== matches[char]) {
|
|
24
|
+
* fail('Parentheses are not balanced');
|
|
25
|
+
* }
|
|
26
|
+
* }
|
|
27
|
+
* }
|
|
28
|
+
* console.log(stack.isEmpty()); // true
|
|
29
|
+
* @example
|
|
30
|
+
* // Expression Evaluation and Conversion
|
|
31
|
+
* const stack = new Stack<number>();
|
|
32
|
+
* const expression = [5, 3, '+']; // Equivalent to 5 + 3
|
|
33
|
+
* expression.forEach(token => {
|
|
34
|
+
* if (typeof token === 'number') {
|
|
35
|
+
* stack.push(token);
|
|
36
|
+
* } else {
|
|
37
|
+
* const b = stack.pop()!;
|
|
38
|
+
* const a = stack.pop()!;
|
|
39
|
+
* stack.push(token === '+' ? a + b : 0); // Only handling '+' here
|
|
40
|
+
* }
|
|
41
|
+
* });
|
|
42
|
+
* console.log(stack.pop()); // 8
|
|
43
|
+
* @example
|
|
44
|
+
* // Depth-First Search (DFS)
|
|
45
|
+
* const stack = new Stack<number>();
|
|
46
|
+
* const graph: { [key in number]: number[] } = { 1: [2, 3], 2: [4], 3: [5], 4: [], 5: [] };
|
|
47
|
+
* const visited: number[] = [];
|
|
48
|
+
* stack.push(1);
|
|
49
|
+
* while (!stack.isEmpty()) {
|
|
50
|
+
* const node = stack.pop()!;
|
|
51
|
+
* if (!visited.includes(node)) {
|
|
52
|
+
* visited.push(node);
|
|
53
|
+
* graph[node].forEach(neighbor => stack.push(neighbor));
|
|
54
|
+
* }
|
|
55
|
+
* }
|
|
56
|
+
* console.log(visited); // [1, 3, 5, 2, 4]
|
|
57
|
+
* @example
|
|
58
|
+
* // Backtracking Algorithms
|
|
59
|
+
* const stack = new Stack<[number, number]>();
|
|
60
|
+
* const maze = [
|
|
61
|
+
* ['S', ' ', 'X'],
|
|
62
|
+
* ['X', ' ', 'X'],
|
|
63
|
+
* [' ', ' ', 'E']
|
|
64
|
+
* ];
|
|
65
|
+
* const start: [number, number] = [0, 0];
|
|
66
|
+
* const end = [2, 2];
|
|
67
|
+
* const directions = [
|
|
68
|
+
* [0, 1], // To the right
|
|
69
|
+
* [1, 0], // down
|
|
70
|
+
* [0, -1], // left
|
|
71
|
+
* [-1, 0] // up
|
|
72
|
+
* ];
|
|
73
|
+
*
|
|
74
|
+
* const visited = new Set<string>(); // Used to record visited nodes
|
|
75
|
+
* stack.push(start);
|
|
76
|
+
* const path: number[][] = [];
|
|
77
|
+
*
|
|
78
|
+
* while (!stack.isEmpty()) {
|
|
79
|
+
* const [x, y] = stack.pop()!;
|
|
80
|
+
* if (visited.has(`${x},${y}`)) continue; // Skip already visited nodes
|
|
81
|
+
* visited.add(`${x},${y}`);
|
|
82
|
+
*
|
|
83
|
+
* path.push([x, y]);
|
|
84
|
+
*
|
|
85
|
+
* if (x === end[0] && y === end[1]) {
|
|
86
|
+
* break; // Find the end point and exit
|
|
87
|
+
* }
|
|
88
|
+
*
|
|
89
|
+
* for (const [dx, dy] of directions) {
|
|
90
|
+
* const nx = x + dx;
|
|
91
|
+
* const ny = y + dy;
|
|
92
|
+
* if (
|
|
93
|
+
* maze[nx]?.[ny] === ' ' || // feasible path
|
|
94
|
+
* maze[nx]?.[ny] === 'E' // destination
|
|
95
|
+
* ) {
|
|
96
|
+
* stack.push([nx, ny]);
|
|
97
|
+
* }
|
|
98
|
+
* }
|
|
99
|
+
* }
|
|
100
|
+
*
|
|
101
|
+
* expect(path).toContainEqual(end);
|
|
102
|
+
* @example
|
|
103
|
+
* // Function Call Stack
|
|
104
|
+
* const functionStack = new Stack<string>();
|
|
105
|
+
* functionStack.push('main');
|
|
106
|
+
* functionStack.push('foo');
|
|
107
|
+
* functionStack.push('bar');
|
|
108
|
+
* console.log(functionStack.pop()); // 'bar'
|
|
109
|
+
* console.log(functionStack.pop()); // 'foo'
|
|
110
|
+
* console.log(functionStack.pop()); // 'main'
|
|
111
|
+
* @example
|
|
112
|
+
* // Simplify File Paths
|
|
113
|
+
* const stack = new Stack<string>();
|
|
114
|
+
* const path = '/a/./b/../../c';
|
|
115
|
+
* path.split('/').forEach(segment => {
|
|
116
|
+
* if (segment === '..') stack.pop();
|
|
117
|
+
* else if (segment && segment !== '.') stack.push(segment);
|
|
118
|
+
* });
|
|
119
|
+
* console.log(stack.elements.join('/')); // 'c'
|
|
120
|
+
* @example
|
|
121
|
+
* // Stock Span Problem
|
|
122
|
+
* const stack = new Stack<number>();
|
|
123
|
+
* const prices = [100, 80, 60, 70, 60, 75, 85];
|
|
124
|
+
* const spans: number[] = [];
|
|
125
|
+
* prices.forEach((price, i) => {
|
|
126
|
+
* while (!stack.isEmpty() && prices[stack.peek()!] <= price) {
|
|
127
|
+
* stack.pop();
|
|
128
|
+
* }
|
|
129
|
+
* spans.push(stack.isEmpty() ? i + 1 : i - stack.peek()!);
|
|
130
|
+
* stack.push(i);
|
|
131
|
+
* });
|
|
132
|
+
* console.log(spans); // [1, 1, 1, 2, 1, 4, 6]
|
|
12
133
|
*/
|
|
13
134
|
class Stack extends base_1.IterableElementBase {
|
|
14
135
|
constructor(elements = [], options) {
|
|
@@ -136,16 +257,6 @@ class Stack extends base_1.IterableElementBase {
|
|
|
136
257
|
const spliced = this.elements.splice(index, 1);
|
|
137
258
|
return spliced.length === 1;
|
|
138
259
|
}
|
|
139
|
-
/**
|
|
140
|
-
* Time Complexity: O(n)
|
|
141
|
-
* Space Complexity: O(n)
|
|
142
|
-
*
|
|
143
|
-
* The toArray function returns a copy of the elements in an array.
|
|
144
|
-
* @returns An array of type E.
|
|
145
|
-
*/
|
|
146
|
-
toArray() {
|
|
147
|
-
return this.elements.slice();
|
|
148
|
-
}
|
|
149
260
|
/**
|
|
150
261
|
* Time Complexity: O(1)
|
|
151
262
|
* Space Complexity: O(1)
|
|
@@ -143,7 +143,7 @@ export declare class TrieNode {
|
|
|
143
143
|
* const subnet = ip.split('.').slice(0, 3).join('.');
|
|
144
144
|
* console.log(ipRoutingTable.hasPrefix(subnet)); // true
|
|
145
145
|
*/
|
|
146
|
-
export declare class Trie<R = any> extends IterableElementBase<string, R
|
|
146
|
+
export declare class Trie<R = any> extends IterableElementBase<string, R> {
|
|
147
147
|
/**
|
|
148
148
|
* The constructor initializes a Trie data structure with optional options and words provided as
|
|
149
149
|
* input.
|
|
@@ -311,7 +311,7 @@ export declare class Trie<R = any> extends IterableElementBase<string, R, Trie<R
|
|
|
311
311
|
* specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
|
|
312
312
|
* @returns The `filter` method is returning an array of strings (`string[]`).
|
|
313
313
|
*/
|
|
314
|
-
filter(predicate: ElementCallback<string, R, boolean
|
|
314
|
+
filter(predicate: ElementCallback<string, R, boolean>, thisArg?: any): Trie<R>;
|
|
315
315
|
/**
|
|
316
316
|
* Time Complexity: O(n)
|
|
317
317
|
* Space Complexity: O(n)
|
|
@@ -330,7 +330,7 @@ export declare class Trie<R = any> extends IterableElementBase<string, R, Trie<R
|
|
|
330
330
|
* value of
|
|
331
331
|
* @returns a new Trie object.
|
|
332
332
|
*/
|
|
333
|
-
map<RM>(callback: ElementCallback<string, R, string
|
|
333
|
+
map<RM>(callback: ElementCallback<string, R, string>, toElementFn?: (rawElement: RM) => string, thisArg?: any): Trie<RM>;
|
|
334
334
|
/**
|
|
335
335
|
* Time Complexity: O(n)
|
|
336
336
|
* Space Complexity: O(n)
|
|
@@ -339,6 +339,7 @@ export declare class Trie<R = any> extends IterableElementBase<string, R, Trie<R
|
|
|
339
339
|
* trie data structure and yields all the paths to the end nodes.
|
|
340
340
|
*/
|
|
341
341
|
protected _getIterator(): IterableIterator<string>;
|
|
342
|
+
protected get _total(): number;
|
|
342
343
|
/**
|
|
343
344
|
* Time Complexity: O(l), where l is the length of the input string.
|
|
344
345
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -573,6 +573,9 @@ class Trie extends base_1.IterableElementBase {
|
|
|
573
573
|
}
|
|
574
574
|
yield* _dfs(this.root, '');
|
|
575
575
|
}
|
|
576
|
+
get _total() {
|
|
577
|
+
return this._size;
|
|
578
|
+
}
|
|
576
579
|
/**
|
|
577
580
|
* Time Complexity: O(l), where l is the length of the input string.
|
|
578
581
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import { IterableElementBase, IterableEntryBase } from '../../../data-structures';
|
|
2
|
-
|
|
3
|
-
export type
|
|
4
|
-
export type
|
|
5
|
-
export type
|
|
2
|
+
import { LinearBase } from '../../../data-structures/base/linear-base';
|
|
3
|
+
export type EntryCallback<K, V, R> = (key: K, value: V, index: number, original: IterableEntryBase<K, V>) => R;
|
|
4
|
+
export type ElementCallback<E, R, RT> = (element: E, index: number, original: IterableElementBase<E, R>) => RT;
|
|
5
|
+
export type ReduceEntryCallback<K, V, R> = (accumulator: R, value: V, key: K, index: number, original: IterableEntryBase<K, V>) => R;
|
|
6
|
+
export type ReduceElementCallback<E, R, RT = E> = (accumulator: RT, element: E, index: number, original: IterableElementBase<E, R>) => RT;
|
|
7
|
+
export type ReduceLinearCallback<E, RT = E> = (accumulator: RT, element: E, index: number, original: LinearBase<E>) => RT;
|
|
6
8
|
export type IterableElementBaseOptions<E, R> = {
|
|
7
9
|
toElementFn?: (rawElement: R) => E;
|
|
8
10
|
};
|
|
11
|
+
export type LinearBaseOptions<E, R> = IterableElementBaseOptions<E, R> & {
|
|
12
|
+
maxLen?: number;
|
|
13
|
+
};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { AVLTreeOptions } from './avl-tree';
|
|
2
|
-
export type AVLTreeMultiMapOptions<K, V, R> =
|
|
2
|
+
export type AVLTreeMultiMapOptions<K, V, R> = AVLTreeOptions<K, V, R> & {};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { RedBlackTreeOptions } from './red-black-tree';
|
|
2
|
-
export type TreeMultiMapOptions<K, V, R> =
|
|
2
|
+
export type TreeMultiMapOptions<K, V, R> = RedBlackTreeOptions<K, V, R> & {};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export type DoublyLinkedListOptions<E, R> =
|
|
1
|
+
import { LinearBaseOptions } from '../base';
|
|
2
|
+
export type DoublyLinkedListOptions<E, R> = LinearBaseOptions<E, R> & {};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export type SinglyLinkedListOptions<E, R> =
|
|
1
|
+
import { LinearBaseOptions } from '../base';
|
|
2
|
+
export type SinglyLinkedListOptions<E, R> = LinearBaseOptions<E, R> & {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "undirected-graph-typed",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "Undirected Graph. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -145,6 +145,6 @@
|
|
|
145
145
|
"typescript": "^4.9.5"
|
|
146
146
|
},
|
|
147
147
|
"dependencies": {
|
|
148
|
-
"data-structure-typed": "^
|
|
148
|
+
"data-structure-typed": "^2.0.1"
|
|
149
149
|
}
|
|
150
150
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ElementCallback, IterableElementBaseOptions, ReduceElementCallback } from '../../types';
|
|
2
2
|
|
|
3
|
-
export abstract class IterableElementBase<E, R
|
|
3
|
+
export abstract class IterableElementBase<E, R> {
|
|
4
4
|
/**
|
|
5
5
|
* The protected constructor initializes the options for the IterableElementBase class, including the
|
|
6
6
|
* toElementFn function.
|
|
@@ -14,17 +14,8 @@ export abstract class IterableElementBase<E, R, C> {
|
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
abstract get size(): number;
|
|
18
|
-
|
|
19
17
|
protected _toElementFn?: (rawElement: R) => E;
|
|
20
18
|
|
|
21
|
-
/**
|
|
22
|
-
* The function returns the _toElementFn property, which is a function that converts a raw element to
|
|
23
|
-
* a specific type.
|
|
24
|
-
* @returns The function `get toElementFn()` is returning either a function that takes a raw element
|
|
25
|
-
* `rawElement` of type `R` and returns an element `E`, or `undefined` if no function is assigned to
|
|
26
|
-
* `_toElementFn`.
|
|
27
|
-
*/
|
|
28
19
|
get toElementFn(): ((rawElement: R) => E) | undefined {
|
|
29
20
|
return this._toElementFn;
|
|
30
21
|
}
|
|
@@ -68,7 +59,7 @@ export abstract class IterableElementBase<E, R, C> {
|
|
|
68
59
|
* @returns The `every` method is returning a boolean value. It returns `true` if every element in
|
|
69
60
|
* the array satisfies the provided predicate function, and `false` otherwise.
|
|
70
61
|
*/
|
|
71
|
-
every(predicate: ElementCallback<E, R, boolean
|
|
62
|
+
every(predicate: ElementCallback<E, R, boolean>, thisArg?: any): boolean {
|
|
72
63
|
let index = 0;
|
|
73
64
|
for (const item of this) {
|
|
74
65
|
if (!predicate.call(thisArg, item, index++, this)) {
|
|
@@ -92,7 +83,7 @@ export abstract class IterableElementBase<E, R, C> {
|
|
|
92
83
|
* @returns a boolean value. It returns true if the predicate function returns true for any element
|
|
93
84
|
* in the collection, and false otherwise.
|
|
94
85
|
*/
|
|
95
|
-
some(predicate: ElementCallback<E, R, boolean
|
|
86
|
+
some(predicate: ElementCallback<E, R, boolean>, thisArg?: any): boolean {
|
|
96
87
|
let index = 0;
|
|
97
88
|
for (const item of this) {
|
|
98
89
|
if (predicate.call(thisArg, item, index++, this)) {
|
|
@@ -115,20 +106,23 @@ export abstract class IterableElementBase<E, R, C> {
|
|
|
115
106
|
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
116
107
|
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
117
108
|
*/
|
|
118
|
-
forEach(callbackfn: ElementCallback<E, R, void
|
|
109
|
+
forEach(callbackfn: ElementCallback<E, R, void>, thisArg?: any): void {
|
|
119
110
|
let index = 0;
|
|
120
111
|
for (const item of this) {
|
|
121
112
|
callbackfn.call(thisArg, item, index++, this);
|
|
122
113
|
}
|
|
123
114
|
}
|
|
124
115
|
|
|
116
|
+
find<S extends E>(predicate: ElementCallback<E, R, S>, thisArg?: any): S | undefined;
|
|
117
|
+
find(predicate: ElementCallback<E, R, unknown>, thisArg?: any): E | undefined;
|
|
118
|
+
|
|
125
119
|
/**
|
|
126
120
|
* Time Complexity: O(n)
|
|
127
121
|
* Space Complexity: O(1)
|
|
128
122
|
*
|
|
129
123
|
* The `find` function iterates over the elements of an array-like object and returns the first
|
|
130
124
|
* element that satisfies the provided callback function.
|
|
131
|
-
* @param
|
|
125
|
+
* @param predicate - The predicate parameter is a function that will be called for each element in
|
|
132
126
|
* the array. It takes three arguments: the current element being processed, the index of the current
|
|
133
127
|
* element, and the array itself. The function should return a boolean value indicating whether the
|
|
134
128
|
* current element matches the desired condition.
|
|
@@ -138,10 +132,10 @@ export abstract class IterableElementBase<E, R, C> {
|
|
|
138
132
|
* @returns The `find` method returns the first element in the array that satisfies the provided
|
|
139
133
|
* callback function. If no element satisfies the callback function, `undefined` is returned.
|
|
140
134
|
*/
|
|
141
|
-
find(
|
|
135
|
+
find(predicate: ElementCallback<E, R, boolean>, thisArg?: any): E | undefined {
|
|
142
136
|
let index = 0;
|
|
143
137
|
for (const item of this) {
|
|
144
|
-
if (
|
|
138
|
+
if (predicate.call(thisArg, item, index++, this)) return item;
|
|
145
139
|
}
|
|
146
140
|
|
|
147
141
|
return;
|
|
@@ -164,6 +158,10 @@ export abstract class IterableElementBase<E, R, C> {
|
|
|
164
158
|
return false;
|
|
165
159
|
}
|
|
166
160
|
|
|
161
|
+
reduce(callbackfn: ReduceElementCallback<E, R>): E;
|
|
162
|
+
reduce(callbackfn: ReduceElementCallback<E, R>, initialValue: E): E;
|
|
163
|
+
reduce<U>(callbackfn: ReduceElementCallback<E, R, U>, initialValue: U): U;
|
|
164
|
+
|
|
167
165
|
/**
|
|
168
166
|
* Time Complexity: O(n)
|
|
169
167
|
* Space Complexity: O(1)
|
|
@@ -177,15 +175,26 @@ export abstract class IterableElementBase<E, R, C> {
|
|
|
177
175
|
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
178
176
|
* all the elements in the array and applying the callback function to each element.
|
|
179
177
|
*/
|
|
180
|
-
reduce<U>(callbackfn: ReduceElementCallback<E, R, U
|
|
181
|
-
let accumulator = initialValue;
|
|
178
|
+
reduce<U>(callbackfn: ReduceElementCallback<E, R, U>, initialValue?: U): U {
|
|
179
|
+
let accumulator = initialValue ?? (0 as U);
|
|
182
180
|
let index = 0;
|
|
183
181
|
for (const item of this) {
|
|
184
|
-
accumulator = callbackfn(accumulator, item
|
|
182
|
+
accumulator = callbackfn(accumulator, item, index++, this);
|
|
185
183
|
}
|
|
186
184
|
return accumulator;
|
|
187
185
|
}
|
|
188
186
|
|
|
187
|
+
/**
|
|
188
|
+
* Time Complexity: O(n)
|
|
189
|
+
* Space Complexity: O(n)
|
|
190
|
+
*
|
|
191
|
+
* The `toArray` function converts a linked list into an array.
|
|
192
|
+
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
193
|
+
*/
|
|
194
|
+
toArray(): E[] {
|
|
195
|
+
return [...this];
|
|
196
|
+
}
|
|
197
|
+
|
|
189
198
|
/**
|
|
190
199
|
* Time Complexity: O(n)
|
|
191
200
|
* Space Complexity: O(n)
|
|
@@ -210,7 +219,7 @@ export abstract class IterableElementBase<E, R, C> {
|
|
|
210
219
|
|
|
211
220
|
abstract clear(): void;
|
|
212
221
|
|
|
213
|
-
abstract clone():
|
|
222
|
+
abstract clone(): IterableElementBase<E, R>;
|
|
214
223
|
|
|
215
224
|
abstract map(...args: any[]): any;
|
|
216
225
|
|