decodierung problem
parent
89fae51f24
commit
2eb372a5d1
67
Main.java
67
Main.java
|
@ -3,6 +3,40 @@ import java.util.*;
|
|||
|
||||
public class Main
|
||||
{
|
||||
public static void test(String text)
|
||||
{
|
||||
Map<Character, Integer> zahlen = Zählen.countEachLetter(text);
|
||||
Node wurzel = Node.erstellen(zahlen);
|
||||
|
||||
System.out.println();
|
||||
System.out.println("Pre-Order Baum:");
|
||||
printBaum(wurzel);
|
||||
|
||||
System.out.println();
|
||||
System.out.println("Anzahl der Zeichen:");
|
||||
Zählen.printZahl(zahlen);
|
||||
|
||||
System.out.println();
|
||||
System.out.println("Binärcode der Zeichen:");
|
||||
codes(text);
|
||||
|
||||
System.out.println();
|
||||
|
||||
decodierungBaum(codierung(text,wurzel), wurzel);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static void printBaum(Node baum)
|
||||
{
|
||||
if(baum == null)return;
|
||||
System.out.println("Buchstabe: " + baum.getBuchstabe() + ", Anzahl: " + baum.getAnzahl());
|
||||
printBaum(baum.getLeft());
|
||||
printBaum(baum.getRight());
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void codes(String text)
|
||||
{
|
||||
Node wurzel = Node.erstellen(Zählen.countEachLetter(text));
|
||||
|
@ -27,9 +61,8 @@ public class Main
|
|||
generieren(codes, node.getRight(), code + "1");
|
||||
}
|
||||
|
||||
public static void codierung(String text)
|
||||
public static String codierung(String text, Node wurzel)
|
||||
{
|
||||
Node wurzel = Node.erstellen(Zählen.countEachLetter(text));
|
||||
HashMap<Character, String> codes = new HashMap<Character, String>();
|
||||
|
||||
if(wurzel.getLeft() == null && wurzel.getRight() == null)
|
||||
|
@ -48,9 +81,37 @@ public class Main
|
|||
code += c;
|
||||
}
|
||||
System.out.println("Huffman-Codierung: " + code);
|
||||
return code;
|
||||
}
|
||||
|
||||
//public String decodierung(
|
||||
public static String decodierungBaum(String code,Node baum)
|
||||
{
|
||||
Node current = baum;
|
||||
String decoded = "";
|
||||
|
||||
for(int i =0;i<code.length();i++)
|
||||
{
|
||||
printBaum(current);
|
||||
System.out.println();
|
||||
|
||||
if(current.getRight() == null && current.getLeft() == null)
|
||||
{
|
||||
decoded += current.getBuchstabe();
|
||||
current = baum;
|
||||
}
|
||||
|
||||
if(code.charAt(i) == '1')
|
||||
{
|
||||
current= baum.getRight();
|
||||
}
|
||||
if(code.charAt(i) == '0')
|
||||
{
|
||||
current = baum.getLeft();
|
||||
}
|
||||
}
|
||||
System.out.println(decoded);
|
||||
return decoded;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -17,12 +17,16 @@ public class Zählen
|
|||
letterCountMap.put(c, letterCountMap.getOrDefault(c, 0) + 1);
|
||||
}
|
||||
|
||||
// Ausgabe der Anzahl jeder einzelnen Buchstaben
|
||||
|
||||
return letterCountMap ;
|
||||
}
|
||||
|
||||
public static void printZahl (Map<Character, Integer> letterCountMap)
|
||||
{
|
||||
for (Map.Entry<Character, Integer> entry : letterCountMap.entrySet())
|
||||
{
|
||||
System.out.println("Buchstabe: " + entry.getKey() + ", Anzahl: " + entry.getValue());
|
||||
}
|
||||
return letterCountMap ;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue