0

I'm trying to create a Huffman Tree implementation, and included in that is a way to search for a character and return the Huffman code for it. I'm having a bit of trouble with it. This is my best attempt so far -

String lookupResult = "";
private void findPath(char ch, Node node, String path) {
    if (node != null) {
        if (node.left != null)
            findPath(ch, node.left, path + '0');
        if (node.right != null)
            findPath(ch, node.right, path + '1');
        if (node.left == null && node.right == null && node.key == ch) {
            System.out.println(path);
            lookupResult = path;
        }
    } 
}

But instead of it returning the correct answer for a test ("01"), I get "101" and I'm not too sure how to fix it. Any help would be appreciated.

A_J
  • 75
  • 1
  • 3
  • Possible duplicate of [Huffman Tree in Java](http://stackoverflow.com/questions/16658419/huffman-tree-in-java) – NewBie1234 Mar 10 '17 at 17:38
  • Looks okay to me. Maybe your tree is wrong or you are not expecting the correct result. Side note: always use braces around `if` statements. It's better for maintainability. – Michael Mar 10 '17 at 17:59

0 Answers0