Skip to content

Instantly share code, notes, and snippets.

@gtke
Created April 13, 2013 02:03
Show Gist options
  • Select an option

  • Save gtke/5376547 to your computer and use it in GitHub Desktop.

Select an option

Save gtke/5376547 to your computer and use it in GitHub Desktop.
public static Map<Character, EncodedString> buildEncodingMap(Node tree) {
if(tree.left == null && tree.right == null){
return null;
}
Map<Character,EncodedString> map = new HashMap<Character,EncodedString>();
dfs(map,tree, new EncodedString());
return map;
}
private static void dfs(Map<Character,EncodedString> currentMap, Node node, EncodedString es){
if(node.left == null && node.right == null){
currentMap.put(node.character, es);
System.out.println(currentMap.entrySet().toString());
}
if(node.left != null){
EncodedString temp = new EncodedString();
temp.zero();
es.concat(temp);
dfs(currentMap, node.left,es);
}
if(node.right != null){
EncodedString temp = new EncodedString();
temp.one();
es.concat(temp);
dfs(currentMap, node.right,es);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment