-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathhuffman_codig.java
More file actions
146 lines (99 loc) · 3.43 KB
/
huffman_codig.java
File metadata and controls
146 lines (99 loc) · 3.43 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;
//To create the node
//Structure of the node
class huffmanNode
{
int freq;
char c;
huffmanNode left = null;
huffmanNode right = null;
}
//comapare the node and sort in ascending order
class MyComparator implements Comparator<huffmanNode> {
public int compare(huffmanNode x, huffmanNode y)
{
return x.freq - y.freq;
}
}
public class huffman_codig {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
//Finding the frequency of all char and storing in map.
Map<Character, Integer> mp = new HashMap<>();
for(int i=0; i<str.length(); i++)
{
char ch = str.charAt(i);
mp.put(ch, mp.getOrDefault(ch, 0)+1);
}
//store the map in priorityQueue
PriorityQueue<Map.Entry<Character, Integer>> que = new PriorityQueue<>((a,b) -> Integer.compare(b.getValue(), a.getValue()));
for(Map.Entry<Character, Integer> entry: mp.entrySet())
{
que.offer(entry);
}
//make the priorityQueue of object of node
PriorityQueue<huffmanNode> q
= new PriorityQueue<huffmanNode>( new MyComparator());
int size = que.size();
for (int i = 0; i < size; i++) {
// creating a Huffman node object
// and add it to the priority queue.
huffmanNode hn = new huffmanNode();
hn.c = que.peek().getKey();
hn.freq = que.poll().getValue();
hn.left = null;
hn.right = null;
// add functions adds
// the huffman node to the queue.
q.add(hn);
}
huffmanNode root = null;
while(q.size()>1)
{
// first min extract.
huffmanNode x = q.peek();
q.poll();
// second min extract.
huffmanNode y = q.peek();
q.poll();
// new node f which is equal
huffmanNode f = new huffmanNode();
// to the sum of the frequency of the two nodes
// assigning values to the f node.
f.freq = x.freq + y.freq;
f.c = '-';
// first extracted node as left child.
f.left = x;
// second extracted node as the right child.
f.right = y;
// marking the f node as the root node.
root = f;
// add this node to the priority-queue.
q.add(f);
}
printCode(root, "");
// //Find the freq of each unique char.
// int size = que.size();
// for(int i=0; i<size; i++)
// {
// System.out.println(que.peek().getKey() + " :" +que.poll().getValue());
// }
}
public static void printCode(huffmanNode root, String s)
{
//return because now it won't read further
if(root.left == null && root.right == null)
{
System.out.println(root.c + " :" + s);
return;
}
//if not then go further
printCode(root.left, s + "0");
printCode(root.right, s + "1");
}
}