Huffman coding assigns code words to fixed length characters based on frequency.
More frequent characters are given shorter code words and less frequent with longer.
Java program for a given string that has encoded and a tree structure, decode the characters .
More frequent characters are given shorter code words and less frequent with longer.
Java program for a given string that has encoded and a tree structure, decode the characters .
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 void decode(String S ,Node root) | |
{ | |
String decode = ""; | |
Node temp = root; | |
for(char c: S.toCharArray()){ | |
//System.out.println(c); | |
if(c=='1') { | |
if(temp.right.data != 0){ | |
decode = decode + temp.right.data; | |
temp = root; | |
} | |
else{ | |
temp = temp.right; | |
} | |
} | |
else if(c=='0'){ | |
if(temp.left.data != 0){ | |
decode = decode + temp.left.data; | |
temp = root; | |
} | |
else{ | |
temp = temp.left; | |
} | |
} | |
} | |
System.out.println(decode); | |
} |
No comments:
Post a Comment