Skip to content

Comprehensive data structures and algorithms implementation in JavaScript/TypeScript for frontend engineers

License

Notifications You must be signed in to change notification settings

ovenzeze/data-structures-and-algorithms-in-javascript

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

24 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Data Structures and Algorithms in JavaScript

GitHub stars GitHub license Language TypeScript JavaScript

A foundational course on data structures and algorithms for frontend engineers. All data structures and algorithms are implemented in JavaScript and TypeScript.

✨ Features

  • πŸ“š Complete Data Structure Implementations

    • Linear data structures: Arrays, Stacks, Queues, Linked Lists (Singly, Doubly, Circular)
    • Non-linear data structures: Binary Trees, Binary Search Trees (BST)
  • πŸ” Common Algorithm Implementations

    • Binary Search (Iterative and Recursive)
    • Binary Tree Traversal (Pre-order, In-order, Post-order)
    • Binary Tree Algorithms (Max Width, Min Depth, Reversal, etc.)
    • Linked List Detection (Cycle Detection)
  • πŸ’» Dual Language Support

    • TypeScript for core data structures (type-safe)
    • JavaScript for extended algorithms (better compatibility)
  • πŸ“– Detailed Comments

    • Each implementation includes comprehensive comments
    • Clear explanations of algorithm logic and implementation details

πŸš€ Quick Start

Install Dependencies

npm install

Run the Project

# Development mode (auto-watch file changes)
npm start

# Compile TypeScript
npm run build

# Watch mode compilation
npm run watch

Usage Examples

import { Stack } from './src/base/stack'
import { Queue } from './src/base/queue'
import { BinarySearch } from './src/base/binary-search'

// Stack usage
const stack = new Stack(['one', 'two', 'three'])
stack.push('four')
stack.pop()
console.log(stack.peek()) // 'three'

// Queue usage
const queue = new Queue(['one', 'two', 'three'])
queue.enqueue('four')
queue.dequeue()
console.log(queue.front()) // 'two'

// Binary search
const list = [1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12]
const index = BinarySearch(list, 11)
console.log(index) // 9

πŸ“ Project Structure

data-structures-and-algorithms-in-javascript/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ base/              # Core data structures implemented in TypeScript
β”‚   β”‚   β”œβ”€β”€ stack.ts       # Stack
β”‚   β”‚   β”œβ”€β”€ queue.ts       # Queue
β”‚   β”‚   β”œβ”€β”€ linked-list.ts # Linked List
β”‚   β”‚   └── binary-search.ts # Binary Search
β”‚   β”œβ”€β”€ js/                # Extended algorithms implemented in JavaScript
β”‚   β”‚   β”œβ”€β”€ binary-tree.js # Binary tree related
β”‚   β”‚   β”œβ”€β”€ linked-list.js # Linked list related
β”‚   β”‚   └── ...
β”‚   └── index.ts           # Entry file (example code)
β”œβ”€β”€ docs/                   # Documentation
β”œβ”€β”€ dist/                   # TypeScript compilation output
└── package.json

πŸ› οΈ Tech Stack

  • TypeScript 3.8+ - Type-safe core data structure implementations
  • JavaScript (ES2017+) - Algorithm implementations
  • Node.js - Runtime environment

πŸ“š Data Structures Explained

Linear Data Structures

Arrays

An array is defined as a linear collection used to store elements. The position of data in this collection is calculated through indices. To ensure efficient data storage and access, indices must be numbers, allowing quick access to the actual data location in memory by calculating offsets through indices.

In JavaScript, arrays are implemented using objects, and numeric indices are converted to strings during use and storage.

Stack

A stack can be simply understood as a restricted array. A stack is a data structure that follows the Last-In-First-Out (LIFO) principle, where only the top element can be accessed or operated on at a time. A real-world analogy would be a stack of plates waiting to be washed in a restaurant.

Main methods:

  • push(item) - Push an item onto the stack
  • pop() - Pop an item from the stack
  • peek() - Get the top element
  • length() - Get the stack length
  • clear() - Clear the stack

Queue

Similar to a stack, a queue can be understood as a restricted array. A queue is a data structure that follows the First-In-First-Out (FIFO) principle, where only the front element can be accessed or operated on, and elements are added at the rear. A real-world analogy would be people waiting in line at a bank to conduct business.

Main methods:

  • enqueue(item) - Enqueue an item
  • dequeue() - Dequeue an item
  • front() - Get the front element
  • back() - Get the rear element
  • display() - Display queue contents

Linked List

A linked list is a non-contiguous, non-sequential storage structure where elements are connected through links (pointers). Linked lists include: singly linked lists, doubly linked lists, and circular linked lists.

Compared to arrays, which use indices (offsets) to associate elements, meaning data must be stored in a contiguous memory space with a fixed size, linked lists access elements through their own links (pointers). Each node records the location of the next node, not relying on storage contiguity and sequentiality.

This project implements:

  • Singly linked lists
  • Doubly linked lists
  • Circular linked lists (singly and doubly)
  • Linked list cycle detection

Non-linear Data Structures

Binary Tree

A tree is a non-linear data structure that stores data in a hierarchical manner. Trees are commonly used to store data with hierarchical relationships, such as files in a file system.

A binary tree restricts each node to have at most two child nodes. A binary search tree is a special type of binary tree where smaller values are stored in the left node and larger values in the right node.

This project implements:

  • Binary Search Tree (BST) insertion, search, and deletion
  • Pre-order, in-order, and post-order traversal
  • Maximum width of binary tree
  • Minimum depth of binary tree
  • Binary tree reversal
  • Binary search tree validation

πŸ” Algorithm Implementations

Search Algorithms

  • Binary Search (BinarySearch)
    • Iterative implementation
    • Recursive implementation
    • Variants for handling duplicate elements
    • Find all matching indices

Tree Algorithms

  • Binary tree traversal (pre-order, in-order, post-order)
  • Maximum width calculation
  • Minimum depth calculation
  • Binary tree reversal
  • Binary search tree validation

Linked List Algorithms

  • Cycle detection
  • Linked list reversal

πŸ“– Learning Value

This project is suitable for:

  • βœ… Frontend engineers learning data structures and algorithms
  • βœ… Developers preparing for technical interviews
  • βœ… JavaScript developers who want to understand underlying implementations
  • βœ… Algorithm learners who need reference implementations

Why JavaScript/TypeScript?

Since JavaScript has inherent limitations in data structure support, this project implements common data structures from scratch to help frontend engineers:

  • Gain deep understanding of the underlying principles of data structures
  • Master the core concepts of algorithm implementation
  • Improve code quality and problem-solving skills

🀝 Contributing

Contributions are welcome! Please feel free to submit Issues and Pull Requests!

  1. Fork this project
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the ISC License.

πŸ‘€ Author

isake


⭐ If this project helps you, please give it a Star!

About

Comprehensive data structures and algorithms implementation in JavaScript/TypeScript for frontend engineers

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •