heap-typed 1.19.3 → 1.19.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.
package/README.md CHANGED
@@ -1,191 +1,139 @@
1
1
  # What
2
2
 
3
3
  ## Brief
4
- Javascript & TypeScript Data Structure Library.
5
4
 
6
- Binary Tree, Binary Search Tree (BST), AVL Tree, Tree Multiset, Segment Tree, Binary Indexed Tree, Graph, Directed Graph, Undirected Graph, Linked List, Singly Linked List, Doubly Linked List, Queue, Object Deque, Array Deque, Stack, Hash, Coordinate Set, Coordinate Map, Heap, Priority Queue, Max Priority Queue, Min Priority Queue, Trie
7
-
8
- ## Algorithms list only a few out, you can discover more in API docs
9
-
10
- DFS, DFSIterative, BFS, morris, Bellman-Ford Algorithm, Dijkstra's Algorithm, Floyd-Warshall Algorithm, Tarjan's Algorithm
11
-
12
- ## Code design
13
- By strictly adhering to object-oriented design (BinaryTree -> BST -> AVLTree -> TreeMultiset), you can seamlessly inherit the existing data structures to implement the customized ones you need. Object-oriented design stands as the optimal approach to data structure design.
5
+ This is a standalone Heap data structure from the data-structure-typed collection. If you wish to access more data
6
+ structures or advanced features, you can transition to directly installing the
7
+ complete [data-structure-typed](https://www.npmjs.com/package/data-structure-typed) package
14
8
 
15
9
  # How
16
10
 
17
11
  ## install
18
- ### yarn
12
+
13
+ ### npm
19
14
 
20
15
  ```bash
21
- yarn add data-structure-typed
16
+ npm i heap-typed
22
17
  ```
23
18
 
24
- ### npm
19
+ ### yarn
25
20
 
26
21
  ```bash
27
- npm install data-structure-typed
22
+ yarn add heap-typed
28
23
  ```
29
24
 
30
- ### Binary Search Tree (BST) snippet
25
+ ### snippet
31
26
 
32
27
  #### TS
33
- ```typescript
34
- import {BST, BSTNode} from 'data-structure-typed';
35
-
36
- const bst = new BST();
37
- bst.add(11);
38
- bst.add(3);
39
- bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
40
- bst.size === 16; // true
41
- bst.has(6); // true
42
- const node6 = bst.get(6);
43
- bst.getHeight(6) === 2; // true
44
- bst.getHeight() === 5; // true
45
- bst.getDepth(6) === 3; // true
46
- const leftMost = bst.getLeftMost();
47
- leftMost?.id === 1; // true
48
- expect(leftMost?.id).toBe(1);
49
- bst.remove(6);
50
- bst.get(6); // null
51
- bst.isAVLBalanced(); // true or false
52
- const bfsIDs = bst.BFS();
53
- bfsIDs[0] === 11; // true
54
- expect(bfsIDs[0]).toBe(11);
55
-
56
- const objBST = new BST<BSTNode<{ id: number, keyA: number }>>();
57
- objBST.add(11, {id: 11, keyA: 11});
58
- objBST.add(3, {id: 3, keyA: 3});
59
-
60
- objBST.addMany([{id: 15, keyA: 15}, {id: 1, keyA: 1}, {id: 8, keyA: 8},
61
- {id: 13, keyA: 13}, {id: 16, keyA: 16}, {id: 2, keyA: 2},
62
- {id: 6, keyA: 6}, {id: 9, keyA: 9}, {id: 12, keyA: 12},
63
- {id: 14, keyA: 14}, {id: 4, keyA: 4}, {id: 7, keyA: 7},
64
- {id: 10, keyA: 10}, {id: 5, keyA: 5}]);
65
-
66
- objBST.remove(11);
67
-
68
-
69
- const avlTree = new AVLTree();
70
- avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
71
- avlTree.isAVLBalanced(); // true
72
- avlTree.remove(10);
73
- avlTree.isAVLBalanced(); // true
74
28
 
75
- ```
76
- #### JS
77
- ```javascript
78
- const {BST, BSTNode} = require('data-structure-typed');
79
-
80
- const bst = new BST();
81
- bst.add(11);
82
- bst.add(3);
83
- bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
84
- bst.size === 16; // true
85
- bst.has(6); // true
86
- const node6 = bst.get(6);
87
- bst.getHeight(6) === 2; // true
88
- bst.getHeight() === 5; // true
89
- bst.getDepth(6) === 3; // true
90
- const leftMost = bst.getLeftMost();
91
- leftMost?.id === 1; // true
92
- expect(leftMost?.id).toBe(1);
93
- bst.remove(6);
94
- bst.get(6); // null
95
- bst.isAVLBalanced(); // true or false
96
- const bfsIDs = bst.BFS();
97
- bfsIDs[0] === 11; // true
98
- expect(bfsIDs[0]).toBe(11);
29
+ ```typescript
30
+ import {MinHeap, MaxHeap} from 'data-structure-typed';
31
+ // /* or if you prefer */ import {MinHeap, MaxHeap} from 'heap-typed';
32
+
33
+ const minNumHeap = new MinHeap<number>();
34
+ minNumHeap.add(1).add(6).add(2).add(0).add(5).add(9);
35
+ minNumHeap.poll() // 0
36
+ minNumHeap.poll() // 1
37
+ minNumHeap.peek() // 2
38
+ minNumHeap.toArray().length // 4
39
+ minNumHeap.toArray()[0] // 2
40
+ minNumHeap.toArray()[1] // 5
41
+ minNumHeap.toArray()[2] // 9
42
+ minNumHeap.toArray()[3] // 6
99
43
 
100
- const objBST = new BST();
101
- objBST.add(11, {id: 11, keyA: 11});
102
- objBST.add(3, {id: 3, keyA: 3});
103
44
 
104
- objBST.addMany([{id: 15, keyA: 15}, {id: 1, keyA: 1}, {id: 8, keyA: 8},
105
- {id: 13, keyA: 13}, {id: 16, keyA: 16}, {id: 2, keyA: 2},
106
- {id: 6, keyA: 6}, {id: 9, keyA: 9}, {id: 12, keyA: 12},
107
- {id: 14, keyA: 14}, {id: 4, keyA: 4}, {id: 7, keyA: 7},
108
- {id: 10, keyA: 10}, {id: 5, keyA: 5}]);
45
+ const maxHeap = new MaxHeap<{ keyA: string }>();
46
+ const myObj1 = {keyA: 'a1'}, myObj6 = {keyA: 'a6'}, myObj5 = {keyA: 'a5'}, myObj2 = {keyA: 'a2'},
47
+ myObj0 = {keyA: 'a0'}, myObj9 = {keyA: 'a9'};
48
+ maxHeap.add(1, myObj1);
49
+ maxHeap.has(myObj1) // true
50
+ maxHeap.has(myObj9) // false
51
+ maxHeap.add(6, myObj6);
52
+ maxHeap.has(myObj6) // true
53
+ maxHeap.add(5, myObj5);
54
+ maxHeap.has(myObj5) // true
55
+ maxHeap.add(2, myObj2);
56
+ maxHeap.has(myObj2) // true
57
+ maxHeap.has(myObj6) // true
58
+ maxHeap.add(0, myObj0);
59
+ maxHeap.has(myObj0) // true
60
+ maxHeap.has(myObj9) // false
61
+ maxHeap.add(9, myObj9);
62
+ maxHeap.has(myObj9) // true
109
63
 
110
- objBST.remove(11);
64
+ const peek9 = maxHeap.peek(true);
65
+ peek9 && peek9.val && peek9.val.keyA // 'a9'
111
66
 
67
+ const heapToArr = maxHeap.toArray(true);
68
+ heapToArr.map(item => item?.val?.keyA) // ['a9', 'a2', 'a6', 'a1', 'a0', 'a5']
112
69
 
113
- const avlTree = new AVLTree();
114
- avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
115
- avlTree.isAVLBalanced(); // true
116
- avlTree.remove(10);
117
- avlTree.isAVLBalanced(); // true
118
-
70
+ const values = ['a9', 'a6', 'a5', 'a2', 'a1', 'a0'];
71
+ let i = 0;
72
+ while (maxHeap.size > 0) {
73
+ const polled = maxHeap.poll(true);
74
+ polled && polled.val && polled.val.keyA // values[i]
75
+ i++;
76
+ }
119
77
  ```
120
78
 
121
- ### Directed Graph simple snippet
122
-
123
- #### TS or JS
124
- ```typescript
125
- import {DirectedGraph} from 'data-structure-typed';
79
+ #### JS
126
80
 
127
- const graph = new DirectedGraph();
128
-
129
- graph.addVertex('A');
130
- graph.addVertex('B');
131
-
132
- graph.hasVertex('A'); // true
133
- graph.hasVertex('B'); // true
134
- graph.hasVertex('C'); // false
135
-
136
- graph.addEdge('A', 'B');
137
- graph.hasEdge('A', 'B'); // true
138
- graph.hasEdge('B', 'A'); // false
81
+ ```javascript
82
+ const {MinHeap, MaxHeap} = require('data-structure-typed');
83
+ // /* or if you prefer */ const {MinHeap, MaxHeap} = require('heap-typed');
84
+
85
+ const minNumHeap = new MinHeap();
86
+ minNumHeap.add(1).add(6).add(2).add(0).add(5).add(9);
87
+ minNumHeap.poll() // 0
88
+ minNumHeap.poll() // 1
89
+ minNumHeap.peek() // 2
90
+ minNumHeap.toArray().length // 4
91
+ minNumHeap.toArray()[0] // 2
92
+ minNumHeap.toArray()[1] // 5
93
+ minNumHeap.toArray()[2] // 9
94
+ minNumHeap.toArray()[3] // 6
139
95
 
140
- graph.removeEdgeSrcToDest('A', 'B');
141
- graph.hasEdge('A', 'B'); // false
142
96
 
143
- graph.addVertex('C');
97
+ const maxHeap = new MaxHeap();
98
+ const myObj1 = {keyA: 'a1'}, myObj6 = {keyA: 'a6'}, myObj5 = {keyA: 'a5'}, myObj2 = {keyA: 'a2'},
99
+ myObj0 = {keyA: 'a0'}, myObj9 = {keyA: 'a9'};
100
+ maxHeap.add(1, myObj1);
101
+ maxHeap.has(myObj1) // true
102
+ maxHeap.has(myObj9) // false
103
+ maxHeap.add(6, myObj6);
104
+ maxHeap.has(myObj6) // true
105
+ maxHeap.add(5, myObj5);
106
+ maxHeap.has(myObj5) // true
107
+ maxHeap.add(2, myObj2);
108
+ maxHeap.has(myObj2) // true
109
+ maxHeap.has(myObj6) // true
110
+ maxHeap.add(0, myObj0);
111
+ maxHeap.has(myObj0) // true
112
+ maxHeap.has(myObj9) // false
113
+ maxHeap.add(9, myObj9);
114
+ maxHeap.has(myObj9) // true
144
115
 
145
- graph.addEdge('A', 'B');
146
- graph.addEdge('B', 'C');
116
+ const peek9 = maxHeap.peek(true);
117
+ peek9 && peek9.val && peek9.val.keyA // 'a9'
147
118
 
148
- const topologicalOrderIds = graph.topologicalSort(); // ['A', 'B', 'C']
149
- ```
150
-
151
- ### Undirected Graph snippet
152
-
153
- #### TS or JS
154
- ```typescript
155
- import {UndirectedGraph} from 'data-structure-typed';
156
-
157
- const graph = new UndirectedGraph();
158
- graph.addVertex('A');
159
- graph.addVertex('B');
160
- graph.addVertex('C');
161
- graph.addVertex('D');
162
- graph.removeVertex('C');
163
- graph.addEdge('A', 'B');
164
- graph.addEdge('B', 'D');
119
+ const heapToArr = maxHeap.toArray(true);
120
+ heapToArr.map(item => item?.val?.keyA) // ['a9', 'a2', 'a6', 'a1', 'a0', 'a5']
165
121
 
166
- const dijkstraResult = graph.dijkstra('A');
167
- Array.from(dijkstraResult?.seen ?? []).map(vertex => vertex.id) // ['A', 'B', 'D']
122
+ const values = ['a9', 'a6', 'a5', 'a2', 'a1', 'a0'];
123
+ let i = 0;
124
+ while (maxHeap.size > 0) {
125
+ const polled = maxHeap.poll(true);
126
+ polled && polled.val && polled.val.keyA // values[i]
127
+ i++;
128
+ }
168
129
  ```
169
130
 
170
- ![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/examples/dfs-pre-order.webp)
171
-
172
- ![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/examples/test-graphs.webp)
173
-
174
- ![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/examples/cut-off-trees-for-golf.webp)
175
-
176
- ![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/examples/parenthesis-check.webp)
177
-
178
-
179
131
  ## API docs & Examples
180
132
 
181
133
  [API Docs](https://data-structure-typed-docs.vercel.app)
182
134
 
183
135
  [Live Examples](https://data-structure-typed-examples.vercel.app)
184
136
 
185
- <a href="https://data-structure-typed-examples.vercel.app" target="_blank">Live Examples</a>
186
-
187
- [//]: # ([Examples Repository]&#40;https://github.com/zrwusa/data-structure-typed-examples&#41;)
188
-
189
137
  <a href="https://github.com/zrwusa/data-structure-typed-examples" target="_blank">Examples Repository</a>
190
138
 
191
139
  ## Data Structures
@@ -203,10 +151,10 @@ import {UndirectedGraph} from 'data-structure-typed';
203
151
  <tbody>
204
152
  <tr>
205
153
  <td>Binary Tree</td>
206
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt="">
207
- </img></td>
208
- <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt="">
209
- </img></td>
154
+ <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""/>
155
+ </td>
156
+ <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""/>
157
+ </td>
210
158
  <td><a href="https://data-structure-typed-docs.vercel.app/classes/BinaryTree.html"><span>Binary Tree</span></a></td>
211
159
  <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
212
160
  </tr>
@@ -381,7 +329,6 @@ import {UndirectedGraph} from 'data-structure-typed';
381
329
  </tbody>
382
330
  </table>
383
331
 
384
-
385
332
  # Why
386
333
 
387
334
  ## Complexities
@@ -653,46 +600,6 @@ import {UndirectedGraph} from 'data-structure-typed';
653
600
 
654
601
  ![complexities of data structures](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/assets/data-structure-complexities.jpg)
655
602
 
656
- [//]: # (![]&#40;https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/binary-tree/bst-rotation.gif&#41;)
657
-
658
- [//]: # ()
659
- [//]: # (![]&#40;https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/binary-tree/avl-tree-inserting.gif&#41;)
660
-
661
- [//]: # ()
662
- [//]: # (![]&#40;https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/tarjan.webp&#41;)
663
-
664
- [//]: # ()
665
- [//]: # (![]&#40;https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/adjacency-list.jpg&#41;)
666
-
667
- [//]: # ()
668
- [//]: # (![]&#40;https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/adjacency-list-pros-cons.jpg&#41;)
669
-
670
- [//]: # ()
671
- [//]: # (![]&#40;https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/adjacency-matrix.jpg&#41;)
672
-
673
- [//]: # ()
674
- [//]: # (![]&#40;https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/adjacency-matrix-pros-cons.jpg&#41;)
675
-
676
- [//]: # ()
677
- [//]: # (![]&#40;https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/dfs-can-do.jpg&#41;)
678
-
679
- [//]: # ()
680
- [//]: # (![]&#40;https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/edge-list.jpg&#41;)
681
-
682
- [//]: # ()
683
- [//]: # (![]&#40;https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/edge-list-pros-cons.jpg&#41;)
684
-
685
- [//]: # ()
686
- [//]: # (![]&#40;https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/max-flow.jpg&#41;)
687
-
688
- [//]: # ()
689
- [//]: # (![]&#40;https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/mst.jpg&#41;)
690
-
691
- [//]: # (![]&#40;https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/tarjan-articulation-point-bridge.png&#41;)
692
-
693
- [//]: # (![]&#40;https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/tarjan-complicate-simple.png&#41;)
694
-
695
- [//]: # (![]&#40;https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/tarjan-strongly-connected-component.png&#41;)
696
603
 
697
604
 
698
605
 
package/dist/index.d.ts CHANGED
@@ -1,3 +1,8 @@
1
- export * from './heap';
2
- export * from './max-heap';
3
- export * from './min-heap';
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
+ export { HeapItem, Heap, MaxHeap, MinHeap } from 'data-structure-typed';
package/dist/index.js CHANGED
@@ -1,19 +1,15 @@
1
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
2
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./heap"), exports);
18
- __exportStar(require("./max-heap"), exports);
19
- __exportStar(require("./min-heap"), exports);
3
+ exports.MinHeap = exports.MaxHeap = exports.Heap = exports.HeapItem = void 0;
4
+ /**
5
+ * data-structure-typed
6
+ *
7
+ * @author Tyler Zeng
8
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
9
+ * @license MIT License
10
+ */
11
+ var data_structure_typed_1 = require("data-structure-typed");
12
+ Object.defineProperty(exports, "HeapItem", { enumerable: true, get: function () { return data_structure_typed_1.HeapItem; } });
13
+ Object.defineProperty(exports, "Heap", { enumerable: true, get: function () { return data_structure_typed_1.Heap; } });
14
+ Object.defineProperty(exports, "MaxHeap", { enumerable: true, get: function () { return data_structure_typed_1.MaxHeap; } });
15
+ Object.defineProperty(exports, "MinHeap", { enumerable: true, get: function () { return data_structure_typed_1.MinHeap; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "heap-typed",
3
- "version": "1.19.3",
3
+ "version": "1.19.6",
4
4
  "description": "Heap. Javascript & Typescript Data Structure.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -57,7 +57,7 @@
57
57
  "typescript": "^4.9.5"
58
58
  },
59
59
  "dependencies": {
60
- "data-structure-typed": "^1.19.3",
60
+ "data-structure-typed": "1.19.6",
61
61
  "zod": "^3.22.2"
62
62
  }
63
63
  }
package/tsconfig.json CHANGED
@@ -3,9 +3,10 @@
3
3
  "declaration": true,
4
4
  "outDir": "./dist",
5
5
  "module": "commonjs",
6
- "target": "es5",
6
+ "target": "es6",
7
7
  "lib": [
8
- "esnext",
8
+ // "es2015",
9
+ "esnext"
9
10
  ],
10
11
  "strict": true,
11
12
  "esModuleInterop": true,
@@ -30,8 +31,9 @@
30
31
  "src",
31
32
  ],
32
33
  "exclude": [
33
- // "node_modules/data-structure-typed",
34
+ // "node_modules/data-structure-typed",
34
35
  "node_modules",
35
36
  "dist"
36
37
  ]
37
- }
38
+ }
39
+
package/dist/heap.d.ts DELETED
@@ -1,88 +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 'data-structure-typed';
9
- import type { HeapOptions } from 'data-structure-typed';
10
- export declare class HeapItem<T = number> {
11
- /**
12
- * The constructor function initializes an instance of a class with a priority and a value.
13
- * @param {number} priority - The `priority` parameter is a number that represents the priority of the value. It is
14
- * optional and has a default value of `NaN`.
15
- * @param {T | null} [val=null] - The `val` parameter is of type `T | null`, which means it can accept a value of type
16
- * `T` or `null`.
17
- */
18
- constructor(priority?: number, val?: T | null);
19
- private _priority;
20
- get priority(): number;
21
- set priority(value: number);
22
- private _val;
23
- get val(): T | null;
24
- set val(value: T | null);
25
- }
26
- export declare abstract class Heap<T = number> {
27
- /**
28
- * The function is a constructor for a class that initializes a priority callback function based on the
29
- * options provided.
30
- * @param [options] - An optional object that contains configuration options for the Heap.
31
- */
32
- protected constructor(options?: HeapOptions<T>);
33
- protected abstract _pq: PriorityQueue<HeapItem<T>>;
34
- get pq(): PriorityQueue<HeapItem<T>>;
35
- protected _priorityExtractor: (val: T) => number;
36
- get priorityExtractor(): (val: T) => number;
37
- /**
38
- * The function returns the size of a priority queue.
39
- * @returns The size of the priority queue.
40
- */
41
- get size(): number;
42
- /**
43
- * The function checks if a priority queue is empty.
44
- * @returns {boolean} A boolean value indicating whether the size of the priority queue is less than 1.
45
- */
46
- isEmpty(): boolean;
47
- /**
48
- * The `peek` function returns the top item in the priority queue without removing it.
49
- * @returns The `peek()` method is returning either a `HeapItem<T>` object or `null`.Returns an val with the highest priority in the queue
50
- */
51
- peek(): HeapItem<T> | null;
52
- /**
53
- * The `peekLast` function returns the last item in the heap.
54
- * @returns The method `peekLast()` returns either a `HeapItem<T>` object or `null`.Returns an val with the lowest priority in the queue
55
- */
56
- peekLast(): HeapItem<T> | null;
57
- /**
58
- * The `add` function adds an val to a priority queue with an optional priority value.
59
- * @param {T} val - The `val` parameter represents the value that you want to add to the heap. It can be of any
60
- * type.
61
- * @param {number} [priority] - The `priority` parameter is an optional number that represents the priority of the
62
- * val being added to the heap. If the `val` parameter is a number, then the `priority` parameter is set to
63
- * the value of `val`. If the `val` parameter is not a number, then the
64
- * @returns The `add` method returns the instance of the `Heap` class.
65
- * @throws {Error} if priority is not a valid number
66
- */
67
- add(val: T, priority?: number): Heap<T>;
68
- /**
69
- * 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
70
- * @returns either a HeapItem<T> object or null.
71
- */
72
- poll(): HeapItem<T> | null;
73
- /**
74
- * The function checks if a given node or value exists in the priority queue.
75
- * @param {T | HeapItem<T>} node - The parameter `node` can be of type `T` or `HeapItem<T>`.
76
- * @returns a boolean value.
77
- */
78
- has(node: T | HeapItem<T>): boolean;
79
- /**
80
- * The `toArray` function returns an array of `HeapItem<T>` objects.
81
- * @returns An array of HeapItem<T> objects.Returns a sorted list of vals
82
- */
83
- toArray(): HeapItem<T>[];
84
- /**
85
- * The clear function clears the priority queue.
86
- */
87
- clear(): void;
88
- }
package/dist/heap.js DELETED
@@ -1,176 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Heap = exports.HeapItem = void 0;
4
- var HeapItem = /** @class */ (function () {
5
- /**
6
- * The constructor function initializes an instance of a class with a priority and a value.
7
- * @param {number} priority - The `priority` parameter is a number that represents the priority of the value. It is
8
- * optional and has a default value of `NaN`.
9
- * @param {T | null} [val=null] - The `val` parameter is of type `T | null`, which means it can accept a value of type
10
- * `T` or `null`.
11
- */
12
- function HeapItem(priority, val) {
13
- if (priority === void 0) { priority = NaN; }
14
- if (val === void 0) { val = null; }
15
- this._val = val;
16
- this._priority = priority;
17
- }
18
- Object.defineProperty(HeapItem.prototype, "priority", {
19
- get: function () {
20
- return this._priority;
21
- },
22
- set: function (value) {
23
- this._priority = value;
24
- },
25
- enumerable: false,
26
- configurable: true
27
- });
28
- Object.defineProperty(HeapItem.prototype, "val", {
29
- get: function () {
30
- return this._val;
31
- },
32
- set: function (value) {
33
- this._val = value;
34
- },
35
- enumerable: false,
36
- configurable: true
37
- });
38
- return HeapItem;
39
- }());
40
- exports.HeapItem = HeapItem;
41
- var Heap = /** @class */ (function () {
42
- /**
43
- * The function is a constructor for a class that initializes a priority callback function based on the
44
- * options provided.
45
- * @param [options] - An optional object that contains configuration options for the Heap.
46
- */
47
- function Heap(options) {
48
- if (options) {
49
- var priorityExtractor = options.priorityExtractor;
50
- if (priorityExtractor !== undefined && typeof priorityExtractor !== 'function') {
51
- throw new Error('.constructor expects a valid priority function');
52
- }
53
- this._priorityExtractor = priorityExtractor || (function (el) { return +el; });
54
- }
55
- else {
56
- this._priorityExtractor = function (el) { return +el; };
57
- }
58
- }
59
- Object.defineProperty(Heap.prototype, "pq", {
60
- get: function () {
61
- return this._pq;
62
- },
63
- enumerable: false,
64
- configurable: true
65
- });
66
- Object.defineProperty(Heap.prototype, "priorityExtractor", {
67
- get: function () {
68
- return this._priorityExtractor;
69
- },
70
- enumerable: false,
71
- configurable: true
72
- });
73
- Object.defineProperty(Heap.prototype, "size", {
74
- /**
75
- * The function returns the size of a priority queue.
76
- * @returns The size of the priority queue.
77
- */
78
- get: function () {
79
- return this._pq.size;
80
- },
81
- enumerable: false,
82
- configurable: true
83
- });
84
- /**
85
- * The function checks if a priority queue is empty.
86
- * @returns {boolean} A boolean value indicating whether the size of the priority queue is less than 1.
87
- */
88
- Heap.prototype.isEmpty = function () {
89
- return this._pq.size < 1;
90
- };
91
- /**
92
- * The `peek` function returns the top item in the priority queue without removing it.
93
- * @returns The `peek()` method is returning either a `HeapItem<T>` object or `null`.Returns an val with the highest priority in the queue
94
- */
95
- Heap.prototype.peek = function () {
96
- return this._pq.peek();
97
- };
98
- /**
99
- * The `peekLast` function returns the last item in the heap.
100
- * @returns The method `peekLast()` returns either a `HeapItem<T>` object or `null`.Returns an val with the lowest priority in the queue
101
- */
102
- Heap.prototype.peekLast = function () {
103
- return this._pq.leaf();
104
- };
105
- /**
106
- * The `add` function adds an val to a priority queue with an optional priority value.
107
- * @param {T} val - The `val` parameter represents the value that you want to add to the heap. It can be of any
108
- * type.
109
- * @param {number} [priority] - The `priority` parameter is an optional number that represents the priority of the
110
- * val being added to the heap. If the `val` parameter is a number, then the `priority` parameter is set to
111
- * the value of `val`. If the `val` parameter is not a number, then the
112
- * @returns The `add` method returns the instance of the `Heap` class.
113
- * @throws {Error} if priority is not a valid number
114
- */
115
- Heap.prototype.add = function (val, priority) {
116
- if (typeof val === 'number') {
117
- priority = val;
118
- }
119
- else {
120
- if (priority === undefined) {
121
- throw new Error('.add expects a numeric priority');
122
- }
123
- }
124
- if (priority && Number.isNaN(+priority)) {
125
- throw new Error('.add expects a numeric priority');
126
- }
127
- if (Number.isNaN(+priority) && Number.isNaN(this._priorityExtractor(val))) {
128
- throw new Error('.add expects a numeric priority '
129
- + 'or a constructor callback that returns a number');
130
- }
131
- var _priority = !Number.isNaN(+priority) ? priority : this._priorityExtractor(val);
132
- this._pq.add(new HeapItem(_priority, val));
133
- return this;
134
- };
135
- /**
136
- * 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
137
- * @returns either a HeapItem<T> object or null.
138
- */
139
- Heap.prototype.poll = function () {
140
- var top = this._pq.poll();
141
- if (!top) {
142
- return null;
143
- }
144
- return top;
145
- };
146
- /**
147
- * The function checks if a given node or value exists in the priority queue.
148
- * @param {T | HeapItem<T>} node - The parameter `node` can be of type `T` or `HeapItem<T>`.
149
- * @returns a boolean value.
150
- */
151
- Heap.prototype.has = function (node) {
152
- if (node instanceof HeapItem) {
153
- return this.pq.getNodes().includes(node);
154
- }
155
- else {
156
- return this.pq.getNodes().findIndex(function (item) {
157
- return item.val === node;
158
- }) !== -1;
159
- }
160
- };
161
- /**
162
- * The `toArray` function returns an array of `HeapItem<T>` objects.
163
- * @returns An array of HeapItem<T> objects.Returns a sorted list of vals
164
- */
165
- Heap.prototype.toArray = function () {
166
- return this._pq.toArray();
167
- };
168
- /**
169
- * The clear function clears the priority queue.
170
- */
171
- Heap.prototype.clear = function () {
172
- this._pq.clear();
173
- };
174
- return Heap;
175
- }());
176
- exports.Heap = Heap;
@@ -1,23 +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 { Heap, HeapItem } from 'data-structure-typed';
9
- import { PriorityQueue } from 'data-structure-typed';
10
- import type { HeapOptions } from 'data-structure-typed';
11
- /**
12
- * @class MaxHeap
13
- * @extends Heap
14
- */
15
- export declare class MaxHeap<T = number> extends Heap<T> {
16
- protected _pq: PriorityQueue<HeapItem<T>>;
17
- /**
18
- * The constructor initializes a PriorityQueue with a custom comparator function.
19
- * @param [options] - The `options` parameter is an optional object that can be passed to the constructor. It is of
20
- * type `HeapOptions<T>`, which is a generic type that represents the options for the heap.
21
- */
22
- constructor(options?: HeapOptions<T>);
23
- }
package/dist/max-heap.js DELETED
@@ -1,48 +0,0 @@
1
- "use strict";
2
- /**
3
- * data-structure-typed
4
- *
5
- * @author Tyler Zeng
6
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
7
- * @license MIT License
8
- */
9
- var __extends = (this && this.__extends) || (function () {
10
- var extendStatics = function (d, b) {
11
- extendStatics = Object.setPrototypeOf ||
12
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
13
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
14
- return extendStatics(d, b);
15
- };
16
- return function (d, b) {
17
- if (typeof b !== "function" && b !== null)
18
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
19
- extendStatics(d, b);
20
- function __() { this.constructor = d; }
21
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
22
- };
23
- })();
24
- Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.MaxHeap = void 0;
26
- var data_structure_typed_1 = require("data-structure-typed");
27
- var data_structure_typed_2 = require("data-structure-typed");
28
- /**
29
- * @class MaxHeap
30
- * @extends Heap
31
- */
32
- var MaxHeap = /** @class */ (function (_super) {
33
- __extends(MaxHeap, _super);
34
- /**
35
- * The constructor initializes a PriorityQueue with a custom comparator function.
36
- * @param [options] - The `options` parameter is an optional object that can be passed to the constructor. It is of
37
- * type `HeapOptions<T>`, which is a generic type that represents the options for the heap.
38
- */
39
- function MaxHeap(options) {
40
- var _this = _super.call(this, options) || this;
41
- _this._pq = new data_structure_typed_2.PriorityQueue({
42
- comparator: function (a, b) { return b.priority - a.priority; }
43
- });
44
- return _this;
45
- }
46
- return MaxHeap;
47
- }(data_structure_typed_1.Heap));
48
- exports.MaxHeap = MaxHeap;
@@ -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 { Heap, HeapItem } from 'data-structure-typed';
9
- import { PriorityQueue } from 'data-structure-typed';
10
- import type { HeapOptions } from 'data-structure-typed';
11
- /**
12
- * @class MinHeap
13
- * @extends Heap
14
- */
15
- export declare class MinHeap<T = number> extends Heap<T> {
16
- protected _pq: PriorityQueue<HeapItem<T>>;
17
- /**
18
- * The constructor initializes a PriorityQueue with a comparator function that compares the priority of two HeapItem
19
- * objects.
20
- * @param [options] - The `options` parameter is an optional object that can be passed to the constructor. It is of
21
- * type `HeapOptions<T>`, which is a generic type that represents the options for the heap.
22
- */
23
- constructor(options?: HeapOptions<T>);
24
- }
package/dist/min-heap.js DELETED
@@ -1,49 +0,0 @@
1
- "use strict";
2
- /**
3
- * data-structure-typed
4
- *
5
- * @author Tyler Zeng
6
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
7
- * @license MIT License
8
- */
9
- var __extends = (this && this.__extends) || (function () {
10
- var extendStatics = function (d, b) {
11
- extendStatics = Object.setPrototypeOf ||
12
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
13
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
14
- return extendStatics(d, b);
15
- };
16
- return function (d, b) {
17
- if (typeof b !== "function" && b !== null)
18
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
19
- extendStatics(d, b);
20
- function __() { this.constructor = d; }
21
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
22
- };
23
- })();
24
- Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.MinHeap = void 0;
26
- var data_structure_typed_1 = require("data-structure-typed");
27
- var data_structure_typed_2 = require("data-structure-typed");
28
- /**
29
- * @class MinHeap
30
- * @extends Heap
31
- */
32
- var MinHeap = /** @class */ (function (_super) {
33
- __extends(MinHeap, _super);
34
- /**
35
- * The constructor initializes a PriorityQueue with a comparator function that compares the priority of two HeapItem
36
- * objects.
37
- * @param [options] - The `options` parameter is an optional object that can be passed to the constructor. It is of
38
- * type `HeapOptions<T>`, which is a generic type that represents the options for the heap.
39
- */
40
- function MinHeap(options) {
41
- var _this = _super.call(this, options) || this;
42
- _this._pq = new data_structure_typed_2.PriorityQueue({
43
- comparator: function (a, b) { return a.priority - b.priority; }
44
- });
45
- return _this;
46
- }
47
- return MinHeap;
48
- }(data_structure_typed_1.Heap));
49
- exports.MinHeap = MinHeap;