shared-huffman
v1.0.6
Published
A minimal Huffman encoder/decoder for message compression in Node.js and browser.
Downloads
10
Maintainers
Readme
🧠 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.
🚀 Install
npm install shared-huffman📦 Import
import {
buildFrequencyMap,
buildHuffmanTree,
generateCodes,
encode,
decode
} from 'shared-huffman';🔍 Example Usage
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 objectbuildHuffmanTree(freqMap)→ Builds Huffman treegenerateCodes(tree)→ Generates binary codes for each characterencode(str, codeMap)→ Encodes a stringdecode(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
👨💻 Author
Mayur Laxkar
Full-stack Developer | CS Undergrad
🌐 GitHub Profile
📄 License
MIT
