master
L 2025-04-29 20:03:28 +02:00
parent 2eb372a5d1
commit 8ad1feabdf
2 changed files with 34 additions and 17 deletions

View File

@ -34,15 +34,21 @@ public class Main
printBaum(baum.getLeft());
printBaum(baum.getRight());
}
public static void codes(String text)
{
Node wurzel = Node.erstellen(Zählen.countEachLetter(text));
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())
{
System.out.println("->" + entry.getKey() + ", Code: " + entry.getValue());
@ -89,25 +95,34 @@ public class Main
Node current = baum;
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++)
{
printBaum(current);
System.out.println();
if(code.charAt(i) == '1')
{
current= current.getRight();
}
if(code.charAt(i) == '0')
{
current = current.getLeft();
}
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;

View File

@ -47,6 +47,8 @@ public class Node
node.setBuchstabe(entry.getKey());
nodes.add(node);
}
while (nodes.size() > 1)
{