I can solve similar problem with 4 symbols. But I cannot solve it when the number of symbols is more than 4. However, here is the probability of the symbols:

I need to develop optimal code with the Huffman method.
The tree which I got looks like this:

Here is how I reduced the symbols:


This algorithm is very simple, as the other posts point out. Doing your example on paper takes almost as long as writing a program, so here is the program.
First, some sample runs including your example.
The algorithm follows, implemented in Perl. There is not much to say about it: start with a forest of singleton trees and iteratively keep merging the two with the smallest sum value, recording the cumulative sums. Traverse the resulting tree with the path to a leaf giving the Huffman code of that leaf.
#! /usr/bin/perl -w # sub buildcode { my ($cref, $pref, $t) = @_; if(exists($t->{label})){ $cref->{$t->{label}} = $pref; return; } buildcode($cref, $pref . '0', $t->{left}); buildcode($cref, $pref . '1', $t->{right}); } MAIN: { my @freq = @ARGV; die "need at least one symbol " if scalar(@freq) == 0; my $n = scalar(@freq); my $total = 0; for(my $pos=0; $pos<$n; $pos++){ my $val = $freq[$pos]; die "not a decimal number: $val" if $val !~ /^\d+(\.\d*)?$/; $total += $freq[$pos]; } if(abs(1-$total) > 1e-12){ warn "scaling by a factor of $total"; for(my $pos=0; $pos<$n; $pos++){ $freq[$pos] /= $total; } } my @pool; for(my $pos=0; $pos<$n; $pos++){ push @pool, { sum => $freq[$pos], label => "X" . ($pos+1), }; } @pool = sort { $a->{sum} <=> $b->{sum} } @pool; while(scalar(@pool) >= 2){ my ($ma, $mb); $ma = shift @pool; $mb = shift @pool; my $node = { sum => $ma->{sum} + $mb->{sum}, left => $ma, right => $mb }; my $pos; for($pos = 0; $pos<scalar(@pool); $pos++){ last if $node->{sum} < $pool[$pos]->{sum}; } splice @pool, $pos, 0, $node; } my $code = {}; buildcode $code, '', $pool[0]; for(my $pos=0; $pos<$n; $pos++){ printf "X%02d %05f %s\n", $pos+1, $freq[$pos], $code->{'X' . ($pos+1)}; } 1; }