xxxxxxxxxx
//User function Template for Java
/*class MinHeapNode
{
char data;
int freq;
MinHeapNode left, right, top;
MinHeapNode(char c, int freq)
{
left = right = null;
this.data = c;
this.freq = freq;
}
}*/
class Decode
{
//Function to return the decoded string.
static String decodeHuffmanData(MinHeapNode root, String str)
{
int n=str.length(); //length of the binary string
String res= ""; //res variable to store result
MinHeapNode curr=root; //creating a curr node to traverse
for(int i=0;i<n;i++){
if(str.charAt(i)=='0'){
curr=curr.left;
}else if(str.charAt(i)=='1'){
curr=curr.right;
}
if(curr.left==null && curr.right==null){
res+=curr.data;
curr=root;
}
}
return res;
}
}