-
Notifications
You must be signed in to change notification settings - Fork 40
/
Huffman.java
113 lines (94 loc) · 2.28 KB
/
Huffman.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
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
package huffman_coding;
import java.util.*;
import java.util.PriorityQueue;
class Node
{
char ch;
int freq;
Node left;
Node right;
Node(char ch, int freq)
{
this.ch=ch;
this.freq=freq;
}
Node(char ch, int freq, Node left, Node right)
{
this.ch =ch;
this.freq=freq;
this.left =left;
this.right = right;
}
}
public class program
{
public static void encode(Node root , String str, Map<Character,String>Huffman_code)
{
if(root==null)
return;
if(root.left==null && root.right==null)
{
Huffman_code.put(root.ch, str);
}
encode(root.left, str+'0',Huffman_code);
encode(root.right,str+'1',Huffman_code);
}
public static int decode(Node root, int index, StringBuilder sb)
{
if(root == null)
return index;
if (root.left==null && root.right==null)
{
System.out.print(root.ch);
return index;
}
index++;
if(sb.charAt(index)=='0')
index = decode(root.left,index,sb);
else
index = decode(root.right,index,sb);
return index;
}
public static void buildHuffmantree(String text)
{
Map<Character, Integer> freq = new HashMap<>();
for(char c: text.toCharArray())
{
freq.put(c, freq.getOrDefault(c,0)+1);
}
PriorityQueue<Node>pq = new PriorityQueue<>(Comparator.comparingInt(l->l.freq));
for(var entry : freq.entrySet())
{
pq.add(new Node(entry.getKey(),entry.getValue()));
}
while(pq.size()!=1)
{
Node left = pq.poll();
Node right = pq.poll();
int sum_freq = left.freq + right.freq;
pq.add(new Node('\0',sum_freq,left,right));
}
Node root = pq.peek();
Map<Character,String> Huffman_code = new HashMap<>();
encode(root,"",Huffman_code);
System.out.println("Huffman codes are: "+Huffman_code);
System.out.println("original string: "+text);
StringBuilder sb = new StringBuilder();
for(char ch : text.toCharArray())
{
sb.append(Huffman_code.get(ch));
}
System.out.println("Encoded string is: "+sb);
System.out.print("Decoded Huffman code is: " );
int index = -1;
while(index<sb.length()-2)
{
index = decode(root,index,sb);
}
}
public static void main(String args[])
{
String text = "aa bb ccc dddd";
buildHuffmantree(text);
}
}