function [a_tree code_huffman]=Huffman_encode(b)
%clc;clear all;close all;
container=[b(1)];
l_b=length(b);
i_b=1;
code_huffman=[];
count=zeros(1,l_b);
count(1)=1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%统计
for i_n=2:l_b
i_m=1;
while i_m<=i_b
if b(i_n)==container(i_m)
count(i_m)=count(i_m)+1;
break;
end
i_m=i_m+1;
end
if i_m==i_b+1
container=[container b(i_n)];
i_b=i_b+1;
count(i_b)=count(i_b)+1;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
value_min1=99999;
value_min2=99999;
l_container=length(container);
tree=count(1:l_container);
K=l_container;
k=K;
for i=1:K
hf(i).weight=count(i);
hf(i).parent=0;
hf(i).cchild=0;
hf(i).rchild=0;
hf(i).flag=2;
hf(i).value=container(i);
end
for i=K+1:2*K-1
hf(i).weight=0;
hf(i).parent=0;
hf(i).cchild=0;
hf(i).rchild=0;
hf(i).flag=2;
end
i_k=1;
i=0;
while(i_k<=k-1)
m1=32767;
m2=32766;
x1=0;
x2=0;
for j=1:k+i
if hf(j).weight<m1 &hf(j).flag==2
m2=m1;
x2=x1;
m1=hf(j).weight;
x1=j;
else
if hf(i_k).weight<m2 & hf(j).flag==2
m2=hf(j).weight;
x2=j;
end
end
end
hf(x1).flag=1;
hf(x2).flag=0;
i=i+1;
hf(k+i_k).weight=hf(x1).weight+hf(x2).weight;
hf(k+i_k).cchild=x1;
hf(k+i_k).rchild=x2;
hf(x1).parent=k+i_k;
hf(x2).parent=k+i_k;
i_k=i_k+1;
end
%%%%%*树的建立
for i_n=1:l_b
for i_m=1:k
if b(i_n)==hf(i_m).value
c=i_m;
top=0;
p=hf(c).parent;
hc=[];
while(p~=0)
top=top+1;
hc=[hc hf(c).flag];
c=p;
p=hf(c).parent;
end
code_huffman=[code_huffman hc(length(hc):-1:1)];
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
a_tree(1,:)=container;
a_tree(2,:)=count(1:length(container));