function [ListBB]=WP_BB_Listree1D_P(En)
%building the best basis tree for wavelet packet transform using 
%values of the cost function 'En' values for each block of the transform
%coefficients, which are presented by a cell array
[Ben ]=WPbeN_1D_bas(En);
[~,he]=size(Ben);

 ListBB=[];
 kk=1;
 stack=[1 1 ];
 [vs, hs]=size(stack);
 while vs>0
     
     s=stack(1,:);
     stack=stack(2:end,:);
   lev=s(1);
   bv=s(2);
  
   b=Ben{lev}(bv);
     while b==1;
         lev=lev+1;
        bv1=2*bv; bv=2*bv-1;
        
   stack=[[lev bv1];stack];  
  [lev bv]; 
  
    b=Ben{lev}(bv);
     end 
  ListBB=[ ListBB;[lev-1,bv-1]];    
 [vs, hs]=size(stack); 
 end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [BEn ]=WPbeN_1D_bas(En)
[ve,he]=size(En);
BEn=cell(ve,1);
%ve=6
 BEn{ve} =zeros(size(En{ve}));

 Ec=En{ve}; 

 for kk=ve-1:-1:1
 Ep=En{kk};   
   E =zeros(size(Ep));
    [~,hp]=size(Ep);
    
for hh=1:hp
            ep=Ep(hh);
            ec=[Ec(2*hh-1),Ec(2*hh)];      ce=(sum(ec));
            if ce<=ep
 E(hh)=1; 
       Ep(hh)=ce;   

        end
        end
   % kk
    
   BEn{kk} =E;
   Ec=Ep;
 end
    % Copyright (c) 2013. Valery A. Zheludev
