Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
2814 connectés 

  FORUM HardWare.fr
  Programmation
  Java

  Huffman [Java]

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Huffman [Java]

n°2177916
walase
Posté le 28-02-2013 à 10:05:19  profilanswer
 

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 :
  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileNotFoundException;
  8. import java.io.FileOutputStream;
  9. import java.io.FileReader;
  10. import java.io.FileWriter;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.io.InputStreamReader;
  14. import java.io.PrintWriter;
  15. import java.io.Reader;
  16. import java.io.StringReader;
  17. import java.io.StringWriter;
  18. import java.util.ArrayList; // Cliquez pour découvrir
  19. import java.util.Collections;
  20. import java.util.HashMap;
  21. class Node implements Comparable<Node> {
  22. char symbol;
  23. double freq;
  24. Node left, right;
  25. public Node(char s, double f) {
  26.  this.symbol = s;
  27.  this.freq = f;
  28.  this.left = this.right = null;
  29. }
  30. public Node(Node left, Node right) {
  31.  this.symbol = (char) 0;
  32.  this.left = left;
  33.  this.right = right;
  34.  this.freq = left.freq + right.freq;
  35. }
  36. public int compareTo(Node other) {
  37.  return this.freq < other.freq ? -1 : 1;
  38. }
  39. }
  40. public class Huffman {
  41. /*
  42.  * public static String encode(String filename) throws FileNotFoundException
  43.  * { FileReader infile = new FileReader(filename); String ret =
  44.  * encode(infile);
  45.  *  
  46.  * try { infile.close(); } catch (IOException ioex) { } // Nothing we can do
  47.  * if it won't close.
  48.  *  
  49.  * return ret; }
  50.  *  
  51.  * public static String encode(InputStream in) { return encode(new
  52.  * InputStreamReader(in)); }
  53.  *  
  54.  * public static String encode(Reader in) {
  55.  *  
  56.  * // You can't reset() a file stream, so store in a String. StringWriter
  57.  * streamcopy = new StringWriter(); try { for (int nextchar = in.read();
  58.  * nextchar >= 0; nextchar = in.read()) streamcopy.write(nextchar); } catch
  59.  * (IOException ioex) {
  60.  * System.err.println("Could not read the input file." ); return null; }
  61.  *  
  62.  * in = new StringReader(streamcopy.toString()); try { in.reset(); } catch
  63.  * (IOException ioex) {
  64.  * System.err.println("Could not reset the input stream: " +
  65.  * ioex.toString()); return null; }
  66.  *  
  67.  *  
  68.  * // Prepare to write the resulting code. StringWriter ret = new
  69.  * StringWriter();
  70.  *  
  71.  * // Read in the file, character by character. int readResult; while (true)
  72.  * { try { readResult = in.read(); } catch (IOException ioex) { // Treat an
  73.  * IOException as an end-of-stream. readResult = -1; }
  74.  *  
  75.  * if (readResult < 0) // End-of-stream; exit the loop. break;
  76.  *  
  77.  * char readChar = (char) readResult;
  78.  *  
  79.  *  
  80.  * }
  81.  *  
  82.  * return ret.toString(); }
  83.  */
  84. static char[] symbols = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
  85.   'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
  86.   'x', 'y', 'z' };
  87. static double[] freqs = { 0.0372262773723, 0.0570802919708,
  88.   0.0229197080292, 0.0439416058394, 0.0363503649635, 0.030802919708,
  89.   0.0160583941606, 0.0554744525547, 0.0459854014599, 0.0110948905109,
  90.   0.0191240875912, 0.0204379562044, 0.0557664233577, 0.0699270072993,
  91.   0.0144525547445, 0.016204379562, 0.0505109489051, 0.0300729927007,
  92.   0.0668613138686, 0.0248175182482, 0.0601459854015, 0.0357664233577,
  93.   0.0583941605839, 0.0315328467153, 0.0496350364964, 0.0394160583942 };
  94. //public static ArrayList<String> al = new ArrayList<String>();
  95. public static Node createTree(char[] symbols, double[] freqs) {
  96.  ArrayList<Node> leaves = new ArrayList<Node>();
  97.  for (int i = 0; i < symbols.length; i++) {
  98.   leaves.add(new Node(symbols[i], freqs[i]));
  99.  }
  100.  while (leaves.size() > 1) {
  101.   Collections.sort(leaves);
  102.   Node n1 = leaves.remove(0);
  103.   Node n2 = leaves.remove(0);
  104.   leaves.add(new Node(n1, n2));
  105.  }
  106.  return leaves.get(0);
  107. }
  108. private static void traversal(Node node, String code, String[] codes) {
  109.  if (node.left == null && node.right == null) {
  110.   codes[node.symbol - 'a'] = code;
  111.  } else {
  112.   traversal(node.left, code + "0", codes);
  113.   traversal(node.right, code + "1", codes);
  114.  }
  115. }
  116. public static String[] createCode(Node root) {
  117.  String[] codes = new String[26];
  118.  traversal(root, "", codes);
  119.  return codes;
  120. }
  121. public static void main(String[] args) throws IOException {
  122.  /*
  123.   * if (args.length != 1) {
  124.   * System.err.println("Usage: java HuffmanCoder <filename>" ); return; }
  125.   *  
  126.   * String encoded; try { encoded = encode(args[0]); } catch
  127.   * (FileNotFoundException fnf) {
  128.   * System.err.println("The specified file could not be found." ); return;
  129.   * }
  130.   *  
  131.   * // That is, the size if we were outputting bits.
  132.   * System.err.println("Encoded size: " + ((int)
  133.   * Math.ceil(encoded.length() / 8)) + " bytes." );
  134.   * System.err.println("Huffman code:\n" ); System.out.print(encoded); }
  135.   */
  136.  BufferedReader in = new BufferedReader(new FileReader(
  137.    "c:\\textes\\test.txt" )); // lit dans la console
  138.  System.out
  139.    .println("\nFichier créé codée en bit et lecture du premier dans la console java\n" );
  140.  String line;
  141.  while ((line = in.readLine()) != null)
  142.   System.out.println(line);
  143.  BufferedInputStream entree = null;
  144.  BufferedOutputStream sortie = null;
  145.  try {
  146.   // * Ouvre les flux. */
  147.   entree = new BufferedInputStream(new FileInputStream(new File(
  148.     "c:\\textes\\test.txt" )));// lit le fichier
  149.   System.out.println("\ndevient\n" );
  150.   String[] codes = createCode(createTree(symbols, freqs));
  151.   for (int i = 0; i < codes.length; i++) {
  152.    System.out.println((char) (i + 'a') + ": " + codes[i]);
  153.    //al.add(codes[i]);
  154.   }
  155.   sortie = new BufferedOutputStream(new FileOutputStream(new File(
  156.     "c:\\textes\\codage.txt" )), 10240); // créer le fichier
  157.   byte[] tampon = new byte[10240]; // 10ko
  158.   int longueur;
  159.   while ((longueur = entree.read(tampon)) > 0) {
  160.    sortie.write(tampon, 0, longueur);
  161.   }
  162.  } finally {
  163.   sortie.close();
  164.   entree.close();
  165.  }
  166. }
  167. }


 
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
mood
Publicité
Posté le 28-02-2013 à 10:05:19  profilanswer
 


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Java

  Huffman [Java]

 

Sujets relatifs
Choix du framework java pour un stage PFEconnection à une base Oracle sous java avec JDBC
Comment créer un dossier avec des espaces en Java sous Linux ?[java] Ligne qui bloque tout mon programme
petite question opérateurs en JAVA (dev. android)Proxy http(s) avec Java ?
flux de données en JAVA[C# C++ JAVA] La programmation, un métier d'avenir ?
Exercice java débutant Trie tableausynchronized java
Plus de sujets relatifs à : Huffman [Java]


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR