-
Notifications
You must be signed in to change notification settings - Fork 8
/
Node.java
31 lines (27 loc) · 909 Bytes
/
Node.java
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
public class Node implements Comparable<Node> {
Node left;
Node right;
char character;
int frequency;
Node(final char character, final int frequency, final Node left, final Node right) {
this.character = character;
if(frequency < 0) throw new IllegalArgumentException("Error: Frequency must be >= 0");
this.frequency = frequency;
this.left = left;
this.right = right;
}
public boolean isLeaf() {
return this.left == null && this.right == null;
}
public int compareTo(final Node that) {
final int frequencyCompare = Integer.compare(this.frequency, that.frequency);
if(frequencyCompare != 0) {
return frequencyCompare;
}
return Integer.compare(this.character, that.character);
}
public String toString() {
String str = "" + character;
return str;
}
}