public class TreeNode extends Object implements Comparable<TreeNode>
Constructor and Description |
---|
TreeNode(int value,
int freq)
construct leaf node (null children)
|
TreeNode(TreeNode leftSubtree,
int value,
TreeNode rightSubtree)
construct internal node (with children).
pre: leftSubtree != null, righSubtree != null The new node's frequency will be the sum of the frequencies of leftSubtree and rightSubtree. |
Modifier and Type | Method and Description |
---|---|
int |
compareTo(TreeNode rhs)
Return value based on comparing this TreeNode to another.
|
int |
getFrequency()
Get the frequency of this node.
|
TreeNode |
getLeft()
Get the left child of this node.
|
TreeNode |
getRight()
Get the right child of this node.
|
int |
getValue()
Get the value stored in this node.
|
boolean |
isLeaf()
Is this node a leaf or not.
|
void |
setLeft(TreeNode newLeft)
Set the left child of this TreeNode to the given value.
|
void |
setRight(TreeNode newRight)
Set the right child of this TreeNode to the given value.
|
String |
toString()
Return a String version of this node.
|
public TreeNode(int value, int freq)
value
- is the value stored in the node (e.g., value from original file)freq
- is number of times value occurred (e.g., count of # occurrences)public TreeNode(TreeNode leftSubtree, int value, TreeNode rightSubtree)
value
- the stored as value of nodeleftSubtree
- is left subtreerightSubtree
- is right subtreepublic int compareTo(TreeNode rhs)
compareTo
in interface Comparable<TreeNode>
public String toString()
public int getValue()
public int getFrequency()
public TreeNode getLeft()
public TreeNode getRight()
public boolean isLeaf()
public void setLeft(TreeNode newLeft)
newLeft
- The new left child for this TreeNode.public void setRight(TreeNode newRight)
newRight
- The new right child for this TreeNode.