data-structure-typed 1.15.1 → 1.16.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +398 -27
- package/dist/data-structures/binary-tree/binary-tree.d.ts +30 -30
- package/dist/data-structures/binary-tree/binary-tree.js +55 -55
- package/dist/data-structures/binary-tree/segment-tree.d.ts +17 -17
- package/dist/data-structures/binary-tree/segment-tree.js +30 -30
- package/dist/data-structures/graph/abstract-graph.d.ts +6 -6
- package/dist/data-structures/graph/abstract-graph.js +6 -6
- package/dist/data-structures/graph/directed-graph.d.ts +4 -4
- package/dist/data-structures/graph/directed-graph.js +6 -6
- package/dist/data-structures/graph/undirected-graph.d.ts +3 -3
- package/dist/data-structures/hash/coordinate-map.d.ts +2 -2
- package/dist/data-structures/hash/coordinate-set.d.ts +2 -2
- package/dist/data-structures/heap/heap.d.ts +40 -25
- package/dist/data-structures/heap/heap.js +72 -27
- package/dist/data-structures/heap/max-heap.d.ts +3 -3
- package/dist/data-structures/heap/min-heap.d.ts +3 -3
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +9 -9
- package/dist/data-structures/linked-list/doubly-linked-list.js +12 -12
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +7 -7
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +4 -5
- package/dist/data-structures/priority-queue/max-priority-queue.js +30 -6
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +4 -5
- package/dist/data-structures/priority-queue/min-priority-queue.js +31 -6
- package/dist/data-structures/priority-queue/priority-queue.d.ts +20 -9
- package/dist/data-structures/priority-queue/priority-queue.js +34 -28
- package/dist/data-structures/queue/deque.d.ts +1 -1
- 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 +9 -0
- package/dist/data-structures/tree/tree.js +52 -0
- package/dist/data-structures/types/heap.d.ts +0 -4
- package/dist/utils/types/utils.d.ts +1 -3
- package/dist/utils/types/utils.js +0 -14
- package/dist/utils/utils.js +0 -197
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +92 -0
- package/docs/assets/main.js +58 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1367 -0
- package/docs/classes/AVLTree.html +2192 -0
- package/docs/classes/AVLTreeNode.html +574 -0
- package/docs/classes/AaTree.html +150 -0
- package/docs/classes/AbstractEdge.html +269 -0
- package/docs/classes/AbstractGraph.html +927 -0
- package/docs/classes/AbstractVertex.html +215 -0
- package/docs/classes/ArrayDeque.html +417 -0
- package/docs/classes/BST.html +2038 -0
- package/docs/classes/BSTNode.html +570 -0
- package/docs/classes/BTree.html +150 -0
- package/docs/classes/BinaryIndexedTree.html +289 -0
- package/docs/classes/BinaryTree.html +1827 -0
- package/docs/classes/BinaryTreeNode.html +533 -0
- package/docs/classes/Character.html +198 -0
- package/docs/classes/CoordinateMap.html +469 -0
- package/docs/classes/CoordinateSet.html +430 -0
- package/docs/classes/Deque.html +767 -0
- package/docs/classes/DirectedEdge.html +354 -0
- package/docs/classes/DirectedGraph.html +1243 -0
- package/docs/classes/DirectedVertex.html +226 -0
- package/docs/classes/DoublyLinkedList.html +733 -0
- package/docs/classes/DoublyLinkedListNode.html +258 -0
- package/docs/classes/Heap.html +482 -0
- package/docs/classes/HeapItem.html +233 -0
- package/docs/classes/Matrix2D.html +480 -0
- package/docs/classes/MatrixNTI2D.html +218 -0
- package/docs/classes/MaxHeap.html +500 -0
- package/docs/classes/MaxPriorityQueue.html +809 -0
- package/docs/classes/MinHeap.html +501 -0
- package/docs/classes/MinPriorityQueue.html +811 -0
- package/docs/classes/Navigator.html +291 -0
- package/docs/classes/ObjectDeque.html +423 -0
- package/docs/classes/PriorityQueue.html +733 -0
- package/docs/classes/Queue.html +370 -0
- package/docs/classes/RBTree.html +150 -0
- package/docs/classes/SegmentTree.html +345 -0
- package/docs/classes/SegmentTreeNode.html +418 -0
- package/docs/classes/SinglyLinkedList.html +1105 -0
- package/docs/classes/SinglyLinkedListNode.html +375 -0
- package/docs/classes/SplayTree.html +150 -0
- package/docs/classes/Stack.html +346 -0
- package/docs/classes/TreeMultiSet.html +2036 -0
- package/docs/classes/TreeNode.html +236 -0
- package/docs/classes/Trie.html +350 -0
- package/docs/classes/TrieNode.html +258 -0
- package/docs/classes/TwoThreeTree.html +150 -0
- package/docs/classes/UndirectedEdge.html +313 -0
- package/docs/classes/UndirectedGraph.html +1080 -0
- package/docs/classes/UndirectedVertex.html +226 -0
- package/docs/classes/Vector2D.html +783 -0
- package/docs/enums/CP.html +159 -0
- package/docs/enums/FamilyPosition.html +159 -0
- package/docs/enums/LoopType.html +160 -0
- package/docs/index.html +494 -0
- package/docs/interfaces/AVLTreeDeleted.html +161 -0
- package/docs/interfaces/BinaryTreeNodeObj.html +168 -0
- package/docs/interfaces/HeapOptions.html +167 -0
- package/docs/interfaces/IDirectedGraph.html +243 -0
- package/docs/interfaces/IGraph.html +427 -0
- package/docs/interfaces/NavigatorParams.html +197 -0
- package/docs/interfaces/PriorityQueueOptions.html +168 -0
- package/docs/modules.html +218 -0
- package/docs/types/BSTComparator.html +140 -0
- package/docs/types/BSTDeletedResult.html +137 -0
- package/docs/types/BinaryTreeDeleted.html +137 -0
- package/docs/types/BinaryTreeNodeId.html +125 -0
- package/docs/types/BinaryTreeNodePropertyName.html +125 -0
- package/docs/types/DFSOrderPattern.html +125 -0
- package/docs/types/DijkstraResult.html +145 -0
- package/docs/types/Direction.html +125 -0
- package/docs/types/DoublyLinkedListGetBy.html +125 -0
- package/docs/types/NodeOrPropertyName.html +125 -0
- package/docs/types/PriorityQueueComparator.html +145 -0
- package/docs/types/PriorityQueueDFSOrderPattern.html +125 -0
- package/docs/types/ResultByProperty.html +130 -0
- package/docs/types/ResultsByProperty.html +130 -0
- package/docs/types/SegmentTreeNodeVal.html +125 -0
- package/docs/types/SpecifyOptional.html +132 -0
- package/docs/types/Thunk.html +133 -0
- package/docs/types/ToThunkFn.html +133 -0
- package/docs/types/TopologicalStatus.html +125 -0
- package/docs/types/TreeMultiSetDeletedResult.html +137 -0
- package/docs/types/TrlAsyncFn.html +138 -0
- package/docs/types/TrlFn.html +138 -0
- package/docs/types/Turning.html +125 -0
- package/docs/types/VertexId.html +125 -0
- package/{tests/unit/data-structures/binary-tree → notes}/bst.test.ts +11 -15
- package/notes/note.md +23 -0
- package/package.json +2 -4
- package/.idea/data-structure-typed.iml +0 -14
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/src/assets/complexities-diff.jpg +0 -0
- package/src/assets/data-structure-complexities.jpg +0 -0
- package/src/assets/logo.png +0 -0
- package/src/data-structures/binary-tree/aa-tree.ts +0 -3
- package/src/data-structures/binary-tree/avl-tree.ts +0 -293
- package/src/data-structures/binary-tree/b-tree.ts +0 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -69
- package/src/data-structures/binary-tree/binary-tree.ts +0 -1485
- package/src/data-structures/binary-tree/bst.ts +0 -497
- 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 -248
- package/src/data-structures/binary-tree/splay-tree.ts +0 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +0 -53
- package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
- package/src/data-structures/diagrams/README.md +0 -5
- package/src/data-structures/graph/abstract-graph.ts +0 -956
- package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
- package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
- package/src/data-structures/graph/diagrams/mst.jpg +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/diagrams/tarjan.webp +0 -0
- package/src/data-structures/graph/directed-graph.ts +0 -424
- package/src/data-structures/graph/index.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +0 -254
- package/src/data-structures/hash/coordinate-map.ts +0 -71
- package/src/data-structures/hash/coordinate-set.ts +0 -60
- package/src/data-structures/hash/hash-table.ts +0 -1
- package/src/data-structures/hash/index.ts +0 -6
- package/src/data-structures/hash/pair.ts +0 -1
- package/src/data-structures/hash/tree-map.ts +0 -1
- package/src/data-structures/hash/tree-set.ts +0 -1
- package/src/data-structures/heap/heap.ts +0 -156
- package/src/data-structures/heap/index.ts +0 -3
- package/src/data-structures/heap/max-heap.ts +0 -31
- package/src/data-structures/heap/min-heap.ts +0 -34
- package/src/data-structures/index.ts +0 -13
- package/src/data-structures/linked-list/doubly-linked-list.ts +0 -351
- package/src/data-structures/linked-list/index.ts +0 -2
- package/src/data-structures/linked-list/singly-linked-list.ts +0 -748
- 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 -27
- package/src/data-structures/matrix/matrix2d.ts +0 -208
- package/src/data-structures/matrix/navigator.ts +0 -122
- package/src/data-structures/matrix/vector2d.ts +0 -316
- package/src/data-structures/priority-queue/index.ts +0 -3
- package/src/data-structures/priority-queue/max-priority-queue.ts +0 -24
- package/src/data-structures/priority-queue/min-priority-queue.ts +0 -24
- package/src/data-structures/priority-queue/priority-queue.ts +0 -346
- package/src/data-structures/queue/deque.ts +0 -241
- package/src/data-structures/queue/index.ts +0 -2
- package/src/data-structures/queue/queue.ts +0 -120
- package/src/data-structures/stack/index.ts +0 -1
- package/src/data-structures/stack/stack.ts +0 -98
- package/src/data-structures/trie/index.ts +0 -1
- package/src/data-structures/trie/trie.ts +0 -225
- 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/index.ts +0 -13
- package/src/data-structures/types/navigator.ts +0 -13
- 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 -1
- package/src/data-structures/types/tree-multiset.ts +0 -3
- package/src/index.ts +0 -1
- package/src/utils/index.ts +0 -2
- package/src/utils/types/index.ts +0 -1
- package/src/utils/types/utils.ts +0 -176
- package/src/utils/utils.ts +0 -290
- package/tests/unit/data-structures/graph/abstract-graph.ts +0 -0
- package/tests/unit/data-structures/graph/directed-graph.test.ts +0 -495
- package/tests/unit/data-structures/graph/index.ts +0 -3
- package/tests/unit/data-structures/graph/undirected-graph.ts +0 -0
|
@@ -1,24 +0,0 @@
|
|
|
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
|
-
*/
|
|
8
|
-
import {PriorityQueue} from './priority-queue';
|
|
9
|
-
import type {PriorityQueueOptions} from '../types';
|
|
10
|
-
|
|
11
|
-
export class MaxPriorityQueue<T = number> extends PriorityQueue<T> {
|
|
12
|
-
/**
|
|
13
|
-
* The constructor initializes a PriorityQueue with optional nodes and a comparator function.
|
|
14
|
-
* @param [options] - An optional object that contains the following properties:
|
|
15
|
-
*/
|
|
16
|
-
constructor(options?: PriorityQueueOptions<T>) {
|
|
17
|
-
super({
|
|
18
|
-
nodes: options?.nodes, comparator: options?.comparator ? options.comparator : (a: T, b: T) => {
|
|
19
|
-
const aKey = a as unknown as number, bKey = b as unknown as number;
|
|
20
|
-
return bKey - aKey;
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
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
|
-
*/
|
|
8
|
-
import {PriorityQueue} from './priority-queue';
|
|
9
|
-
import type {PriorityQueueOptions} from '../types';
|
|
10
|
-
|
|
11
|
-
export class MinPriorityQueue<T = number> extends PriorityQueue<T> {
|
|
12
|
-
/**
|
|
13
|
-
* The constructor initializes a PriorityQueue with optional nodes and a comparator function.
|
|
14
|
-
* @param [options] - An optional object that contains the following properties:
|
|
15
|
-
*/
|
|
16
|
-
constructor(options?: PriorityQueueOptions<T>) {
|
|
17
|
-
super({
|
|
18
|
-
nodes: options?.nodes, comparator: options?.comparator ? options.comparator : (a: T, b: T) => {
|
|
19
|
-
const aKey = a as unknown as number, bKey = b as unknown as number;
|
|
20
|
-
return aKey - bKey;
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
}
|
|
@@ -1,346 +0,0 @@
|
|
|
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
|
-
*/
|
|
8
|
-
import type {PriorityQueueComparator, PriorityQueueDFSOrderPattern, PriorityQueueOptions} from '../types';
|
|
9
|
-
|
|
10
|
-
export class PriorityQueue<T = number> {
|
|
11
|
-
|
|
12
|
-
protected _nodes: T[] = [];
|
|
13
|
-
get nodes(): T[] {
|
|
14
|
-
return this._nodes;
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
18
|
-
*/
|
|
19
|
-
getNodes(): T[] {
|
|
20
|
-
return this._nodes;
|
|
21
|
-
}
|
|
22
|
-
protected set nodes(value: T[]) {
|
|
23
|
-
this._nodes = value;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* The constructor initializes a priority queue with the given options, including an array of nodes and a comparator
|
|
28
|
-
* function.
|
|
29
|
-
* @param options - The `options` parameter is an object that contains the following properties:
|
|
30
|
-
*/
|
|
31
|
-
constructor(options: PriorityQueueOptions<T>) {
|
|
32
|
-
const {nodes, comparator, isFix = true} = options;
|
|
33
|
-
this._comparator = comparator;
|
|
34
|
-
|
|
35
|
-
if (nodes && nodes instanceof Array && nodes.length > 0) {
|
|
36
|
-
// TODO support distinct
|
|
37
|
-
this._nodes = Array.isArray(nodes) ? [...nodes] : [];
|
|
38
|
-
isFix && this._fix();
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
get size(): number {
|
|
43
|
-
return this.nodes.length;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* The `heapify` function creates a new PriorityQueue instance and fixes the heap property.
|
|
48
|
-
* @param options - The "options" parameter is an object that contains the configuration options for the PriorityQueue.
|
|
49
|
-
* It can include properties such as "comparator" which specifies the comparison function used to order the elements in
|
|
50
|
-
* the priority queue, and "initialValues" which is an array of initial values to be added to the priority
|
|
51
|
-
* @returns a new instance of the PriorityQueue class after performing the heapify operation on it.
|
|
52
|
-
*/
|
|
53
|
-
static heapify<T>(options: PriorityQueueOptions<T>) {
|
|
54
|
-
const heap = new PriorityQueue(options);
|
|
55
|
-
heap._fix();
|
|
56
|
-
return heap;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* The function checks if a priority queue is valid by creating a new priority queue with a fix option and then calling
|
|
61
|
-
* the isValid method.
|
|
62
|
-
* @param options - An object containing options for creating a priority queue. The options object should have the
|
|
63
|
-
* following properties:
|
|
64
|
-
* @returns the result of calling the `isValid()` method on a new instance of the `PriorityQueue` class.
|
|
65
|
-
*/
|
|
66
|
-
static isPriorityQueueified<T>(options: Omit<PriorityQueueOptions<T>, 'isFix'>) {
|
|
67
|
-
return new PriorityQueue({...options, isFix: true}).isValid();
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* The "add" function adds a node to the heap and ensures that the heap property is maintained.
|
|
72
|
-
* @param {T} node - The parameter "node" is of type T, which means it can be any data type. It represents the node
|
|
73
|
-
* that needs to be added to the heap.
|
|
74
|
-
*/
|
|
75
|
-
add(node: T) {
|
|
76
|
-
this.nodes.push(node);
|
|
77
|
-
this._heapifyUp(this.size - 1);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* The `peek` function returns the first element of the `nodes` array if it exists, otherwise it returns `null`.
|
|
82
|
-
* @returns The `peek()` function is returning the first element (`T`) of the `nodes` array if the `size` is not zero.
|
|
83
|
-
* Otherwise, it returns `null`.
|
|
84
|
-
*/
|
|
85
|
-
peek(): T | null {
|
|
86
|
-
return this.size ? this.nodes[0] : null;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* The `poll` function removes and returns the top element from a heap data structure.
|
|
91
|
-
* @returns The `poll()` method returns a value of type `T` or `null`.
|
|
92
|
-
*/
|
|
93
|
-
poll(): T | null {
|
|
94
|
-
let res: T | null = null;
|
|
95
|
-
if (this.size > 1) {
|
|
96
|
-
this._swap(0, this.nodes.length - 1);
|
|
97
|
-
res = this.nodes.pop() ?? null;
|
|
98
|
-
this._heapifyDown(0);
|
|
99
|
-
} else if (this.size === 1) {
|
|
100
|
-
res = this.nodes.pop() ?? null;
|
|
101
|
-
}
|
|
102
|
-
return res;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* The `leaf` function returns the last element in the `nodes` array or `null` if the array is empty.
|
|
107
|
-
* @returns The method `leaf()` is returning the last element (`T`) in the `nodes` array if it exists. If the array is
|
|
108
|
-
* empty or the last element is `null`, then it returns `null`.
|
|
109
|
-
*/
|
|
110
|
-
leaf(): T | null {
|
|
111
|
-
return this.nodes[this.size - 1] ?? null;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* The function checks if the size of an object is equal to zero and returns a boolean value indicating whether the
|
|
116
|
-
* object is empty or not.
|
|
117
|
-
* @returns The method `isEmpty()` is returning a boolean value indicating whether the size of the object is equal to
|
|
118
|
-
* 0.
|
|
119
|
-
*/
|
|
120
|
-
isEmpty() {
|
|
121
|
-
return this.size === 0;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* The clear function clears the nodes array.
|
|
126
|
-
*/
|
|
127
|
-
clear() {
|
|
128
|
-
this.nodes = [];
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* The toArray function returns an array containing all the elements in the nodes property.
|
|
133
|
-
* @returns An array of type T, which is the elements of the nodes property.
|
|
134
|
-
*/
|
|
135
|
-
toArray(): T[] {
|
|
136
|
-
return [...this.nodes];
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* The `clone` function returns a new instance of the `PriorityQueue` class with the same nodes and comparator as the
|
|
141
|
-
* original instance.
|
|
142
|
-
* @returns The `clone()` method is returning a new instance of the `PriorityQueue` class with the same `nodes` and
|
|
143
|
-
* `comparator` properties as the original instance.
|
|
144
|
-
*/
|
|
145
|
-
clone(): PriorityQueue<T> {
|
|
146
|
-
return new PriorityQueue<T>({nodes: this.nodes, comparator: this._comparator});
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// --- start additional methods ---
|
|
150
|
-
/**
|
|
151
|
-
* The `isValid` function recursively checks if a binary tree satisfies a certain condition.
|
|
152
|
-
* @returns The function `isValid()` returns a boolean value.
|
|
153
|
-
*/
|
|
154
|
-
isValid(): boolean {
|
|
155
|
-
const isValidRecursive = (parentIndex: number): boolean => {
|
|
156
|
-
let isValidLeft = true;
|
|
157
|
-
let isValidRight = true;
|
|
158
|
-
|
|
159
|
-
if (this._getLeft(parentIndex) !== -1) {
|
|
160
|
-
const leftChildIndex = (parentIndex * 2) + 1;
|
|
161
|
-
if (!this._compare(parentIndex, leftChildIndex)) return false;
|
|
162
|
-
isValidLeft = isValidRecursive(leftChildIndex);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
if (this._getRight(parentIndex) !== -1) {
|
|
166
|
-
const rightChildIndex = (parentIndex * 2) + 2;
|
|
167
|
-
if (!this._compare(parentIndex, rightChildIndex)) return false;
|
|
168
|
-
isValidRight = isValidRecursive(rightChildIndex);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
return isValidLeft && isValidRight;
|
|
172
|
-
};
|
|
173
|
-
|
|
174
|
-
return isValidRecursive(0);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* The function sorts the elements in a data structure and returns them in ascending order.
|
|
179
|
-
* @returns The `sort()` function is returning an array of type `T[]`.
|
|
180
|
-
*/
|
|
181
|
-
sort(): T[] {
|
|
182
|
-
const visitedNode: T[] = [];
|
|
183
|
-
while (this.size !== 0) {
|
|
184
|
-
const top = this.poll();
|
|
185
|
-
if (top) visitedNode.push(top);
|
|
186
|
-
}
|
|
187
|
-
return visitedNode;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* The DFS function performs a depth-first search traversal on a binary tree and returns an array of visited nodes
|
|
192
|
-
* based on the specified traversal order.
|
|
193
|
-
* @param {PriorityQueueDFSOrderPattern} dfsMode - The dfsMode parameter is a string that specifies the order in which
|
|
194
|
-
* the nodes should be visited during the Depth-First Search (DFS) traversal. It can have one of the following values:
|
|
195
|
-
* @returns an array of type `(T | null)[]`.
|
|
196
|
-
*/
|
|
197
|
-
DFS(dfsMode: PriorityQueueDFSOrderPattern): (T | null)[] {
|
|
198
|
-
const visitedNode: (T | null)[] = [];
|
|
199
|
-
|
|
200
|
-
const traverse = (cur: number) => {
|
|
201
|
-
const leftChildIndex = this._getLeft(cur);
|
|
202
|
-
const rightChildIndex = this._getRight(cur);
|
|
203
|
-
switch (dfsMode) {
|
|
204
|
-
case 'in':
|
|
205
|
-
this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
|
|
206
|
-
visitedNode.push(this.nodes[cur] ?? null);
|
|
207
|
-
this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
|
|
208
|
-
break;
|
|
209
|
-
case 'pre':
|
|
210
|
-
visitedNode.push(this.nodes[cur] ?? null);
|
|
211
|
-
this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
|
|
212
|
-
this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
|
|
213
|
-
break;
|
|
214
|
-
case 'post':
|
|
215
|
-
this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
|
|
216
|
-
this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
|
|
217
|
-
visitedNode.push(this.nodes[cur] ?? null);
|
|
218
|
-
break;
|
|
219
|
-
}
|
|
220
|
-
};
|
|
221
|
-
|
|
222
|
-
this._isValidIndex(0) && traverse(0);
|
|
223
|
-
return visitedNode;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
protected readonly _comparator: PriorityQueueComparator<T> = (a: T, b: T) => {
|
|
227
|
-
const aKey = a as unknown as number, bKey = b as unknown as number;
|
|
228
|
-
return aKey - bKey;
|
|
229
|
-
};
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* The function compares two numbers using a custom comparator function.
|
|
233
|
-
* @param {number} a - The parameter "a" is a number that represents the index of a node in an array.
|
|
234
|
-
* @param {number} b - The parameter "b" is a number.
|
|
235
|
-
* @returns the result of the comparison between the elements at indices `a` and `b` in the `nodes` array. The
|
|
236
|
-
* comparison is done using the `_comparator` function, and if the result is greater than 0, `true` is returned,
|
|
237
|
-
* indicating that the element at index `a` is greater than the element at index `b`.
|
|
238
|
-
*/
|
|
239
|
-
protected _compare(a: number, b: number) {
|
|
240
|
-
return this._comparator(this.nodes[a], this.nodes[b]) > 0;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* The function swaps two elements in an array.
|
|
245
|
-
* @param {number} a - The parameter "a" is a number that represents the index of an element in an array.
|
|
246
|
-
* @param {number} b - The parameter "b" is a number.
|
|
247
|
-
*/
|
|
248
|
-
protected _swap(a: number, b: number) {
|
|
249
|
-
const temp = this.nodes[a];
|
|
250
|
-
this.nodes[a] = this.nodes[b];
|
|
251
|
-
this.nodes[b] = temp;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
/**
|
|
255
|
-
* The function checks if a given index is valid within an array.
|
|
256
|
-
* @param {number} index - The parameter "index" is of type number and represents the index value that needs to be
|
|
257
|
-
* checked for validity.
|
|
258
|
-
* @returns A boolean value indicating whether the given index is valid or not.
|
|
259
|
-
*/
|
|
260
|
-
protected _isValidIndex(index: number): boolean {
|
|
261
|
-
return index > -1 && index < this.nodes.length;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
/**
|
|
265
|
-
* The function returns the index of the parent node given the index of a child node in a binary tree.
|
|
266
|
-
* @param {number} child - The "child" parameter is a number representing the index of a child node in a binary tree.
|
|
267
|
-
* @returns the parent of the given child node.
|
|
268
|
-
*/
|
|
269
|
-
protected _getParent(child: number): number {
|
|
270
|
-
return Math.floor((child - 1) / 2);
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
/**
|
|
274
|
-
* The function returns the index of the left child node in a binary tree given the index of its parent node.
|
|
275
|
-
* @param {number} parent - The parameter "parent" is a number that represents the index of a node in a binary tree.
|
|
276
|
-
* @returns the left child of a given parent node in a binary tree.
|
|
277
|
-
*/
|
|
278
|
-
protected _getLeft(parent: number): number {
|
|
279
|
-
return (2 * parent) + 1;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
/**
|
|
283
|
-
* The function returns the index of the right child node in a binary tree given the index of its parent node.
|
|
284
|
-
* @param {number} parent - The parameter "parent" is a number that represents the index of a node in a binary tree.
|
|
285
|
-
* @returns the right child of a given parent node in a binary tree.
|
|
286
|
-
*/
|
|
287
|
-
protected _getRight(parent: number): number {
|
|
288
|
-
return (2 * parent) + 2;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
/**
|
|
292
|
-
* The function returns the index of the smallest child node of a given parent node.
|
|
293
|
-
* @param {number} parent - The parent parameter is a number that represents the index of the parent node in a binary
|
|
294
|
-
* tree.
|
|
295
|
-
* @returns the minimum value between the parent node and its left and right child nodes.
|
|
296
|
-
*/
|
|
297
|
-
protected _getComparedChild(parent: number) {
|
|
298
|
-
let min = parent;
|
|
299
|
-
const left = this._getLeft(parent), right = this._getRight(parent);
|
|
300
|
-
|
|
301
|
-
if (left < this.size && this._compare(min, left)) {
|
|
302
|
-
min = left;
|
|
303
|
-
}
|
|
304
|
-
if (right < this.size && this._compare(min, right)) {
|
|
305
|
-
min = right;
|
|
306
|
-
}
|
|
307
|
-
return min;
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
/**
|
|
311
|
-
* The function `_heapifyUp` is used to maintain the heap property by moving an element up the heap until it is in the
|
|
312
|
-
* correct position.
|
|
313
|
-
* @param {number} start - The start parameter is the index of the element that needs to be moved up in the heap.
|
|
314
|
-
*/
|
|
315
|
-
protected _heapifyUp(start: number) {
|
|
316
|
-
while (start > 0 && this._compare(this._getParent(start), start)) {
|
|
317
|
-
const parent = this._getParent(start);
|
|
318
|
-
this._swap(start, parent);
|
|
319
|
-
start = parent;
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
/**
|
|
324
|
-
* The function performs a heapify operation by comparing and swapping elements in a binary heap.
|
|
325
|
-
* @param {number} start - The start parameter is the index of the element in the heap from where the heapifyDown
|
|
326
|
-
* operation should start.
|
|
327
|
-
*/
|
|
328
|
-
protected _heapifyDown(start: number) {
|
|
329
|
-
let min = this._getComparedChild(start);
|
|
330
|
-
while (this._compare(start, min)) {
|
|
331
|
-
this._swap(min, start);
|
|
332
|
-
start = min;
|
|
333
|
-
min = this._getComparedChild(start);
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
/**
|
|
338
|
-
* The _fix function performs a heapify operation on the elements of the heap starting from the middle and moving
|
|
339
|
-
* towards the root.
|
|
340
|
-
*/
|
|
341
|
-
protected _fix() {
|
|
342
|
-
for (let i = Math.floor(this.size / 2); i > -1; i--) this._heapifyDown(i);
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
// --- end additional methods ---
|
|
346
|
-
}
|
|
@@ -1,241 +0,0 @@
|
|
|
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
|
-
*/
|
|
8
|
-
import {DoublyLinkedList} from '../linked-list';
|
|
9
|
-
|
|
10
|
-
// O(n) time complexity of obtaining the value
|
|
11
|
-
// O(1) time complexity of adding at the beginning and the end
|
|
12
|
-
export class Deque<T> extends DoublyLinkedList<T> {
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
// O(1) time complexity of obtaining the value
|
|
17
|
-
// O(n) time complexity of adding at the beginning and the end
|
|
18
|
-
// todo tested slowest one
|
|
19
|
-
export class ObjectDeque<T> {
|
|
20
|
-
private _nodes: { [key: number]: T } = {};
|
|
21
|
-
get nodes(): { [p: number]: T } {
|
|
22
|
-
return this._nodes;
|
|
23
|
-
}
|
|
24
|
-
protected set nodes(value: { [p: number]: T }) {
|
|
25
|
-
this._nodes = value;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
private _capacity = Number.MAX_SAFE_INTEGER;
|
|
29
|
-
get capacity(): number {
|
|
30
|
-
return this._capacity;
|
|
31
|
-
}
|
|
32
|
-
set capacity(value: number) {
|
|
33
|
-
this._capacity = value;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
private _first: number = -1;
|
|
37
|
-
get first(): number {
|
|
38
|
-
return this._first;
|
|
39
|
-
}
|
|
40
|
-
set first(value: number) {
|
|
41
|
-
this._first = value;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
private _last: number = -1;
|
|
45
|
-
get last(): number {
|
|
46
|
-
return this._last;
|
|
47
|
-
}
|
|
48
|
-
set last(value: number) {
|
|
49
|
-
this._last = value;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
private _size: number = 0;
|
|
53
|
-
get size(): number {
|
|
54
|
-
return this._size;
|
|
55
|
-
}
|
|
56
|
-
protected set size(value: number) {
|
|
57
|
-
this._size = value;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
constructor(capacity?: number) {
|
|
61
|
-
if (capacity !== undefined) this._capacity = capacity;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
addFirst(value: T) {
|
|
65
|
-
if (this._size === 0) {
|
|
66
|
-
const mid = Math.floor(this._capacity / 2);
|
|
67
|
-
this._first = mid;
|
|
68
|
-
this._last = mid;
|
|
69
|
-
} else {
|
|
70
|
-
this._first--;
|
|
71
|
-
}
|
|
72
|
-
this._nodes[this._first] = value;
|
|
73
|
-
this._size++;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
addLast(value: T) {
|
|
77
|
-
if (this._size === 0) {
|
|
78
|
-
const mid = Math.floor(this._capacity / 2);
|
|
79
|
-
this._first = mid;
|
|
80
|
-
this._last = mid;
|
|
81
|
-
} else {
|
|
82
|
-
this._last++;
|
|
83
|
-
}
|
|
84
|
-
this._nodes[this._last] = value;
|
|
85
|
-
this._size++;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
pollFirst() {
|
|
89
|
-
if (!this._size) return;
|
|
90
|
-
const value = this.peekFirst();
|
|
91
|
-
delete this._nodes[this._first];
|
|
92
|
-
this._first++;
|
|
93
|
-
this._size--;
|
|
94
|
-
return value;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
peekFirst() {
|
|
98
|
-
if (this._size) return this._nodes[this._first];
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
pollLast() {
|
|
102
|
-
if (!this._size) return;
|
|
103
|
-
const value = this.peekLast();
|
|
104
|
-
delete this._nodes[this._last];
|
|
105
|
-
this._last--;
|
|
106
|
-
this._size--;
|
|
107
|
-
|
|
108
|
-
return value;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
peekLast() {
|
|
112
|
-
if (this._size) return this._nodes[this._last];
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
get(index: number) {
|
|
116
|
-
return this._nodes[this._first + index] || null;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
isEmpty() {
|
|
120
|
-
return this._size <= 0;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// O(1) time complexity of obtaining the value
|
|
125
|
-
// O(n) time complexity of adding at the beginning and the end
|
|
126
|
-
export class ArrayDeque<T> {
|
|
127
|
-
protected _nodes: T[] = [];
|
|
128
|
-
|
|
129
|
-
get size() {
|
|
130
|
-
return this._nodes.length;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* The function "addLast" adds a value to the end of an array.
|
|
135
|
-
* @param {T} value - The value parameter represents the value that you want to add to the end of the array.
|
|
136
|
-
* @returns The return value is the new length of the array after the value has been added.
|
|
137
|
-
*/
|
|
138
|
-
addLast(value: T) {
|
|
139
|
-
return this._nodes.push(value);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* The function "pollLast" returns and removes the last element from an array, or returns null if the array is empty.
|
|
144
|
-
* @returns The method `pollLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
145
|
-
*/
|
|
146
|
-
pollLast(): T | null {
|
|
147
|
-
return this._nodes.pop() ?? null;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* The `pollFirst` function removes and returns the first element from an array, or returns null if the array is empty.
|
|
152
|
-
* @returns The `pollFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
|
|
153
|
-
* empty.
|
|
154
|
-
*/
|
|
155
|
-
pollFirst(): T | null {
|
|
156
|
-
return this._nodes.shift() ?? null;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* The function "addFirst" adds a value to the beginning of an array.
|
|
161
|
-
* @param {T} value - The value parameter represents the value that you want to add to the beginning of the array.
|
|
162
|
-
* @returns The return value of the `addFirst` function is the new length of the array `_nodes` after adding the
|
|
163
|
-
* `value` at the beginning.
|
|
164
|
-
*/
|
|
165
|
-
addFirst(value: T) {
|
|
166
|
-
return this._nodes.unshift(value);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* The `peekFirst` function returns the first element of an array or null if the array is empty.
|
|
171
|
-
* @returns The function `peekFirst()` is returning the first element (`T`) of the `_nodes` array. If the array is
|
|
172
|
-
* empty, it will return `null`.
|
|
173
|
-
*/
|
|
174
|
-
peekFirst(): T | null {
|
|
175
|
-
return this._nodes[0] ?? null;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* The `peekLast` function returns the last element of an array or null if the array is empty.
|
|
180
|
-
* @returns The method `peekLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
181
|
-
*/
|
|
182
|
-
peekLast(): T | null {
|
|
183
|
-
return this._nodes[this._nodes.length - 1] ?? null;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* The get function returns the element at the specified index in an array, or null if the index is out of bounds.
|
|
188
|
-
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
189
|
-
* retrieve from the array.
|
|
190
|
-
* @returns The method is returning the element at the specified index in the `_nodes` array. If the element exists, it
|
|
191
|
-
* will be returned. If the element does not exist (i.e., the index is out of bounds), `null` will be returned.
|
|
192
|
-
*/
|
|
193
|
-
get(index: number): T | null {
|
|
194
|
-
return this._nodes[index] ?? null;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* The set function assigns a value to a specific index in an array.
|
|
199
|
-
* @param {number} index - The index parameter is a number that represents the position of the element in the array
|
|
200
|
-
* that you want to set a new value for.
|
|
201
|
-
* @param {T} value - The value parameter represents the new value that you want to set at the specified index in the
|
|
202
|
-
* _nodes array.
|
|
203
|
-
* @returns The value that is being set at the specified index in the `_nodes` array.
|
|
204
|
-
*/
|
|
205
|
-
set(index: number, value: T) {
|
|
206
|
-
return this._nodes[index] = value;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* The insert function adds a value at a specified index in an array.
|
|
211
|
-
* @param {number} index - The index parameter specifies the position at which the value should be inserted in the
|
|
212
|
-
* array. It is a number that represents the index of the array where the value should be inserted. The index starts
|
|
213
|
-
* from 0, so the first element of the array has an index of 0, the second element has
|
|
214
|
-
* @param {T} value - The value parameter represents the value that you want to insert into the array at the specified
|
|
215
|
-
* index.
|
|
216
|
-
* @returns The splice method returns an array containing the removed elements, if any. In this case, since no elements
|
|
217
|
-
* are being removed, an empty array will be returned.
|
|
218
|
-
*/
|
|
219
|
-
insert(index: number, value: T) {
|
|
220
|
-
return this._nodes.splice(index, 0, value);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
/**
|
|
224
|
-
* The remove function removes an element from an array at a specified index.
|
|
225
|
-
* @param {number} index - The index parameter specifies the position of the element to be removed from the array. It
|
|
226
|
-
* is a number that represents the index of the element to be removed.
|
|
227
|
-
* @returns The method is returning an array containing the removed element.
|
|
228
|
-
*/
|
|
229
|
-
remove(index: number) {
|
|
230
|
-
return this._nodes.splice(index, 1);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* The function checks if an array called "_nodes" is empty.
|
|
235
|
-
* @returns The method `isEmpty()` is returning a boolean value. It returns `true` if the length of the `_nodes` array
|
|
236
|
-
* is 0, indicating that the array is empty. Otherwise, it returns `false`.
|
|
237
|
-
*/
|
|
238
|
-
isEmpty() {
|
|
239
|
-
return this._nodes.length === 0;
|
|
240
|
-
}
|
|
241
|
-
}
|