Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public HashMap<Character, String> returnValues() {
- HashMap<Character, String> rtn = new HashMap<>();
- preOrder(this, "", rtn);
- printPreOrder(this);
- System.out.println(rtn);
- return rtn;
- }
- /**
- * Traverses the tree and stores the binary representations of the leaves
- * @param node The current node (C in CLR)
- * @param binRep The binary representation of the current node
- * @param hm The hashmap that holds the binary
- */
- private void preOrder(HuffmanNode node, String binRep, HashMap<Character, String> hm) {
- if(node.isLeaf()) { //base case
- hm.put(Character.valueOf(node.s.charAt(0)), binRep);
- }
- if(node.getLeft() != null) {
- binRep += "0";
- preOrder(node.getLeft(), binRep, hm);
- }
- if(node.getRight() != null) {
- if(node.getLeft() != null) {
- //try creating a new variable here
- binRep = binRep.substring(0, binRep.length() - 1) + "1";
- }
- else {
- binRep += "1";
- }
- preOrder(node.getRight(), binRep, hm);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement