-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHuffman.java
More file actions
87 lines (76 loc) · 2.7 KB
/
Copy pathHuffman.java
File metadata and controls
87 lines (76 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import java.util.*;
class Node
{
char ch; int freq; Node left, right;
Node(char ch, int freq)
{
this.ch = ch;
this.freq = freq;
}
}
class Huffman
{
static void generateCodes(Node root, String code, Map<Character, String> codesMap)
{
if (root == null) return;
if (root.left == null && root.right == null)
{
codesMap.put(root.ch, code);
return;
}
generateCodes(root.left, code + "0", codesMap);
generateCodes(root.right, code + "1", codesMap);
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of symbols: ");
int n = sc.nextInt();
char[] chars = new char[n];
int[] freq = new int[n];
System.out.println("Enter symbols:");
for (int i = 0; i < n; i++) chars[i] = sc.next().charAt(0);
System.out.println("Enter frequencies:");
for (int i = 0; i < n; i++) freq[i] = sc.nextInt();
PriorityQueue<Node> pq = new PriorityQueue<>( Comparator.comparingInt(a -> a.freq) );
for (int i = 0; i < n; i++) pq.add(new Node(chars[i], freq[i]));
while (pq.size() > 1)
{
Node x = pq.poll(); Node y = pq.poll();
Node z = new Node('-', x.freq + y.freq);
z.left = x; z.right = y;
pq.add(z);
}
Node root = pq.poll();
Map<Character, String> codesMap = new HashMap<>();
generateCodes(root, "", codesMap);
System.out.println("\nHuffman Codes:");
for (Map.Entry<Character, String> entry : codesMap.entrySet())
System.out.println(entry.getKey() + " : " + entry.getValue());
while (true)
{
System.out.print("\nEnter a word to encode (or type 'exit' to quit): ");
String input = sc.next();
if (input.equalsIgnoreCase("exit"))
{
System.out.println("Program terminated. Goodbye!");
break;
}
StringBuilder encodedString = new StringBuilder();
boolean isValid = true;
for (int i = 0; i < input.length(); i++)
{
char current = input.charAt(i);
if (codesMap.containsKey(current)) encodedString.append(codesMap.get(current));
else
{
System.out.println("Error: Character '" + current + "' was not recognized in the symbol list.");
isValid = false;
break;
}
}
if (isValid) System.out.println("Encoded Word: " + encodedString.toString());
}
sc.close();
}
}