2024-12-31

Algostructs - A NPM package providing the DS & Algos that JS is missing

Background

Years ago, I embarked on a journey to create a library for implementing core data structures and algorithms in JavaScript. However, I paused the effort, recognizing that I wanted a deeper understanding and to learn how other languages approached these concepts. After spending time delving into languages like Java, I returned to the idea, inspired by its robust implementation of data structures. The result is Algostructs, a TypeScript library designed to make these foundational tools accessible and efficient for JavaScript developers.

Why Algostructs is Structured the Way It Is

Java's approach to data structures is renowned for its blend of flexibility and rigor. Many Java developers leverage interfaces and classes to ensure modularity, reusability, and adherence to well-defined contracts. Algostructs borrows this philosophy:

  1. Modular Design: Each data structure and algorithm is encapsulated in its own module for clarity and reusability.
  2. Generic Typing: Like Java's generics, Algostructs employs TypeScript’s generics to provide type safety without sacrificing flexibility.
  3. Iterators and Utility Methods: Structures like the doubly linked list and trees include iterator implementations, echoing Java's emphasis on iteration and traversal utilities.

This approach ensures that the library is intuitive for developers familiar with Java while embracing idiomatic JavaScript practices.

Features of Algostructs

  1. Core Data Structures

    • Linked List: Implements singly and doubly linked lists with utilities for insertion, deletion, and iteration. Includes a reverse iterator for efficient traversal from the tail.

    • Tree: Offers a generic tree implementation with methods for traversal (in-order, pre-order, post-order) and node management.

    • Heap: Provides a binary heap with support for min-heap and max-heap configurations, useful for priority queues and sorting.

    • Queue: A simple queue implementation, including circular queues for scenarios requiring fixed-size buffers. While this part is a bit redundent due to the inclusion of Heaps, I grew to appreciate Java's implementation and so I added them :)

  2. Sorting Algorithms

    • Algostructs includes a variety of sorting algorithms:

    • QuickSort: In-place, non-stable sort for general-purpose use.

    • MergeSort: Stable, recursive sort for applications requiring element order preservation.

    • HeapSort: An efficient, in-place sorting technique for large datasets.

    • Counting Sort: A specialized algorithm for integer arrays with known ranges.

    • Hybrid Sort: Dynamically selects the best algorithm based on the input's size and structure.

  3. Search Algorithms

    • Binary Search: Optimized for sorted arrays, providing logarithmic time complexity.

    • Linear Search: Simple, works on unsorted arrays.

    • Jump Search, Interpolation Search, Exponential Search: Niche algorithms for specific use cases like uniformly distributed data or unbounded arrays.

  4. Additional Features

    • Smart Algorithm Selection: Functions like Sorting.sort and Searching.search intelligently choose the best algorithm based on input characteristics.

    • Detailed Results: Searching algorithms return metadata such as the number of comparisons and indices, enabling deeper insights into their performance.

When to Use Algostructs

Here's a couple scenerios where using Algostructs make sense:

  1. Learning and Experimentation: Perfect for developers studying data structures and algorithms, with clean, readable implementations.

  2. Application Development: Use the library’s efficient implementations for tasks like queue management, priority scheduling, or custom data storage solutions.

Key Takeaways

  • Inspired by Java, Optimized for JavaScript: Algostructs takes the rigor of Java's data structures and adapts it to the dynamic and flexible nature of JavaScript.

  • Type Safety: By leveraging TypeScript, the library ensures type safety without compromising usability.

  • Balanced Simplicity and Power: Whether you're solving basic interview problems or building complex applications, Algostructs provides tools that scale with your needs.

Future Plans

As the library grows, I’m considering expanding its scope to include:

  • Advanced graph algorithms (Dijkstra, A*).

  • Persistent data structures for immutable state management.

  • Integrations with visualization libraries for educational purposes.

Check out Algostructs on GitHub or NPM and start exploring today! I’d love to hear feedback and ideas for improvements or new features.