Last active
April 22, 2018 17:31
-
-
Save DarioAle/2eeb8ebf01cb9e25978142c3f5556cd4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package huffmanTarea; | |
import java.io.*; | |
import java.util.regex.Pattern; | |
//import java.util.Arrays; | |
public class huffAlgorithm { | |
public static int[] countCharacters(String fileName) throws IOException{ | |
FileInputStream in = null; | |
int [] a = new int[256]; | |
in = new FileInputStream(fileName); | |
int c; | |
while ((c = in.read()) != -1) | |
a[c]++; | |
in.close(); | |
return a; | |
} | |
public static priorityQueue cretateQueue(int [] frequencyArray){ | |
int count = 0; | |
for(int e = 0; e < 128; e++) | |
if(frequencyArray[e] != 0) | |
count++; | |
priorityQueue pq0 = new priorityQueue(count); | |
for(int e = 0; e < 128; e++) | |
if(frequencyArray[e] != 0) { | |
Node n = new Node((char) e, frequencyArray[e]); | |
pq0.offer(n); | |
} | |
return pq0; | |
} | |
public static Node buildHuffman(priorityQueue pq0) { | |
while(pq0.size > 1) { | |
Node p = pq0.poll(); | |
Node q = pq0.poll(); | |
Node n = new Node('_', p.w + q.w); | |
n.left = p; | |
n.right = q; | |
pq0.offer(n); | |
} | |
Node root = pq0.poll(); | |
return root; | |
} | |
public static String writeOuput(String FileName, int[][] map, priorityQueue pq) throws IOException{ | |
FileInputStream in2 = null; | |
FileOutputStream out = null; | |
String delimiter = "\\."; | |
Pattern pattern = Pattern.compile(delimiter, | |
Pattern.CASE_INSENSITIVE); | |
// Used to perform case insensitive search of the string | |
String[] result = pattern.split(FileName); | |
String outputName = result[0] + "Compressed.bin"; | |
in2 = new FileInputStream(FileName); | |
out = new FileOutputStream(outputName); | |
short totalNumberofBytes = ((short) (6 * pq.capacity)); | |
//System.out.println(totalNumberofBytes); | |
out.write((totalNumberofBytes >> 8) & 0xFF); | |
out.write((totalNumberofBytes & 0xFF)); | |
for(int i = 0; i < pq.size; i++) { | |
char charTemp = pq.minHeap[i].s; | |
int intTemp = pq.minHeap[i].w; | |
out.write((intTemp >> 24) & 0xFF); | |
out.write((intTemp >> 16) & 0xFF); | |
out.write((intTemp >> 8) & 0xFF); | |
out.write((intTemp >> 0) & 0xFF); | |
out.write((charTemp >> 8) & 0xFF); | |
out.write((charTemp >> 0) & 0xFF); | |
} | |
char buffer = 0; | |
byte AvailableBits = 7; | |
int c; | |
while ((c = in2.read()) != -1) { | |
int temp = map[0][c]; | |
for(int i = 0; i < map[1][c]; i++) { | |
System.out.printf("%d",temp & 1); | |
buffer |= (temp & 1) << AvailableBits; | |
if(--AvailableBits < 0) { | |
out.write(buffer); | |
buffer = 0; | |
AvailableBits = 7; | |
} | |
temp >>= 1; | |
} | |
} | |
in2.close(); | |
out.close(); | |
return outputName; | |
} | |
public static void print(Node node, int spaces) { | |
if(node.right != null)print(node.right, spaces + 1); | |
for(int i = 0; i < spaces; i++)System.out.printf("-");System.out.println(node); | |
if(node.left != null)print(node.left, spaces + 1); | |
} | |
public static void encode(Node node, int register, int[][] charArray, int length ) { | |
if(node.left == null && node.right == null) { | |
System.out.printf("%d, %c, %d, %d\n",register, node.s, length, reverse(length,register)); | |
charArray[0][node.s] = reverse(length,register); | |
charArray[1][node.s] = length; | |
} | |
if(node.left != null)encode(node.left, register << 1, charArray, length + 1); | |
if(node.right != null)encode(node.right, (register << 1) | 1, charArray, length + 1); | |
} | |
private static int reverse(int n, int reverse) { | |
int aux = reverse; | |
reverse = 0; | |
for(int i = 0; i < n; i++) { | |
// System.out.printf("iteration %d, %d, %d\n",i,reverse, ((aux >> i) & 1)); | |
reverse |= (aux >> i) & 1; | |
reverse <<= 1; | |
} | |
return reverse >> 1; | |
} | |
static class Node implements Comparable<Node>{ | |
Node left = null, right = null; | |
int w = 0; | |
char s = 0x00; | |
public Node(char c, int w) { | |
s = c; | |
this.w = w; | |
} | |
@Override | |
public int compareTo(Node o) { | |
return this.w - | |
o.w; | |
} | |
@Override | |
public String toString() { | |
//return String.format("%d, %d, (%c)\n",(int)s, w, s); | |
return String.format("%d, (%c)\n", w, s); | |
} | |
@Override | |
public Node clone() { | |
Node n = new Node(this.s, this.w); | |
n.left = null; | |
n.right = null; | |
return n; | |
} | |
} | |
static class priorityQueue { | |
int capacity = 0; | |
int size = 0; | |
Node[] minHeap; | |
public priorityQueue(int c) { | |
this.capacity = c; | |
this.minHeap = new Node[this.capacity]; | |
} | |
public void offer(Node e) { | |
int index = size; | |
minHeap[index] = e; | |
while(minHeap[index].compareTo(minHeap[(index - 1) / 2]) < 0 && index != 0){ | |
Node temp = minHeap[(index - 1) / 2]; | |
minHeap[(index - 1) / 2] = minHeap[index]; | |
minHeap[index] = temp; | |
index = (index - 1) / 2; | |
} | |
size++; | |
} | |
public int min(int index) { | |
index <<= 1; | |
if(index + 1 >= size) return size; | |
if(index + 2 >= size) return index + 1; | |
return minHeap[index + 1].compareTo(minHeap[index + 2]) <= 0 ? index + 1 : index + 2; | |
} | |
public Node poll() { | |
Node root = minHeap[0]; | |
minHeap[0] = minHeap[size - 1]; | |
int index = 0; | |
int smallest = min(index); | |
while(minHeap[index].compareTo(minHeap[smallest]) > 0) { | |
Node temp = minHeap[smallest]; | |
minHeap[smallest] = minHeap[index]; | |
minHeap[index] = temp; | |
index = smallest; | |
smallest = min(index); | |
if(smallest >= size) break; | |
} | |
size--; | |
return root; | |
} | |
public Node peek() { | |
return minHeap[0]; | |
} | |
@Override | |
public priorityQueue clone() { | |
priorityQueue p = new priorityQueue(this.capacity); | |
p.size = this.size; | |
for(int i = 0; i < this.minHeap.length; i++) | |
p.minHeap[i] = this.minHeap[i].clone(); | |
return p; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment