-
-
Notifications
You must be signed in to change notification settings - Fork 360
Added java implementation of Huffman compression #181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
} | ||
|
||
class HuffmanTree { | ||
private Map<String, Integer> map = new HashMap<>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could use a more descriptive name
private Map<String, Integer> map = new HashMap<>(); | ||
private Map<String, String> codeBook = new HashMap<>(), reverseCodeBook = new HashMap<>(); | ||
private TreeNode root; | ||
private String s; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly, single-letter class members are usually not a good idea
for (int i = 0; i < s.length(); i++) { | ||
String key = Character.toString(s.charAt(i)); | ||
|
||
if (!map.containsKey(key)) map.put(key, 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the else branch has braces, so should the if
|
||
|
||
while (priorityQueue.size() > 1) { | ||
TreeNode temp1 = priorityQueue.remove(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
left
and right
are probably better names here
while (priorityQueue.size() > 1) { | ||
TreeNode temp1 = priorityQueue.remove(); | ||
TreeNode temp2 = priorityQueue.remove(); | ||
TreeNode node; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least merge this declaration with the line below but you could also just dopriorityQueue.add(new TreeNode(...));
private TreeNode root; | ||
private String s; | ||
|
||
public HuffmanTree(String s) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should probay use better variable names for the constructor too
|
||
} | ||
|
||
private void traverse(TreeNode temp, String w) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once more, these argument names are not the best.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, a StringBuilder would be much better here
private void traverse(TreeNode temp, String w) { | ||
if (temp.left == null && temp.right == null) | ||
codeBook.put(temp.key, w); | ||
if (temp.left != null) traverse(temp.left, w + 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you can add integers to strings. Make sure to always test if your program compiles and works before submitting a PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The program is compiling fine. You can add integers to string in Java. I'll use StringBuilder as you say.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, you're right about the addition. Weird, not many languages allow that and Java's usually pretty strict (I've never seen it used in practice either)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I was shocked too when I first learned about it. But you are right it creates unnecessary confusion.
|
||
public String encode() { | ||
traverse(root, ""); | ||
String enc = ""; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A StringBuilder would be more efficoentt here
} | ||
|
||
public String decode(String enc) { | ||
String dec = "", key = ""; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, a StringBuilder and variable name suggestion. Since this is an educational resource I would go with encoded/decoded instead of enc/dec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I'll change the variable names and make them more descriptive.
I've made some of the requested changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should add the implementation to the chapter as well, otherwise it won't show up :)
Ok, done. |
No description provided.