-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHuffmanEncoding.java
54 lines (54 loc) · 1.88 KB
/
HuffmanEncoding.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.*;
class Node {
char ch;
int frequency;
Node left, right;
Node(char ch, int frequency) {
this.ch = ch;
this.frequency = frequency;
left = right = null;
}
}// Node class for Huffman Tree
public class HuffmanEncoding {
public static void printCodes(Node root, String code) {
if (root == null)
return;
if (root.left == null && root.right == null) {
System.out.println(root.ch + ": " + code);
return;
}
printCodes(root.left, code + "0");
printCodes(root.right, code + "1");
}
public static void huffmanCoding(char[] chars, int[] freqs) {
int n = chars.length;
PriorityQueue<Node> queue = new PriorityQueue<>((a, b) -> a.frequency - b.frequency);
for (int i = 0; i < n; i++) {
queue.add(new Node(chars[i], freqs[i]));
}
while (queue.size() > 1) {
Node left = queue.poll();
Node right = queue.poll();
Node newNode = new Node('-', left.frequency + right.frequency);
newNode.left = left;
newNode.right = right;
queue.add(newNode);
}
printCodes(queue.poll(), "");
}//Function to build Huffman Tree and generate codes
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of characters: ");
int n = sc.nextInt();
char[] chars = new char[n];
int[] freqs = new int[n];
System.out.println("Enter characters and their frequencies:");
for (int i = 0; i < n; i++) {
System.out.print("Character " + (i + 1) + ": ");
chars[i] = sc.next().charAt(0);
System.out.print("Frequency of " + chars[i] + ": ");
freqs[i] = sc.nextInt();
}
huffmanCoding(chars, freqs);
}
}