IMPLEMENTACION DEL ALGORITMO DE HUFFMAN EN JAVA
Clase nodo:
package huffmanexe;
public class Node
{
public int frequency;
public char c;
public Node left;
public Node right;
public Node(int frequency, char c, Node left, Node right)
{
this.frequency = frequency;
this.c = c;
this.left = left;
this.right = right;
}
public Node()
{
//does Nothing
}
public Node addNode(Node node1, Node node2)
{
if(node1.frequency < node2.frequency)
{
left = node1;
right = node2;
}
else
{
right = node1;
left = node2;
}
frequency = node1.frequency + node2.frequency;
return this;
}
}
Clase tree:
package huffmanexe;
/**
*
* @author franklin aimituma suyo
*/
public class tree extends Node
{
private Node root;
public tree()
{
root = null;
}
public tree(Node node1, Node node2)
{
root = super.addNode(node1, node2);
}
public void insertNode(int freq, char c)
{
root.frequency = freq;
root.c = c;
root.left = null;
root.right = null;
}
public void insertNode(int freq, char c, Node left, Node right)
{
root.frequency = freq;
root.c = c;
this.root.left = left;
this.root.right = right;
}
public void insertNode(Node node)
{
this.root.frequency = node.frequency;
this.root.c = node.c;
this.root.left = node.left;
this.root.right = node.right;
}
public void insertNode(Node node1, Node node2)
{
root = super.addNode(node1, node2);
}
}
Clase transversor:
package huffmanexe;
public class HuffmanTransversor
{
public Node rootNode;
public char c;
public char charArray[];
public int i=0;
public String finalBitPattern = "";
public HuffmanTransversor(Node myNode, char [] charArray)
{
String temp;
int i;
rootNode = myNode;
this.charArray = charArray;
for(i = 0; i < charArray.length; i++)
{
temp = search(rootNode, "", charArray[i]);
finalBitPattern += temp+" ";
System.out.println("Mis valores: "+charArray[i]+" = "+temp);
}
// System.out.println("My bit padre final: "+finalBitPattern);
}
public String search(Node rootNode, String value,char myChar)
{
String valueL ="";
if(rootNode != null)
{
if(rootNode.left != null)
valueL = search(rootNode.left, value+"0", myChar);
if(rootNode.c == myChar)
return value;
else
{
if(valueL == "")
{
return search(rootNode.right, value+"1",myChar);
}
else
{
return valueL;
}
}
}
else
{
return "";
}
}
}
Clase Huffman:
package huffmanexe;
import java.util.*;
/**
* @author franklin aimituma suyo
*/
public class Huffman {
//
private static String value;
private static char charArray[];
//convertidor de string a caracteres
private static int table[] = new int[0x7f];
//mantiene la frecuencia del elemento
private static Node myNode[];
//la principal prioridad de la cola, el rbol final generada se almacena en lugar de 0.
private static int lengthOfTable = 0;
//La verdadera longitud de la tabla que contiene los caracteres.
private static tree myTree;
//Una variable que contiene el rbol
private static int lengthOfNode = 0;
//all increments or decrements are made to this value
public static HuffmanTransversor hC;
//The Class Responsible for Decoding the Huffman Tree
public Huffman(String value)
{
frequencyTable(value);
nodeArrange();
Node x = createTree();
hC = new HuffmanTransversor(x,charArray);
}
public static void frequencyTable(String value)
{
int i;
charArray = value.toCharArray();
for(i = 0; i < charArray.length; i++)
table[getAscii(charArray[i])] += 1;
}
public static int getAscii(char substringValue)
{
return substringValue&0x7f;
}
public static void nodeArrange()
{
int counter = 0;
int j = 0;
for(int i = 0; i < table.length; i++)
{
if(table[i]>0)
counter++;
}
lengthOfTable = counter;
counter = 0;
myNode = new Node[lengthOfTable];
for(int i = 0; i < 127; i++)
{
if(table[i] != 0)
{
myNode[counter]= new Node(table[i], (char)i, null, null);
counter++;
}
}
lengthOfNode = myNode.length;
sort();
public static Node createTree()
{
for(int i = 1; i < lengthOfNode; i++)
{
try
{
if(myNode[1].frequency >= myNode[0].frequency)
{
myTree = new tree(myNode[0],myNode[i]);
myNode[0] = myTree;
moveItems(i, lengthOfNode);
lengthOfNode -= 1;
i -= 1;
sort();
}
else
{
if(i+1 < lengthOfNode)
{
myTree = new tree(myNode[i], myNode[i+1]);
myNode[1] = myTree;
moveItems(i+1, lengthOfNode);
sort();
lengthOfNode -= 1;
i -= 1;
}
else
{
myNode[1] = myNode[1];//revisarlo..........
myNode[0] = new tree(myNode[0], myNode[1]);
}
}
}
catch(Exception e)
{
//I dare this program to crash...hahaha
}
}
return myNode[0];
private static void moveItems(int index, int length)
{ try
{
for(int i = index; i < length; i++)
myNode[i] = myNode[i+1];
}
catch(Exception e)
{
//
}
}
private static void sort()
{
Node temp;
for(int i = lengthOfNode-1; i > 1; i--)
{
for(int j = 0; j < i; j++)
{
if(myNode[j].frequency > myNode[j+1].frequency)
{
temp = myNode[j+1];
myNode[j+1] = myNode[j];
myNode[j] = temp;
}
if(myNode[j].frequency == myNode[j+1].frequency && myNode[j].left != null)
{
temp = myNode[j+1];
myNode[j+1] = myNode[j];
myNode[j] = temp;
}
}
}
}
}
Clase principal:
import java.util.*;
/**
*
* @franklin aimituma suyo
*/
public class HuffmanExe {
/**
* @param args the command line arguments
*/
static Huffman huffman;
private static Scanner input = new Scanner(System.in);
private static String value;
public static void main(String args[])
{
System.out.print("ingrese palabra: ");
value = input.nextLine();
System.out.println("el valor introducido es: "+value);
huffman = new Huffman(value);
System.out.println("La representacin de bits de la cadena que has escrito es:
"+huffman.hC.finalBitPattern);
}
}
Ventana de ejecucin de programa: