bis punkt 3 gemacht
parent
0e27743f78
commit
89fae51f24
|
@ -0,0 +1,65 @@
|
|||
import java.util.HashMap;
|
||||
import java.util.*;
|
||||
|
||||
public class Main
|
||||
{
|
||||
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,"");
|
||||
|
||||
for (var entry : codes.entrySet())
|
||||
{
|
||||
System.out.println("->" + entry.getKey() + ", Code: " + entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public static void generieren(HashMap<Character, String> codes, Node node, String code)
|
||||
{
|
||||
if(node == null)return;
|
||||
if(node.getLeft() == null && node.getRight() == null)
|
||||
{
|
||||
codes.put(node.getBuchstabe(), code);
|
||||
return;
|
||||
}
|
||||
generieren(codes, node.getLeft(), code + "0");
|
||||
generieren(codes, node.getRight(), code + "1");
|
||||
}
|
||||
|
||||
public static void codierung(String text)
|
||||
{
|
||||
Node wurzel = Node.erstellen(Zählen.countEachLetter(text));
|
||||
HashMap<Character, String> codes = new HashMap<Character, String>();
|
||||
|
||||
if(wurzel.getLeft() == null && wurzel.getRight() == null)
|
||||
{
|
||||
codes.put(wurzel.getBuchstabe(), "0");
|
||||
}
|
||||
else
|
||||
{
|
||||
generieren(codes,wurzel,"");
|
||||
}
|
||||
String code = "";
|
||||
for (int i=0; i<text.length();i++)
|
||||
{
|
||||
char b = text.charAt(i);
|
||||
String c = codes.get(b);
|
||||
code += c;
|
||||
}
|
||||
System.out.println("Huffman-Codierung: " + code);
|
||||
}
|
||||
|
||||
//public String decodierung(
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
68
Node.java
68
Node.java
|
@ -1,6 +1,7 @@
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.ArrayList;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.Comparator;
|
||||
|
||||
public class Node
|
||||
{
|
||||
|
@ -19,65 +20,44 @@ public class Node
|
|||
this.right = right;
|
||||
}
|
||||
|
||||
public Node getLeft()
|
||||
{
|
||||
return left;
|
||||
}
|
||||
public Node getLeft(){return left;}
|
||||
|
||||
public Node getRight()
|
||||
{
|
||||
return right;
|
||||
}
|
||||
public void setLeft(Node left){this.left = left;}
|
||||
|
||||
public void setLeft(Node left)
|
||||
{
|
||||
this.left = left;
|
||||
}
|
||||
public Node getRight(){return right;}
|
||||
|
||||
public void setRight(Node right)
|
||||
{
|
||||
this.right = right;
|
||||
}
|
||||
public void setRight(Node right){this.right = right;}
|
||||
|
||||
public int getAnzahl()
|
||||
{
|
||||
return anzahl;
|
||||
}
|
||||
public int getAnzahl(){return anzahl;}
|
||||
|
||||
public Character getBuchstabe()
|
||||
{
|
||||
return buchstabe;
|
||||
}
|
||||
public void setAnzahl(int anzahl){this.anzahl = anzahl;}
|
||||
|
||||
public void setAnzahl(int anzahl)
|
||||
{
|
||||
this.anzahl = anzahl;
|
||||
}
|
||||
public Character getBuchstabe(){return buchstabe;}
|
||||
|
||||
public void setBuchstabe(Character buchstabe)
|
||||
{
|
||||
this.buchstabe = buchstabe;
|
||||
}
|
||||
public void setBuchstabe(Character buchstabe){this.buchstabe = buchstabe;}
|
||||
|
||||
public static Node erstellen(Map<Character, Integer> häufigkeiten)
|
||||
{
|
||||
ArrayList<Node> nodes = new ArrayList<Node>();
|
||||
PriorityQueue<Node> nodes = new PriorityQueue<Node>(Comparator.comparingInt(n -> n.anzahl));
|
||||
|
||||
for (Map.Entry<Character, Integer> entry : häufigkeiten.entrySet())
|
||||
for (var entry : häufigkeiten.entrySet())
|
||||
{
|
||||
Character character = entry.getKey();
|
||||
int anzahl = entry.getValue();
|
||||
Node node = new Node();
|
||||
node.setAnzahl(entry.getValue());
|
||||
node.setBuchstabe(entry.getKey());
|
||||
nodes.add(node);
|
||||
}
|
||||
|
||||
while (nodes.size() > 1)
|
||||
{
|
||||
Node min1 = nodes.get(1);
|
||||
|
||||
for (Node node : nodes)
|
||||
{
|
||||
//if(node.getCount() < min1.getCount())
|
||||
Node min1 = nodes.poll();
|
||||
Node min2 = nodes.poll();
|
||||
Node wurzel = new Node();
|
||||
wurzel.setLeft(min1);
|
||||
wurzel.setRight(min2);
|
||||
wurzel.setAnzahl(min1.getAnzahl() + min2.getAnzahl());
|
||||
nodes.add(wurzel);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return nodes.poll();
|
||||
}
|
||||
}
|
||||
|
|
11
Zählen.java
11
Zählen.java
|
@ -1,7 +1,10 @@
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
public class Zählen {
|
||||
public static void countEachLetter(String input) {
|
||||
|
||||
public class Zählen
|
||||
{
|
||||
public static Map<Character, Integer> countEachLetter(String input)
|
||||
{
|
||||
// Konvertiere den String in ein char-Array
|
||||
char[] characters = input.toCharArray();
|
||||
|
||||
|
@ -15,9 +18,11 @@ public class Zählen {
|
|||
}
|
||||
|
||||
// Ausgabe der Anzahl jeder einzelnen Buchstaben
|
||||
for (Map.Entry<Character, Integer> entry : letterCountMap.entrySet()) {
|
||||
for (Map.Entry<Character, Integer> entry : letterCountMap.entrySet())
|
||||
{
|
||||
System.out.println("Buchstabe: " + entry.getKey() + ", Anzahl: " + entry.getValue());
|
||||
}
|
||||
return letterCountMap ;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue