-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakeTree.java
More file actions
59 lines (49 loc) · 1.64 KB
/
Copy pathMakeTree.java
File metadata and controls
59 lines (49 loc) · 1.64 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
import java.util.HashMap;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
public class MakeTree {
private String src;
MakeTree(String _src) {
this.src = _src;
}
public HashMap<Character, Integer> getFrequency() {
HashMap<Character, Integer> map = new HashMap<>();
for(char c: src.toCharArray()) {
map.put(c, map.getOrDefault(c, 0)+1);
}
return map;
}
public HuffmanNode getHuffmanTree() {
HashMap<Character, Integer> freq = getFrequency();
PriorityQueue<HuffmanNode> pq = new PriorityQueue<>((a,b) -> a.getValue()-b.getValue());
for(char key: freq.keySet()) {
pq.add(new HuffmanNode(freq.get(key), key));
}
while(pq.size()>1) {
HuffmanNode node1 = pq.poll();
HuffmanNode node2 = pq.poll();
HuffmanNode new_node = new HuffmanNode(node1.getValue()+node2.getValue(), '-');
new_node.setLeaves(node1, node2);
pq.add(new_node);
}
return pq.poll();
}
public String getSerializedTree() {
HuffmanNode root = getHuffmanTree();
StringBuilder str = new StringBuilder("");
Queue<HuffmanNode> q = new LinkedList<>();
q.add(root);
while(!q.isEmpty()) {
HuffmanNode node = q.poll();
if(node==null) {
str.append("*|");
continue;
}
str.append(node.getValue()+""+node.getData()+"|");
q.add(node.getLeftLeaf());
q.add(node.getRightLeaf());
}
return str.toString();
}
}