min-heap-typed 2.1.1 → 2.1.2
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/cjs/index.cjs +987 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/esm/index.mjs +980 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/types/data-structures/base/index.d.ts +2 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +182 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +135 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +291 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +174 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +754 -29
- package/dist/types/data-structures/binary-tree/bst.d.ts +413 -12
- package/dist/types/data-structures/binary-tree/index.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +208 -3
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +160 -1
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +190 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +270 -2
- package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -14
- package/dist/types/data-structures/graph/directed-graph.d.ts +207 -1
- package/dist/types/data-structures/graph/index.d.ts +2 -1
- package/dist/types/data-structures/graph/map-graph.d.ts +78 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +188 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +345 -19
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +503 -5
- package/dist/types/data-structures/heap/index.d.ts +2 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +32 -1
- package/dist/types/data-structures/heap/min-heap.d.ts +33 -1
- package/dist/types/data-structures/index.d.ts +7 -7
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +769 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +451 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -4
- package/dist/types/data-structures/matrix/index.d.ts +1 -1
- package/dist/types/data-structures/matrix/matrix.d.ts +168 -7
- package/dist/types/data-structures/matrix/navigator.d.ts +54 -13
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +27 -1
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +26 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +15 -2
- package/dist/types/data-structures/queue/deque.d.ts +431 -4
- package/dist/types/data-structures/queue/queue.d.ts +308 -4
- package/dist/types/data-structures/stack/stack.d.ts +306 -2
- package/dist/types/data-structures/tree/tree.d.ts +62 -1
- package/dist/types/data-structures/trie/trie.d.ts +350 -4
- package/dist/types/index.d.ts +13 -2
- package/dist/{interfaces → types/interfaces}/binary-tree.d.ts +1 -1
- package/dist/types/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +12 -0
- package/dist/{data-structures → types/types/data-structures}/binary-tree/index.d.ts +2 -3
- package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/graph/abstract-graph.d.ts +14 -0
- package/dist/types/types/data-structures/graph/directed-graph.d.ts +1 -0
- package/dist/{data-structures → types/types/data-structures}/graph/index.d.ts +1 -2
- package/dist/types/types/data-structures/graph/map-graph.d.ts +1 -0
- package/dist/types/types/data-structures/graph/undirected-graph.d.ts +1 -0
- package/dist/types/types/data-structures/hash/hash-map.d.ts +19 -0
- package/dist/types/types/data-structures/hash/index.d.ts +2 -0
- package/dist/types/types/data-structures/heap/heap.d.ts +5 -0
- package/dist/types/types/data-structures/heap/index.d.ts +1 -0
- package/dist/types/types/data-structures/heap/max-heap.d.ts +1 -0
- package/dist/types/types/data-structures/heap/min-heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +4 -0
- package/dist/types/types/data-structures/matrix/matrix.d.ts +7 -0
- package/dist/types/types/data-structures/matrix/navigator.d.ts +14 -0
- package/dist/types/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +2 -0
- package/dist/types/types/data-structures/queue/deque.d.ts +4 -0
- package/dist/types/types/data-structures/queue/queue.d.ts +4 -0
- package/dist/types/types/data-structures/stack/stack.d.ts +2 -0
- package/dist/types/types/data-structures/tree/tree.d.ts +1 -0
- package/dist/types/types/data-structures/trie/trie.d.ts +4 -0
- package/dist/types/types/index.d.ts +3 -0
- package/dist/types/types/utils/index.d.ts +2 -0
- package/dist/types/types/utils/utils.d.ts +22 -0
- package/dist/types/utils/index.d.ts +1 -1
- package/dist/types/utils/utils.d.ts +209 -22
- package/dist/umd/max-heap-typed.js +992 -0
- package/dist/umd/max-heap-typed.js.map +1 -0
- package/dist/umd/max-heap-typed.min.js +16 -0
- package/dist/umd/max-heap-typed.min.js.map +1 -0
- package/package.json +25 -5
- package/src/data-structures/binary-tree/avl-tree-counter.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +4 -4
- package/src/data-structures/binary-tree/bst.ts +1 -1
- package/src/data-structures/binary-tree/red-black-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-counter.ts +4 -4
- package/src/data-structures/binary-tree/tree-multi-map.ts +1 -1
- package/src/data-structures/heap/heap.ts +5 -5
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -2
- package/src/interfaces/binary-tree.ts +1 -1
- package/tsconfig.base.json +23 -0
- package/tsconfig.json +8 -34
- package/tsconfig.test.json +8 -0
- package/tsconfig.types.json +15 -0
- package/tsup.config.js +28 -0
- package/tsup.node.config.js +37 -0
- package/dist/common/index.js +0 -28
- package/dist/constants/index.js +0 -8
- package/dist/data-structures/base/index.d.ts +0 -2
- package/dist/data-structures/base/index.js +0 -18
- package/dist/data-structures/base/iterable-element-base.js +0 -243
- package/dist/data-structures/base/iterable-entry-base.js +0 -183
- package/dist/data-structures/base/linear-base.js +0 -415
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +0 -182
- package/dist/data-structures/binary-tree/avl-tree-counter.js +0 -374
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -135
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +0 -250
- package/dist/data-structures/binary-tree/avl-tree.d.ts +0 -291
- package/dist/data-structures/binary-tree/avl-tree.js +0 -611
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -174
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +0 -294
- package/dist/data-structures/binary-tree/binary-tree.d.ts +0 -754
- package/dist/data-structures/binary-tree/binary-tree.js +0 -1925
- package/dist/data-structures/binary-tree/bst.d.ts +0 -413
- package/dist/data-structures/binary-tree/bst.js +0 -903
- package/dist/data-structures/binary-tree/index.js +0 -26
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +0 -208
- package/dist/data-structures/binary-tree/red-black-tree.js +0 -546
- package/dist/data-structures/binary-tree/segment-tree.d.ts +0 -160
- package/dist/data-structures/binary-tree/segment-tree.js +0 -297
- package/dist/data-structures/binary-tree/tree-counter.d.ts +0 -190
- package/dist/data-structures/binary-tree/tree-counter.js +0 -413
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +0 -270
- package/dist/data-structures/binary-tree/tree-multi-map.js +0 -384
- package/dist/data-structures/graph/abstract-graph.d.ts +0 -340
- package/dist/data-structures/graph/abstract-graph.js +0 -896
- package/dist/data-structures/graph/directed-graph.d.ts +0 -207
- package/dist/data-structures/graph/directed-graph.js +0 -525
- package/dist/data-structures/graph/index.js +0 -20
- package/dist/data-structures/graph/map-graph.d.ts +0 -78
- package/dist/data-structures/graph/map-graph.js +0 -107
- package/dist/data-structures/graph/undirected-graph.d.ts +0 -188
- package/dist/data-structures/graph/undirected-graph.js +0 -424
- package/dist/data-structures/hash/hash-map.d.ts +0 -345
- package/dist/data-structures/hash/hash-map.js +0 -692
- package/dist/data-structures/hash/index.d.ts +0 -1
- package/dist/data-structures/hash/index.js +0 -17
- package/dist/data-structures/heap/heap.d.ts +0 -503
- package/dist/data-structures/heap/heap.js +0 -901
- package/dist/data-structures/heap/index.d.ts +0 -3
- package/dist/data-structures/heap/index.js +0 -19
- package/dist/data-structures/heap/max-heap.d.ts +0 -32
- package/dist/data-structures/heap/max-heap.js +0 -40
- package/dist/data-structures/heap/min-heap.d.ts +0 -33
- package/dist/data-structures/heap/min-heap.js +0 -31
- package/dist/data-structures/index.js +0 -28
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -769
- package/dist/data-structures/linked-list/doubly-linked-list.js +0 -1111
- package/dist/data-structures/linked-list/index.js +0 -19
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +0 -451
- package/dist/data-structures/linked-list/singly-linked-list.js +0 -850
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -27
- package/dist/data-structures/linked-list/skip-linked-list.js +0 -144
- package/dist/data-structures/matrix/index.js +0 -18
- package/dist/data-structures/matrix/matrix.d.ts +0 -168
- package/dist/data-structures/matrix/matrix.js +0 -448
- package/dist/data-structures/matrix/navigator.d.ts +0 -55
- package/dist/data-structures/matrix/navigator.js +0 -111
- package/dist/data-structures/priority-queue/index.js +0 -19
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +0 -27
- package/dist/data-structures/priority-queue/max-priority-queue.js +0 -34
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +0 -26
- package/dist/data-structures/priority-queue/min-priority-queue.js +0 -24
- package/dist/data-structures/priority-queue/priority-queue.d.ts +0 -15
- package/dist/data-structures/priority-queue/priority-queue.js +0 -20
- package/dist/data-structures/queue/deque.d.ts +0 -431
- package/dist/data-structures/queue/deque.js +0 -879
- package/dist/data-structures/queue/index.js +0 -18
- package/dist/data-structures/queue/queue.d.ts +0 -308
- package/dist/data-structures/queue/queue.js +0 -473
- package/dist/data-structures/stack/index.js +0 -17
- package/dist/data-structures/stack/stack.d.ts +0 -306
- package/dist/data-structures/stack/stack.js +0 -401
- package/dist/data-structures/tree/index.js +0 -17
- package/dist/data-structures/tree/tree.d.ts +0 -62
- package/dist/data-structures/tree/tree.js +0 -107
- package/dist/data-structures/trie/index.js +0 -17
- package/dist/data-structures/trie/trie.d.ts +0 -350
- package/dist/data-structures/trie/trie.js +0 -610
- package/dist/index.d.ts +0 -14
- package/dist/index.js +0 -30
- package/dist/interfaces/binary-tree.js +0 -2
- package/dist/interfaces/doubly-linked-list.js +0 -2
- package/dist/interfaces/graph.js +0 -2
- package/dist/interfaces/heap.js +0 -2
- package/dist/interfaces/index.js +0 -24
- package/dist/interfaces/navigator.js +0 -2
- package/dist/interfaces/priority-queue.js +0 -2
- package/dist/interfaces/segment-tree.js +0 -2
- package/dist/interfaces/singly-linked-list.js +0 -2
- package/dist/types/common.js +0 -2
- package/dist/types/data-structures/base/base.js +0 -2
- package/dist/types/data-structures/base/index.js +0 -17
- package/dist/types/data-structures/binary-tree/avl-tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/bst.js +0 -2
- package/dist/types/data-structures/binary-tree/index.js +0 -25
- package/dist/types/data-structures/binary-tree/red-black-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/segment-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.js +0 -2
- package/dist/types/data-structures/graph/abstract-graph.js +0 -2
- package/dist/types/data-structures/graph/directed-graph.js +0 -2
- package/dist/types/data-structures/graph/index.js +0 -19
- package/dist/types/data-structures/graph/map-graph.js +0 -2
- package/dist/types/data-structures/graph/undirected-graph.js +0 -2
- package/dist/types/data-structures/hash/hash-map.js +0 -2
- package/dist/types/data-structures/hash/index.js +0 -17
- package/dist/types/data-structures/heap/heap.js +0 -2
- package/dist/types/data-structures/heap/index.js +0 -17
- package/dist/types/data-structures/heap/max-heap.js +0 -2
- package/dist/types/data-structures/heap/min-heap.js +0 -2
- package/dist/types/data-structures/index.js +0 -28
- package/dist/types/data-structures/linked-list/doubly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/index.js +0 -19
- package/dist/types/data-structures/linked-list/singly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.js +0 -2
- package/dist/types/data-structures/matrix/index.js +0 -18
- package/dist/types/data-structures/matrix/matrix.js +0 -2
- package/dist/types/data-structures/matrix/navigator.js +0 -2
- package/dist/types/data-structures/priority-queue/index.js +0 -19
- package/dist/types/data-structures/priority-queue/max-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/min-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/priority-queue.js +0 -2
- package/dist/types/data-structures/queue/deque.js +0 -2
- package/dist/types/data-structures/queue/index.js +0 -18
- package/dist/types/data-structures/queue/queue.js +0 -2
- package/dist/types/data-structures/stack/index.js +0 -17
- package/dist/types/data-structures/stack/stack.js +0 -2
- package/dist/types/data-structures/tree/index.js +0 -17
- package/dist/types/data-structures/tree/tree.js +0 -2
- package/dist/types/data-structures/trie/index.js +0 -17
- package/dist/types/data-structures/trie/trie.js +0 -2
- package/dist/types/index.js +0 -19
- package/dist/types/utils/index.js +0 -18
- package/dist/types/utils/utils.js +0 -2
- package/dist/types/utils/validate-type.js +0 -2
- package/dist/utils/index.d.ts +0 -2
- package/dist/utils/index.js +0 -18
- package/dist/utils/number.js +0 -24
- package/dist/utils/utils.d.ts +0 -209
- package/dist/utils/utils.js +0 -353
- package/dist/{common → types/common}/index.d.ts +0 -0
- package/dist/{constants → types/constants}/index.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-element-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-entry-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/linear-base.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/doubly-linked-list.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/graph.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/heap.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/index.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/navigator.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/priority-queue.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/segment-tree.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/singly-linked-list.d.ts +0 -0
- package/dist/types/{common.d.ts → types/common.d.ts} +0 -0
- package/dist/types/{data-structures → types/data-structures}/base/base.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/index.d.ts +7 -7
- package/dist/{data-structures → types/types/data-structures}/linked-list/index.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/matrix/index.d.ts +1 -1
- /package/dist/{data-structures → types/types/data-structures}/priority-queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/stack/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/tree/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/trie/index.d.ts +0 -0
- /package/dist/types/{utils → types/utils}/validate-type.d.ts +0 -0
- /package/dist/{utils → types/utils}/number.d.ts +0 -0
|
@@ -1,4 +1,308 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
import type { ElementCallback, LinearBaseOptions, QueueOptions } from '../../types';
|
|
9
|
+
import { SinglyLinkedList } from '../linked-list';
|
|
10
|
+
import { LinearBase } from '../base/linear-base';
|
|
11
|
+
/**
|
|
12
|
+
* Array-backed queue with amortized O(1) enqueue/dequeue via an offset pointer and optional auto-compaction.
|
|
13
|
+
* @remarks Time O(1), Space O(1)
|
|
14
|
+
* @template E
|
|
15
|
+
* @template R
|
|
16
|
+
* 1. First In, First Out (FIFO): The core feature of a queue is its first in, first out nature. The element added to the queue first will be the one to be removed first.
|
|
17
|
+
* 2. Operations: The main operations include enqueue (adding an element to the end of the queue) and dequeue (removing and returning the element at the front of the queue). Typically, there is also a peek operation (looking at the front element without removing it).
|
|
18
|
+
* 3. Uses: Queues are commonly used to manage a series of tasks or elements that need to be processed in order. For example, managing task queues in a multi-threaded environment, or in algorithms for data structures like trees and graphs for breadth-first search.
|
|
19
|
+
* 4. Task Scheduling: Managing the order of task execution in operating systems or applications.
|
|
20
|
+
* 5. Data Buffering: Acting as a buffer for data packets in network communication.
|
|
21
|
+
* 6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store elements that are to be visited.
|
|
22
|
+
* 7. Real-time Queuing: Like queuing systems in banks or supermarkets.
|
|
23
|
+
* @example
|
|
24
|
+
* // Sliding Window using Queue
|
|
25
|
+
* const nums = [2, 3, 4, 1, 5];
|
|
26
|
+
* const k = 2;
|
|
27
|
+
* const queue = new Queue<number>();
|
|
28
|
+
*
|
|
29
|
+
* let maxSum = 0;
|
|
30
|
+
* let currentSum = 0;
|
|
31
|
+
*
|
|
32
|
+
* nums.forEach(num => {
|
|
33
|
+
* queue.push(num);
|
|
34
|
+
* currentSum += num;
|
|
35
|
+
*
|
|
36
|
+
* if (queue.length > k) {
|
|
37
|
+
* currentSum -= queue.shift()!;
|
|
38
|
+
* }
|
|
39
|
+
*
|
|
40
|
+
* if (queue.length === k) {
|
|
41
|
+
* maxSum = Math.max(maxSum, currentSum);
|
|
42
|
+
* }
|
|
43
|
+
* });
|
|
44
|
+
*
|
|
45
|
+
* console.log(maxSum); // 7
|
|
46
|
+
* @example
|
|
47
|
+
* // Breadth-First Search (BFS) using Queue
|
|
48
|
+
* const graph: { [key in number]: number[] } = {
|
|
49
|
+
* 1: [2, 3],
|
|
50
|
+
* 2: [4, 5],
|
|
51
|
+
* 3: [],
|
|
52
|
+
* 4: [],
|
|
53
|
+
* 5: []
|
|
54
|
+
* };
|
|
55
|
+
*
|
|
56
|
+
* const queue = new Queue<number>();
|
|
57
|
+
* const visited: number[] = [];
|
|
58
|
+
*
|
|
59
|
+
* queue.push(1);
|
|
60
|
+
*
|
|
61
|
+
* while (!queue.isEmpty()) {
|
|
62
|
+
* const node = queue.shift()!;
|
|
63
|
+
* if (!visited.includes(node)) {
|
|
64
|
+
* visited.push(node);
|
|
65
|
+
* graph[node].forEach(neighbor => queue.push(neighbor));
|
|
66
|
+
* }
|
|
67
|
+
* }
|
|
68
|
+
*
|
|
69
|
+
* console.log(visited); // [1, 2, 3, 4, 5]
|
|
70
|
+
*/
|
|
71
|
+
export declare class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
72
|
+
/**
|
|
73
|
+
* Create a Queue and optionally bulk-insert elements.
|
|
74
|
+
* @remarks Time O(N), Space O(N)
|
|
75
|
+
* @param [elements] - Iterable of elements (or raw records if toElementFn is set).
|
|
76
|
+
* @param [options] - Options such as toElementFn, maxLen, and autoCompactRatio.
|
|
77
|
+
* @returns New Queue instance.
|
|
78
|
+
*/
|
|
79
|
+
constructor(elements?: Iterable<E> | Iterable<R>, options?: QueueOptions<E, R>);
|
|
80
|
+
protected _elements: E[];
|
|
81
|
+
/**
|
|
82
|
+
* Get the underlying array buffer.
|
|
83
|
+
* @remarks Time O(1), Space O(1)
|
|
84
|
+
* @returns Backing array of elements.
|
|
85
|
+
*/
|
|
86
|
+
get elements(): E[];
|
|
87
|
+
protected _offset: number;
|
|
88
|
+
/**
|
|
89
|
+
* Get the current start offset into the array.
|
|
90
|
+
* @remarks Time O(1), Space O(1)
|
|
91
|
+
* @returns Zero-based offset.
|
|
92
|
+
*/
|
|
93
|
+
get offset(): number;
|
|
94
|
+
protected _autoCompactRatio: number;
|
|
95
|
+
/**
|
|
96
|
+
* Get the compaction threshold (offset/size).
|
|
97
|
+
* @remarks Time O(1), Space O(1)
|
|
98
|
+
* @returns Auto-compaction ratio in (0,1].
|
|
99
|
+
*/
|
|
100
|
+
get autoCompactRatio(): number;
|
|
101
|
+
/**
|
|
102
|
+
* Set the compaction threshold.
|
|
103
|
+
* @remarks Time O(1), Space O(1)
|
|
104
|
+
* @param value - New ratio; compacts when offset/size exceeds this value.
|
|
105
|
+
* @returns void
|
|
106
|
+
*/
|
|
107
|
+
set autoCompactRatio(value: number);
|
|
108
|
+
/**
|
|
109
|
+
* Get the number of elements currently in the queue.
|
|
110
|
+
* @remarks Time O(1), Space O(1)
|
|
111
|
+
* @returns Current length.
|
|
112
|
+
*/
|
|
113
|
+
get length(): number;
|
|
114
|
+
/**
|
|
115
|
+
* Get the first element (front) without removing it.
|
|
116
|
+
* @remarks Time O(1), Space O(1)
|
|
117
|
+
* @returns Front element or undefined.
|
|
118
|
+
*/
|
|
119
|
+
get first(): E | undefined;
|
|
120
|
+
/**
|
|
121
|
+
* Get the last element (back) without removing it.
|
|
122
|
+
* @remarks Time O(1), Space O(1)
|
|
123
|
+
* @returns Back element or undefined.
|
|
124
|
+
*/
|
|
125
|
+
get last(): E | undefined;
|
|
126
|
+
/**
|
|
127
|
+
* Create a queue from an array of elements.
|
|
128
|
+
* @remarks Time O(N), Space O(N)
|
|
129
|
+
* @template E
|
|
130
|
+
* @param elements - Array of elements to enqueue in order.
|
|
131
|
+
* @returns A new queue populated from the array.
|
|
132
|
+
*/
|
|
133
|
+
static fromArray<E>(elements: E[]): Queue<E>;
|
|
134
|
+
/**
|
|
135
|
+
* Check whether the queue is empty.
|
|
136
|
+
* @remarks Time O(1), Space O(1)
|
|
137
|
+
* @returns True if length is 0.
|
|
138
|
+
*/
|
|
139
|
+
isEmpty(): boolean;
|
|
140
|
+
/**
|
|
141
|
+
* Enqueue one element at the back.
|
|
142
|
+
* @remarks Time O(1), Space O(1)
|
|
143
|
+
* @param element - Element to enqueue.
|
|
144
|
+
* @returns True on success.
|
|
145
|
+
*/
|
|
146
|
+
push(element: E): boolean;
|
|
147
|
+
/**
|
|
148
|
+
* Enqueue many elements from an iterable.
|
|
149
|
+
* @remarks Time O(N), Space O(1)
|
|
150
|
+
* @param elements - Iterable of elements (or raw records if toElementFn is set).
|
|
151
|
+
* @returns Array of per-element success flags.
|
|
152
|
+
*/
|
|
153
|
+
pushMany(elements: Iterable<E> | Iterable<R>): boolean[];
|
|
154
|
+
/**
|
|
155
|
+
* Dequeue one element from the front (amortized via offset).
|
|
156
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
157
|
+
* @returns Removed element or undefined.
|
|
158
|
+
*/
|
|
159
|
+
shift(): E | undefined;
|
|
160
|
+
/**
|
|
161
|
+
* Delete the first occurrence of a specific element.
|
|
162
|
+
* @remarks Time O(N), Space O(1)
|
|
163
|
+
* @param element - Element to remove (strict equality via Object.is).
|
|
164
|
+
* @returns True if an element was removed.
|
|
165
|
+
*/
|
|
166
|
+
delete(element: E): boolean;
|
|
167
|
+
/**
|
|
168
|
+
* Get the element at a given logical index.
|
|
169
|
+
* @remarks Time O(1), Space O(1)
|
|
170
|
+
* @param index - Zero-based index from the front.
|
|
171
|
+
* @returns Element or undefined.
|
|
172
|
+
*/
|
|
173
|
+
at(index: number): E | undefined;
|
|
174
|
+
/**
|
|
175
|
+
* Delete the element at a given index.
|
|
176
|
+
* @remarks Time O(N), Space O(1)
|
|
177
|
+
* @param index - Zero-based index from the front.
|
|
178
|
+
* @returns Removed element or undefined.
|
|
179
|
+
*/
|
|
180
|
+
deleteAt(index: number): E | undefined;
|
|
181
|
+
/**
|
|
182
|
+
* Insert a new element at a given index.
|
|
183
|
+
* @remarks Time O(N), Space O(1)
|
|
184
|
+
* @param index - Zero-based index from the front.
|
|
185
|
+
* @param newElement - Element to insert.
|
|
186
|
+
* @returns True if inserted.
|
|
187
|
+
*/
|
|
188
|
+
addAt(index: number, newElement: E): boolean;
|
|
189
|
+
/**
|
|
190
|
+
* Replace the element at a given index.
|
|
191
|
+
* @remarks Time O(1), Space O(1)
|
|
192
|
+
* @param index - Zero-based index from the front.
|
|
193
|
+
* @param newElement - New element to set.
|
|
194
|
+
* @returns True if updated.
|
|
195
|
+
*/
|
|
196
|
+
setAt(index: number, newElement: E): boolean;
|
|
197
|
+
/**
|
|
198
|
+
* Reverse the queue in-place by compacting then reversing.
|
|
199
|
+
* @remarks Time O(N), Space O(N)
|
|
200
|
+
* @returns This queue.
|
|
201
|
+
*/
|
|
202
|
+
reverse(): this;
|
|
203
|
+
/**
|
|
204
|
+
* Remove all elements and reset offset.
|
|
205
|
+
* @remarks Time O(1), Space O(1)
|
|
206
|
+
* @returns void
|
|
207
|
+
*/
|
|
208
|
+
clear(): void;
|
|
209
|
+
/**
|
|
210
|
+
* Compact storage by discarding consumed head elements.
|
|
211
|
+
* @remarks Time O(N), Space O(N)
|
|
212
|
+
* @returns True when compaction performed.
|
|
213
|
+
*/
|
|
214
|
+
compact(): boolean;
|
|
215
|
+
/**
|
|
216
|
+
* Remove and/or insert elements at a position (array-like).
|
|
217
|
+
* @remarks Time O(N + M), Space O(M)
|
|
218
|
+
* @param start - Start index (clamped to [0, length]).
|
|
219
|
+
* @param [deleteCount] - Number of elements to remove (default 0).
|
|
220
|
+
* @param [items] - Elements to insert after `start`.
|
|
221
|
+
* @returns A new queue containing the removed elements (typed as `this`).
|
|
222
|
+
*/
|
|
223
|
+
splice(start: number, deleteCount?: number, ...items: E[]): this;
|
|
224
|
+
/**
|
|
225
|
+
* Deep clone this queue and its parameters.
|
|
226
|
+
* @remarks Time O(N), Space O(N)
|
|
227
|
+
* @returns A new queue with the same content and options.
|
|
228
|
+
*/
|
|
229
|
+
clone(): this;
|
|
230
|
+
/**
|
|
231
|
+
* Filter elements into a new queue of the same class.
|
|
232
|
+
* @remarks Time O(N), Space O(N)
|
|
233
|
+
* @param predicate - Predicate (element, index, queue) → boolean to keep element.
|
|
234
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
235
|
+
* @returns A new queue with kept elements.
|
|
236
|
+
*/
|
|
237
|
+
filter(predicate: ElementCallback<E, R, boolean>, thisArg?: unknown): this;
|
|
238
|
+
/**
|
|
239
|
+
* Map each element to a new element in a possibly different-typed queue.
|
|
240
|
+
* @remarks Time O(N), Space O(N)
|
|
241
|
+
* @template EM
|
|
242
|
+
* @template RM
|
|
243
|
+
* @param callback - Mapping function (element, index, queue) → newElement.
|
|
244
|
+
* @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
|
|
245
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
246
|
+
* @returns A new Queue with mapped elements.
|
|
247
|
+
*/
|
|
248
|
+
map<EM, RM>(callback: ElementCallback<E, R, EM>, options?: QueueOptions<EM, RM>, thisArg?: unknown): Queue<EM, RM>;
|
|
249
|
+
/**
|
|
250
|
+
* Map each element to a new value of the same type.
|
|
251
|
+
* @remarks Time O(N), Space O(N)
|
|
252
|
+
* @param callback - Mapping function (element, index, queue) → element.
|
|
253
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
254
|
+
* @returns A new queue with mapped elements (same element type).
|
|
255
|
+
*/
|
|
256
|
+
mapSame(callback: ElementCallback<E, R, E>, thisArg?: unknown): this;
|
|
257
|
+
/**
|
|
258
|
+
* (Protected) Set the internal auto-compaction ratio.
|
|
259
|
+
* @remarks Time O(1), Space O(1)
|
|
260
|
+
* @param value - New ratio to assign.
|
|
261
|
+
* @returns void
|
|
262
|
+
*/
|
|
263
|
+
protected _setAutoCompactRatio(value: number): void;
|
|
264
|
+
/**
|
|
265
|
+
* (Protected) Iterate elements from front to back.
|
|
266
|
+
* @remarks Time O(N), Space O(1)
|
|
267
|
+
* @returns Iterator of E.
|
|
268
|
+
*/
|
|
269
|
+
protected _getIterator(): IterableIterator<E>;
|
|
270
|
+
/**
|
|
271
|
+
* (Protected) Iterate elements from back to front.
|
|
272
|
+
* @remarks Time O(N), Space O(1)
|
|
273
|
+
* @returns Iterator of E.
|
|
274
|
+
*/
|
|
275
|
+
protected _getReverseIterator(): IterableIterator<E>;
|
|
276
|
+
/**
|
|
277
|
+
* (Protected) Create an empty instance of the same concrete class.
|
|
278
|
+
* @remarks Time O(1), Space O(1)
|
|
279
|
+
* @param [options] - Options forwarded to the constructor.
|
|
280
|
+
* @returns An empty like-kind queue instance.
|
|
281
|
+
*/
|
|
282
|
+
protected _createInstance(options?: LinearBaseOptions<E, R>): this;
|
|
283
|
+
/**
|
|
284
|
+
* (Protected) Create a like-kind queue and seed it from an iterable.
|
|
285
|
+
* @remarks Time O(N), Space O(N)
|
|
286
|
+
* @template EM
|
|
287
|
+
* @template RM
|
|
288
|
+
* @param [elements] - Iterable used to seed the new queue.
|
|
289
|
+
* @param [options] - Options forwarded to the constructor.
|
|
290
|
+
* @returns A like-kind Queue instance.
|
|
291
|
+
*/
|
|
292
|
+
protected _createLike<EM = E, RM = R>(elements?: Iterable<EM> | Iterable<RM>, options?: QueueOptions<EM, RM>): Queue<EM, RM>;
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Queue implemented over a singly linked list; preserves head/tail operations with linear scans for queries.
|
|
296
|
+
* @remarks Time O(1), Space O(1)
|
|
297
|
+
* @template E
|
|
298
|
+
* @template R
|
|
299
|
+
* @example examples will be generated by unit test
|
|
300
|
+
*/
|
|
301
|
+
export declare class LinkedListQueue<E = any, R = any> extends SinglyLinkedList<E, R> {
|
|
302
|
+
/**
|
|
303
|
+
* Deep clone this linked-list-based queue.
|
|
304
|
+
* @remarks Time O(N), Space O(N)
|
|
305
|
+
* @returns A new queue with the same sequence of elements.
|
|
306
|
+
*/
|
|
307
|
+
clone(): this;
|
|
308
|
+
}
|
|
@@ -1,2 +1,306 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
import type { ElementCallback, IterableElementBaseOptions, StackOptions } from '../../types';
|
|
9
|
+
import { IterableElementBase } from '../base';
|
|
10
|
+
/**
|
|
11
|
+
* LIFO stack with array storage and optional record→element conversion.
|
|
12
|
+
* @remarks Time O(1), Space O(1)
|
|
13
|
+
* @template E
|
|
14
|
+
* @template R
|
|
15
|
+
* 1. Last In, First Out (LIFO): The core characteristic of a stack is its last in, first out nature, meaning the last element added to the stack will be the first to be removed.
|
|
16
|
+
* 2. Uses: Stacks are commonly used for managing a series of tasks or elements that need to be processed in a last in, first out manner. They are widely used in various scenarios, such as in function calls in programming languages, evaluation of arithmetic expressions, and backtracking algorithms.
|
|
17
|
+
* 3. Performance: Stack operations are typically O(1) in time complexity, meaning that regardless of the stack's size, adding, removing, and viewing the top element are very fast operations.
|
|
18
|
+
* 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.
|
|
19
|
+
* 5. Expression Evaluation: Used for the evaluation of arithmetic or logical expressions, especially when dealing with parenthesis matching and operator precedence.
|
|
20
|
+
* 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.
|
|
21
|
+
* @example
|
|
22
|
+
* // Balanced Parentheses or Brackets
|
|
23
|
+
* type ValidCharacters = ')' | '(' | ']' | '[' | '}' | '{';
|
|
24
|
+
*
|
|
25
|
+
* const stack = new Stack<string>();
|
|
26
|
+
* const input: ValidCharacters[] = '[({})]'.split('') as ValidCharacters[];
|
|
27
|
+
* const matches: { [key in ValidCharacters]?: ValidCharacters } = { ')': '(', ']': '[', '}': '{' };
|
|
28
|
+
* for (const char of input) {
|
|
29
|
+
* if ('([{'.includes(char)) {
|
|
30
|
+
* stack.push(char);
|
|
31
|
+
* } else if (')]}'.includes(char)) {
|
|
32
|
+
* if (stack.pop() !== matches[char]) {
|
|
33
|
+
* fail('Parentheses are not balanced');
|
|
34
|
+
* }
|
|
35
|
+
* }
|
|
36
|
+
* }
|
|
37
|
+
* console.log(stack.isEmpty()); // true
|
|
38
|
+
* @example
|
|
39
|
+
* // Expression Evaluation and Conversion
|
|
40
|
+
* const stack = new Stack<number>();
|
|
41
|
+
* const expression = [5, 3, '+']; // Equivalent to 5 + 3
|
|
42
|
+
* expression.forEach(token => {
|
|
43
|
+
* if (typeof token === 'number') {
|
|
44
|
+
* stack.push(token);
|
|
45
|
+
* } else {
|
|
46
|
+
* const b = stack.pop()!;
|
|
47
|
+
* const a = stack.pop()!;
|
|
48
|
+
* stack.push(token === '+' ? a + b : 0); // Only handling '+' here
|
|
49
|
+
* }
|
|
50
|
+
* });
|
|
51
|
+
* console.log(stack.pop()); // 8
|
|
52
|
+
* @example
|
|
53
|
+
* // Depth-First Search (DFS)
|
|
54
|
+
* const stack = new Stack<number>();
|
|
55
|
+
* const graph: { [key in number]: number[] } = { 1: [2, 3], 2: [4], 3: [5], 4: [], 5: [] };
|
|
56
|
+
* const visited: number[] = [];
|
|
57
|
+
* stack.push(1);
|
|
58
|
+
* while (!stack.isEmpty()) {
|
|
59
|
+
* const node = stack.pop()!;
|
|
60
|
+
* if (!visited.includes(node)) {
|
|
61
|
+
* visited.push(node);
|
|
62
|
+
* graph[node].forEach(neighbor => stack.push(neighbor));
|
|
63
|
+
* }
|
|
64
|
+
* }
|
|
65
|
+
* console.log(visited); // [1, 3, 5, 2, 4]
|
|
66
|
+
* @example
|
|
67
|
+
* // Backtracking Algorithms
|
|
68
|
+
* const stack = new Stack<[number, number]>();
|
|
69
|
+
* const maze = [
|
|
70
|
+
* ['S', ' ', 'X'],
|
|
71
|
+
* ['X', ' ', 'X'],
|
|
72
|
+
* [' ', ' ', 'E']
|
|
73
|
+
* ];
|
|
74
|
+
* const start: [number, number] = [0, 0];
|
|
75
|
+
* const end = [2, 2];
|
|
76
|
+
* const directions = [
|
|
77
|
+
* [0, 1], // To the right
|
|
78
|
+
* [1, 0], // down
|
|
79
|
+
* [0, -1], // left
|
|
80
|
+
* [-1, 0] // up
|
|
81
|
+
* ];
|
|
82
|
+
*
|
|
83
|
+
* const visited = new Set<string>(); // Used to record visited nodes
|
|
84
|
+
* stack.push(start);
|
|
85
|
+
* const path: number[][] = [];
|
|
86
|
+
*
|
|
87
|
+
* while (!stack.isEmpty()) {
|
|
88
|
+
* const [x, y] = stack.pop()!;
|
|
89
|
+
* if (visited.has(`${x},${y}`)) continue; // Skip already visited nodes
|
|
90
|
+
* visited.add(`${x},${y}`);
|
|
91
|
+
*
|
|
92
|
+
* path.push([x, y]);
|
|
93
|
+
*
|
|
94
|
+
* if (x === end[0] && y === end[1]) {
|
|
95
|
+
* break; // Find the end point and exit
|
|
96
|
+
* }
|
|
97
|
+
*
|
|
98
|
+
* for (const [dx, dy] of directions) {
|
|
99
|
+
* const nx = x + dx;
|
|
100
|
+
* const ny = y + dy;
|
|
101
|
+
* if (
|
|
102
|
+
* maze[nx]?.[ny] === ' ' || // feasible path
|
|
103
|
+
* maze[nx]?.[ny] === 'E' // destination
|
|
104
|
+
* ) {
|
|
105
|
+
* stack.push([nx, ny]);
|
|
106
|
+
* }
|
|
107
|
+
* }
|
|
108
|
+
* }
|
|
109
|
+
*
|
|
110
|
+
* expect(path).toContainEqual(end);
|
|
111
|
+
* @example
|
|
112
|
+
* // Function Call Stack
|
|
113
|
+
* const functionStack = new Stack<string>();
|
|
114
|
+
* functionStack.push('main');
|
|
115
|
+
* functionStack.push('foo');
|
|
116
|
+
* functionStack.push('bar');
|
|
117
|
+
* console.log(functionStack.pop()); // 'bar'
|
|
118
|
+
* console.log(functionStack.pop()); // 'foo'
|
|
119
|
+
* console.log(functionStack.pop()); // 'main'
|
|
120
|
+
* @example
|
|
121
|
+
* // Simplify File Paths
|
|
122
|
+
* const stack = new Stack<string>();
|
|
123
|
+
* const path = '/a/./b/../../c';
|
|
124
|
+
* path.split('/').forEach(segment => {
|
|
125
|
+
* if (segment === '..') stack.pop();
|
|
126
|
+
* else if (segment && segment !== '.') stack.push(segment);
|
|
127
|
+
* });
|
|
128
|
+
* console.log(stack.elements.join('/')); // 'c'
|
|
129
|
+
* @example
|
|
130
|
+
* // Stock Span Problem
|
|
131
|
+
* const stack = new Stack<number>();
|
|
132
|
+
* const prices = [100, 80, 60, 70, 60, 75, 85];
|
|
133
|
+
* const spans: number[] = [];
|
|
134
|
+
* prices.forEach((price, i) => {
|
|
135
|
+
* while (!stack.isEmpty() && prices[stack.peek()!] <= price) {
|
|
136
|
+
* stack.pop();
|
|
137
|
+
* }
|
|
138
|
+
* spans.push(stack.isEmpty() ? i + 1 : i - stack.peek()!);
|
|
139
|
+
* stack.push(i);
|
|
140
|
+
* });
|
|
141
|
+
* console.log(spans); // [1, 1, 1, 2, 1, 4, 6]
|
|
142
|
+
*/
|
|
143
|
+
export declare class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
144
|
+
protected _equals: (a: E, b: E) => boolean;
|
|
145
|
+
/**
|
|
146
|
+
* Create a Stack and optionally bulk-push elements.
|
|
147
|
+
* @remarks Time O(N), Space O(N)
|
|
148
|
+
* @param [elements] - Iterable of elements (or raw records if toElementFn is set).
|
|
149
|
+
* @param [options] - Options such as toElementFn and equality function.
|
|
150
|
+
* @returns New Stack instance.
|
|
151
|
+
*/
|
|
152
|
+
constructor(elements?: Iterable<E> | Iterable<R>, options?: StackOptions<E, R>);
|
|
153
|
+
protected _elements: E[];
|
|
154
|
+
/**
|
|
155
|
+
* Get the backing array of elements.
|
|
156
|
+
* @remarks Time O(1), Space O(1)
|
|
157
|
+
* @returns Internal elements array.
|
|
158
|
+
*/
|
|
159
|
+
get elements(): E[];
|
|
160
|
+
/**
|
|
161
|
+
* Get the number of stored elements.
|
|
162
|
+
* @remarks Time O(1), Space O(1)
|
|
163
|
+
* @returns Current size.
|
|
164
|
+
*/
|
|
165
|
+
get size(): number;
|
|
166
|
+
/**
|
|
167
|
+
* Create a stack from an array of elements.
|
|
168
|
+
* @remarks Time O(N), Space O(N)
|
|
169
|
+
* @template E
|
|
170
|
+
* @template R
|
|
171
|
+
* @param this - The constructor (subclass) to instantiate.
|
|
172
|
+
* @param elements - Array of elements to push in order.
|
|
173
|
+
* @param [options] - Options forwarded to the constructor.
|
|
174
|
+
* @returns A new Stack populated from the array.
|
|
175
|
+
*/
|
|
176
|
+
static fromArray<E, R = any>(this: new (elements?: Iterable<E> | Iterable<R>, options?: StackOptions<E, R>) => any, elements: E[], options?: StackOptions<E, R>): any;
|
|
177
|
+
/**
|
|
178
|
+
* Check whether the stack is empty.
|
|
179
|
+
* @remarks Time O(1), Space O(1)
|
|
180
|
+
* @returns True if size is 0.
|
|
181
|
+
*/
|
|
182
|
+
isEmpty(): boolean;
|
|
183
|
+
/**
|
|
184
|
+
* Get the top element without removing it.
|
|
185
|
+
* @remarks Time O(1), Space O(1)
|
|
186
|
+
* @returns Top element or undefined.
|
|
187
|
+
*/
|
|
188
|
+
peek(): E | undefined;
|
|
189
|
+
/**
|
|
190
|
+
* Push one element onto the top.
|
|
191
|
+
* @remarks Time O(1), Space O(1)
|
|
192
|
+
* @param element - Element to push.
|
|
193
|
+
* @returns True when pushed.
|
|
194
|
+
*/
|
|
195
|
+
push(element: E): boolean;
|
|
196
|
+
/**
|
|
197
|
+
* Pop and return the top element.
|
|
198
|
+
* @remarks Time O(1), Space O(1)
|
|
199
|
+
* @returns Removed element or undefined.
|
|
200
|
+
*/
|
|
201
|
+
pop(): E | undefined;
|
|
202
|
+
/**
|
|
203
|
+
* Push many elements from an iterable.
|
|
204
|
+
* @remarks Time O(N), Space O(1)
|
|
205
|
+
* @param elements - Iterable of elements (or raw records if toElementFn is set).
|
|
206
|
+
* @returns Array of per-element success flags.
|
|
207
|
+
*/
|
|
208
|
+
pushMany(elements: Iterable<E> | Iterable<R>): boolean[];
|
|
209
|
+
/**
|
|
210
|
+
* Delete the first occurrence of a specific element.
|
|
211
|
+
* @remarks Time O(N), Space O(1)
|
|
212
|
+
* @param element - Element to remove (using the configured equality).
|
|
213
|
+
* @returns True if an element was removed.
|
|
214
|
+
*/
|
|
215
|
+
delete(element: E): boolean;
|
|
216
|
+
/**
|
|
217
|
+
* Delete the element at an index.
|
|
218
|
+
* @remarks Time O(N), Space O(1)
|
|
219
|
+
* @param index - Zero-based index from the bottom.
|
|
220
|
+
* @returns True if removed.
|
|
221
|
+
*/
|
|
222
|
+
deleteAt(index: number): boolean;
|
|
223
|
+
/**
|
|
224
|
+
* Delete the first element that satisfies a predicate.
|
|
225
|
+
* @remarks Time O(N), Space O(1)
|
|
226
|
+
* @param predicate - Function (value, index, stack) → boolean to decide deletion.
|
|
227
|
+
* @returns True if a match was removed.
|
|
228
|
+
*/
|
|
229
|
+
deleteWhere(predicate: (value: E, index: number, stack: this) => boolean): boolean;
|
|
230
|
+
/**
|
|
231
|
+
* Remove all elements and reset storage.
|
|
232
|
+
* @remarks Time O(1), Space O(1)
|
|
233
|
+
* @returns void
|
|
234
|
+
*/
|
|
235
|
+
clear(): void;
|
|
236
|
+
/**
|
|
237
|
+
* Deep clone this stack.
|
|
238
|
+
* @remarks Time O(N), Space O(N)
|
|
239
|
+
* @returns A new stack with the same content.
|
|
240
|
+
*/
|
|
241
|
+
clone(): this;
|
|
242
|
+
/**
|
|
243
|
+
* Filter elements into a new stack of the same class.
|
|
244
|
+
* @remarks Time O(N), Space O(N)
|
|
245
|
+
* @param predicate - Predicate (value, index, stack) → boolean to keep value.
|
|
246
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
247
|
+
* @returns A new stack with kept values.
|
|
248
|
+
*/
|
|
249
|
+
filter(predicate: ElementCallback<E, R, boolean>, thisArg?: unknown): this;
|
|
250
|
+
/**
|
|
251
|
+
* Map values into a new stack of the same element type.
|
|
252
|
+
* @remarks Time O(N), Space O(N)
|
|
253
|
+
* @param callback - Mapping function (value, index, stack) → newValue.
|
|
254
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
255
|
+
* @returns A new stack with mapped values.
|
|
256
|
+
*/
|
|
257
|
+
mapSame(callback: ElementCallback<E, R, E>, thisArg?: unknown): this;
|
|
258
|
+
/**
|
|
259
|
+
* Map values into a new stack (possibly different element type).
|
|
260
|
+
* @remarks Time O(N), Space O(N)
|
|
261
|
+
* @template EM
|
|
262
|
+
* @template RM
|
|
263
|
+
* @param callback - Mapping function (value, index, stack) → newElement.
|
|
264
|
+
* @param [options] - Options for the output stack (e.g., toElementFn).
|
|
265
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
266
|
+
* @returns A new Stack with mapped elements.
|
|
267
|
+
*/
|
|
268
|
+
map<EM, RM>(callback: ElementCallback<E, R, EM>, options?: IterableElementBaseOptions<EM, RM>, thisArg?: unknown): Stack<EM, RM>;
|
|
269
|
+
/**
|
|
270
|
+
* Set the equality comparator used by delete/search operations.
|
|
271
|
+
* @remarks Time O(1), Space O(1)
|
|
272
|
+
* @param equals - Equality predicate (a, b) → boolean.
|
|
273
|
+
* @returns This stack.
|
|
274
|
+
*/
|
|
275
|
+
setEquality(equals: (a: E, b: E) => boolean): this;
|
|
276
|
+
/**
|
|
277
|
+
* (Protected) Find the index of a target element using the equality function.
|
|
278
|
+
* @remarks Time O(N), Space O(1)
|
|
279
|
+
* @param target - Element to search for.
|
|
280
|
+
* @returns Index or -1 if not found.
|
|
281
|
+
*/
|
|
282
|
+
protected _indexOfByEquals(target: E): number;
|
|
283
|
+
/**
|
|
284
|
+
* (Protected) Create an empty instance of the same concrete class.
|
|
285
|
+
* @remarks Time O(1), Space O(1)
|
|
286
|
+
* @param [options] - Options forwarded to the constructor.
|
|
287
|
+
* @returns An empty like-kind stack instance.
|
|
288
|
+
*/
|
|
289
|
+
protected _createInstance(options?: StackOptions<E, R>): this;
|
|
290
|
+
/**
|
|
291
|
+
* (Protected) Create a like-kind stack and seed it from an iterable.
|
|
292
|
+
* @remarks Time O(N), Space O(N)
|
|
293
|
+
* @template T
|
|
294
|
+
* @template RR
|
|
295
|
+
* @param [elements] - Iterable used to seed the new stack.
|
|
296
|
+
* @param [options] - Options forwarded to the constructor.
|
|
297
|
+
* @returns A like-kind Stack instance.
|
|
298
|
+
*/
|
|
299
|
+
protected _createLike<T = E, RR = R>(elements?: Iterable<T> | Iterable<RR>, options?: StackOptions<T, RR>): Stack<T, RR>;
|
|
300
|
+
/**
|
|
301
|
+
* (Protected) Iterate elements from bottom to top.
|
|
302
|
+
* @remarks Time O(N), Space O(1)
|
|
303
|
+
* @returns Iterator of elements.
|
|
304
|
+
*/
|
|
305
|
+
protected _getIterator(): IterableIterator<E>;
|
|
306
|
+
}
|
|
@@ -1 +1,62 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export declare class TreeNode<V = any> {
|
|
2
|
+
/**
|
|
3
|
+
* The constructor function initializes a TreeNode object with a key, optional value, and optional
|
|
4
|
+
* children.
|
|
5
|
+
* @param {string} key - A string representing the key of the tree node.
|
|
6
|
+
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the
|
|
7
|
+
* value associated with the node. If no value is provided, it defaults to `undefined`.
|
|
8
|
+
* @param {TreeNode<V>[]} [children] - The `children` parameter is an optional array of `TreeNode<V>`
|
|
9
|
+
* objects. It represents the child nodes of the current node. If no children are provided, the
|
|
10
|
+
* default value is an empty array.
|
|
11
|
+
*/
|
|
12
|
+
constructor(key: string, value?: V, children?: TreeNode<V>[]);
|
|
13
|
+
protected _key: string;
|
|
14
|
+
/**
|
|
15
|
+
* The function returns the value of the protected variable _key.
|
|
16
|
+
* @returns The value of the `_key` property, which is a string.
|
|
17
|
+
*/
|
|
18
|
+
get key(): string;
|
|
19
|
+
/**
|
|
20
|
+
* The above function sets the value of a protected variable called "key".
|
|
21
|
+
* @param {string} value - The value parameter is a string that represents the value to be assigned
|
|
22
|
+
* to the key.
|
|
23
|
+
*/
|
|
24
|
+
set key(value: string);
|
|
25
|
+
protected _value?: V | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* The function returns the value stored in a variable, or undefined if the variable is empty.
|
|
28
|
+
* @returns The value of the variable `_value` is being returned.
|
|
29
|
+
*/
|
|
30
|
+
get value(): V | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* The function sets the value of a variable.
|
|
33
|
+
* @param {V | undefined} value - The parameter "value" is of type "V | undefined", which means it
|
|
34
|
+
* can accept a value of type "V" or it can be undefined.
|
|
35
|
+
*/
|
|
36
|
+
set value(value: V | undefined);
|
|
37
|
+
protected _children?: TreeNode<V>[] | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* The function returns an array of TreeNode objects or undefined.
|
|
40
|
+
* @returns The `children` property is being returned. It is of type `TreeNode<V>[] | undefined`,
|
|
41
|
+
* which means it can either be an array of `TreeNode<V>` objects or `undefined`.
|
|
42
|
+
*/
|
|
43
|
+
get children(): TreeNode<V>[] | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* The function sets the value of the children property of a TreeNode object.
|
|
46
|
+
* @param {TreeNode<V>[] | undefined} value - The value parameter is of type TreeNode<V>[] |
|
|
47
|
+
* undefined. This means that it can accept an array of TreeNode objects or undefined.
|
|
48
|
+
*/
|
|
49
|
+
set children(value: TreeNode<V>[] | undefined);
|
|
50
|
+
/**
|
|
51
|
+
* The function `addChildren` adds one or more child nodes to the current node.
|
|
52
|
+
* @param {TreeNode<V> | TreeNode<V>[]} children - The `children` parameter can be either a single
|
|
53
|
+
* `TreeNode<V>` object or an array of `TreeNode<V>` objects.
|
|
54
|
+
*/
|
|
55
|
+
addChildren(children: TreeNode<V> | TreeNode<V>[]): void;
|
|
56
|
+
/**
|
|
57
|
+
* The function `getHeight()` calculates the maximum depth of a tree structure by performing a
|
|
58
|
+
* breadth-first search.
|
|
59
|
+
* @returns the maximum depth or height of the tree.
|
|
60
|
+
*/
|
|
61
|
+
getHeight(): number;
|
|
62
|
+
}
|