Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. public HashMap<Character, String> returnValues() {
  2. HashMap<Character, String> rtn = new HashMap<>();
  3. preOrder(this, "", rtn);
  4. printPreOrder(this);
  5. System.out.println(rtn);
  6. return rtn;
  7. }
  8. /**
  9. * Traverses the tree and stores the binary representations of the leaves
  10. * @param node The current node (C in CLR)
  11. * @param binRep The binary representation of the current node
  12. * @param hm The hashmap that holds the binary
  13. */
  14. private void preOrder(HuffmanNode node, String binRep, HashMap<Character, String> hm) {
  15. if(node.isLeaf()) { //base case
  16. hm.put(Character.valueOf(node.s.charAt(0)), binRep);
  17. }
  18. if(node.getLeft() != null) {
  19. binRep += "0";
  20. preOrder(node.getLeft(), binRep, hm);
  21. }
  22. if(node.getRight() != null) {
  23. if(node.getLeft() != null) {
  24. //try creating a new variable here
  25. binRep = binRep.substring(0, binRep.length() - 1) + "1";
  26. }
  27. else {
  28. binRep += "1";
  29. }
  30. preOrder(node.getRight(), binRep, hm);
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement