-3
\$\begingroup\$

I've implemented a Huffman tree for one of my academic coursework. For each of the nodes in the tree, there will be a left child, right child, and a parent. For each Node class, to add the children, I have the functions, addLeftChild() and addRightChild(). All is good.

However, I am a bit divided for the function that adds the parent to the node. I am unsure whether to use addParent() or setParent().

My personal argument for using addParent() is that it is consistent with the naming of the other 'addX()' functions. What deters me is that using 'add' implies that there's another parent. For using setParent(), semantically it makes more sense for me that you would set another node to be a parent.

Here is my code:

package app.utils.ds;

public class HuffmanNode
{
    private HuffmanNode leftChild;
    private HuffmanNode rightChild;
    private HuffmanNode parent;
    private char letter;
    private int frequency;

    public HuffmanNode(char letter, int frequency)
    {
        this.letter = letter;
        this.frequency = frequency;
        this.leftChild = null;
        this.rightChild = null;
        this.parent = null;
    }

    public void setParent(HuffmanNode parent)
    {
        this.parent = parent;
    }

    public void addLeftChild(HuffmanNode child)
    {
        // I'm thinking of renaming this to setLeftChild()
        this.leftChild = child;
        this.frequency += this.leftChild.getFrequency();
    }

    public void addRightChild(HuffmanNode child)
    {
        // I'm thinking of renaming this to setRightChild()
        this.rightChild = child;
        this.frequency += this.rightChild.getFrequency();
    }

    public HuffmanNode getLeftChild()
    {
        return this.leftChild;
    }

    public HuffmanNode getRightChild()
    {
        return this.rightChild;
    }

    public char getLetter()
    {
        return this.letter;
    }

    public int getFrequency()
    {
        return this.frequency;
    }
}

What is your opinion on this? Also, how else can I improve the code?

\$\endgroup\$
2
\$\begingroup\$

A node can have at most one left child, and at most one right child. If you call addLeftChild() twice, the second call would overwrite the result of the first. Therefore, I would prefer setLeftChild() over addLeftChild().

The same naming advice goes for the parent node. However, I would change setParent() to be private. Users of this class should not be required to help you set the parent node pointers; the HuffmanNode class can do it automatically within setLeftChild() and setRightChild(). That would eliminate the possibility of letting the caller create an inconsistent data structure.

Is it important to allow child nodes to be removed? If so, setLeftChild() and setRightChild() should be prepared to handle a null argument.

In the constructor, you could eliminate the three … = null assignments. Those are implicit already.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.