The following is an excerpt from CLRS's Introduction To Algorithms:
Although we don't prove it here, a prefix code can always achieve the optimal data compression among any character code, and so we suffer no loss of generality by restricting our attention to prefix codes.
Please answer the question by providing or referring to a valid proof of the above statement.