Skip to content

Commit cf17ced

Browse files
Create Huffman.java
1 parent a358394 commit cf17ced

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed

17 Greedy Algorithms/Huffman.java

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

0 commit comments

Comments
 (0)