The standard algorithm for generating the optimal ternary Huffman code (as alluded to by rici) involves first making sure there are an odd number of symbols -- by adding a dummy symbol (of frequency 0) if necessary.
In this case, we start with an even number of symbols, so we need to add the dummy symbol that I call Z:
freq(a)=5 freq(b)=3 freq(c)=2 freq(d)=2 freq(Z)=0.
Then as Photon described, we repeatedly combine the 3 nodes with the lowest frequencies into 1 combined symbol. Each time we replace 3 nodes with 1 node, we reduce the total number of nodes by 2, and so the total number of nodes remains odd at each step. In the last step (if we've added the correct number of dummy symbols) we will combine 3 final nodes into a single root node.
abcdZ:12
/ | \
2/ 1| 0\
cdZ:4 b:3 a:5
/ | \
2/ 1| 0\
Z:0 d:2 c:2
So in this case one optimal (Huffman) ternary coding is:
a: 0
b: 1
c: 20
d: 21
Z: 22 (should never occur).
See
https://en.wikipedia.org/wiki/Huffman_coding#n-ary_Huffman_coding
for more details.