@nextian/dsa-js
v1.0.4
Published
A JavaScript package for common Data Structures and Algorithms
Readme
@nextian/dsa-js
A JavaScript library offering Data Structures and Algorithms, designed for learning, interview prep, and competitive programming.
✨ Features
- ✅ Stack
- ✅ Queue
- ✅ Deque
- ✅ LinkedList
- ✅ MinHeap & MaxHeap (Priority Queue)
- ✅ Binary Search Tree
- ✅ HashMap
- ✅ HashSet
- ✅ Binary Search
- ✅
sortwith C++-like custom comparator support
📦 Installation
npm install @nextian/dsa-js🚀 Usage
Stack
import { Stack } from '@nextian/dsa-js';
const st = new Stack();
st.push(10);
st.push(20);
console.log(st.top()); // 20
st.pop();
console.log(st.top()); // 10Queue
import { Queue } from '@nextian/dsa-js';
const q = new Queue();
q.push(5);
q.push(15);
console.log(q.front()); // 5
q.pop();
console.log(q.front()); // 15Deque
import { Deque } from '@nextian/dsa-js';
const dq = new Deque();
dq.push_back(10);
dq.push_front(20);
console.log(dq.front()); // 20
console.log(dq.back()); // 10
dq.pop_front();
console.log(dq.front()); // 10LinkedList
import { LinkedList } from '@nextian/dsa-js';
const ll = new LinkedList();
ll.push_back(1);
ll.push_back(2);
ll.push_front(0);
console.log(ll.toArray()); // [0, 1, 2]
ll.pop_front();
console.log(ll.toArray()); // [1, 2]HashMap
import { HashMap } from '@nextian/dsa-js';
const map = new HashMap();
map.insert('key1', 'value1');
console.log(map.get('key1')); // 'value1'
map.erase('key1');
console.log(map.find('key1')); // falseHashSet
import { HashSet } from '@nextian/dsa-js';
const set = new HashSet();
set.insert(10);
set.insert(20);
console.log(set.count(10)); // 1
set.erase(10);
console.log(set.count(10)); // 0
BinarySearchTree
import { BinarySearchTree } from '@nextian/dsa-js';
const bst = new BinarySearchTree();
bst.insert(5);
bst.insert(3);
bst.insert(7);
console.log(bst.search(3)); // true
console.log(bst.search(8)); // falseHeap (MinHeap example)
import { MinHeap } from '@nextian/dsa-js';
const heap = new MinHeap();
heap.push(3);
heap.push(1);
console.log(heap.top()); // 1Sort with Custom Comparator
import { sort } from '@nextian/dsa-js';
const arr = [3, 1, 4, 2];
sort(arr); // Default ascending
console.log(arr); // [1, 2, 3, 4]
sort(arr, (a, b) => a > b); // Descending
console.log(arr); // [4, 3, 2, 1]Binary Search
import { binarySearch, lower_bound, upper_bound } from '@nextian/dsa-js';
const arr = [1, 3, 3, 5, 7, 9];
console.log(binarySearch(arr, 5)); // 3
console.log(lower_bound(arr, 3)); // 1
console.log(upper_bound(arr, 3)); // 3
console.log(binarySearch(arr, 6)); // -1 (not found)