-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNode.java
More file actions
34 lines (28 loc) · 1.14 KB
/
Node.java
File metadata and controls
34 lines (28 loc) · 1.14 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
import java.util.*;
public class Node implements Comparable<Node> { //The comparable class is implemented to compare the nodes. The <Node> makes sure that the Comparable interface is being parameterized with the type Node.
private final int frequency;
private Node leftNode;
private Node rightNode;
public Node(Node leftNode, Node rightNode) { //Constructor for the Node class
this.leftNode = leftNode;
this.rightNode = rightNode;
if (leftNode != null && rightNode != null) {
this.frequency = leftNode.getFrequency() + rightNode.getFrequency();
} else {
this.frequency = 0; // Set frequency to 0 if either leftNode or rightNode is null
}
}
public int getFrequency() {
return this.frequency;
}
public Node getLeftNode() {
return leftNode;
}
public Node getRightNode() {
return rightNode;
}
@Override
public int compareTo(Node node) { //overriding the compareTo method, returns a possitive integer if frequency is greater than Node frequency and so on...
return Integer.compare(frequency, node.getFrequency());
}
}