Skip to content

huffman encoding solution #498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions cchuffmancompression/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# 🗜️ Huffman Compression Tool in Node.js

This project implements a basic **file compressor and decompressor** using the [Huffman coding algorithm](https://en.wikipedia.org/wiki/Huffman_coding), built entirely in **Node.js**.

> 💡 This project was created as part of [John Crickett’s]() _"Coding Challenges"_ challenge series.

---

## 📦 Features

- 📊 Builds a Huffman tree from character frequencies
- 🔢 Generates binary codes for each character
- 💾 Compresses and decompresses `.txt` files
- 🧠 Minimal implementation without external libraries
- ⚡ Optimized with priority queues and efficient tree traversal

---

## 🚀 Getting Started

### Prerequisites

- [Node.js](https://nodejs.org/) (v14 or newer recommended)

### Clone the Repository

```bash
git clone https://github.com/your-username/huffman-compressor.git
cd huffman-compressor
```

### Usage

#### 1. Compress a file

```bash
node index..js input.txt --compress
```

- Creates a file called `compressed.txt`

#### 2. Decompress a file

```bash
node index.js compressed.txt --decompress
```

- Creates a file called `decompressed.txt`

---

## 🧠 How It Works

### Compression

1. Count character frequencies
2. Build a min-heap (priority queue)
3. Construct a binary Huffman tree
4. Generate binary codes for each character
5. Encode message using generated codes
6. Serialize the tree + encoded string to a file

### Decompression

1. Deserialize the Huffman tree
2. Traverse the tree according to each bit
3. Recover original characters and write output

---

## 📁 Project Structure

```
huffman-compressor/
├── index.js # Main script (compress/decompress)
├── input.txt # Sample input file
├── compressed.txt # Output after compression
├── decompressed.txt # Output after decompression
└── README.md # This file
```

---

## 🔧 Example

```
Input: hello huffman
Compressed: (binary string + serialized tree)
Decompressed: hello huffman
```

---
2 changes: 2 additions & 0 deletions cchuffmancompression/compressed.txt

Large diffs are not rendered by default.

Loading