Fix critical bugs, optimize performance, add test suite#1
Open
ghostyappzeta[bot] wants to merge 3 commits intomasterfrom
Open
Fix critical bugs, optimize performance, add test suite#1ghostyappzeta[bot] wants to merge 3 commits intomasterfrom
ghostyappzeta[bot] wants to merge 3 commits intomasterfrom
Conversation
- Fix decoder padding bit corruption: encoder writes total symbol count as first line of code_table.txt, decoder stops after that many symbols - Fix single-symbol input crash: wrap lone leaf in internal node so gen_codes produces code '0', handle in encoder fillNwrite_code_table - Fix BitSet.toByteArray() returning empty array for all-zero bits - Fix empty input crash: guard against empty freq_data before Collections.max() call, return early with friendly message - Fix static count accumulation: reset count=0 at start of build_freq_table - Fix resource leaks: use try-with-resources for BufferedReader instances, remove @SuppressWarnings annotations - Add input validation: catch NumberFormatException with line number context, reject negative values with helpful error message Co-authored-by: Vishal Gupta <vishal.gupta4081@gmail.com>
- Remove unnecessary Node/PairNode allocations in heap swap operations - Convert recursive manage_heap_upwards/downwards to iterative while loops - Fix comparison operators (>= to >) in heap upwards to avoid unnecessary swaps - Optimize Huffman code generation: StringBuilder with backtracking instead of String concatenation - Remove dead code: stub() methods and commented-out debug prints - Rename encoder/decoder classes to Encoder/Decoder (Java naming conventions) - Make heap_size private with getSize() accessor in BinaryHeap and D_aryHeap Co-authored-by: Vishal Gupta <vishal.gupta4081@gmail.com>
- Set up JUnit 5 test infrastructure with standalone console launcher JAR - Add BinaryHeap tests (8 tests): insert/del_min ordering, is_empty, single element, equal frequencies, get_root, del_min on empty, many elements - Add D_aryHeap tests (9 tests): 4-way, 2-way, 8-way variants plus same patterns as BinaryHeap - Add PairingHeap tests (8 tests): same patterns as other heaps - Add HuffmanTreeTest (8 tests): prefix-free property for all 3 heap builders, frequency-code-length relationship, single/two symbol trees, build_freq_table correctness - Add RoundtripTest (4 tests): full encode/decode pipeline verification for small input, two symbols, single symbol, many symbols - Add EdgeCaseTest (4 tests): empty input, single line, large numbers, all same symbol - Update Makefile with test target and .gitignore with lib/ exclusion - 41 total test methods, all passing Co-authored-by: Vishal Gupta <vishal.gupta4081@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request was generated by @kiro-agent 👻
Comment with /kiro fix to address specific feedback or /kiro all to address everything.
Learn about Kiro autonomous agent
Summary
A thorough exploration of the Huffman Coding codebase identified several gaps and optimization opportunities. This PR addresses them across three areas:
1. Critical Bug Fixes
code_table.txt; decoder stops after decoding exactly that many symbols instead of producing garbage from padding bitsbuild_tree_*methods detect single-symbol input and wrap the leaf in an internal nodecountis now reset to 0 at the start ofbuild_freq_table()BufferedReaderinstances wrapped in try-with-resourcesNumberFormatExceptioncaught with line-number context; negative integers detected and reported2. Performance and Code Quality
new Node(0,-1)temp variables that were immediately overwritten in all heap swap/del_min operationsmanage_heap_upwardsandmanage_heap_downwardsin BinaryHeap and D_aryHeap converted from tail recursion to while loops (prevents StackOverflowError on large heaps)>=to>inmanage_heap_upwardsto avoid unnecessary swaps on equal frequenciesgen_codes()andfillNwrite_code_table()now useStringBuilderwith backtracking instead ofcode+"0"/code+"1"stub()methods and commented-out debug codeencodertoEncoder,decodertoDecoderheap_sizemade private withgetSize()accessor3. Automated Test Suite (41 JUnit 5 tests, previously zero)
BinaryHeapTestD_aryHeapTestPairingHeapTestHuffmanTreeTestRoundtripTestEdgeCaseTestTesting
makeFiles Changed
16 files changed, 1,060 insertions, 232 deletions