I tried to implement the algorithm according to the example:
"Shannon Fano"
According to this decision, I have to get A = 11, B = 101, C = 100, D = 00, E = 011, F = 010. But i get A = 11 B = 101 C = 100 D = 01 E = 001 F = 000 This my code:
Input parametrs: frequencies = [50, 39, 18, 49, 35, 24] и chars = [A, B, C, D, E, F]
OnClick: SearchTree(' ',' ', 1, charCount,Memo1);
procedure SearchTree(branch:char; full_branch:string; start_pos:integer; end_pos:integer; memo:TMemo);
var
dS:real;
i, m, S:integer;
c_branch:string;
x,y,j:integer;
begin
if (branch<>' ') then c_branch := full_branch + branch
else c_branch := '';
if (start_pos = end_pos) then
begin
memo.Lines.Add(chars[start_pos]+ ' = ' + c_branch);
exit;
end;
x:=0; y:=0;
i:=start_pos-1; j:=end_pos;
repeat
Inc(i);
x:=x+frequencies[i];
while ((x>=y) and (i<>j))do
begin
y:=y+frequencies[j];
Dec(j);
end;
m:=i;
until (i=j);
SearchTree('1', c_branch, start_pos, m,memo);
SearchTree('0', c_branch, m+1, end_pos,memo);
end;
Prompt if I understood the algorithm and what is my problem?