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(
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
94
Node.java
94
Node.java
|
@ -1,6 +1,7 @@
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.ArrayList;
|
import java.util.PriorityQueue;
|
||||||
|
import java.util.Comparator;
|
||||||
|
|
||||||
public class Node
|
public class Node
|
||||||
{
|
{
|
||||||
|
@ -8,76 +9,55 @@ public class Node
|
||||||
private Node right;
|
private Node right;
|
||||||
private int anzahl;
|
private int anzahl;
|
||||||
private Character buchstabe;
|
private Character buchstabe;
|
||||||
|
|
||||||
public Node()
|
public Node()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public Node(Node left, Node right)
|
public Node(Node left, Node right)
|
||||||
{
|
{
|
||||||
this.left = left;
|
this.left = left;
|
||||||
this.right = right;
|
this.right = right;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Node getLeft()
|
public Node getLeft(){return left;}
|
||||||
{
|
|
||||||
return left;
|
public void setLeft(Node left){this.left = left;}
|
||||||
}
|
|
||||||
|
public Node getRight(){return right;}
|
||||||
public Node getRight()
|
|
||||||
{
|
public void setRight(Node right){this.right = right;}
|
||||||
return right;
|
|
||||||
}
|
public int getAnzahl(){return anzahl;}
|
||||||
|
|
||||||
public void setLeft(Node left)
|
public void setAnzahl(int anzahl){this.anzahl = anzahl;}
|
||||||
{
|
|
||||||
this.left = left;
|
public Character getBuchstabe(){return buchstabe;}
|
||||||
}
|
|
||||||
|
public void setBuchstabe(Character buchstabe){this.buchstabe = buchstabe;}
|
||||||
public void setRight(Node right)
|
|
||||||
{
|
|
||||||
this.right = right;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getAnzahl()
|
|
||||||
{
|
|
||||||
return anzahl;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Character getBuchstabe()
|
|
||||||
{
|
|
||||||
return buchstabe;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAnzahl(int anzahl)
|
|
||||||
{
|
|
||||||
this.anzahl = anzahl;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setBuchstabe(Character buchstabe)
|
|
||||||
{
|
|
||||||
this.buchstabe = buchstabe;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Node erstellen(Map<Character, Integer> häufigkeiten)
|
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();
|
Node node = new Node();
|
||||||
int anzahl = entry.getValue();
|
node.setAnzahl(entry.getValue());
|
||||||
|
node.setBuchstabe(entry.getKey());
|
||||||
|
nodes.add(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (nodes.size() > 1)
|
while (nodes.size() > 1)
|
||||||
{
|
{
|
||||||
Node min1 = nodes.get(1);
|
Node min1 = nodes.poll();
|
||||||
|
Node min2 = nodes.poll();
|
||||||
for (Node node : nodes)
|
Node wurzel = new Node();
|
||||||
{
|
wurzel.setLeft(min1);
|
||||||
//if(node.getCount() < min1.getCount())
|
wurzel.setRight(min2);
|
||||||
}
|
wurzel.setAnzahl(min1.getAnzahl() + min2.getAnzahl());
|
||||||
|
nodes.add(wurzel);
|
||||||
}
|
}
|
||||||
return null;
|
return nodes.poll();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
15
Zählen.java
15
Zählen.java
|
@ -1,23 +1,28 @@
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
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
|
// Konvertiere den String in ein char-Array
|
||||||
char[] characters = input.toCharArray();
|
char[] characters = input.toCharArray();
|
||||||
|
|
||||||
// Initialisiere die Map zum Speichern der Buchstabenanzahl
|
// Initialisiere die Map zum Speichern der Buchstabenanzahl
|
||||||
Map<Character, Integer> letterCountMap = new HashMap<>();
|
Map<Character, Integer> letterCountMap = new HashMap<>();
|
||||||
|
|
||||||
// Durchlaufe das Array und zählt alle zeichen
|
// Durchlaufe das Array und zählt alle zeichen
|
||||||
for (char c : characters)
|
for (char c : characters)
|
||||||
{
|
{
|
||||||
letterCountMap.put(c, letterCountMap.getOrDefault(c, 0) + 1);
|
letterCountMap.put(c, letterCountMap.getOrDefault(c, 0) + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ausgabe der Anzahl jeder einzelnen Buchstaben
|
// 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());
|
System.out.println("Buchstabe: " + entry.getKey() + ", Anzahl: " + entry.getValue());
|
||||||
}
|
}
|
||||||
|
return letterCountMap ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue