data-structure-typed 1.33.2 → 1.33.6

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.
Files changed (148) hide show
  1. package/{.eslintrc.json → .eslintrc.js} +2 -1
  2. package/.github/workflows/ci.yml +15 -3
  3. package/.github/workflows/release-package.yml +32 -0
  4. package/{.prettierrc → .prettierrc.js} +1 -1
  5. package/CHANGELOG.md +5 -1
  6. package/README.md +2 -3
  7. package/coverage/coverage-final.json +64 -64
  8. package/coverage/coverage-summary.json +2 -2
  9. package/dist/data-structures/graph/abstract-graph.js +12 -12
  10. package/dist/data-structures/graph/abstract-graph.js.map +1 -1
  11. package/dist/data-structures/priority-queue/priority-queue.js +6 -6
  12. package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
  13. package/docs/index.html +2 -3
  14. package/package.json +68 -60
  15. package/src/data-structures/binary-tree/aa-tree.ts +1 -0
  16. package/src/data-structures/binary-tree/abstract-binary-tree.ts +1608 -0
  17. package/src/data-structures/binary-tree/avl-tree.ts +307 -0
  18. package/src/data-structures/binary-tree/b-tree.ts +1 -0
  19. package/src/data-structures/binary-tree/binary-indexed-tree.ts +76 -0
  20. package/src/data-structures/binary-tree/binary-tree.ts +47 -0
  21. package/src/data-structures/binary-tree/bst.ts +537 -0
  22. package/src/data-structures/binary-tree/index.ts +12 -0
  23. package/src/data-structures/binary-tree/rb-tree.ts +366 -0
  24. package/src/data-structures/binary-tree/segment-tree.ts +242 -0
  25. package/src/data-structures/binary-tree/splay-tree.ts +1 -0
  26. package/src/data-structures/binary-tree/tree-multiset.ts +700 -0
  27. package/src/data-structures/binary-tree/two-three-tree.ts +1 -0
  28. package/src/data-structures/graph/abstract-graph.ts +1040 -0
  29. package/src/data-structures/graph/directed-graph.ts +470 -0
  30. package/src/data-structures/graph/index.ts +4 -0
  31. package/src/data-structures/graph/map-graph.ts +129 -0
  32. package/src/data-structures/graph/undirected-graph.ts +274 -0
  33. package/src/data-structures/hash/coordinate-map.ts +67 -0
  34. package/src/data-structures/hash/coordinate-set.ts +56 -0
  35. package/src/data-structures/hash/hash-table.ts +157 -0
  36. package/src/data-structures/hash/index.ts +6 -0
  37. package/src/data-structures/hash/pair.ts +1 -0
  38. package/src/data-structures/hash/tree-map.ts +1 -0
  39. package/src/data-structures/hash/tree-set.ts +1 -0
  40. package/src/data-structures/heap/heap.ts +212 -0
  41. package/src/data-structures/heap/index.ts +3 -0
  42. package/src/data-structures/heap/max-heap.ts +31 -0
  43. package/src/data-structures/heap/min-heap.ts +32 -0
  44. package/src/data-structures/index.ts +11 -0
  45. package/src/data-structures/linked-list/doubly-linked-list.ts +636 -0
  46. package/src/data-structures/linked-list/index.ts +3 -0
  47. package/src/data-structures/linked-list/singly-linked-list.ts +501 -0
  48. package/src/data-structures/linked-list/skip-linked-list.ts +1 -0
  49. package/src/data-structures/matrix/index.ts +4 -0
  50. package/src/data-structures/matrix/matrix.ts +27 -0
  51. package/src/data-structures/matrix/matrix2d.ts +213 -0
  52. package/src/data-structures/matrix/navigator.ts +121 -0
  53. package/src/data-structures/matrix/vector2d.ts +316 -0
  54. package/src/data-structures/priority-queue/index.ts +3 -0
  55. package/src/data-structures/priority-queue/max-priority-queue.ts +56 -0
  56. package/src/data-structures/priority-queue/min-priority-queue.ts +57 -0
  57. package/src/data-structures/priority-queue/priority-queue.ts +359 -0
  58. package/src/data-structures/queue/deque.ts +297 -0
  59. package/src/data-structures/queue/index.ts +2 -0
  60. package/src/data-structures/queue/queue.ts +191 -0
  61. package/src/data-structures/stack/index.ts +1 -0
  62. package/src/data-structures/stack/stack.ts +98 -0
  63. package/src/data-structures/tree/index.ts +1 -0
  64. package/src/data-structures/tree/tree.ts +69 -0
  65. package/src/data-structures/trie/index.ts +1 -0
  66. package/src/data-structures/trie/trie.ts +225 -0
  67. package/src/index.ts +4 -0
  68. package/src/interfaces/abstract-binary-tree.ts +189 -0
  69. package/src/interfaces/abstract-graph.ts +31 -0
  70. package/src/interfaces/avl-tree.ts +25 -0
  71. package/src/interfaces/binary-tree.ts +6 -0
  72. package/src/interfaces/bst.ts +31 -0
  73. package/src/interfaces/directed-graph.ts +20 -0
  74. package/src/interfaces/doubly-linked-list.ts +1 -0
  75. package/src/interfaces/heap.ts +1 -0
  76. package/src/interfaces/index.ts +15 -0
  77. package/src/interfaces/navigator.ts +1 -0
  78. package/src/interfaces/priority-queue.ts +1 -0
  79. package/src/interfaces/rb-tree.ts +9 -0
  80. package/src/interfaces/segment-tree.ts +1 -0
  81. package/src/interfaces/singly-linked-list.ts +1 -0
  82. package/src/interfaces/tree-multiset.ts +7 -0
  83. package/src/interfaces/undirected-graph.ts +6 -0
  84. package/src/types/data-structures/abstract-binary-tree.ts +50 -0
  85. package/src/types/data-structures/abstract-graph.ts +11 -0
  86. package/src/types/data-structures/avl-tree.ts +5 -0
  87. package/src/types/data-structures/binary-tree.ts +5 -0
  88. package/src/types/data-structures/bst.ts +13 -0
  89. package/src/types/data-structures/directed-graph.ts +8 -0
  90. package/src/types/data-structures/doubly-linked-list.ts +1 -0
  91. package/src/types/data-structures/heap.ts +5 -0
  92. package/src/types/data-structures/index.ts +15 -0
  93. package/src/types/data-structures/map-graph.ts +1 -0
  94. package/src/types/data-structures/navigator.ts +13 -0
  95. package/src/types/data-structures/priority-queue.ts +9 -0
  96. package/src/types/data-structures/rb-tree.ts +8 -0
  97. package/src/types/data-structures/segment-tree.ts +1 -0
  98. package/src/types/data-structures/singly-linked-list.ts +1 -0
  99. package/src/types/data-structures/tree-multiset.ts +6 -0
  100. package/src/types/helpers.ts +1 -0
  101. package/src/types/index.ts +3 -0
  102. package/src/types/utils/index.ts +2 -0
  103. package/src/types/utils/utils.ts +6 -0
  104. package/src/types/utils/validate-type.ts +35 -0
  105. package/src/utils/index.ts +1 -0
  106. package/src/utils/utils.ts +79 -0
  107. package/test/integration/avl-tree.test.ts +14 -17
  108. package/test/integration/bst.test.ts +50 -41
  109. package/test/integration/heap.test.js +0 -3
  110. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +14 -17
  111. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +0 -4
  112. package/test/unit/data-structures/binary-tree/bst.test.ts +50 -41
  113. package/test/unit/data-structures/binary-tree/overall.test.ts +36 -28
  114. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +0 -1
  115. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +23 -12
  116. package/test/unit/data-structures/graph/directed-graph.test.ts +27 -25
  117. package/test/unit/data-structures/graph/map-graph.test.ts +4 -5
  118. package/test/unit/data-structures/graph/overall.test.ts +10 -11
  119. package/test/unit/data-structures/graph/undirected-graph.test.ts +0 -1
  120. package/test/unit/data-structures/heap/heap.test.ts +7 -8
  121. package/test/unit/data-structures/heap/max-heap.test.ts +7 -5
  122. package/test/unit/data-structures/heap/min-heap.test.ts +6 -5
  123. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +9 -10
  124. package/test/unit/data-structures/linked-list/linked-list.test.ts +1 -1
  125. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +4 -7
  126. package/test/unit/data-structures/linked-list/skip-linked-list.test.ts +3 -3
  127. package/test/unit/data-structures/matrix/matrix.test.ts +4 -4
  128. package/test/unit/data-structures/matrix/navigator.test.ts +4 -5
  129. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +5 -7
  130. package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +13 -13
  131. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +8 -8
  132. package/test/unit/data-structures/queue/deque.test.ts +0 -1
  133. package/test/unit/data-structures/queue/queue.test.ts +1 -5
  134. package/test/unit/data-structures/trie/trie.test.ts +2 -2
  135. package/test/utils/magnitude.ts +3 -3
  136. package/tsconfig.json +3 -12
  137. package/tsconfig.prod.json +25 -0
  138. package/.auto-changelog +0 -9
  139. package/.gitattributes +0 -112
  140. package/.idea/codeStyles/Project.xml +0 -61
  141. package/.idea/codeStyles/codeStyleConfig.xml +0 -5
  142. package/.idea/data-structure-typed.iml +0 -19
  143. package/.idea/inspectionProfiles/Project_Default.xml +0 -6
  144. package/.idea/misc.xml +0 -6
  145. package/.idea/modules.xml +0 -8
  146. package/.idea/vcs.xml +0 -6
  147. package/.prettierignore +0 -6
  148. package/webpack.config.js +0 -28
@@ -0,0 +1,212 @@
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 {HeapOptions} from '../../types';
10
+
11
+ export class HeapItem<V = any> {
12
+ /**
13
+ * The constructor function initializes an instance of a class with a priority and a value.
14
+ * @param {number} priority - The `priority` parameter is a number that represents the priority of the value. It is
15
+ * optional and has a default value of `NaN`.
16
+ * @param {V | null} [val=null] - The `val` parameter is of type `V | null`, which means it can accept a value of type
17
+ * `V` or `null`.
18
+ */
19
+ constructor(priority: number = Number.MAX_SAFE_INTEGER, val: V | null = null) {
20
+ this._val = val;
21
+ this._priority = priority;
22
+ }
23
+
24
+ private _priority: number;
25
+
26
+ get priority(): number {
27
+ return this._priority;
28
+ }
29
+
30
+ set priority(value: number) {
31
+ this._priority = value;
32
+ }
33
+
34
+ private _val: V | null;
35
+
36
+ get val(): V | null {
37
+ return this._val;
38
+ }
39
+
40
+ set val(value: V | null) {
41
+ this._val = value;
42
+ }
43
+ }
44
+
45
+ export abstract class Heap<V = number> {
46
+ /**
47
+ * The function is a constructor for a class that initializes a priority callback function based on the
48
+ * options provided.
49
+ * @param [options] - An optional object that contains configuration options for the Heap.
50
+ */
51
+ protected constructor(options?: HeapOptions<V>) {
52
+ if (options) {
53
+ const {priorityExtractor} = options;
54
+ if (priorityExtractor !== undefined && typeof priorityExtractor !== 'function') {
55
+ throw new Error('.constructor expects a valid priority function');
56
+ }
57
+ this._priorityExtractor = priorityExtractor || (el => +el);
58
+ } else {
59
+ this._priorityExtractor = el => +el;
60
+ }
61
+ }
62
+
63
+ protected abstract _pq: PriorityQueue<HeapItem<V>>;
64
+
65
+ get pq() {
66
+ return this._pq;
67
+ }
68
+
69
+ protected _priorityExtractor: (val: V) => number;
70
+ get priorityExtractor() {
71
+ return this._priorityExtractor;
72
+ }
73
+
74
+ /**
75
+ * The function returns the size of a priority queue.
76
+ * @returns The size of the priority queue.
77
+ */
78
+ get size(): number {
79
+ return this._pq.size;
80
+ }
81
+
82
+ /**
83
+ * The function checks if a priority queue is empty.
84
+ * @returns {boolean} A boolean value indicating whether the size of the priority queue is less than 1.
85
+ */
86
+ isEmpty(): boolean {
87
+ return this._pq.size < 1;
88
+ }
89
+
90
+ peek(isItem?: undefined): V | undefined;
91
+ peek(isItem: false): V | undefined;
92
+ peek(isItem: true): HeapItem<V> | null;
93
+
94
+ /**
95
+ * The `peek` function returns the top item in the priority queue without removing it.
96
+ * @returns The `peek()` method is returning either a `HeapItem<V>` object or `null`.Returns an val with the highest priority in the queue
97
+ */
98
+ peek(isItem?: boolean): HeapItem<V> | null | V | undefined {
99
+ isItem = isItem ?? false;
100
+ const peeked = this._pq.peek();
101
+
102
+ return isItem ? peeked : peeked?.val;
103
+ }
104
+
105
+ peekLast(isItem?: undefined): V | undefined;
106
+ peekLast(isItem: false): V | undefined;
107
+ peekLast(isItem: true): HeapItem<V> | null;
108
+
109
+ /**
110
+ * The `peekLast` function returns the last item in the heap.
111
+ * @returns The method `peekLast()` returns either a `HeapItem<V>` object or `null`.Returns an val with the lowest priority in the queue
112
+ */
113
+ peekLast(isItem?: boolean): HeapItem<V> | null | V | undefined {
114
+ isItem = isItem ?? false;
115
+ const leafItem = this._pq.leaf();
116
+
117
+ return isItem ? leafItem : leafItem?.val;
118
+ }
119
+
120
+ /**
121
+ * The `add` function adds an val to a priority queue with an optional priority value.
122
+ * @param {V} val - The `val` parameter represents the value that you want to add to the heap. It can be of any
123
+ * type.
124
+ * @param {number} [priority] - The `priority` parameter is an optional number that represents the priority of the
125
+ * val being added to the heap. If the `val` parameter is a number, then the `priority` parameter is set to
126
+ * the value of `val`. If the `val` parameter is not a number, then the
127
+ * @returns The `add` method returns the instance of the `Heap` class.
128
+ * @throws {Error} if priority is not a valid number
129
+ */
130
+ add(priority: number, val?: V): Heap<V> {
131
+ val = val === undefined ? (priority as unknown as V) : val;
132
+ this._pq.add(new HeapItem<V>(priority, val));
133
+
134
+ return this;
135
+ }
136
+
137
+ poll(isItem?: undefined): V | undefined;
138
+ poll(isItem: false): V | undefined;
139
+ poll(isItem: true): HeapItem<V> | null;
140
+
141
+ /**
142
+ * The `poll` function returns the top item from a priority queue or null if the queue is empty.Removes and returns an val with the highest priority in the queue
143
+ * @returns either a HeapItem<V> object or null.
144
+ */
145
+ poll(isItem?: boolean): HeapItem<V> | null | V | undefined {
146
+ isItem = isItem ?? false;
147
+ const top = this._pq.poll();
148
+ if (!top) {
149
+ return null;
150
+ }
151
+
152
+ return isItem ? top : top.val;
153
+ }
154
+
155
+ /**
156
+ * The function checks if a given node or value exists in the priority queue.
157
+ * @param {V | HeapItem<V>} node - The parameter `node` can be of type `V` or `HeapItem<V>`.
158
+ * @returns a boolean value.
159
+ */
160
+ has(node: V | HeapItem<V>): boolean {
161
+ if (node instanceof HeapItem) {
162
+ return this.pq.getNodes().includes(node);
163
+ } else {
164
+ return (
165
+ this.pq.getNodes().findIndex(item => {
166
+ return item.val === node;
167
+ }) !== -1
168
+ );
169
+ }
170
+ }
171
+
172
+ toArray(isItem?: undefined): (V | undefined)[];
173
+ toArray(isItem: false): (V | undefined)[];
174
+ toArray(isItem: true): (HeapItem<V> | null)[];
175
+
176
+ /**
177
+ * The `toArray` function returns an array of `HeapItem<V>` objects.
178
+ * @returns An array of HeapItem<V> objects.Returns a sorted list of vals
179
+ */
180
+ toArray(isItem?: boolean): (HeapItem<V> | null | V | undefined)[] {
181
+ isItem = isItem ?? false;
182
+ const itemArray = this._pq.toArray();
183
+
184
+ return isItem ? itemArray : itemArray.map(item => item.val);
185
+ }
186
+
187
+ sort(isItem?: undefined): (V | undefined)[];
188
+ sort(isItem: false): (V | undefined)[];
189
+ sort(isItem: true): (HeapItem<V> | null)[];
190
+
191
+ /**
192
+ * The function sorts the elements in the priority queue and returns either the sorted items or their values depending
193
+ * on the value of the isItem parameter.
194
+ * @param {boolean} [isItem] - The `isItem` parameter is a boolean flag that indicates whether the sorted result should
195
+ * be an array of `HeapItem<V>` objects or an array of the values (`V`) of those objects. If `isItem` is `true`, the
196
+ * sorted result will be an array of `HeapItem
197
+ * @returns an array of either `HeapItem<V>`, `null`, `V`, or `undefined` values.
198
+ */
199
+ sort(isItem?: boolean): (HeapItem<V> | null | V | undefined)[] {
200
+ isItem = isItem ?? false;
201
+ const sorted = this._pq.sort();
202
+
203
+ return isItem ? sorted : sorted.map(item => item.val);
204
+ }
205
+
206
+ /**
207
+ * The clear function clears the priority queue.
208
+ */
209
+ clear(): void {
210
+ this._pq.clear();
211
+ }
212
+ }
@@ -0,0 +1,3 @@
1
+ export * from './max-heap';
2
+ export * from './min-heap';
3
+ export * from './heap';
@@ -0,0 +1,31 @@
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
+
9
+ import {Heap, HeapItem} from './heap';
10
+ import {PriorityQueue} from '../priority-queue';
11
+ import type {HeapOptions} from '../../types';
12
+
13
+ /**
14
+ * @class MaxHeap
15
+ * @extends Heap
16
+ */
17
+ export class MaxHeap<V = any> extends Heap<V> {
18
+ protected _pq: PriorityQueue<HeapItem<V>>;
19
+
20
+ /**
21
+ * The constructor initializes a PriorityQueue with a custom comparator function.
22
+ * @param [options] - The `options` parameter is an optional object that can be passed to the constructor. It is of
23
+ * type `HeapOptions<V>`, which is a generic type that represents the options for the heap.
24
+ */
25
+ constructor(options?: HeapOptions<V>) {
26
+ super(options);
27
+ this._pq = new PriorityQueue<HeapItem<V>>({
28
+ comparator: (a, b) => b.priority - a.priority
29
+ });
30
+ }
31
+ }
@@ -0,0 +1,32 @@
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
+
9
+ import {Heap, HeapItem} from './heap';
10
+ import {PriorityQueue} from '../priority-queue';
11
+ import type {HeapOptions} from '../../types';
12
+
13
+ /**
14
+ * @class MinHeap
15
+ * @extends Heap
16
+ */
17
+ export class MinHeap<V = any> extends Heap<V> {
18
+ protected _pq: PriorityQueue<HeapItem<V>>;
19
+
20
+ /**
21
+ * The constructor initializes a PriorityQueue with a comparator function that compares the priority of two HeapItem
22
+ * objects.
23
+ * @param [options] - The `options` parameter is an optional object that can be passed to the constructor. It is of
24
+ * type `HeapOptions<V>`, which is a generic type that represents the options for the heap.
25
+ */
26
+ constructor(options?: HeapOptions<V>) {
27
+ super(options);
28
+ this._pq = new PriorityQueue<HeapItem<V>>({
29
+ comparator: (a, b) => a.priority - b.priority
30
+ });
31
+ }
32
+ }
@@ -0,0 +1,11 @@
1
+ export * from './hash';
2
+ export * from './linked-list';
3
+ export * from './stack';
4
+ export * from './queue';
5
+ export * from './graph';
6
+ export * from './binary-tree';
7
+ export * from './tree';
8
+ export * from './heap';
9
+ export * from './priority-queue';
10
+ export * from './matrix';
11
+ export * from './trie';