-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMsgTree.java
More file actions
113 lines (101 loc) · 3.58 KB
/
MsgTree.java
File metadata and controls
113 lines (101 loc) · 3.58 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package Hw3;
/**
* The MsgTree class represents a node in a Huffman tree used for message encoding and decoding.
* Each node can either be a non-leaf node (internal node) or a leaf node (representing a character).
* @author bohte
*/
public class MsgTree {
public char payloadChar;
public MsgTree left;
public MsgTree right;
private static int staticCharIdx = 0;
private static int numChar = 0;
/**
* Constructs a MsgTree object from an encodingString.
*
* @param encodingString The string representation of the tree structure and character encodings.
* Each character represents a node, and '^' represents a non-leaf node.
*/
public MsgTree(String encodingString) {
if (encodingString.charAt(staticCharIdx) == '^') {
staticCharIdx++;
left = new MsgTree(encodingString);
right = new MsgTree(encodingString);
} else {
payloadChar = encodingString.charAt(staticCharIdx);
staticCharIdx++;
}
}
/**
* Constructs a MsgTree object representing a leaf node with the given payload character.
*
* @param payloadChar The character payload of the leaf node.
*/
public MsgTree(char payloadChar) {
this.left = null;
this.right = null;
this.payloadChar = payloadChar;
}
/**
* Recursively prints the character codes of the Huffman tree.
*
* @param root The root of the Huffman tree.
* @param code The current code string representing the path from the root to a character.
*/
public static void printCodes(MsgTree root, String code) {
if (root == null) {
return;
}
if (root.payloadChar != '^' && root.payloadChar != '\0') {
if (root.payloadChar == '\n') {
String newLine = "\\n";
System.out.println(" " + newLine + " " + code);
} else {
System.out.println(" " + root.payloadChar + " " + code);
}
}
if (root.left != null) {
printCodes(root.left, code + "0");
}
if (root.right != null) {
printCodes(root.right, code + "1");
}
}
/**
* Decodes the given message using the provided Huffman tree.
* Prints the decoded message to the console.
*
* @param codes The root of the Huffman tree.
* @param msg The encoded message to be decoded.
*/
public void decode(MsgTree codes, String msg) {
MsgTree tempTree = codes;
int msgLength = msg.length();
int i = 0;
while (i < msgLength) {
while (i < msgLength && tempTree.left != null && tempTree.right != null) {
if (msg.charAt(i) == '0') {
tempTree = tempTree.left;
} else if (msg.charAt(i) == '1') {
tempTree = tempTree.right;
}
i++;
}
if (tempTree.left != null || tempTree.right != null) {
System.out.println("Incomplete code at the end of the message.");
return;
}
System.out.print(tempTree.payloadChar);
tempTree = codes;
numChar++;
}
}
/**
* Returns the total number of characters encountered during decoding.
*
* @return The total number of characters encountered during decoding.
*/
public int getNumChar() {
return numChar;
}
}