-2

I'm trying to use a recursive method to traverse a Huffman tree, and for each leaf node, add a Code record to an ArrayList. This is what I have so far.

private void traverse(ArrayList<Code> code, BinaryTreeNode<Letter> node,
                     String prefix) {
        // TODO:  Fill in this method
    if (root!=null){
        traverse(code, node.left, prefix);
    }
    if (root!=null){
        traverse(code, node.right, prefix);
    }
    if(node.left==null && node.right==null){
        code.add(node);
    }

The code.add(node) is giving an error as well.

teej
  • 1
  • 1

1 Answers1

0

ArrayList code is a type Code and at the last line you are adding a node type object to code.what you need is to change your code to match the following:

One more thing is to include the value of all node ( assuming you want to add the value of each node)

private void traverse(ArrayList<Letter> code, BinaryTreeNode<Letter> node,
                     String prefix) {
        // TODO:  Fill in this method
    if(node==null) return; //return if this node is empty.
    if (node!=null){
        traverse(code, node.left, prefix);
    }
    if (node!=null){
        traverse(code, node.right, prefix);
    }
    //if you want to include this node value into ur array then add it like this
    code.add(node.getData());             //assuming your BinaryTreeNode has a method getData()

}

Note: I coulnd't figure out what String prefix supposed to be. so I just left it as it is.

nafas
  • 5,283
  • 3
  • 29
  • 57