data-structure-typed 0.9.16 → 1.3.0
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/LICENSE +21 -0
- package/README.md +665 -172
- package/dist/bundle.js +2 -0
- package/dist/bundle.js.LICENSE.txt +13 -0
- package/dist/data-structures/binary-tree/aa-tree.js +2 -5
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +364 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +1308 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +85 -14
- package/dist/data-structures/binary-tree/avl-tree.js +142 -116
- package/dist/data-structures/binary-tree/b-tree.js +2 -5
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +39 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +54 -13
- package/dist/data-structures/binary-tree/binary-tree.d.ts +29 -126
- package/dist/data-structures/binary-tree/binary-tree.js +31 -1093
- package/dist/data-structures/binary-tree/bst.d.ts +117 -23
- package/dist/data-structures/binary-tree/bst.js +233 -240
- package/dist/data-structures/binary-tree/index.d.ts +1 -0
- package/dist/data-structures/binary-tree/index.js +1 -0
- package/dist/data-structures/binary-tree/rb-tree.d.ts +18 -1
- package/dist/data-structures/binary-tree/rb-tree.js +40 -5
- package/dist/data-structures/binary-tree/segment-tree.d.ts +61 -11
- package/dist/data-structures/binary-tree/segment-tree.js +126 -93
- package/dist/data-structures/binary-tree/splay-tree.js +2 -5
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +213 -6
- package/dist/data-structures/binary-tree/tree-multiset.js +687 -34
- package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
- package/dist/data-structures/graph/abstract-graph.d.ts +270 -36
- package/dist/data-structures/graph/abstract-graph.js +610 -572
- package/dist/data-structures/graph/directed-graph.d.ts +173 -16
- package/dist/data-structures/graph/directed-graph.js +345 -313
- package/dist/data-structures/graph/index.d.ts +1 -0
- package/dist/data-structures/graph/index.js +1 -0
- package/dist/data-structures/graph/map-graph.d.ts +79 -0
- package/dist/data-structures/graph/map-graph.js +111 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +111 -9
- package/dist/data-structures/graph/undirected-graph.js +203 -178
- package/dist/data-structures/hash/coordinate-map.d.ts +38 -1
- package/dist/data-structures/hash/coordinate-map.js +59 -36
- package/dist/data-structures/hash/coordinate-set.d.ts +32 -2
- package/dist/data-structures/hash/coordinate-set.js +49 -33
- package/dist/data-structures/hash/hash-table.d.ts +2 -1
- package/dist/data-structures/hash/hash-table.js +4 -0
- package/dist/data-structures/hash/pair.d.ts +2 -1
- package/dist/data-structures/hash/pair.js +4 -0
- package/dist/data-structures/hash/tree-map.d.ts +2 -1
- package/dist/data-structures/hash/tree-map.js +4 -0
- package/dist/data-structures/hash/tree-set.d.ts +2 -1
- package/dist/data-structures/hash/tree-set.js +4 -0
- package/dist/data-structures/heap/heap.d.ts +62 -45
- package/dist/data-structures/heap/heap.js +124 -86
- package/dist/data-structures/heap/max-heap.d.ts +13 -5
- package/dist/data-structures/heap/max-heap.js +18 -28
- package/dist/data-structures/heap/min-heap.d.ts +14 -5
- package/dist/data-structures/heap/min-heap.js +19 -28
- package/dist/data-structures/index.d.ts +1 -1
- package/dist/data-structures/index.js +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +193 -56
- package/dist/data-structures/linked-list/doubly-linked-list.js +484 -220
- package/dist/data-structures/linked-list/index.d.ts +1 -0
- package/dist/data-structures/linked-list/index.js +1 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +117 -315
- package/dist/data-structures/linked-list/singly-linked-list.js +374 -727
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -1
- package/dist/data-structures/linked-list/skip-linked-list.js +4 -0
- package/dist/data-structures/matrix/matrix.d.ts +12 -0
- package/dist/data-structures/matrix/matrix.js +21 -8
- package/dist/data-structures/matrix/matrix2d.d.ts +85 -2
- package/dist/data-structures/matrix/matrix2d.js +146 -80
- package/dist/data-structures/matrix/navigator.d.ts +36 -1
- package/dist/data-structures/matrix/navigator.js +46 -37
- package/dist/data-structures/matrix/vector2d.d.ts +142 -15
- package/dist/data-structures/matrix/vector2d.js +215 -109
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -2
- package/dist/data-structures/priority-queue/max-priority-queue.js +33 -26
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -2
- package/dist/data-structures/priority-queue/min-priority-queue.js +34 -26
- package/dist/data-structures/priority-queue/priority-queue.d.ts +153 -3
- package/dist/data-structures/priority-queue/priority-queue.js +244 -143
- package/dist/data-structures/queue/deque.d.ts +141 -13
- package/dist/data-structures/queue/deque.js +200 -82
- package/dist/data-structures/queue/queue.d.ts +65 -38
- package/dist/data-structures/queue/queue.js +110 -66
- package/dist/data-structures/stack/stack.d.ts +27 -32
- package/dist/data-structures/stack/stack.js +47 -53
- package/dist/data-structures/tree/index.d.ts +1 -0
- package/dist/data-structures/tree/index.js +17 -0
- package/dist/data-structures/tree/tree.d.ts +14 -0
- package/dist/data-structures/tree/tree.js +60 -0
- package/dist/data-structures/trie/trie.d.ts +33 -10
- package/dist/data-structures/trie/trie.js +123 -208
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/interfaces/abstract-binary-tree.d.ts +90 -0
- package/dist/interfaces/abstract-graph.d.ts +17 -0
- package/dist/interfaces/avl-tree.d.ts +9 -0
- package/dist/interfaces/binary-tree.d.ts +6 -0
- package/dist/interfaces/bst.d.ts +17 -0
- package/dist/interfaces/directed-graph.d.ts +12 -0
- package/{src/data-structures/types/index.ts → dist/interfaces/index.d.ts} +10 -8
- package/dist/interfaces/index.js +31 -0
- package/{src/data-structures/hash/hash-table.ts → dist/interfaces/priority-queue.d.ts} +1 -1
- package/dist/interfaces/rb-tree.d.ts +8 -0
- package/dist/interfaces/segment-tree.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.js +2 -0
- package/dist/interfaces/tree-multiset.d.ts +7 -0
- package/dist/interfaces/tree-multiset.js +2 -0
- package/dist/interfaces/undirected-graph.d.ts +5 -0
- package/dist/interfaces/undirected-graph.js +2 -0
- package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
- package/dist/types/data-structures/abstract-binary-tree.js +25 -0
- package/dist/types/data-structures/abstract-graph.d.ts +11 -0
- package/dist/types/data-structures/abstract-graph.js +2 -0
- package/dist/types/data-structures/avl-tree.d.ts +4 -0
- package/dist/types/data-structures/avl-tree.js +2 -0
- package/dist/types/data-structures/binary-tree.d.ts +4 -0
- package/dist/types/data-structures/binary-tree.js +2 -0
- package/dist/types/data-structures/bst.d.ts +13 -0
- package/dist/types/data-structures/bst.js +9 -0
- package/dist/types/data-structures/directed-graph.d.ts +6 -0
- package/dist/types/data-structures/directed-graph.js +9 -0
- package/dist/types/data-structures/doubly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/doubly-linked-list.js +2 -0
- package/dist/types/data-structures/heap.d.ts +3 -0
- package/dist/types/data-structures/heap.js +2 -0
- package/dist/{data-structures/types → types/data-structures}/index.d.ts +3 -1
- package/dist/{data-structures/types → types/data-structures}/index.js +3 -1
- package/dist/types/data-structures/map-graph.d.ts +1 -0
- package/dist/types/data-structures/map-graph.js +2 -0
- package/dist/{data-structures/types → types/data-structures}/navigator.d.ts +2 -2
- package/dist/types/data-structures/navigator.js +2 -0
- package/dist/{data-structures/types → types/data-structures}/priority-queue.d.ts +2 -2
- package/dist/types/data-structures/priority-queue.js +2 -0
- package/dist/types/data-structures/rb-tree.d.ts +8 -0
- package/dist/types/data-structures/rb-tree.js +8 -0
- package/dist/types/data-structures/segment-tree.js +2 -0
- package/dist/types/data-structures/singly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/singly-linked-list.js +2 -0
- package/dist/types/data-structures/tree-multiset.d.ts +4 -0
- package/dist/types/data-structures/tree-multiset.js +2 -0
- package/dist/types/helpers.d.ts +1 -0
- package/dist/types/helpers.js +2 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.js +19 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/index.js +18 -0
- package/dist/types/utils/utils.d.ts +7 -0
- package/dist/types/utils/utils.js +2 -0
- package/dist/types/utils/validate-type.d.ts +19 -0
- package/dist/types/utils/validate-type.js +2 -0
- package/dist/utils/utils.d.ts +17 -103
- package/dist/utils/utils.js +40 -625
- package/package.json +96 -23
- package/.idea/data-structure-typed.iml +0 -12
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/dist/data-structures/trampoline.d.ts +0 -16
- package/dist/data-structures/trampoline.js +0 -130
- package/dist/data-structures/types/abstract-graph.d.ts +0 -29
- package/dist/data-structures/types/avl-tree.d.ts +0 -5
- package/dist/data-structures/types/binary-tree.d.ts +0 -16
- package/dist/data-structures/types/bst.d.ts +0 -7
- package/dist/data-structures/types/directed-graph.d.ts +0 -10
- package/dist/data-structures/types/doubly-linked-list.d.ts +0 -1
- package/dist/data-structures/types/heap.d.ts +0 -7
- package/dist/data-structures/types/singly-linked-list.d.ts +0 -5
- package/dist/data-structures/types/tree-multiset.d.ts +0 -5
- package/dist/data-structures/types/utils.d.ts +0 -52
- package/dist/data-structures/types/utils.js +0 -54
- package/src/data-structures/binary-tree/aa-tree.ts +0 -3
- package/src/data-structures/binary-tree/avl-tree.ts +0 -227
- package/src/data-structures/binary-tree/b-tree.ts +0 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -33
- package/src/data-structures/binary-tree/binary-tree.ts +0 -1133
- package/src/data-structures/binary-tree/bst.ts +0 -395
- package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
- package/src/data-structures/binary-tree/index.ts +0 -11
- package/src/data-structures/binary-tree/rb-tree.ts +0 -3
- package/src/data-structures/binary-tree/segment-tree.ts +0 -172
- package/src/data-structures/binary-tree/splay-tree.ts +0 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +0 -18
- package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
- package/src/data-structures/diagrams/README.md +0 -7
- package/src/data-structures/graph/abstract-graph.ts +0 -753
- package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
- package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
- package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/edge-list.png +0 -0
- package/src/data-structures/graph/diagrams/max-flow.png +0 -0
- package/src/data-structures/graph/diagrams/mst.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
- package/src/data-structures/graph/directed-graph.ts +0 -306
- package/src/data-structures/graph/index.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +0 -155
- package/src/data-structures/hash/coordinate-map.ts +0 -24
- package/src/data-structures/hash/coordinate-set.ts +0 -20
- package/src/data-structures/hash/index.ts +0 -6
- package/src/data-structures/heap/heap.ts +0 -127
- package/src/data-structures/heap/index.ts +0 -3
- package/src/data-structures/heap/max-heap.ts +0 -23
- package/src/data-structures/heap/min-heap.ts +0 -25
- package/src/data-structures/index.ts +0 -12
- package/src/data-structures/linked-list/doubly-linked-list.ts +0 -250
- package/src/data-structures/linked-list/index.ts +0 -2
- package/src/data-structures/linked-list/singly-linked-list.ts +0 -736
- package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
- package/src/data-structures/matrix/index.ts +0 -4
- package/src/data-structures/matrix/matrix.ts +0 -13
- package/src/data-structures/matrix/matrix2d.ts +0 -125
- package/src/data-structures/matrix/navigator.ts +0 -87
- package/src/data-structures/matrix/vector2d.ts +0 -189
- package/src/data-structures/priority-queue/index.ts +0 -3
- package/src/data-structures/priority-queue/max-priority-queue.ts +0 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +0 -13
- package/src/data-structures/priority-queue/priority-queue.ts +0 -200
- package/src/data-structures/queue/deque.ts +0 -139
- package/src/data-structures/queue/index.ts +0 -2
- package/src/data-structures/queue/queue.ts +0 -122
- package/src/data-structures/stack/index.ts +0 -1
- package/src/data-structures/stack/stack.ts +0 -103
- package/src/data-structures/trampoline.ts +0 -51
- package/src/data-structures/trie/index.ts +0 -1
- package/src/data-structures/trie/trie.ts +0 -203
- package/src/data-structures/types/abstract-graph.ts +0 -51
- package/src/data-structures/types/avl-tree.ts +0 -6
- package/src/data-structures/types/binary-tree.ts +0 -15
- package/src/data-structures/types/bst.ts +0 -5
- package/src/data-structures/types/directed-graph.ts +0 -18
- package/src/data-structures/types/doubly-linked-list.ts +0 -1
- package/src/data-structures/types/heap.ts +0 -8
- package/src/data-structures/types/navigator.ts +0 -12
- package/src/data-structures/types/priority-queue.ts +0 -9
- package/src/data-structures/types/segment-tree.ts +0 -1
- package/src/data-structures/types/singly-linked-list.ts +0 -15
- package/src/data-structures/types/tree-multiset.ts +0 -3
- package/src/data-structures/types/utils.ts +0 -173
- package/src/index.ts +0 -1
- package/src/utils/index.ts +0 -1
- package/src/utils/utils.ts +0 -505
- package/tsconfig.json +0 -56
- /package/dist/{data-structures/types/abstract-graph.js → interfaces/abstract-binary-tree.js} +0 -0
- /package/dist/{data-structures/types/avl-tree.js → interfaces/abstract-graph.js} +0 -0
- /package/dist/{data-structures/types/binary-tree.js → interfaces/avl-tree.js} +0 -0
- /package/dist/{data-structures/types/bst.js → interfaces/binary-tree.js} +0 -0
- /package/dist/{data-structures/types/directed-graph.js → interfaces/bst.js} +0 -0
- /package/dist/{data-structures/types/doubly-linked-list.js → interfaces/directed-graph.js} +0 -0
- /package/{src/data-structures/hash/pair.ts → dist/interfaces/doubly-linked-list.d.ts} +0 -0
- /package/dist/{data-structures/types/heap.js → interfaces/doubly-linked-list.js} +0 -0
- /package/{src/data-structures/hash/tree-map.ts → dist/interfaces/heap.d.ts} +0 -0
- /package/dist/{data-structures/types/navigator.js → interfaces/heap.js} +0 -0
- /package/{src/data-structures/hash/tree-set.ts → dist/interfaces/navigator.d.ts} +0 -0
- /package/dist/{data-structures/types/priority-queue.js → interfaces/navigator.js} +0 -0
- /package/dist/{data-structures/types/segment-tree.js → interfaces/priority-queue.js} +0 -0
- /package/dist/{data-structures/types/singly-linked-list.js → interfaces/rb-tree.js} +0 -0
- /package/dist/{data-structures/types/tree-multiset.js → interfaces/segment-tree.js} +0 -0
- /package/dist/{data-structures/types → types/data-structures}/segment-tree.d.ts +0 -0
|
@@ -1,48 +1,91 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Queue = void 0;
|
|
3
|
+
exports.ArrayQueue = exports.Queue = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* @license MIT
|
|
6
|
-
* @copyright
|
|
6
|
+
* @copyright Tyler Zeng <zrwusa@gmail.com>
|
|
7
7
|
* @class
|
|
8
8
|
*/
|
|
9
|
-
|
|
9
|
+
const linked_list_1 = require("../linked-list");
|
|
10
|
+
class Queue extends linked_list_1.SinglyLinkedList {
|
|
10
11
|
/**
|
|
11
|
-
*
|
|
12
|
-
* @param {
|
|
12
|
+
* The enqueue function adds a value to the end of an array.
|
|
13
|
+
* @param {T} value - The value parameter represents the value that you want to add to the queue.
|
|
13
14
|
*/
|
|
14
|
-
|
|
15
|
+
enqueue(value) {
|
|
16
|
+
this.push(value);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The `dequeue` function removes and returns the first element from a queue, or returns null if the queue is empty.
|
|
20
|
+
* @returns The method is returning the element at the front of the queue, or null if the queue is empty.
|
|
21
|
+
*/
|
|
22
|
+
dequeue() {
|
|
23
|
+
return this.shift();
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
27
|
+
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
28
|
+
*/
|
|
29
|
+
peek() {
|
|
30
|
+
var _a;
|
|
31
|
+
return (_a = this.head) === null || _a === void 0 ? void 0 : _a.val;
|
|
32
|
+
}
|
|
33
|
+
*[Symbol.iterator]() {
|
|
34
|
+
let current = this.head;
|
|
35
|
+
while (current) {
|
|
36
|
+
yield current.val;
|
|
37
|
+
current = current.next;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.Queue = Queue;
|
|
42
|
+
class ArrayQueue {
|
|
43
|
+
/**
|
|
44
|
+
* The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
|
|
45
|
+
* @param {T[]} [elements] - The `elements` parameter is an optional array of elements of type `T`. If provided, it
|
|
46
|
+
* will be used to initialize the `_nodes` property of the class. If not provided, the `_nodes` property will be
|
|
47
|
+
* initialized as an empty array.
|
|
48
|
+
*/
|
|
49
|
+
constructor(elements) {
|
|
15
50
|
this._nodes = elements || [];
|
|
16
51
|
this._offset = 0;
|
|
17
52
|
}
|
|
18
53
|
/**
|
|
19
|
-
*
|
|
54
|
+
* The size function returns the number of elements in an array.
|
|
55
|
+
* @returns {number} The size of the array, which is the difference between the length of the array and the offset.
|
|
56
|
+
*/
|
|
57
|
+
get size() {
|
|
58
|
+
return this._nodes.length - this._offset;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.
|
|
20
62
|
* @public
|
|
21
63
|
* @static
|
|
22
|
-
* @param {
|
|
23
|
-
* @
|
|
64
|
+
* @param {T[]} elements - The "elements" parameter is an array of elements of type T.
|
|
65
|
+
* @returns The method is returning a new instance of the Queue class, initialized with the elements from the input
|
|
66
|
+
* array.
|
|
24
67
|
*/
|
|
25
|
-
|
|
26
|
-
return new
|
|
27
|
-
}
|
|
68
|
+
static fromArray(elements) {
|
|
69
|
+
return new ArrayQueue(elements);
|
|
70
|
+
}
|
|
28
71
|
/**
|
|
29
|
-
* Adds an element at the back of the queue.
|
|
30
|
-
* @
|
|
31
|
-
* @
|
|
72
|
+
* The push function adds an element to the end of the queue and returns the updated queue.Adds an element at the back of the queue.
|
|
73
|
+
* @param {T} element - The `element` parameter represents the element that you want to add to the queue.
|
|
74
|
+
* @returns The `add` method is returning a `Queue<T>` object.
|
|
32
75
|
*/
|
|
33
|
-
|
|
76
|
+
push(element) {
|
|
34
77
|
this._nodes.push(element);
|
|
35
78
|
return this;
|
|
36
|
-
}
|
|
79
|
+
}
|
|
37
80
|
/**
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
* @returns
|
|
81
|
+
* The `shift` function removes and returns the first element in the queue, and adjusts the internal data structure if
|
|
82
|
+
* necessary to optimize performance.
|
|
83
|
+
* @returns The function `shift()` returns either the first element in the queue or `null` if the queue is empty.
|
|
41
84
|
*/
|
|
42
|
-
|
|
43
|
-
if (this.size
|
|
85
|
+
shift() {
|
|
86
|
+
if (this.size === 0)
|
|
44
87
|
return null;
|
|
45
|
-
|
|
88
|
+
const first = this.peek();
|
|
46
89
|
this._offset += 1;
|
|
47
90
|
if (this._offset * 2 < this._nodes.length)
|
|
48
91
|
return first;
|
|
@@ -51,63 +94,64 @@ var Queue = /** @class */ (function () {
|
|
|
51
94
|
this._nodes = this._nodes.slice(this._offset);
|
|
52
95
|
this._offset = 0;
|
|
53
96
|
return first;
|
|
54
|
-
}
|
|
97
|
+
}
|
|
55
98
|
/**
|
|
56
|
-
*
|
|
57
|
-
* @
|
|
58
|
-
*
|
|
99
|
+
* The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
|
|
100
|
+
* @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
101
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
|
|
59
102
|
*/
|
|
60
|
-
|
|
61
|
-
return this.size
|
|
62
|
-
}
|
|
103
|
+
peek() {
|
|
104
|
+
return this.size > 0 ? this._nodes[this._offset] : null;
|
|
105
|
+
}
|
|
63
106
|
/**
|
|
64
|
-
*
|
|
65
|
-
* @
|
|
66
|
-
*
|
|
107
|
+
* The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty.
|
|
108
|
+
* @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
109
|
+
* array is empty, it returns `null`.
|
|
67
110
|
*/
|
|
68
|
-
|
|
69
|
-
return this.size
|
|
70
|
-
}
|
|
111
|
+
peekLast() {
|
|
112
|
+
return this.size > 0 ? this._nodes[this._nodes.length - 1] : null;
|
|
113
|
+
}
|
|
71
114
|
/**
|
|
72
|
-
*
|
|
73
|
-
* @
|
|
74
|
-
* @returns {number}
|
|
115
|
+
* The enqueue function adds a value to the end of a queue.
|
|
116
|
+
* @param {T} value - The value parameter represents the value that you want to add to the queue.
|
|
75
117
|
*/
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
118
|
+
enqueue(value) {
|
|
119
|
+
this.push(value);
|
|
120
|
+
}
|
|
79
121
|
/**
|
|
80
|
-
*
|
|
81
|
-
* @
|
|
82
|
-
* @returns {boolean}
|
|
122
|
+
* The `dequeue` function removes and returns the first element from a queue, or returns null if the queue is empty.
|
|
123
|
+
* @returns The method is returning a value of type T or null.
|
|
83
124
|
*/
|
|
84
|
-
|
|
85
|
-
return this.
|
|
86
|
-
}
|
|
125
|
+
dequeue() {
|
|
126
|
+
return this.shift();
|
|
127
|
+
}
|
|
87
128
|
/**
|
|
88
|
-
*
|
|
89
|
-
* @
|
|
90
|
-
* @returns {array}
|
|
129
|
+
* The function checks if a data structure is empty by comparing its size to zero.
|
|
130
|
+
* @returns {boolean} A boolean value indicating whether the size of the object is 0 or not.
|
|
91
131
|
*/
|
|
92
|
-
|
|
132
|
+
isEmpty() {
|
|
133
|
+
return this.size === 0;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* The toArray() function returns an array of elements from the current offset to the end of the _nodes array.
|
|
137
|
+
* @returns An array of type T is being returned.
|
|
138
|
+
*/
|
|
139
|
+
toArray() {
|
|
93
140
|
return this._nodes.slice(this._offset);
|
|
94
|
-
}
|
|
141
|
+
}
|
|
95
142
|
/**
|
|
96
|
-
*
|
|
97
|
-
* @public
|
|
143
|
+
* The clear function resets the nodes array and offset to their initial values.
|
|
98
144
|
*/
|
|
99
|
-
|
|
145
|
+
clear() {
|
|
100
146
|
this._nodes = [];
|
|
101
147
|
this._offset = 0;
|
|
102
|
-
}
|
|
148
|
+
}
|
|
103
149
|
/**
|
|
104
|
-
*
|
|
105
|
-
* @
|
|
106
|
-
* @return {Queue}
|
|
150
|
+
* The `clone()` function returns a new Queue object with the same elements as the original Queue.
|
|
151
|
+
* @returns The `clone()` method is returning a new instance of the `Queue` class.
|
|
107
152
|
*/
|
|
108
|
-
|
|
109
|
-
return new
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
exports.Queue = Queue;
|
|
153
|
+
clone() {
|
|
154
|
+
return new ArrayQueue(this._nodes.slice(this._offset));
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
exports.ArrayQueue = ArrayQueue;
|
|
@@ -1,68 +1,63 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @license MIT
|
|
3
|
-
* @copyright
|
|
3
|
+
* @copyright Tyler Zeng <zrwusa@gmail.com>
|
|
4
4
|
* @class
|
|
5
5
|
*/
|
|
6
|
-
export declare class Stack<T> {
|
|
6
|
+
export declare class Stack<T = number> {
|
|
7
7
|
protected _elements: T[];
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
10
|
-
* @param {
|
|
9
|
+
* The constructor initializes an array of elements, which can be provided as an optional parameter.
|
|
10
|
+
* @param {T[]} [elements] - The `elements` parameter is an optional parameter of type `T[]`, which represents an array
|
|
11
|
+
* of elements of type `T`. It is used to initialize the `_elements` property of the class. If the `elements` parameter
|
|
12
|
+
* is provided and is an array, it is assigned to the `_elements
|
|
11
13
|
*/
|
|
12
14
|
constructor(elements?: T[]);
|
|
13
15
|
/**
|
|
14
|
-
*
|
|
15
|
-
* @
|
|
16
|
-
* @
|
|
17
|
-
*
|
|
18
|
-
* @return {Stack}
|
|
16
|
+
* The function "fromArray" creates a new Stack object from an array of elements.
|
|
17
|
+
* @param {T[]} elements - The `elements` parameter is an array of elements of type `T`.
|
|
18
|
+
* @returns {Stack} The method is returning a new instance of the Stack class, initialized with the elements from the input
|
|
19
|
+
* array.
|
|
19
20
|
*/
|
|
20
21
|
static fromArray<T>(elements: T[]): Stack<T>;
|
|
21
22
|
/**
|
|
22
|
-
*
|
|
23
|
-
* @
|
|
24
|
-
* @returns {boolean}
|
|
23
|
+
* The function checks if an array is empty and returns a boolean value.
|
|
24
|
+
* @returns A boolean value indicating whether the `_elements` array is empty or not.
|
|
25
25
|
*/
|
|
26
26
|
isEmpty(): boolean;
|
|
27
27
|
/**
|
|
28
|
-
*
|
|
29
|
-
* @
|
|
30
|
-
* @returns {number}
|
|
28
|
+
* The size() function returns the number of elements in an array.
|
|
29
|
+
* @returns The size of the elements array.
|
|
31
30
|
*/
|
|
32
31
|
size(): number;
|
|
33
32
|
/**
|
|
34
|
-
*
|
|
35
|
-
* @
|
|
36
|
-
* @returns {object}
|
|
33
|
+
* The `peek` function returns the last element of an array, or null if the array is empty.
|
|
34
|
+
* @returns The `peek()` function returns the last element of the `_elements` array, or `null` if the array is empty.
|
|
37
35
|
*/
|
|
38
36
|
peek(): T | null;
|
|
39
37
|
/**
|
|
40
|
-
*
|
|
41
|
-
* @
|
|
42
|
-
* @
|
|
38
|
+
* The push function adds an element to the stack and returns the updated stack.
|
|
39
|
+
* @param {T} element - The parameter "element" is of type T, which means it can be any data type.
|
|
40
|
+
* @returns The `push` method is returning the updated `Stack<T>` object.
|
|
43
41
|
*/
|
|
44
42
|
push(element: T): Stack<T>;
|
|
45
43
|
/**
|
|
46
|
-
*
|
|
47
|
-
* @
|
|
48
|
-
*
|
|
44
|
+
* The `pop` function removes and returns the last element from an array, or returns null if the array is empty.
|
|
45
|
+
* @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
|
|
46
|
+
* array is empty, it returns `null`.
|
|
49
47
|
*/
|
|
50
48
|
pop(): T | null;
|
|
51
49
|
/**
|
|
52
|
-
*
|
|
53
|
-
* @
|
|
54
|
-
* @returns {array}
|
|
50
|
+
* The toArray function returns a copy of the elements in an array.
|
|
51
|
+
* @returns An array of type T.
|
|
55
52
|
*/
|
|
56
53
|
toArray(): T[];
|
|
57
54
|
/**
|
|
58
|
-
*
|
|
59
|
-
* @public
|
|
55
|
+
* The clear function clears the elements array.
|
|
60
56
|
*/
|
|
61
57
|
clear(): void;
|
|
62
58
|
/**
|
|
63
|
-
*
|
|
64
|
-
* @
|
|
65
|
-
* @return {Stack}
|
|
59
|
+
* The `clone()` function returns a new `Stack` object with the same elements as the original stack.
|
|
60
|
+
* @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
|
|
66
61
|
*/
|
|
67
62
|
clone(): Stack<T>;
|
|
68
63
|
}
|
|
@@ -3,95 +3,89 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.Stack = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* @license MIT
|
|
6
|
-
* @copyright
|
|
6
|
+
* @copyright Tyler Zeng <zrwusa@gmail.com>
|
|
7
7
|
* @class
|
|
8
8
|
*/
|
|
9
|
-
|
|
9
|
+
class Stack {
|
|
10
10
|
/**
|
|
11
|
-
*
|
|
12
|
-
* @param {
|
|
11
|
+
* The constructor initializes an array of elements, which can be provided as an optional parameter.
|
|
12
|
+
* @param {T[]} [elements] - The `elements` parameter is an optional parameter of type `T[]`, which represents an array
|
|
13
|
+
* of elements of type `T`. It is used to initialize the `_elements` property of the class. If the `elements` parameter
|
|
14
|
+
* is provided and is an array, it is assigned to the `_elements
|
|
13
15
|
*/
|
|
14
|
-
|
|
16
|
+
constructor(elements) {
|
|
15
17
|
this._elements = Array.isArray(elements) ? elements : [];
|
|
16
18
|
}
|
|
17
19
|
/**
|
|
18
|
-
*
|
|
19
|
-
* @
|
|
20
|
-
* @
|
|
21
|
-
*
|
|
22
|
-
* @return {Stack}
|
|
20
|
+
* The function "fromArray" creates a new Stack object from an array of elements.
|
|
21
|
+
* @param {T[]} elements - The `elements` parameter is an array of elements of type `T`.
|
|
22
|
+
* @returns {Stack} The method is returning a new instance of the Stack class, initialized with the elements from the input
|
|
23
|
+
* array.
|
|
23
24
|
*/
|
|
24
|
-
|
|
25
|
+
static fromArray(elements) {
|
|
25
26
|
return new Stack(elements);
|
|
26
|
-
}
|
|
27
|
+
}
|
|
27
28
|
/**
|
|
28
|
-
*
|
|
29
|
-
* @
|
|
30
|
-
* @returns {boolean}
|
|
29
|
+
* The function checks if an array is empty and returns a boolean value.
|
|
30
|
+
* @returns A boolean value indicating whether the `_elements` array is empty or not.
|
|
31
31
|
*/
|
|
32
|
-
|
|
32
|
+
isEmpty() {
|
|
33
33
|
return this._elements.length === 0;
|
|
34
|
-
}
|
|
34
|
+
}
|
|
35
35
|
/**
|
|
36
|
-
*
|
|
37
|
-
* @
|
|
38
|
-
* @returns {number}
|
|
36
|
+
* The size() function returns the number of elements in an array.
|
|
37
|
+
* @returns The size of the elements array.
|
|
39
38
|
*/
|
|
40
|
-
|
|
39
|
+
size() {
|
|
41
40
|
return this._elements.length;
|
|
42
|
-
}
|
|
41
|
+
}
|
|
43
42
|
/**
|
|
44
|
-
*
|
|
45
|
-
* @
|
|
46
|
-
* @returns {object}
|
|
43
|
+
* The `peek` function returns the last element of an array, or null if the array is empty.
|
|
44
|
+
* @returns The `peek()` function returns the last element of the `_elements` array, or `null` if the array is empty.
|
|
47
45
|
*/
|
|
48
|
-
|
|
46
|
+
peek() {
|
|
49
47
|
if (this.isEmpty())
|
|
50
48
|
return null;
|
|
51
49
|
return this._elements[this._elements.length - 1];
|
|
52
|
-
}
|
|
50
|
+
}
|
|
53
51
|
/**
|
|
54
|
-
*
|
|
55
|
-
* @
|
|
56
|
-
* @
|
|
52
|
+
* The push function adds an element to the stack and returns the updated stack.
|
|
53
|
+
* @param {T} element - The parameter "element" is of type T, which means it can be any data type.
|
|
54
|
+
* @returns The `push` method is returning the updated `Stack<T>` object.
|
|
57
55
|
*/
|
|
58
|
-
|
|
56
|
+
push(element) {
|
|
59
57
|
this._elements.push(element);
|
|
60
58
|
return this;
|
|
61
|
-
}
|
|
59
|
+
}
|
|
62
60
|
/**
|
|
63
|
-
*
|
|
64
|
-
* @
|
|
65
|
-
*
|
|
61
|
+
* The `pop` function removes and returns the last element from an array, or returns null if the array is empty.
|
|
62
|
+
* @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
|
|
63
|
+
* array is empty, it returns `null`.
|
|
66
64
|
*/
|
|
67
|
-
|
|
65
|
+
pop() {
|
|
68
66
|
if (this.isEmpty())
|
|
69
67
|
return null;
|
|
70
68
|
return this._elements.pop() || null;
|
|
71
|
-
}
|
|
69
|
+
}
|
|
72
70
|
/**
|
|
73
|
-
*
|
|
74
|
-
* @
|
|
75
|
-
* @returns {array}
|
|
71
|
+
* The toArray function returns a copy of the elements in an array.
|
|
72
|
+
* @returns An array of type T.
|
|
76
73
|
*/
|
|
77
|
-
|
|
74
|
+
toArray() {
|
|
78
75
|
return this._elements.slice();
|
|
79
|
-
}
|
|
76
|
+
}
|
|
80
77
|
/**
|
|
81
|
-
*
|
|
82
|
-
* @public
|
|
78
|
+
* The clear function clears the elements array.
|
|
83
79
|
*/
|
|
84
|
-
|
|
80
|
+
clear() {
|
|
85
81
|
this._elements = [];
|
|
86
|
-
}
|
|
82
|
+
}
|
|
87
83
|
/**
|
|
88
|
-
*
|
|
89
|
-
* @
|
|
90
|
-
* @return {Stack}
|
|
84
|
+
* The `clone()` function returns a new `Stack` object with the same elements as the original stack.
|
|
85
|
+
* @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
|
|
91
86
|
*/
|
|
92
|
-
|
|
87
|
+
clone() {
|
|
93
88
|
return new Stack(this._elements.slice());
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
}());
|
|
89
|
+
}
|
|
90
|
+
}
|
|
97
91
|
exports.Stack = Stack;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './tree';
|
|
@@ -0,0 +1,17 @@
|
|
|
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);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare class TreeNode<T = any> {
|
|
2
|
+
constructor(id: string, value?: T, children?: TreeNode<T>[]);
|
|
3
|
+
private _id;
|
|
4
|
+
get id(): string;
|
|
5
|
+
set id(value: string);
|
|
6
|
+
private _value?;
|
|
7
|
+
get value(): T | undefined;
|
|
8
|
+
set value(value: T | undefined);
|
|
9
|
+
private _children?;
|
|
10
|
+
get children(): TreeNode<T>[] | undefined;
|
|
11
|
+
set children(value: TreeNode<T>[] | undefined);
|
|
12
|
+
addChildren(children: TreeNode<T> | TreeNode<T>[]): void;
|
|
13
|
+
getHeight(): number;
|
|
14
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TreeNode = void 0;
|
|
4
|
+
class TreeNode {
|
|
5
|
+
constructor(id, value, children) {
|
|
6
|
+
this._id = id;
|
|
7
|
+
this._value = value || undefined;
|
|
8
|
+
this._children = children || [];
|
|
9
|
+
}
|
|
10
|
+
get id() {
|
|
11
|
+
return this._id;
|
|
12
|
+
}
|
|
13
|
+
set id(value) {
|
|
14
|
+
this._id = value;
|
|
15
|
+
}
|
|
16
|
+
get value() {
|
|
17
|
+
return this._value;
|
|
18
|
+
}
|
|
19
|
+
set value(value) {
|
|
20
|
+
this._value = value;
|
|
21
|
+
}
|
|
22
|
+
get children() {
|
|
23
|
+
return this._children;
|
|
24
|
+
}
|
|
25
|
+
set children(value) {
|
|
26
|
+
this._children = value;
|
|
27
|
+
}
|
|
28
|
+
addChildren(children) {
|
|
29
|
+
if (!this.children) {
|
|
30
|
+
this.children = [];
|
|
31
|
+
}
|
|
32
|
+
if (children instanceof TreeNode) {
|
|
33
|
+
this.children.push(children);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
this.children = this.children.concat(children);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
getHeight() {
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
41
|
+
const beginRoot = this;
|
|
42
|
+
let maxDepth = 1;
|
|
43
|
+
if (beginRoot) {
|
|
44
|
+
const bfs = (node, level) => {
|
|
45
|
+
if (level > maxDepth) {
|
|
46
|
+
maxDepth = level;
|
|
47
|
+
}
|
|
48
|
+
const { children } = node;
|
|
49
|
+
if (children) {
|
|
50
|
+
for (let i = 0, len = children.length; i < len; i++) {
|
|
51
|
+
bfs(children[i], level + 1);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
bfs(beginRoot, 1);
|
|
56
|
+
}
|
|
57
|
+
return maxDepth;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
exports.TreeNode = TreeNode;
|
|
@@ -1,38 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
1
8
|
export declare class TrieNode {
|
|
2
|
-
protected _value: string;
|
|
3
9
|
constructor(v: string);
|
|
10
|
+
private _val;
|
|
11
|
+
get val(): string;
|
|
12
|
+
set val(v: string);
|
|
4
13
|
protected _children: Map<string, TrieNode>;
|
|
5
14
|
get children(): Map<string, TrieNode>;
|
|
6
15
|
set children(v: Map<string, TrieNode>);
|
|
7
16
|
protected _isEnd: boolean;
|
|
8
17
|
get isEnd(): boolean;
|
|
9
18
|
set isEnd(v: boolean);
|
|
10
|
-
get val(): string;
|
|
11
|
-
set val(v: string);
|
|
12
19
|
}
|
|
13
20
|
export declare class Trie {
|
|
14
21
|
constructor(words?: string[]);
|
|
15
22
|
protected _root: TrieNode;
|
|
16
23
|
get root(): TrieNode;
|
|
17
24
|
set root(v: TrieNode);
|
|
18
|
-
|
|
25
|
+
add(word: string): boolean;
|
|
19
26
|
has(input: string): boolean;
|
|
20
27
|
remove(word: string): boolean;
|
|
21
28
|
/**
|
|
22
|
-
* Only can present as a prefix, not a word
|
|
23
|
-
* @param input
|
|
29
|
+
* The function checks if a given input string has an absolute prefix in a tree data structure.Only can present as a prefix, not a word
|
|
30
|
+
* @param {string} input - The input parameter is a string that represents the input value for the function.
|
|
31
|
+
* @returns a boolean value.
|
|
24
32
|
*/
|
|
25
33
|
isAbsPrefix(input: string): boolean;
|
|
26
34
|
/**
|
|
27
|
-
* Can present as a abs prefix or word
|
|
28
|
-
* @param input
|
|
35
|
+
* The function checks if a given input string is a prefix of any existing string in a tree structure.Can present as a abs prefix or word
|
|
36
|
+
* @param {string} input - The input parameter is a string that represents the prefix we want to check.
|
|
37
|
+
* @returns a boolean value.
|
|
29
38
|
*/
|
|
30
39
|
isPrefix(input: string): boolean;
|
|
31
40
|
/**
|
|
32
|
-
* Check if the input string is the common prefix of all the words
|
|
33
|
-
* @param input
|
|
41
|
+
* The function checks if the input string is a common prefix in a Trie data structure.Check if the input string is the common prefix of all the words
|
|
42
|
+
* @param {string} input - The input parameter is a string that represents the common prefix that we want to check for
|
|
43
|
+
* in the Trie data structure.
|
|
44
|
+
* @returns a boolean value indicating whether the input string is a common prefix in the Trie data structure.
|
|
34
45
|
*/
|
|
35
46
|
isCommonPrefix(input: string): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* The function `getLongestCommonPrefix` returns the longest common prefix among all the words stored in a Trie data
|
|
49
|
+
* structure.
|
|
50
|
+
* @returns The function `getLongestCommonPrefix` returns a string, which is the longest common prefix found in the
|
|
51
|
+
* Trie.
|
|
52
|
+
*/
|
|
36
53
|
getLongestCommonPrefix(): string;
|
|
54
|
+
/**
|
|
55
|
+
* The `getAll` function returns an array of all words in a Trie data structure that start with a given prefix.
|
|
56
|
+
* @param [prefix] - The `prefix` parameter is a string that represents the prefix that we want to search for in the
|
|
57
|
+
* trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
|
|
58
|
+
* @returns an array of strings.
|
|
59
|
+
*/
|
|
37
60
|
getAll(prefix?: string): string[];
|
|
38
61
|
}
|