UNPKG

shared-huffman

Version:

A minimal Huffman encoder/decoder for message compression in Node.js and browser.

93 lines (65 loc) • 2.35 kB
# 🧠 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>