shared-huffman
Version:
A minimal Huffman encoder/decoder for message compression in Node.js and browser.
93 lines (65 loc) ⢠2.35 kB
Markdown
# š§ shared-huffman
**A lightweight Huffman compression algorithm implementation in JavaScript.**
Effortlessly encode and decode strings using binary trees. Great for learning or integrating into web projects.
<p align="center">
<a href="https://www.npmjs.com/package/shared-huffman">
<img src="https://img.shields.io/npm/v/shared-huffman?color=blue&style=flat-square" alt="npm version" />
</a>
</p>
---
## š Install
```bash
npm install shared-huffman
```
## š¦ Import
```js
import {
buildFrequencyMap,
buildHuffmanTree,
generateCodes,
encode,
decode
} from 'shared-huffman';
```
## š Example Usage
```js
const str = "hello huffman";
const freqMap = buildFrequencyMap(str);
const tree = buildHuffmanTree(freqMap);
const codeMap = generateCodes(tree);
const encoded = encode(str, codeMap);
console.log("Encoded:", encoded);
const decoded = decode(encoded, tree);
console.log("Decoded:", decoded);
```
## š§© Features
- ā
Simple & Dependency-Free
- ā
Modern ES Module support
- ā
Educational use-case for DSA learners
- ā
Accurate string compression & decompression
- ā
Plug-and-play for frontend/backend
## š API Reference
- `buildFrequencyMap(str)` ā Returns character frequency object
- `buildHuffmanTree(freqMap)` ā Builds Huffman tree
- `generateCodes(tree)` ā Generates binary codes for each character
- `encode(str, codeMap)` ā Encodes a string
- `decode(encodedStr, tree)` ā Decodes a binary string
## š Learnings & Why This?
Huffman coding is a fundamental compression technique taught in DSA courses and coding interviews. This project helps you understand:
- ā
Greedy algorithms in action
- ā
Binary Trees & Traversals
- ā
Priority Queues (via frequency sorting)
- ā
Bitwise compression concepts
## š Useful Links
- [š¦ View on NPM](https://www.npmjs.com/package/shared-huffman)
- [š» Source on GitHub](https://github.com/mayur777-ui/shared-huffman)
## šØāš» Author
**Mayur Laxkar**
Full-stack Developer | CS Undergrad
š [GitHub Profile](https://github.com/mayur777-ui)
## š License
MIT
---
<p align="center">
ā If you like this project, please give it a star on <a href="https://github.com/mayur777-ui/huffpack">GitHub</a>!
</p>