Skip to content

Cluuny/DataStructures

Repository files navigation

Data Structures in JavaScript

A comprehensive collection of data structures implemented in JavaScript with full documentation, testing, and modern ES6+ features.

Features

  • Singly Linked List - Linear data structure with nodes containing data and next pointer
  • Doubly Linked List - Linear data structure with nodes containing data, next, and previous pointers
  • Stack - LIFO (Last In, First Out) data structure
  • Queue - FIFO (First In, First Out) data structure
  • Hash Table - Key-value storage with collision resolution
  • Priority Queue - Queue where elements are served based on priority
  • Binary Tree - Hierarchical data structure with at most two children per node
  • Graph - Network of nodes connected by edges (directed/undirected)

Installation

npm install

Usage

Importing Data Structures

// ES6 Modules
import { SimpleLinkedList } from './SimpleLinkedList.js';
import { DoubleLinkedList } from './DoubleLinkedList.js';
import { Stack } from './Stack.js';
import { Queue } from './Queue.js';
import { HashTable } from './HashTable.js';
import { PriorityQueue } from './PriorityQueue.js';
import { BinaryTree } from './tree/BinaryTree.js';
import { Graph } from './graph/Graph.js';

Examples

Simple Linked List

import { SimpleLinkedList } from './SimpleLinkedList.js';

const list = new SimpleLinkedList();
list.add(1);
list.add(2);
list.add(3);
console.log(list.toString()); // [1, 2, 3]

// Iterate over the list
for (const value of list) {
  console.log(value);
}

// Convert to array
const array = list.toArray(); // [1, 2, 3]

Stack

import { Stack } from './Stack.js';

const stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
console.log(stack.peek()); // 3
console.log(stack.pop()); // 3
console.log(stack.toString()); // Stack(2, 1)

Queue

import { Queue } from './Queue.js';

const queue = new Queue();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
console.log(queue.front()); // 1
console.log(queue.dequeue()); // 1
console.log(queue.toString()); // Queue(2, 3)

Hash Table

import { HashTable } from './HashTable.js';

const hashTable = new HashTable();
hashTable.set('name', 'John');
hashTable.set('age', 30);
console.log(hashTable.get('name')); // John
console.log(hashTable.has('age')); // true
console.log(hashTable.toString()); // HashTable(name: John, age: 30)

Priority Queue

import { PriorityQueue } from './PriorityQueue.js';

const pq = new PriorityQueue();
pq.enqueue('low priority', 1);
pq.enqueue('high priority', 3);
pq.enqueue('medium priority', 2);
console.log(pq.peek()); // high priority
console.log(pq.dequeue()); // high priority

Binary Tree

import { BinaryTree } from './tree/BinaryTree.js';

const tree = new BinaryTree(1);
tree.insert(2, null, 'left');
tree.insert(3, null, 'right');
console.log(tree.inorderTraversal()); // [2, 1, 3]
console.log(tree.getHeight()); // 1

Graph

import { Graph } from './graph/Graph.js';

const graph = new Graph();
graph.addNode('A', 'B', 'C');
graph.addEdge('A', 'B', 5);
graph.addEdge('B', 'C', 3);
console.log(graph.breadthFirstSearch('A')); // ['A', 'B', 'C']
console.log(graph.dijkstra('A', 'C')); // Shortest path

Testing

Run the test suite:

# Run all tests
npm test

# Run tests in watch mode
npm run test:watch

# Run tests with coverage
npm run test:coverage

API Documentation

Common Methods

All data structures implement these common methods:

  • isEmpty() - Returns true if the structure is empty
  • getSize() / size() - Returns the number of elements
  • clear() - Removes all elements
  • toArray() - Converts to array representation
  • toString() - Returns string representation
  • [Symbol.iterator]() - Enables iteration with for...of loops

Time Complexity

Each method includes JSDoc comments with Big O time complexity:

  • O(1) - Constant time operations
  • O(log n) - Logarithmic time operations
  • O(n) - Linear time operations
  • O(n log n) - Linearithmic time operations

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Ensure all tests pass
  5. Submit a pull request

License

ISC License - see LICENSE file for details.

About

Estructuras de datos útiles dentro de JavaScript ;)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published