Question
Java help. Create the following class with the given guidelines. public class HuffTree implements Comparable { private HuffNode root; /** * Create a default binary
Java help.
Create the following class with the given guidelines.
public class HuffTree implements Comparable { private HuffNode root;
/** * Create a default binary tree */ public HuffTree() { root = null; }
/** * Create a tree with two subtrees */ public HuffTree (HuffTree t1, HuffTree t2) { // add code }
/** * Create a tree containing a leaf node */ public HuffTree (HuffElement element) { // add code }
/** * Returns the root of the tree */ public HuffNode getRoot() { // add code }
/** * Compare the roots of the HuffTrees */ @Override public int compareTo (HuffTree ht) { // add code }
// Inner class HuffNode whose element is a HuffElement
public static class HuffNode implements Comparable { protected HuffElement element; public HuffNode left; public HuffNode right;
/** * Create a node with passed in element */ public HuffNode (HuffElement elem) { // add code }
/** * Return the element character */ public char getChar() { // add code }
/** * Return the element character count */ public int getCount() { // add code }
/** * Set the element character count */ public void setCount (int count) { // add code }
/** * Return the element character binary code */ public String getCode() { // add code }
/** * Set the element character binary code */ public void setCode (String str) { // add code }
/** * Compare the nodes by comparing the elements */ @Override public int compareTo (HuffNode node) { // add code }
/** * Return node as a String using the element */ @Override public String toString() { // add code } } }
.........................................................................
Guidelines
Description
HuffTree API:
HuffNode root
Character from the file string.
HuffTree ()
Constructor: default
Set root to null
HuffTree (HuffTree t1,
HuffTree t2)
Constructor:
Initializes root to a new HuffNode with new HuffElement
Adds in t1 as root left subtree
Adds in t2 as root right subtree
Updates roots element to contain a sum of the counts in t1.root element and t2.root element
HuffTree (HuffElement element)
Constructor:
Uses the element to construct a tree with only a root node with the element
HuffNode getRoot()
Returns root.
int compareTo (HuffTree ht)
Compares the root node of this tree to the root node of ht.
HuffNode API:
HuffElement element;
The node element
public HuffNode left;
Left child pointer
public HuffNode right;
Right child pointer
HuffNode (HuffElement elem)
Constructor:
Initializes element to elem
Set left to null
Set right to null
char getChar()
Return the character from the element
int getCount()
Return the character count from the element
void setCount(int count)
Set the count in the element
String getCode()
Return the code from the element
void setCode(String str)
Set the code contained in str in the element
int compareTo (HuffNode node)
Compare this node to node using element.
String toString()
Return string version of the node using the element toString()
.........................................................................................................
Huff Element Class:
public class HuffElement { private char ch; private int chCount; private String code; /** * Constructor: initializes * ch to the blank characterchCount to 0 * code to the empty string */ public HuffElement () { ch=0; chCount=0; code=""; } /** * Constructor: initializes * ch to the incoming char c. * chCount to 0 * code to the empty string */ public HuffElement (char c) { this.ch=c; chCount=0; code=""; } /** * Returns ch. */ public char getChar() { return ch; } /** * Sets ch to the incoming char c. */ public void setChar (char c) { ch=c; } /** * Returns chCount. */ public int getCount() { return chCount; } /** * Sets chCount to the incoming count c. */ public void setCount (int c) { chCount=c; } /** * Increments chCount */ public void incCount() { chCount++; } /** * Returns code. */ public String getCode() { return code; } /** * Sets code to the incoming String c. */ public void setCode (String c) { code=c; } /** * Returns -1, 0, or 1: * Compare the counts. * See implementing Comparable Interface */ int compareTo (HuffElement he) { if(this.getCount()>he.getCount()) { return 1; } else if(this.getCount() }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started