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.