when i am running this code on my laptop, the coded string is coming different but the final output is coming correct. I have written correct code.
Doubt in code,coded string is different
please,someone answer…
@apoorvsingh27
you code is absolutely correct but the variation in coded string is because when we have frequency of two or more characters same like in this case a,b,c have same frequency you dont know which node will come out of the minheap first that why you output is different.
in order to resolve this you can modify you compareTo function like this
@Override
public int compareTo(Node o) {
// comparing two nodes on the basis of cost
return (this.cost == o.cost)?this.data-o.data:this.cost-o.cost;
}
you will get the desired result from this.
Hepe it helps you .
if you still have any doubt feel free to ask otherwise mark this doubt as resolved in my doubt section and rate me as well
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.