Skip to content

Commit 7be7ef2

Browse files
aayushi2601abranhe
authored andcommitted
Adding Huffman Coding Algorithm
1 parent 6e7e8b9 commit 7be7ef2

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

greedyalgorithms/HuffmanCoding.java

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import java.util.PriorityQueue;
2+
import java.util.Scanner;
3+
import java.util.Comparator;
4+
5+
// Implementation of Hoffman Coding algoritms.
6+
class HuffmanNode {
7+
8+
int data;
9+
char c;
10+
HuffmanNode left;
11+
HuffmanNode right;
12+
}
13+
14+
// comparator class helps to compare the node
15+
// on the basis of one of its attribute.
16+
// Here we will be compared
17+
// on the basis of data values of the nodes.
18+
class MyComparator implements Comparator<HuffmanNode> {
19+
public int compare(HuffmanNode x, HuffmanNode y)
20+
{
21+
22+
return x.data - y.data;
23+
}
24+
}
25+
26+
public class Huffman {
27+
28+
// recursive function to print the
29+
// huffman-code through the tree traversal.
30+
// Here s is the huffman - code generated.
31+
public static void printCode(HuffmanNode root, String s)
32+
{
33+
34+
// base case; if the left and right are null
35+
// then its a leaf node and we print
36+
// the code s generated by traversing the tree.
37+
if (root.left == null && root.right == null && Character.isLetter(root.c)) {
38+
// c is the character in the node
39+
System.out.println(root.c + ":" + s);
40+
return;
41+
}
42+
43+
// if we go to left then add "0" to the code.
44+
// if we go to the right add"1" to the code.
45+
46+
// recursive calls for left and
47+
// right sub-tree of the generated tree.
48+
printCode(root.left, s + "0");
49+
printCode(root.right, s + "1");
50+
}
51+
52+
// main function
53+
public static void main(String[] args)
54+
{
55+
56+
Scanner s = new Scanner(System.in);
57+
// number of characters.
58+
int n = 6;
59+
char[] charArray = { 'a', 'b', 'c', 'd', 'e', 'f' };
60+
int[] charfreq = { 5, 9, 12, 13, 16, 45 };
61+
62+
// creating a priority queue q.
63+
// makes a min-priority queue(min-heap).
64+
PriorityQueue<HuffmanNode> q
65+
= new PriorityQueue<HuffmanNode>(n, new MyComparator());
66+
67+
for (int i = 0; i < n; i++) {
68+
69+
// creating a huffman node object
70+
// and adding it to the priority-queue.
71+
HuffmanNode hn = new HuffmanNode();
72+
73+
hn.c = charArray[i];
74+
hn.data = charfreq[i];
75+
76+
hn.left = null;
77+
hn.right = null;
78+
79+
// add functions adds
80+
// the huffman node to the queue.
81+
q.add(hn);
82+
}
83+
84+
// create a root node
85+
HuffmanNode root = null;
86+
87+
// Here we will extract the two minimum value
88+
// from the heap each time until
89+
// its size reduces to 1, extract until
90+
// all the nodes are extracted.
91+
while (q.size() > 1) {
92+
93+
// first min extract.
94+
HuffmanNode x = q.peek();
95+
q.poll();
96+
97+
// second min extarct.
98+
HuffmanNode y = q.peek();
99+
q.poll();
100+
101+
// new node f which is equal
102+
HuffmanNode f = new HuffmanNode();
103+
104+
// to the sum of the frequency of the two nodes
105+
// assigning values to the f node.
106+
f.data = x.data + y.data;
107+
f.c = '-';
108+
109+
// first extracted node as left child.
110+
f.left = x;
111+
112+
// second extracted node as the right child.
113+
f.right = y;
114+
115+
// marking the f node as the root node.
116+
root = f;
117+
118+
// add this node to the priority-queue.
119+
q.add(f);
120+
}
121+
122+
// print the codes by traversing the tree
123+
printCode(root, "");
124+
}
125+
}

0 commit comments

Comments
 (0)