Created
April 13, 2013 02:03
-
-
Save gtke/5376547 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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