fix
parent
2eb372a5d1
commit
8ad1feabdf
49
Main.java
49
Main.java
|
@ -34,15 +34,21 @@ public class Main
|
||||||
printBaum(baum.getLeft());
|
printBaum(baum.getLeft());
|
||||||
printBaum(baum.getRight());
|
printBaum(baum.getRight());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static void codes(String text)
|
public static void codes(String text)
|
||||||
{
|
{
|
||||||
Node wurzel = Node.erstellen(Zählen.countEachLetter(text));
|
Node wurzel = Node.erstellen(Zählen.countEachLetter(text));
|
||||||
HashMap<Character, String> codes = new HashMap<Character, String>();
|
HashMap<Character, String> codes = new HashMap<Character, String>();
|
||||||
generieren(codes,wurzel,"");
|
|
||||||
|
if(wurzel.getLeft() == null && wurzel.getRight() == null)
|
||||||
|
{
|
||||||
|
codes.put(wurzel.getBuchstabe(), "0");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
generieren(codes,wurzel,"");
|
||||||
|
}
|
||||||
|
|
||||||
for (var entry : codes.entrySet())
|
for (var entry : codes.entrySet())
|
||||||
{
|
{
|
||||||
System.out.println("->" + entry.getKey() + ", Code: " + entry.getValue());
|
System.out.println("->" + entry.getKey() + ", Code: " + entry.getValue());
|
||||||
|
@ -89,25 +95,34 @@ public class Main
|
||||||
Node current = baum;
|
Node current = baum;
|
||||||
String decoded = "";
|
String decoded = "";
|
||||||
|
|
||||||
|
boolean hatNurEinBuchstabenart = current.getRight() == null && current.getLeft() == null;
|
||||||
|
if(hatNurEinBuchstabenart)
|
||||||
|
{
|
||||||
|
for(int i=0;i<code.length();i++)
|
||||||
|
{
|
||||||
|
decoded += baum.getBuchstabe();
|
||||||
|
}
|
||||||
|
System.out.println(decoded);
|
||||||
|
return decoded;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
for(int i =0;i<code.length();i++)
|
for(int i =0;i<code.length();i++)
|
||||||
{
|
{
|
||||||
printBaum(current);
|
if(code.charAt(i) == '1')
|
||||||
System.out.println();
|
{
|
||||||
|
current= current.getRight();
|
||||||
|
}
|
||||||
|
if(code.charAt(i) == '0')
|
||||||
|
{
|
||||||
|
current = current.getLeft();
|
||||||
|
}
|
||||||
if(current.getRight() == null && current.getLeft() == null)
|
if(current.getRight() == null && current.getLeft() == null)
|
||||||
{
|
{
|
||||||
decoded += current.getBuchstabe();
|
decoded += current.getBuchstabe();
|
||||||
current = baum;
|
current = baum;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(code.charAt(i) == '1')
|
|
||||||
{
|
|
||||||
current= baum.getRight();
|
|
||||||
}
|
|
||||||
if(code.charAt(i) == '0')
|
|
||||||
{
|
|
||||||
current = baum.getLeft();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
System.out.println(decoded);
|
System.out.println(decoded);
|
||||||
return decoded;
|
return decoded;
|
||||||
|
|
Loading…
Reference in New Issue