@rwitankar/javascript-data-structures 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ # Changelog
2
+
3
+ ## 1.0.0 - 2026-11-14
4
+
5
+ - Added Stack, Queue, Deque, linked lists, heaps, PriorityQueue, Graph, Trie, and UnionFind.
6
+ - Added CommonJS and ES Module package entry points.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,77 @@
1
- # Temporary Holding Version
1
+ # JavaScript Data Structures
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Small, dependency-free JavaScript implementations of common data structures. The package exposes CommonJS and ES Module entry points.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install @rwitankar/javascript-data-structures
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ CommonJS:
14
+
15
+ ```js
16
+ const { Stack, PriorityQueue } = require('@rwitankar/javascript-data-structures');
17
+ const stack = new Stack();
18
+ stack.push('value');
19
+ console.log(stack.pop());
20
+ ```
21
+
22
+ ES Modules:
23
+
24
+ ```js
25
+ import { Graph, Trie } from '@rwitankar/javascript-data-structures';
26
+ const graph = new Graph();
27
+ graph.addVertex('A').addVertex('B').addEdge('A', 'B');
28
+ console.log(graph.BFS('A'));
29
+ ```
30
+
31
+ ## Data structures and APIs
32
+
33
+ - `Stack`: `push`, `pop`, `peek`, `isEmpty`, `size`, `clear`, `toArray` (LIFO).
34
+ - `Queue`: `enqueue`, `dequeue`, `front`, `rear`, `isEmpty`, `size`, `clear`, `toArray` (FIFO).
35
+ - `Deque`: `addFront`, `addRear`, `removeFront`, `removeRear`, `peekFront`, `peekRear`, `isEmpty`, `size`.
36
+ - `SinglyLinkedList`: `append`, `prepend`, `insertAt`, `removeAt`, `remove`, `find`, `get`, `reverse`, `size`, `isEmpty`, `toArray`.
37
+ - `DoublyLinkedList`: `append`, `prepend`, `insertAt`, `removeAt`, `remove`, `find`, `reverse`, `size`, `toArray`.
38
+ - `MinHeap` / `MaxHeap`: `insert`, `extractMin` / `extractMax`, `peek`, `size`, `isEmpty`, `clear`, `toArray`.
39
+ - `PriorityQueue`: `enqueue(value, priority)`, `dequeue`, `peek`, `size`, `isEmpty`, `clear`. Lower numeric priorities leave first; equal priorities are FIFO.
40
+ - `Graph`: `addVertex`, `addEdge`, `removeVertex`, `removeEdge`, `hasVertex`, `hasEdge`, `getNeighbors`, `BFS`, `DFS`, `isDirected`, `display`. Construct with `new Graph(true)` for directed edges; edges are unweighted.
41
+ - `Trie`: `insert`, `search`, `startsWith`, `delete`, `getWordsWithPrefix`. Words are strings; prefix results are lexicographically ordered.
42
+ - `UnionFind`: `find`, `union`, `connected`, `count`. Construct with a non-negative element count; elements are zero-based.
43
+
44
+ Empty removal/peek operations throw `Error`. Invalid indexes throw `RangeError`; invalid constructor values, words, or priorities throw `TypeError`/`RangeError` as appropriate. Methods that mutate a structure return the structure for chaining unless documented otherwise. `remove` and `delete` return booleans.
45
+
46
+ ## Complexity
47
+
48
+ | Structure | Main operations | Space |
49
+ | --- | --- | --- |
50
+ | Stack | push/pop/peek O(1) | O(n) |
51
+ | Queue | enqueue/dequeue/front/rear O(1) amortized | O(n) |
52
+ | Deque | rear operations O(1); front array operations O(n) | O(n) |
53
+ | Singly linked list | append/prepend O(1); indexed operations O(n) | O(n) |
54
+ | Doubly linked list | append/prepend O(1); indexed lookup O(n) | O(n) |
55
+ | Heaps | insert/extract O(log n); peek O(1) | O(n) |
56
+ | Priority queue | enqueue/dequeue O(log n); peek O(1) | O(n) |
57
+ | Graph | edge/vertex lookup O(1) average; BFS/DFS O(V+E) | O(V+E) |
58
+ | Trie | insert/search/prefix O(k), excluding output size | O(total characters) |
59
+ | UnionFind | near O(1) amortized (`α(n)`) | O(n) |
60
+
61
+ ## Compatibility
62
+
63
+ The package is tested with Node.js `v26.5.0` in this project. Node.js `18+` is declared in `package.json`, but other versions are not verified here.
64
+
65
+ ## Development and publishing
66
+
67
+ ```bash
68
+ npm test
69
+ npm pack --dry-run
70
+ npm publish
71
+ ```
72
+
73
+ Authenticate with an npm account that owns the `rwitankar` scope, then review the package contents before publishing. Publishing is intentionally not automated.
74
+
75
+ ## Contributing
76
+
77
+ Keep each structure independent, add Node built-in tests for behavior and edge cases, and update this README when a public API changes.
package/index.js ADDED
@@ -0,0 +1,6 @@
1
+ module.exports = {
2
+ Stack: require('./src/linear/Stack'), Queue: require('./src/linear/Queue'), Deque: require('./src/linear/Deque'),
3
+ SinglyLinkedList: require('./src/linked-list/SinglyLinkedList'), DoublyLinkedList: require('./src/linked-list/DoublyLinkedList'),
4
+ MinHeap: require('./src/heaps/MinHeap'), MaxHeap: require('./src/heaps/MaxHeap'), PriorityQueue: require('./src/priority-queue/PriorityQueue'),
5
+ Graph: require('./src/graphs/Graph'), Trie: require('./src/trees/Trie'), UnionFind: require('./src/disjoint-set/UnionFind')
6
+ };
package/index.mjs ADDED
@@ -0,0 +1,3 @@
1
+ import api from './index.js';
2
+ export const { Stack, Queue, Deque, SinglyLinkedList, DoublyLinkedList, MinHeap, MaxHeap, PriorityQueue, Graph, Trie, UnionFind } = api;
3
+ export default api;
package/package.json CHANGED
@@ -1,6 +1,26 @@
1
1
  {
2
2
  "name": "@rwitankar/javascript-data-structures",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0",
4
+ "description": "Common JavaScript data structures and algorithms.",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/rwitankar-byte/JS_dataStructure_npm.git"
8
+ },
9
+ "license": "MIT",
10
+ "type": "commonjs",
11
+ "main": "./index.js",
12
+ "module": "./index.mjs",
13
+ "exports": {
14
+ ".": {
15
+ "require": "./index.js",
16
+ "import": "./index.mjs"
17
+ }
18
+ },
19
+ "files": ["index.js", "index.mjs", "src", "README.md", "LICENSE", "CHANGELOG.md"],
20
+ "scripts": {"test": "node --test"},
21
+ "engines": {"node": ">=18"},
22
+ "publishConfig": {
23
+ "registry": "https://registry.npmjs.org",
24
+ "access": "public"
25
+ }
26
+ }
@@ -0,0 +1,10 @@
1
+ /** Disjoint-set structure with path compression and union by rank. */
2
+ class UnionFind {
3
+ constructor(size) { if (!Number.isInteger(size) || size < 0) throw new RangeError('Size must be a non-negative integer'); this.parent = Array.from({ length: size }, (_, i) => i); this.rank = Array(size).fill(0); this.components = size; }
4
+ _check(value) { if (!Number.isInteger(value) || value < 0 || value >= this.parent.length) throw new RangeError('Element out of bounds'); }
5
+ find(value) { this._check(value); if (this.parent[value] !== value) this.parent[value] = this.find(this.parent[value]); return this.parent[value]; }
6
+ union(a, b) { const rootA = this.find(a), rootB = this.find(b); if (rootA === rootB) return false; if (this.rank[rootA] < this.rank[rootB]) this.parent[rootA] = rootB; else { this.parent[rootB] = rootA; if (this.rank[rootA] === this.rank[rootB]) this.rank[rootA]++; } this.components--; return true; }
7
+ connected(a, b) { return this.find(a) === this.find(b); }
8
+ count() { return this.components; }
9
+ }
10
+ module.exports = UnionFind;
@@ -0,0 +1,16 @@
1
+ /** Directed or undirected unweighted graph. */
2
+ class Graph {
3
+ constructor(directed = false) { if (typeof directed !== 'boolean') throw new TypeError('Directed must be a boolean'); this.directed = directed; this.adjacency = new Map(); }
4
+ addVertex(vertex) { if (!this.adjacency.has(vertex)) this.adjacency.set(vertex, new Set()); return this; }
5
+ addEdge(from, to) { if (!this.adjacency.has(from) || !this.adjacency.has(to)) throw new Error('Both vertices must exist'); this.adjacency.get(from).add(to); if (!this.directed) this.adjacency.get(to).add(from); return this; }
6
+ removeVertex(vertex) { if (!this.adjacency.delete(vertex)) return false; for (const neighbors of this.adjacency.values()) neighbors.delete(vertex); return true; }
7
+ removeEdge(from, to) { const removed = this.adjacency.get(from)?.delete(to) ?? false; if (!this.directed) this.adjacency.get(to)?.delete(from); return removed; }
8
+ hasVertex(vertex) { return this.adjacency.has(vertex); }
9
+ hasEdge(from, to) { return this.adjacency.get(from)?.has(to) ?? false; }
10
+ getNeighbors(vertex) { if (!this.adjacency.has(vertex)) throw new Error('Vertex does not exist'); return [...this.adjacency.get(vertex)]; }
11
+ BFS(start) { if (!this.adjacency.has(start)) throw new Error('Vertex does not exist'); const seen = new Set([start]), result = [], queue = [start]; for (let i = 0; i < queue.length; i++) { const v = queue[i]; result.push(v); for (const n of this.adjacency.get(v)) if (!seen.has(n)) { seen.add(n); queue.push(n); } } return result; }
12
+ DFS(start) { if (!this.adjacency.has(start)) throw new Error('Vertex does not exist'); const result = [], seen = new Set(); const visit = v => { seen.add(v); result.push(v); for (const n of this.adjacency.get(v)) if (!seen.has(n)) visit(n); }; visit(start); return result; }
13
+ isDirected() { return this.directed; }
14
+ display() { return Object.fromEntries([...this.adjacency].map(([v, n]) => [v, [...n]])); }
15
+ }
16
+ module.exports = Graph;
@@ -0,0 +1,8 @@
1
+ /** Binary max-heap for values comparable with `>` and `>=`. */
2
+ class MaxHeap extends require('./MinHeap') {
3
+ _up(i) { while (i) { const p = (i - 1) >> 1; if (this.items[p] >= this.items[i]) break; [this.items[p], this.items[i]] = [this.items[i], this.items[p]]; i = p; } }
4
+ _down(i) { while (true) { let c = i * 2 + 1; if (c >= this.items.length) return; if (c + 1 < this.items.length && this.items[c + 1] > this.items[c]) c++; if (this.items[i] >= this.items[c]) return; [this.items[i], this.items[c]] = [this.items[c], this.items[i]]; i = c; } }
5
+ extractMax() { return super.extractMin(); }
6
+ peek() { return super.peek(); }
7
+ }
8
+ module.exports = MaxHeap;
@@ -0,0 +1,14 @@
1
+ /** Binary min-heap for values comparable with `<` and `<=`. */
2
+ class MinHeap {
3
+ constructor() { this.items = []; }
4
+ insert(value) { this.items.push(value); this._up(this.items.length - 1); return this; }
5
+ _up(i) { while (i) { const p = (i - 1) >> 1; if (this.items[p] <= this.items[i]) break; [this.items[p], this.items[i]] = [this.items[i], this.items[p]]; i = p; } }
6
+ _down(i) { while (true) { let c = i * 2 + 1; if (c >= this.items.length) return; if (c + 1 < this.items.length && this.items[c + 1] < this.items[c]) c++; if (this.items[i] <= this.items[c]) return; [this.items[i], this.items[c]] = [this.items[c], this.items[i]]; i = c; } }
7
+ extractMin() { if (this.isEmpty()) throw new Error('Heap is empty'); const value = this.items[0], last = this.items.pop(); if (this.items.length) { this.items[0] = last; this._down(0); } return value; }
8
+ peek() { if (this.isEmpty()) throw new Error('Heap is empty'); return this.items[0]; }
9
+ size() { return this.items.length; }
10
+ isEmpty() { return !this.items.length; }
11
+ clear() { this.items.length = 0; }
12
+ toArray() { return [...this.items]; }
13
+ }
14
+ module.exports = MinHeap;
@@ -0,0 +1,13 @@
1
+ /** Double-ended collection. */
2
+ class Deque {
3
+ constructor() { this.items = []; }
4
+ addFront(value) { this.items.unshift(value); return this; }
5
+ addRear(value) { this.items.push(value); return this; }
6
+ removeFront() { if (this.isEmpty()) throw new Error('Deque is empty'); return this.items.shift(); }
7
+ removeRear() { if (this.isEmpty()) throw new Error('Deque is empty'); return this.items.pop(); }
8
+ peekFront() { if (this.isEmpty()) throw new Error('Deque is empty'); return this.items[0]; }
9
+ peekRear() { if (this.isEmpty()) throw new Error('Deque is empty'); return this.items.at(-1); }
10
+ isEmpty() { return this.items.length === 0; }
11
+ size() { return this.items.length; }
12
+ }
13
+ module.exports = Deque;
@@ -0,0 +1,13 @@
1
+ /** FIFO collection with amortized constant-time dequeue. */
2
+ class Queue {
3
+ constructor() { this.items = []; this.head = 0; }
4
+ enqueue(value) { this.items.push(value); return this; }
5
+ dequeue() { if (this.isEmpty()) throw new Error('Queue is empty'); const value = this.items[this.head++]; if (this.head > 64 && this.head * 2 >= this.items.length) { this.items = this.items.slice(this.head); this.head = 0; } return value; }
6
+ front() { if (this.isEmpty()) throw new Error('Queue is empty'); return this.items[this.head]; }
7
+ rear() { if (this.isEmpty()) throw new Error('Queue is empty'); return this.items.at(-1); }
8
+ isEmpty() { return this.head >= this.items.length; }
9
+ size() { return this.items.length - this.head; }
10
+ clear() { this.items = []; this.head = 0; }
11
+ toArray() { return this.items.slice(this.head); }
12
+ }
13
+ module.exports = Queue;
@@ -0,0 +1,12 @@
1
+ /** LIFO collection with constant-time top operations. */
2
+ class Stack {
3
+ constructor() { this.items = []; }
4
+ push(value) { this.items.push(value); return this; }
5
+ pop() { if (!this.items.length) throw new Error('Stack is empty'); return this.items.pop(); }
6
+ peek() { if (!this.items.length) throw new Error('Stack is empty'); return this.items.at(-1); }
7
+ isEmpty() { return this.items.length === 0; }
8
+ size() { return this.items.length; }
9
+ clear() { this.items.length = 0; }
10
+ toArray() { return [...this.items]; }
11
+ }
12
+ module.exports = Stack;
@@ -0,0 +1,16 @@
1
+ /** Mutable doubly linked list with zero-based indexing. */
2
+ class DoublyLinkedList {
3
+ constructor() { this.head = null; this.tail = null; this.length = 0; }
4
+ append(value) { const node = { value, prev: this.tail, next: null }; if (this.tail) this.tail.next = node; else this.head = node; this.tail = node; this.length++; return this; }
5
+ prepend(value) { const node = { value, prev: null, next: this.head }; if (this.head) this.head.prev = node; else this.tail = node; this.head = node; this.length++; return this; }
6
+ _index(index, allowEnd = false) { if (!Number.isInteger(index) || index < 0 || index >= this.length + (allowEnd ? 1 : 0)) throw new RangeError('Index out of bounds'); }
7
+ insertAt(index, value) { this._index(index, true); if (index === 0) return this.prepend(value); if (index === this.length) return this.append(value); const next = this._node(index), node = { value, prev: next.prev, next }; next.prev.next = node; next.prev = node; this.length++; return this; }
8
+ _node(index) { let n; if (index < this.length / 2) { n = this.head; while (index--) n = n.next; } else { n = this.tail; for (let i = this.length - 1; i > index; i--) n = n.prev; } return n; }
9
+ removeAt(index) { this._index(index); const n = this._node(index); if (n.prev) n.prev.next = n.next; else this.head = n.next; if (n.next) n.next.prev = n.prev; else this.tail = n.prev; this.length--; return n.value; }
10
+ remove(value) { const i = this.find(value); if (i === -1) return false; this.removeAt(i); return true; }
11
+ find(value) { let n = this.head, i = 0; while (n) { if (Object.is(n.value, value)) return i; n = n.next; i++; } return -1; }
12
+ reverse() { let n = this.head; while (n) { [n.next, n.prev] = [n.prev, n.next]; n = n.prev; } [this.head, this.tail] = [this.tail, this.head]; return this; }
13
+ size() { return this.length; }
14
+ toArray() { const result = []; for (let n = this.head; n; n = n.next) result.push(n.value); return result; }
15
+ }
16
+ module.exports = DoublyLinkedList;
@@ -0,0 +1,17 @@
1
+ /** Mutable singly linked list with zero-based indexing. */
2
+ class SinglyLinkedList {
3
+ constructor() { this.head = null; this.tail = null; this.length = 0; }
4
+ append(value) { const node = { value, next: null }; if (!this.head) this.head = node; else this.tail.next = node; this.tail = node; this.length++; return this; }
5
+ prepend(value) { const node = { value, next: this.head }; this.head = node; if (!this.tail) this.tail = node; this.length++; return this; }
6
+ _index(index) { if (!Number.isInteger(index) || index < 0 || index >= this.length) throw new RangeError('Index out of bounds'); }
7
+ insertAt(index, value) { if (!Number.isInteger(index) || index < 0 || index > this.length) throw new RangeError('Index out of bounds'); if (index === 0) return this.prepend(value); if (index === this.length) return this.append(value); let prev = this.head; for (let i = 1; i < index; i++) prev = prev.next; prev.next = { value, next: prev.next }; this.length++; return this; }
8
+ removeAt(index) { this._index(index); let removed; if (index === 0) { removed = this.head; this.head = this.head.next; } else { let prev = this.head; for (let i = 1; i < index; i++) prev = prev.next; removed = prev.next; prev.next = removed.next; if (removed === this.tail) this.tail = prev; } this.length--; if (!this.length) this.tail = null; return removed.value; }
9
+ remove(value) { const i = this.find(value); if (i === -1) return false; this.removeAt(i); return true; }
10
+ find(value) { let node = this.head, i = 0; while (node) { if (Object.is(node.value, value)) return i; node = node.next; i++; } return -1; }
11
+ get(index) { this._index(index); let node = this.head; for (let i = 0; i < index; i++) node = node.next; return node.value; }
12
+ reverse() { let previous = null, node = this.head; this.tail = this.head; while (node) { const next = node.next; node.next = previous; previous = node; node = next; } this.head = previous; return this; }
13
+ size() { return this.length; }
14
+ isEmpty() { return this.length === 0; }
15
+ toArray() { const result = []; for (let n = this.head; n; n = n.next) result.push(n.value); return result; }
16
+ }
17
+ module.exports = SinglyLinkedList;
@@ -0,0 +1,14 @@
1
+ /** Stable min-priority queue; lower numeric priorities dequeue first. */
2
+ class PriorityQueue {
3
+ constructor() { this.items = []; this.sequence = 0; }
4
+ enqueue(value, priority) { if (typeof priority !== 'number' || Number.isNaN(priority)) throw new TypeError('Priority must be a number'); this.items.push({ value, priority, sequence: this.sequence++ }); this._up(this.items.length - 1); return this; }
5
+ _before(a, b) { return a.priority < b.priority || (a.priority === b.priority && a.sequence < b.sequence); }
6
+ _up(i) { while (i) { const p = (i - 1) >> 1; if (this._before(this.items[p], this.items[i])) break; [this.items[p], this.items[i]] = [this.items[i], this.items[p]]; i = p; } }
7
+ _down(i) { while (true) { let c = i * 2 + 1; if (c >= this.items.length) return; if (c + 1 < this.items.length && this._before(this.items[c + 1], this.items[c])) c++; if (this._before(this.items[i], this.items[c])) return; [this.items[i], this.items[c]] = [this.items[c], this.items[i]]; i = c; } }
8
+ dequeue() { if (this.isEmpty()) throw new Error('Priority queue is empty'); const value = this.items[0].value, last = this.items.pop(); if (this.items.length) { this.items[0] = last; this._down(0); } return value; }
9
+ peek() { if (this.isEmpty()) throw new Error('Priority queue is empty'); return this.items[0].value; }
10
+ size() { return this.items.length; }
11
+ isEmpty() { return !this.items.length; }
12
+ clear() { this.items.length = 0; }
13
+ }
14
+ module.exports = PriorityQueue;
@@ -0,0 +1,13 @@
1
+ /** Prefix tree for string values. */
2
+ class Trie {
3
+ constructor() { this.root = { children: new Map(), word: false }; }
4
+ _validate(word) { if (typeof word !== 'string') throw new TypeError('Word must be a string'); }
5
+ insert(word) { this._validate(word); let node = this.root; for (const char of word) { if (!node.children.has(char)) node.children.set(char, { children: new Map(), word: false }); node = node.children.get(char); } node.word = true; return this; }
6
+ _node(prefix) { let node = this.root; for (const char of prefix) { node = node.children.get(char); if (!node) return null; } return node; }
7
+ search(word) { this._validate(word); return !!this._node(word)?.word; }
8
+ startsWith(prefix) { this._validate(prefix); return !!this._node(prefix); }
9
+ delete(word) { this._validate(word); const path = [], node = this._nodeWithPath(word, path); if (!node?.word) return false; node.word = false; for (let i = path.length - 1; i >= 0; i--) { const [parent, char, child] = path[i]; if (child.word || child.children.size) break; parent.children.delete(char); } return true; }
10
+ _nodeWithPath(word, path) { let node = this.root; for (const char of word) { const child = node.children.get(char); if (!child) return null; path.push([node, char, child]); node = child; } return node; }
11
+ getWordsWithPrefix(prefix) { this._validate(prefix); const node = this._node(prefix); if (!node) return []; const words = []; const visit = (n, suffix) => { if (n.word) words.push(prefix + suffix); for (const char of [...n.children.keys()].sort()) visit(n.children.get(char), suffix + char); }; visit(node, ''); return words; }
12
+ }
13
+ module.exports = Trie;