walase | Bonjour désolé de vous déranger mais j'ai besoin d'aide pour un devoir à rendre mercredi prochain qui concerne le codage et decodage d'huffman
Le code est en java
Code :
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.io.Reader;
- import java.io.StringReader;
- import java.io.StringWriter;
- import java.util.ArrayList; // Cliquez pour découvrir
- import java.util.Collections;
- import java.util.HashMap;
- class Node implements Comparable<Node> {
- char symbol;
- double freq;
- Node left, right;
- public Node(char s, double f) {
- this.symbol = s;
- this.freq = f;
- this.left = this.right = null;
- }
- public Node(Node left, Node right) {
- this.symbol = (char) 0;
- this.left = left;
- this.right = right;
- this.freq = left.freq + right.freq;
- }
- public int compareTo(Node other) {
- return this.freq < other.freq ? -1 : 1;
- }
- }
- public class Huffman {
- /*
- * public static String encode(String filename) throws FileNotFoundException
- * { FileReader infile = new FileReader(filename); String ret =
- * encode(infile);
- *
- * try { infile.close(); } catch (IOException ioex) { } // Nothing we can do
- * if it won't close.
- *
- * return ret; }
- *
- * public static String encode(InputStream in) { return encode(new
- * InputStreamReader(in)); }
- *
- * public static String encode(Reader in) {
- *
- * // You can't reset() a file stream, so store in a String. StringWriter
- * streamcopy = new StringWriter(); try { for (int nextchar = in.read();
- * nextchar >= 0; nextchar = in.read()) streamcopy.write(nextchar); } catch
- * (IOException ioex) {
- * System.err.println("Could not read the input file." ); return null; }
- *
- * in = new StringReader(streamcopy.toString()); try { in.reset(); } catch
- * (IOException ioex) {
- * System.err.println("Could not reset the input stream: " +
- * ioex.toString()); return null; }
- *
- *
- * // Prepare to write the resulting code. StringWriter ret = new
- * StringWriter();
- *
- * // Read in the file, character by character. int readResult; while (true)
- * { try { readResult = in.read(); } catch (IOException ioex) { // Treat an
- * IOException as an end-of-stream. readResult = -1; }
- *
- * if (readResult < 0) // End-of-stream; exit the loop. break;
- *
- * char readChar = (char) readResult;
- *
- *
- * }
- *
- * return ret.toString(); }
- */
- static char[] symbols = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
- 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
- 'x', 'y', 'z' };
- static double[] freqs = { 0.0372262773723, 0.0570802919708,
- 0.0229197080292, 0.0439416058394, 0.0363503649635, 0.030802919708,
- 0.0160583941606, 0.0554744525547, 0.0459854014599, 0.0110948905109,
- 0.0191240875912, 0.0204379562044, 0.0557664233577, 0.0699270072993,
- 0.0144525547445, 0.016204379562, 0.0505109489051, 0.0300729927007,
- 0.0668613138686, 0.0248175182482, 0.0601459854015, 0.0357664233577,
- 0.0583941605839, 0.0315328467153, 0.0496350364964, 0.0394160583942 };
- //public static ArrayList<String> al = new ArrayList<String>();
- public static Node createTree(char[] symbols, double[] freqs) {
- ArrayList<Node> leaves = new ArrayList<Node>();
- for (int i = 0; i < symbols.length; i++) {
- leaves.add(new Node(symbols[i], freqs[i]));
- }
- while (leaves.size() > 1) {
- Collections.sort(leaves);
- Node n1 = leaves.remove(0);
- Node n2 = leaves.remove(0);
- leaves.add(new Node(n1, n2));
- }
- return leaves.get(0);
- }
- private static void traversal(Node node, String code, String[] codes) {
- if (node.left == null && node.right == null) {
- codes[node.symbol - 'a'] = code;
- } else {
- traversal(node.left, code + "0", codes);
- traversal(node.right, code + "1", codes);
- }
- }
- public static String[] createCode(Node root) {
- String[] codes = new String[26];
- traversal(root, "", codes);
- return codes;
- }
- public static void main(String[] args) throws IOException {
- /*
- * if (args.length != 1) {
- * System.err.println("Usage: java HuffmanCoder <filename>" ); return; }
- *
- * String encoded; try { encoded = encode(args[0]); } catch
- * (FileNotFoundException fnf) {
- * System.err.println("The specified file could not be found." ); return;
- * }
- *
- * // That is, the size if we were outputting bits.
- * System.err.println("Encoded size: " + ((int)
- * Math.ceil(encoded.length() / 8)) + " bytes." );
- * System.err.println("Huffman code:\n" ); System.out.print(encoded); }
- */
- BufferedReader in = new BufferedReader(new FileReader(
- "c:\\textes\\test.txt" )); // lit dans la console
- System.out
- .println("\nFichier créé codée en bit et lecture du premier dans la console java\n" );
- String line;
- while ((line = in.readLine()) != null)
- System.out.println(line);
- BufferedInputStream entree = null;
- BufferedOutputStream sortie = null;
- try {
- // * Ouvre les flux. */
- entree = new BufferedInputStream(new FileInputStream(new File(
- "c:\\textes\\test.txt" )));// lit le fichier
- System.out.println("\ndevient\n" );
- String[] codes = createCode(createTree(symbols, freqs));
- for (int i = 0; i < codes.length; i++) {
- System.out.println((char) (i + 'a') + ": " + codes[i]);
- //al.add(codes[i]);
- }
- sortie = new BufferedOutputStream(new FileOutputStream(new File(
- "c:\\textes\\codage.txt" )), 10240); // créer le fichier
- byte[] tampon = new byte[10240]; // 10ko
- int longueur;
- while ((longueur = entree.read(tampon)) > 0) {
- sortie.write(tampon, 0, longueur);
- }
- } finally {
- sortie.close();
- entree.close();
- }
- }
- }
|
Donc voila j'ai réussis à faire l'arbre etc , on doit assigné aux caractères des fréquences prédéfinis
Je voudrais maintenant passé au codage des lettres , mon arbre donne donc ça :
a: 11011
b: 0110
c: 00000
d: 11110
e: 11010
f: 10010
g: 101100
h: 0011
i: 11111
j: 010000
k: 111010
l: 111011
m: 0101
n: 1100
o: 010001
p: 101101
q: 0010
r: 01001
s: 1010
t: 00001
u: 1000
v: 10111
w: 0111
x: 10011
y: 0001
z: 11100
Mais je vois pas du tout comment je peux faire pour ecrire un texte : abcjfejrejfjdj par exemple et que ceci affiche le texte en bit 010101010 en fonction des lettres
Bon c'est un peu codé comme un cochon par contre désolé de pas avoir supprimer les trucs inutile.
Merci d'avance si on peut m'apporter des précisions Message édité par walase le 28-02-2013 à 10:06:51
|