galera consegui desenrolar o algoritmo até aqui: e agora faço o q? quem ajudar será recompensado pela boa vontade divina, pois nao aguento mais bater cabeça e Deus perceberá a boa vontade
Roteiro:
tenho q ir nun aquivo txt, ler o arquivo, salvar na string palavra, daeh fazer tudo isso ai do code, depois salvar num arquivo o texto codificado. tou batendo mais cabeça nisso aqui: palavra = baba b=01, a=10, como faria para a palavra ir ao arquivo e depois eu poder descodificar mantendo a ordem…
import java.io.*;
import java.util.*;
class Node
implements Comparable
{
private int value;
private char content;
private Node left;
private Node right;
public Node(char content, int value)
{
this.content = content;
this.value = value;
}
public Node(Node left, Node right)
{
// Assumes that the left three is always the one that is lowest
this.content = (left.content < right.content) ? left.content : right.content;
this.value = left.value + right.value;
this.left = left;
this.right = right;
}
//método para comparar
public int compareTo(Object arg)
{
Node other = (Node) arg;
// Content value has priority and then the lowest letter
if (this.value == other.value)
return this.content-other.content;
else
return this.value-other.value;
}
////////////////
private void printNode(String path)
{
if ((left==null) && (right==null))
System.out.println(content + " " + path);
if (left != null)
left.printNode(path + '0');
if (right != null)
right.printNode(path + '1');
}
public static void printTree(Node tree)
{
tree.printNode("");
}
}
class Huffman
{
private static void processo(String palavra)
{
TreeSet<Node> trees = new TreeSet<Node>(); // List containing all trees -- ORDERED!
// Build the frequency table of each letter
for (int i=0; i<palavra.length(); i++)
{
char ch = Character.toUpperCase(palavra.charAt(i));
if ((ch >= 'A') && (ch <= 'Z'))
++frequencia[ch - 'A'];
}
// Build up the initial trees
for (int i=0; i<'Z'-'A'+1; i++)
{
if (frequencia[i] > 0)
{
Node n = new Node((char)('A'+i), frequencia[i]);
trees.add(n);
}
}
while (trees.size() > 1)
{
Node tree1 = (Node) trees.first();
trees.remove(tree1);
Node tree2 = (Node) trees.first();
trees.remove(tree2);
Node merged = new Node(tree1, tree2);
trees.add(merged);
}
// Print the resulting tree
if (trees.size() > 0)
{
Node theTree = (Node) trees.first();
Node.printTree(theTree);
}
else
System.out.println("A string nao possui caracter");
}
static int[] frequencia = new int[26];
public static void main(String[] args){
String palavra="Allan Glauber ";
Huffman ob = new Huffman();
System.out.println(palavra);
ob.processo(palavra);
for (int q=0;q<=frequencia.length;q++)
System.out.print(frequencia[q]+" ");
}
/* throws IOException
{
StringBuffer fileContents = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while ((line = br.readLine()) != null)
fileContents.append("\n").append(line);
processFile(fileContents.toString());
}*/
}
:roll: