The algorithm yields optimal ternary codes
We can’t prove that, but we can disprove it. The given algorithm is not always optimal. In step&nbsp;2, three nodes may not be available, so there is more than one way to build the tree. That can’t happen in the binary case.

Consider a min heap containing four characters, whose frequencies are 0.4, 0.3, 0.2, and 0.1. The given algorithm yields the tree on the left, with mean length 1.6. The optimal tree is shown on the right, mean length 1.3.

@PeterShor’s comment above indicates that a correct algorithm is known, but I haven’t seen it.