Skip to content

Commit 8f76df3

Browse files
authored
Add Compression Algorithm
1 parent e44016c commit 8f76df3

8 files changed

+209
-0
lines changed
859 Bytes
Binary file not shown.

Compression/bin/HEncoder$Node.class

683 Bytes
Binary file not shown.

Compression/bin/HEncoder.class

3.59 KB
Binary file not shown.

Compression/bin/compressclient.class

756 Bytes
Binary file not shown.

Compression/bin/genericheap.class

2.79 KB
Binary file not shown.

Compression/src/HEncoder.java

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import java.util.ArrayList;
2+
import java.util.Comparator;
3+
import java.util.HashMap;
4+
5+
public class HEncoder {
6+
7+
public HashMap<Character, String> encoder = new HashMap<>();
8+
public HashMap<String, Character> decoder = new HashMap<>();
9+
10+
private static class Node {
11+
12+
Character ch;
13+
Integer freq;
14+
Node left;
15+
Node right;
16+
17+
public static final Nodecomparator Ctor = new Nodecomparator();
18+
19+
public static class Nodecomparator implements Comparator<Node> {
20+
21+
@Override
22+
public int compare(Node o1, Node o2) {
23+
return o2.freq - o1.freq;
24+
}
25+
26+
}
27+
}
28+
29+
public HEncoder(String feeder) {
30+
// 1. freq map
31+
HashMap<Character, Integer> freqmap = new HashMap<>();
32+
for (int i = 0; i < feeder.length(); ++i) {
33+
char ch = feeder.charAt(i);
34+
if (freqmap.containsKey(ch)) {
35+
freqmap.put(ch, freqmap.get(ch) + 1);
36+
} else {
37+
freqmap.put(ch, 1);
38+
}
39+
}
40+
41+
// 2. prepare the heap from keyset
42+
genericheap<Node> heap = new genericheap<Node>(Node.Ctor);
43+
ArrayList<Character> k = new ArrayList<>(freqmap.keySet());
44+
for (Character c : k) {
45+
Node n = new Node();
46+
n.ch = c;
47+
n.left = null;
48+
n.right = null;
49+
n.freq = freqmap.get(c);
50+
heap.add(n);
51+
}
52+
53+
// 3.Prepare tree, remove two , merge, add it back
54+
Node fn = new Node();
55+
while (heap.size() != 1) {
56+
Node n1 = heap.removeHP();
57+
Node n2 = heap.removeHP();
58+
fn = new Node();
59+
60+
fn.freq = n1.freq + n2.freq;
61+
fn.left = n1;
62+
fn.right = n2;
63+
64+
heap.add(fn);
65+
}
66+
67+
// 4. traverse
68+
69+
traverse(heap.removeHP(), "");
70+
}
71+
72+
private void traverse(Node node, String osf) {
73+
74+
if (node.left == null && node.right == null) {
75+
encoder.put(node.ch, osf);
76+
decoder.put(osf, node.ch);
77+
return;
78+
}
79+
traverse(node.left, osf + "0");
80+
traverse(node.right, osf + "1");
81+
82+
}
83+
84+
public String compress(String str) {
85+
String rv = "";
86+
for (int i = 0; i < str.length(); ++i) {
87+
rv += encoder.get(str.charAt(i));
88+
}
89+
return rv;
90+
}
91+
92+
public String decompress(String str) {
93+
String s = "";
94+
String code = "";
95+
for (int i = 0; i < str.length(); ++i) {
96+
code += str.charAt(i);
97+
if (decoder.containsKey(code)) {
98+
s += decoder.get(code);
99+
code = "";
100+
}
101+
}
102+
103+
return s;
104+
}
105+
}

Compression/src/compressclient.java

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
public class compressclient {
3+
4+
public static void main(String[] args) {
5+
6+
HEncoder h= new HEncoder("aaaabbbcccccccccccdddd");
7+
System.out.println(h.compress("aabccd"));
8+
System.out.println(h.decompress("101011000111"));
9+
}
10+
11+
}

Compression/src/genericheap.java

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
2+
3+
import java.util.ArrayList;
4+
import java.util.Comparator;
5+
6+
public class genericheap<T> {
7+
8+
private ArrayList<T> data = new ArrayList<>();
9+
private Comparator<T> ctor;
10+
11+
public genericheap(Comparator<T> ctor) {
12+
this.ctor=ctor;
13+
}
14+
15+
public int size() {
16+
return data.size();
17+
}
18+
19+
public boolean isEmpty() {
20+
return data.isEmpty();
21+
}
22+
23+
public void display() {
24+
System.out.println(this.data);
25+
}
26+
27+
public void add(T integer) {
28+
data.add(integer);
29+
upheapify(data.size() - 1);
30+
}
31+
32+
private void upheapify(int ci) {
33+
if (ci == 0) {
34+
return;
35+
}
36+
int pi = (ci - 1) / 2;
37+
if (isLarger(ci,pi) == true) {
38+
swap(ci, pi);
39+
upheapify(pi);
40+
}
41+
}
42+
43+
private boolean isLarger(int i, int j) {
44+
T ith = data.get(i);
45+
T jth = data.get(j);
46+
if(ctor.compare(ith,jth)>0)
47+
{
48+
return true;
49+
}
50+
else
51+
{
52+
return false;
53+
}
54+
}
55+
56+
private void swap(int ci, int pi) {
57+
T ith = data.get(ci);
58+
T jth=data.get(pi);
59+
data.set(ci, jth);
60+
data.set(pi, ith);
61+
}
62+
63+
public T getHP() {
64+
return data.get(0);
65+
}
66+
67+
public T removeHP() {
68+
69+
swap(0, data.size() - 1);
70+
T rv=data.remove(data.size()-1);
71+
downheapify(0);
72+
return rv;
73+
}
74+
75+
private void downheapify(int pi) {
76+
int lci = 2 * pi + 1;
77+
int rci = 2 * pi + 2;
78+
79+
int max = pi;
80+
81+
if (lci < data.size() && isLarger(lci, max) == true) {
82+
max = lci;
83+
}
84+
if (rci < data.size() && isLarger(rci, max) == true) {
85+
max = rci;
86+
}
87+
if (max != pi) {
88+
swap(pi, max);
89+
downheapify(max);
90+
}
91+
}
92+
93+
}

0 commit comments

Comments
 (0)