-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathhuffmanEncoder.java
More file actions
65 lines (59 loc) · 1.86 KB
/
huffmanEncoder.java
File metadata and controls
65 lines (59 loc) · 1.86 KB
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
55
56
57
58
59
60
61
62
63
64
65
import java.util.Comparator;
import java.util.PriorityQueue;
public class huffmanAlgo {
static class Node {
int data;
char ch;
Node left;
Node right;
public Node(int data) {
this.data = data;
}
public Node(int data, char ch) {
this.data = data;
this.ch = ch;
}
}
public static void printcode(Node n){
printcode(n,"");
}
public static void printcode(Node n, String str) { //printing the encoded huffman code
if (n.left == null && n.right == null)//always only the leaf node will have a character stored
{
System.out.println(n.ch + ":\t" + str);
return;
}
printcode(n.left, str + "0");
//recursing ustil reaching the leaf
printcode(n.right, str + "1");
}
static class comp implements Comparator<Node> {
public int compare(Node a, Node b) {
return a.data - b.data;
}
}
public static void main(String[] args) {
char[] arr = { 'h', 'e', 't', 'r', 'p', 'a' ,'l'};
int[] freq = {1,2,5,8,11,20,21};
//taking priorly sorted elements with their frequencies
PriorityQueue<Node> q= new PriorityQueue<>(freq.length,new comp());
int i = 0;
while(i<freq.length){
q.add(new Node(freq[i],arr[i]));
i++;//transferring elements to Node
}
Node n = null;
while(q.size() != 1) {
int sNode = 0;
Node temp1 = q.remove();
//step by step removing nodes from tree and storing their freq sum as parent node
Node temp2 = q.remove();
sNode += temp1.data + temp2.data;
n = new Node(sNode);
q.add(n);
n.left = temp1;
n.right = temp2;
}
printcode(n);
}
}