Skip to content

feat: add HuffmanCoding with fail-fast validation and immutable design#7289

Merged
DenizAltunkapan merged 1 commit intoTheAlgorithms:masterfrom
singhc7:feat/add-huffman-compression
Feb 26, 2026
Merged

feat: add HuffmanCoding with fail-fast validation and immutable design#7289
DenizAltunkapan merged 1 commit intoTheAlgorithms:masterfrom
singhc7:feat/add-huffman-compression

Conversation

@singhc7
Copy link
Contributor

@singhc7 singhc7 commented Feb 25, 2026

Description

This PR implements the Huffman Coding compression algorithm as requested in #6935.

Key Implementation Details:

  • Algorithm: Implements a greedy algorithm utilizing a PriorityQueue (min-heap) to build the optimal prefix tree.
  • Robustness: Incorporates fail-fast validation to immediately throw exceptions for unsupported characters, malformed binary payloads, and incomplete sequences.
  • Safety: Secures the internal dictionary state using Collections.unmodifiableMap to prevent external tampering.
  • Testing: Includes a comprehensive JUnit test suite (HuffmanCodingTest.java) covering standard usage, edge cases (nulls, empty strings, single characters), Unicode/special characters, and volume stress tests.

Complexity Analysis

  • Time Complexity: $O(n \log k)$, where $n$ is the length of the string and $k$ is the number of unique characters. (In the absolute worst case where all characters are unique, $k = n$, making it $O(n \log n)$).
  • Space Complexity: $O(k)$ to store the frequency map, the priority queue, and the resulting Huffman tree structure.

Closes #6935

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new algorithms include a corresponding test class that validates their functionality.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

@codecov-commenter
Copy link

codecov-commenter commented Feb 25, 2026

Codecov Report

❌ Patch coverage is 88.75000% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.40%. Comparing base (023f856) to head (a88aa03).

Files with missing lines Patch % Lines
...a/com/thealgorithms/compression/HuffmanCoding.java 88.75% 4 Missing and 5 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7289      +/-   ##
============================================
+ Coverage     79.37%   79.40%   +0.02%     
- Complexity     7020     7049      +29     
============================================
  Files           786      787       +1     
  Lines         23025    23105      +80     
  Branches       4523     4542      +19     
============================================
+ Hits          18276    18346      +70     
- Misses         4019     4024       +5     
- Partials        730      735       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@singhc7 singhc7 force-pushed the feat/add-huffman-compression branch from ce78182 to a88aa03 Compare February 25, 2026 04:09
@singhc7 singhc7 marked this pull request as ready for review February 25, 2026 04:13
@DenizAltunkapan DenizAltunkapan merged commit 7e5d9d4 into TheAlgorithms:master Feb 26, 2026
7 checks passed
@singhc7 singhc7 deleted the feat/add-huffman-compression branch February 26, 2026 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE REQUEST] Huffman Coding Compression Algorithm

3 participants