stack-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 +552 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/esm/index.mjs +548 -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 +11 -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/stack-typed.js +563 -0
- package/dist/umd/stack-typed.js.map +1 -0
- package/dist/umd/stack-typed.min.js +9 -0
- package/dist/umd/stack-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 -12
- package/dist/index.js +0 -28
- 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,401 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* data-structure-typed
|
|
4
|
-
*
|
|
5
|
-
* @author Pablo Zeng
|
|
6
|
-
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
7
|
-
* @license MIT License
|
|
8
|
-
*/
|
|
9
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
exports.Stack = void 0;
|
|
11
|
-
const base_1 = require("../base");
|
|
12
|
-
/**
|
|
13
|
-
* LIFO stack with array storage and optional record→element conversion.
|
|
14
|
-
* @remarks Time O(1), Space O(1)
|
|
15
|
-
* @template E
|
|
16
|
-
* @template R
|
|
17
|
-
* 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.
|
|
18
|
-
* 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.
|
|
19
|
-
* 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.
|
|
20
|
-
* 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.
|
|
21
|
-
* 5. Expression Evaluation: Used for the evaluation of arithmetic or logical expressions, especially when dealing with parenthesis matching and operator precedence.
|
|
22
|
-
* 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.
|
|
23
|
-
* @example
|
|
24
|
-
* // Balanced Parentheses or Brackets
|
|
25
|
-
* type ValidCharacters = ')' | '(' | ']' | '[' | '}' | '{';
|
|
26
|
-
*
|
|
27
|
-
* const stack = new Stack<string>();
|
|
28
|
-
* const input: ValidCharacters[] = '[({})]'.split('') as ValidCharacters[];
|
|
29
|
-
* const matches: { [key in ValidCharacters]?: ValidCharacters } = { ')': '(', ']': '[', '}': '{' };
|
|
30
|
-
* for (const char of input) {
|
|
31
|
-
* if ('([{'.includes(char)) {
|
|
32
|
-
* stack.push(char);
|
|
33
|
-
* } else if (')]}'.includes(char)) {
|
|
34
|
-
* if (stack.pop() !== matches[char]) {
|
|
35
|
-
* fail('Parentheses are not balanced');
|
|
36
|
-
* }
|
|
37
|
-
* }
|
|
38
|
-
* }
|
|
39
|
-
* console.log(stack.isEmpty()); // true
|
|
40
|
-
* @example
|
|
41
|
-
* // Expression Evaluation and Conversion
|
|
42
|
-
* const stack = new Stack<number>();
|
|
43
|
-
* const expression = [5, 3, '+']; // Equivalent to 5 + 3
|
|
44
|
-
* expression.forEach(token => {
|
|
45
|
-
* if (typeof token === 'number') {
|
|
46
|
-
* stack.push(token);
|
|
47
|
-
* } else {
|
|
48
|
-
* const b = stack.pop()!;
|
|
49
|
-
* const a = stack.pop()!;
|
|
50
|
-
* stack.push(token === '+' ? a + b : 0); // Only handling '+' here
|
|
51
|
-
* }
|
|
52
|
-
* });
|
|
53
|
-
* console.log(stack.pop()); // 8
|
|
54
|
-
* @example
|
|
55
|
-
* // Depth-First Search (DFS)
|
|
56
|
-
* const stack = new Stack<number>();
|
|
57
|
-
* const graph: { [key in number]: number[] } = { 1: [2, 3], 2: [4], 3: [5], 4: [], 5: [] };
|
|
58
|
-
* const visited: number[] = [];
|
|
59
|
-
* stack.push(1);
|
|
60
|
-
* while (!stack.isEmpty()) {
|
|
61
|
-
* const node = stack.pop()!;
|
|
62
|
-
* if (!visited.includes(node)) {
|
|
63
|
-
* visited.push(node);
|
|
64
|
-
* graph[node].forEach(neighbor => stack.push(neighbor));
|
|
65
|
-
* }
|
|
66
|
-
* }
|
|
67
|
-
* console.log(visited); // [1, 3, 5, 2, 4]
|
|
68
|
-
* @example
|
|
69
|
-
* // Backtracking Algorithms
|
|
70
|
-
* const stack = new Stack<[number, number]>();
|
|
71
|
-
* const maze = [
|
|
72
|
-
* ['S', ' ', 'X'],
|
|
73
|
-
* ['X', ' ', 'X'],
|
|
74
|
-
* [' ', ' ', 'E']
|
|
75
|
-
* ];
|
|
76
|
-
* const start: [number, number] = [0, 0];
|
|
77
|
-
* const end = [2, 2];
|
|
78
|
-
* const directions = [
|
|
79
|
-
* [0, 1], // To the right
|
|
80
|
-
* [1, 0], // down
|
|
81
|
-
* [0, -1], // left
|
|
82
|
-
* [-1, 0] // up
|
|
83
|
-
* ];
|
|
84
|
-
*
|
|
85
|
-
* const visited = new Set<string>(); // Used to record visited nodes
|
|
86
|
-
* stack.push(start);
|
|
87
|
-
* const path: number[][] = [];
|
|
88
|
-
*
|
|
89
|
-
* while (!stack.isEmpty()) {
|
|
90
|
-
* const [x, y] = stack.pop()!;
|
|
91
|
-
* if (visited.has(`${x},${y}`)) continue; // Skip already visited nodes
|
|
92
|
-
* visited.add(`${x},${y}`);
|
|
93
|
-
*
|
|
94
|
-
* path.push([x, y]);
|
|
95
|
-
*
|
|
96
|
-
* if (x === end[0] && y === end[1]) {
|
|
97
|
-
* break; // Find the end point and exit
|
|
98
|
-
* }
|
|
99
|
-
*
|
|
100
|
-
* for (const [dx, dy] of directions) {
|
|
101
|
-
* const nx = x + dx;
|
|
102
|
-
* const ny = y + dy;
|
|
103
|
-
* if (
|
|
104
|
-
* maze[nx]?.[ny] === ' ' || // feasible path
|
|
105
|
-
* maze[nx]?.[ny] === 'E' // destination
|
|
106
|
-
* ) {
|
|
107
|
-
* stack.push([nx, ny]);
|
|
108
|
-
* }
|
|
109
|
-
* }
|
|
110
|
-
* }
|
|
111
|
-
*
|
|
112
|
-
* expect(path).toContainEqual(end);
|
|
113
|
-
* @example
|
|
114
|
-
* // Function Call Stack
|
|
115
|
-
* const functionStack = new Stack<string>();
|
|
116
|
-
* functionStack.push('main');
|
|
117
|
-
* functionStack.push('foo');
|
|
118
|
-
* functionStack.push('bar');
|
|
119
|
-
* console.log(functionStack.pop()); // 'bar'
|
|
120
|
-
* console.log(functionStack.pop()); // 'foo'
|
|
121
|
-
* console.log(functionStack.pop()); // 'main'
|
|
122
|
-
* @example
|
|
123
|
-
* // Simplify File Paths
|
|
124
|
-
* const stack = new Stack<string>();
|
|
125
|
-
* const path = '/a/./b/../../c';
|
|
126
|
-
* path.split('/').forEach(segment => {
|
|
127
|
-
* if (segment === '..') stack.pop();
|
|
128
|
-
* else if (segment && segment !== '.') stack.push(segment);
|
|
129
|
-
* });
|
|
130
|
-
* console.log(stack.elements.join('/')); // 'c'
|
|
131
|
-
* @example
|
|
132
|
-
* // Stock Span Problem
|
|
133
|
-
* const stack = new Stack<number>();
|
|
134
|
-
* const prices = [100, 80, 60, 70, 60, 75, 85];
|
|
135
|
-
* const spans: number[] = [];
|
|
136
|
-
* prices.forEach((price, i) => {
|
|
137
|
-
* while (!stack.isEmpty() && prices[stack.peek()!] <= price) {
|
|
138
|
-
* stack.pop();
|
|
139
|
-
* }
|
|
140
|
-
* spans.push(stack.isEmpty() ? i + 1 : i - stack.peek()!);
|
|
141
|
-
* stack.push(i);
|
|
142
|
-
* });
|
|
143
|
-
* console.log(spans); // [1, 1, 1, 2, 1, 4, 6]
|
|
144
|
-
*/
|
|
145
|
-
class Stack extends base_1.IterableElementBase {
|
|
146
|
-
/**
|
|
147
|
-
* Create a Stack and optionally bulk-push elements.
|
|
148
|
-
* @remarks Time O(N), Space O(N)
|
|
149
|
-
* @param [elements] - Iterable of elements (or raw records if toElementFn is set).
|
|
150
|
-
* @param [options] - Options such as toElementFn and equality function.
|
|
151
|
-
* @returns New Stack instance.
|
|
152
|
-
*/
|
|
153
|
-
constructor(elements = [], options) {
|
|
154
|
-
super(options);
|
|
155
|
-
this._equals = Object.is;
|
|
156
|
-
this._elements = [];
|
|
157
|
-
this.pushMany(elements);
|
|
158
|
-
}
|
|
159
|
-
/**
|
|
160
|
-
* Get the backing array of elements.
|
|
161
|
-
* @remarks Time O(1), Space O(1)
|
|
162
|
-
* @returns Internal elements array.
|
|
163
|
-
*/
|
|
164
|
-
get elements() {
|
|
165
|
-
return this._elements;
|
|
166
|
-
}
|
|
167
|
-
/**
|
|
168
|
-
* Get the number of stored elements.
|
|
169
|
-
* @remarks Time O(1), Space O(1)
|
|
170
|
-
* @returns Current size.
|
|
171
|
-
*/
|
|
172
|
-
get size() {
|
|
173
|
-
return this.elements.length;
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Create a stack from an array of elements.
|
|
177
|
-
* @remarks Time O(N), Space O(N)
|
|
178
|
-
* @template E
|
|
179
|
-
* @template R
|
|
180
|
-
* @param this - The constructor (subclass) to instantiate.
|
|
181
|
-
* @param elements - Array of elements to push in order.
|
|
182
|
-
* @param [options] - Options forwarded to the constructor.
|
|
183
|
-
* @returns A new Stack populated from the array.
|
|
184
|
-
*/
|
|
185
|
-
static fromArray(elements, options) {
|
|
186
|
-
return new this(elements, options);
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* Check whether the stack is empty.
|
|
190
|
-
* @remarks Time O(1), Space O(1)
|
|
191
|
-
* @returns True if size is 0.
|
|
192
|
-
*/
|
|
193
|
-
isEmpty() {
|
|
194
|
-
return this.elements.length === 0;
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Get the top element without removing it.
|
|
198
|
-
* @remarks Time O(1), Space O(1)
|
|
199
|
-
* @returns Top element or undefined.
|
|
200
|
-
*/
|
|
201
|
-
peek() {
|
|
202
|
-
return this.isEmpty() ? undefined : this.elements[this.elements.length - 1];
|
|
203
|
-
}
|
|
204
|
-
/**
|
|
205
|
-
* Push one element onto the top.
|
|
206
|
-
* @remarks Time O(1), Space O(1)
|
|
207
|
-
* @param element - Element to push.
|
|
208
|
-
* @returns True when pushed.
|
|
209
|
-
*/
|
|
210
|
-
push(element) {
|
|
211
|
-
this.elements.push(element);
|
|
212
|
-
return true;
|
|
213
|
-
}
|
|
214
|
-
/**
|
|
215
|
-
* Pop and return the top element.
|
|
216
|
-
* @remarks Time O(1), Space O(1)
|
|
217
|
-
* @returns Removed element or undefined.
|
|
218
|
-
*/
|
|
219
|
-
pop() {
|
|
220
|
-
return this.isEmpty() ? undefined : this.elements.pop();
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Push many elements from an iterable.
|
|
224
|
-
* @remarks Time O(N), Space O(1)
|
|
225
|
-
* @param elements - Iterable of elements (or raw records if toElementFn is set).
|
|
226
|
-
* @returns Array of per-element success flags.
|
|
227
|
-
*/
|
|
228
|
-
pushMany(elements) {
|
|
229
|
-
const ans = [];
|
|
230
|
-
for (const el of elements) {
|
|
231
|
-
if (this.toElementFn)
|
|
232
|
-
ans.push(this.push(this.toElementFn(el)));
|
|
233
|
-
else
|
|
234
|
-
ans.push(this.push(el));
|
|
235
|
-
}
|
|
236
|
-
return ans;
|
|
237
|
-
}
|
|
238
|
-
/**
|
|
239
|
-
* Delete the first occurrence of a specific element.
|
|
240
|
-
* @remarks Time O(N), Space O(1)
|
|
241
|
-
* @param element - Element to remove (using the configured equality).
|
|
242
|
-
* @returns True if an element was removed.
|
|
243
|
-
*/
|
|
244
|
-
delete(element) {
|
|
245
|
-
const idx = this._indexOfByEquals(element);
|
|
246
|
-
return this.deleteAt(idx);
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* Delete the element at an index.
|
|
250
|
-
* @remarks Time O(N), Space O(1)
|
|
251
|
-
* @param index - Zero-based index from the bottom.
|
|
252
|
-
* @returns True if removed.
|
|
253
|
-
*/
|
|
254
|
-
deleteAt(index) {
|
|
255
|
-
if (index < 0 || index >= this.elements.length)
|
|
256
|
-
return false;
|
|
257
|
-
const spliced = this.elements.splice(index, 1);
|
|
258
|
-
return spliced.length === 1;
|
|
259
|
-
}
|
|
260
|
-
/**
|
|
261
|
-
* Delete the first element that satisfies a predicate.
|
|
262
|
-
* @remarks Time O(N), Space O(1)
|
|
263
|
-
* @param predicate - Function (value, index, stack) → boolean to decide deletion.
|
|
264
|
-
* @returns True if a match was removed.
|
|
265
|
-
*/
|
|
266
|
-
deleteWhere(predicate) {
|
|
267
|
-
for (let i = 0; i < this.elements.length; i++) {
|
|
268
|
-
if (predicate(this.elements[i], i, this)) {
|
|
269
|
-
this.elements.splice(i, 1);
|
|
270
|
-
return true;
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
return false;
|
|
274
|
-
}
|
|
275
|
-
/**
|
|
276
|
-
* Remove all elements and reset storage.
|
|
277
|
-
* @remarks Time O(1), Space O(1)
|
|
278
|
-
* @returns void
|
|
279
|
-
*/
|
|
280
|
-
clear() {
|
|
281
|
-
this._elements = [];
|
|
282
|
-
}
|
|
283
|
-
/**
|
|
284
|
-
* Deep clone this stack.
|
|
285
|
-
* @remarks Time O(N), Space O(N)
|
|
286
|
-
* @returns A new stack with the same content.
|
|
287
|
-
*/
|
|
288
|
-
clone() {
|
|
289
|
-
const out = this._createInstance({ toElementFn: this.toElementFn });
|
|
290
|
-
for (const v of this)
|
|
291
|
-
out.push(v);
|
|
292
|
-
return out;
|
|
293
|
-
}
|
|
294
|
-
/**
|
|
295
|
-
* Filter elements into a new stack of the same class.
|
|
296
|
-
* @remarks Time O(N), Space O(N)
|
|
297
|
-
* @param predicate - Predicate (value, index, stack) → boolean to keep value.
|
|
298
|
-
* @param [thisArg] - Value for `this` inside the predicate.
|
|
299
|
-
* @returns A new stack with kept values.
|
|
300
|
-
*/
|
|
301
|
-
filter(predicate, thisArg) {
|
|
302
|
-
const out = this._createInstance({ toElementFn: this.toElementFn });
|
|
303
|
-
let index = 0;
|
|
304
|
-
for (const v of this) {
|
|
305
|
-
if (predicate.call(thisArg, v, index, this))
|
|
306
|
-
out.push(v);
|
|
307
|
-
index++;
|
|
308
|
-
}
|
|
309
|
-
return out;
|
|
310
|
-
}
|
|
311
|
-
/**
|
|
312
|
-
* Map values into a new stack of the same element type.
|
|
313
|
-
* @remarks Time O(N), Space O(N)
|
|
314
|
-
* @param callback - Mapping function (value, index, stack) → newValue.
|
|
315
|
-
* @param [thisArg] - Value for `this` inside the callback.
|
|
316
|
-
* @returns A new stack with mapped values.
|
|
317
|
-
*/
|
|
318
|
-
mapSame(callback, thisArg) {
|
|
319
|
-
const out = this._createInstance({ toElementFn: this.toElementFn });
|
|
320
|
-
let index = 0;
|
|
321
|
-
for (const v of this) {
|
|
322
|
-
const mv = thisArg === undefined ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
|
|
323
|
-
out.push(mv);
|
|
324
|
-
}
|
|
325
|
-
return out;
|
|
326
|
-
}
|
|
327
|
-
/**
|
|
328
|
-
* Map values into a new stack (possibly different element type).
|
|
329
|
-
* @remarks Time O(N), Space O(N)
|
|
330
|
-
* @template EM
|
|
331
|
-
* @template RM
|
|
332
|
-
* @param callback - Mapping function (value, index, stack) → newElement.
|
|
333
|
-
* @param [options] - Options for the output stack (e.g., toElementFn).
|
|
334
|
-
* @param [thisArg] - Value for `this` inside the callback.
|
|
335
|
-
* @returns A new Stack with mapped elements.
|
|
336
|
-
*/
|
|
337
|
-
map(callback, options, thisArg) {
|
|
338
|
-
const out = this._createLike([], Object.assign({}, (options !== null && options !== void 0 ? options : {})));
|
|
339
|
-
let index = 0;
|
|
340
|
-
for (const v of this) {
|
|
341
|
-
out.push(thisArg === undefined ? callback(v, index, this) : callback.call(thisArg, v, index, this));
|
|
342
|
-
index++;
|
|
343
|
-
}
|
|
344
|
-
return out;
|
|
345
|
-
}
|
|
346
|
-
/**
|
|
347
|
-
* Set the equality comparator used by delete/search operations.
|
|
348
|
-
* @remarks Time O(1), Space O(1)
|
|
349
|
-
* @param equals - Equality predicate (a, b) → boolean.
|
|
350
|
-
* @returns This stack.
|
|
351
|
-
*/
|
|
352
|
-
setEquality(equals) {
|
|
353
|
-
this._equals = equals;
|
|
354
|
-
return this;
|
|
355
|
-
}
|
|
356
|
-
/**
|
|
357
|
-
* (Protected) Find the index of a target element using the equality function.
|
|
358
|
-
* @remarks Time O(N), Space O(1)
|
|
359
|
-
* @param target - Element to search for.
|
|
360
|
-
* @returns Index or -1 if not found.
|
|
361
|
-
*/
|
|
362
|
-
_indexOfByEquals(target) {
|
|
363
|
-
for (let i = 0; i < this.elements.length; i++)
|
|
364
|
-
if (this._equals(this.elements[i], target))
|
|
365
|
-
return i;
|
|
366
|
-
return -1;
|
|
367
|
-
}
|
|
368
|
-
/**
|
|
369
|
-
* (Protected) Create an empty instance of the same concrete class.
|
|
370
|
-
* @remarks Time O(1), Space O(1)
|
|
371
|
-
* @param [options] - Options forwarded to the constructor.
|
|
372
|
-
* @returns An empty like-kind stack instance.
|
|
373
|
-
*/
|
|
374
|
-
_createInstance(options) {
|
|
375
|
-
const Ctor = this.constructor;
|
|
376
|
-
return new Ctor([], options);
|
|
377
|
-
}
|
|
378
|
-
/**
|
|
379
|
-
* (Protected) Create a like-kind stack and seed it from an iterable.
|
|
380
|
-
* @remarks Time O(N), Space O(N)
|
|
381
|
-
* @template T
|
|
382
|
-
* @template RR
|
|
383
|
-
* @param [elements] - Iterable used to seed the new stack.
|
|
384
|
-
* @param [options] - Options forwarded to the constructor.
|
|
385
|
-
* @returns A like-kind Stack instance.
|
|
386
|
-
*/
|
|
387
|
-
_createLike(elements = [], options) {
|
|
388
|
-
const Ctor = this.constructor;
|
|
389
|
-
return new Ctor(elements, options);
|
|
390
|
-
}
|
|
391
|
-
/**
|
|
392
|
-
* (Protected) Iterate elements from bottom to top.
|
|
393
|
-
* @remarks Time O(N), Space O(1)
|
|
394
|
-
* @returns Iterator of elements.
|
|
395
|
-
*/
|
|
396
|
-
*_getIterator() {
|
|
397
|
-
for (let i = 0; i < this.elements.length; i++)
|
|
398
|
-
yield this.elements[i];
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
exports.Stack = Stack;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./tree"), exports);
|
|
@@ -1,62 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TreeNode = void 0;
|
|
4
|
-
class TreeNode {
|
|
5
|
-
/**
|
|
6
|
-
* The constructor function initializes a TreeNode object with a key, optional value, and optional
|
|
7
|
-
* children.
|
|
8
|
-
* @param {string} key - A string representing the key of the tree node.
|
|
9
|
-
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the
|
|
10
|
-
* value associated with the node. If no value is provided, it defaults to `undefined`.
|
|
11
|
-
* @param {TreeNode<V>[]} [children] - The `children` parameter is an optional array of `TreeNode<V>`
|
|
12
|
-
* objects. It represents the child nodes of the current node. If no children are provided, the
|
|
13
|
-
* default value is an empty array.
|
|
14
|
-
*/
|
|
15
|
-
constructor(key, value, children) {
|
|
16
|
-
this._key = key;
|
|
17
|
-
this._value = value || undefined;
|
|
18
|
-
if (children)
|
|
19
|
-
this._children = children;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* The function returns the value of the protected variable _key.
|
|
23
|
-
* @returns The value of the `_key` property, which is a string.
|
|
24
|
-
*/
|
|
25
|
-
get key() {
|
|
26
|
-
return this._key;
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* The above function sets the value of a protected variable called "key".
|
|
30
|
-
* @param {string} value - The value parameter is a string that represents the value to be assigned
|
|
31
|
-
* to the key.
|
|
32
|
-
*/
|
|
33
|
-
set key(value) {
|
|
34
|
-
this._key = value;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* The function returns the value stored in a variable, or undefined if the variable is empty.
|
|
38
|
-
* @returns The value of the variable `_value` is being returned.
|
|
39
|
-
*/
|
|
40
|
-
get value() {
|
|
41
|
-
return this._value;
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* The function sets the value of a variable.
|
|
45
|
-
* @param {V | undefined} value - The parameter "value" is of type "V | undefined", which means it
|
|
46
|
-
* can accept a value of type "V" or it can be undefined.
|
|
47
|
-
*/
|
|
48
|
-
set value(value) {
|
|
49
|
-
this._value = value;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* The function returns an array of TreeNode objects or undefined.
|
|
53
|
-
* @returns The `children` property is being returned. It is of type `TreeNode<V>[] | undefined`,
|
|
54
|
-
* which means it can either be an array of `TreeNode<V>` objects or `undefined`.
|
|
55
|
-
*/
|
|
56
|
-
get children() {
|
|
57
|
-
return this._children;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* The function sets the value of the children property of a TreeNode object.
|
|
61
|
-
* @param {TreeNode<V>[] | undefined} value - The value parameter is of type TreeNode<V>[] |
|
|
62
|
-
* undefined. This means that it can accept an array of TreeNode objects or undefined.
|
|
63
|
-
*/
|
|
64
|
-
set children(value) {
|
|
65
|
-
this._children = value;
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* The function `addChildren` adds one or more child nodes to the current node.
|
|
69
|
-
* @param {TreeNode<V> | TreeNode<V>[]} children - The `children` parameter can be either a single
|
|
70
|
-
* `TreeNode<V>` object or an array of `TreeNode<V>` objects.
|
|
71
|
-
*/
|
|
72
|
-
addChildren(children) {
|
|
73
|
-
if (!this._children) {
|
|
74
|
-
this._children = [];
|
|
75
|
-
}
|
|
76
|
-
if (children instanceof TreeNode) {
|
|
77
|
-
this._children.push(children);
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
this._children = this._children.concat(children);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* The function `getHeight()` calculates the maximum depth of a tree structure by performing a
|
|
85
|
-
* breadth-first search.
|
|
86
|
-
* @returns the maximum depth or height of the tree.
|
|
87
|
-
*/
|
|
88
|
-
getHeight() {
|
|
89
|
-
let maxDepth = 0;
|
|
90
|
-
if (this) {
|
|
91
|
-
const bfs = (node, level) => {
|
|
92
|
-
if (level > maxDepth) {
|
|
93
|
-
maxDepth = level;
|
|
94
|
-
}
|
|
95
|
-
const { _children } = node;
|
|
96
|
-
if (_children) {
|
|
97
|
-
for (let i = 0, len = _children.length; i < len; i++) {
|
|
98
|
-
bfs(_children[i], level + 1);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
bfs(this, 0);
|
|
103
|
-
}
|
|
104
|
-
return maxDepth;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
exports.TreeNode = TreeNode;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./trie"), exports);
|