-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHuffManCoding.java
More file actions
60 lines (50 loc) · 1.47 KB
/
HuffManCoding.java
File metadata and controls
60 lines (50 loc) · 1.47 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
package Greedy;
import java.util.ArrayList;
import java.util.PriorityQueue;
public class HuffManCoding {
public ArrayList<String> huffmanCodes(String S, int[] f, int N) {
PriorityQueue<Node> heap = new PriorityQueue<>((a, b) -> {
if (a.data == b.data) {
return 0;
}
return a.data - b.data;
});
for (int i = 0; i < f.length; i++) {
heap.add(new Node(f[i]));
}
while (heap.size() >= 2) {
Node temp1 = heap.remove();
Node temp2 = heap.remove();
heap.add(new Node(temp1.data + temp2.data, temp1, temp2));
}
Node tree = heap.remove();
ArrayList<String> ans = new ArrayList<>();
findCode(tree, "", ans);
return ans;
}
public void findCode(Node root, String str, ArrayList<String> ans) {
if (root == null)
return;
if (root.left == null && root.right == null) {
ans.add(str);
return;
}
findCode(root.left, str + "0", ans);
findCode(root.right, str + "1", ans);
}
static class Node {
int data;
Node left;
Node right;
Node(int data) {
this.data = data;
this.left = null;
this.right = null;
}
Node(int data, Node left, Node right) {
this.data = data;
this.left = left;
this.right = right;
}
}
}