Sir I am getting different sequence of encode for same input string as in your lecture but getting the correct result.
Can you please illustrate the cause for difference in these two encoded strings if I am correct.
Encoding String
yes, different sequence of encode is possible.Due to
- As you can see the entryset(keyset of frequency map) is an unordered set. so the min heap created from this set can be different in different execution.
2.now you are picking up 2 min elements from this heap, order of which actually defines the code given to character. think about the cases when frequency of three characters are same or freq(‘a’) = freq(‘b’)+freq(‘c’) = freq(‘d’) and many such cases possible.
in these cases the structure of heap decides which 2 to pickup first and thus defines the encoding.
So encoding can be different.
Thanks.