Skip to content

Commit 1b6caf9

Browse files
authored
feat: add Huffman Coding greedy algorithm implementation
Added Huffman Coding algorithm implementation. Huffman Coding is a greedy algorithm used for optimal prefix coding and data compression. Reference: https://en.wikipedia.org/wiki/Huffman_coding
1 parent 98431e2 commit 1b6caf9

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/main/java/com/thealgorithms/greedyalgorithms/HuffmanCoding.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44

55
/**
66
* Huffman Coding Algorithm
7-
* Greedy algorithm used for optimal prefix codes.
7+
*
8+
* Greedy algorithm used for optimal prefix coding and data compression.
9+
*
10+
* Time Complexity: O(n log n)
11+
* Space Complexity: O(n)
12+
*
13+
* https://en.wikipedia.org/wiki/Huffman_coding
814
*/
915
public class HuffmanCoding {
1016

@@ -19,8 +25,9 @@ static class Node implements Comparable<Node> {
1925
this.frequency = frequency;
2026
}
2127

28+
@Override
2229
public int compareTo(Node other) {
23-
return this.frequency - other.frequency;
30+
return Integer.compare(this.frequency, other.frequency);
2431
}
2532
}
2633

0 commit comments

Comments
 (0)