function Maxb = NRTmax(Sbar, N)
% Octave/MATLAB function to compute maximums
% along paths in a non-recombining tree (NRT).
% INPUTS:                             (Example)
%   Sbar = NRT array of length 2*2^N-1   (1:15)
%   N = tree depth, must be >=0             (3)
% OUTPUT:
%   Maxb = NRT of maximums, same size as Sbar
% EXAMPLE:
%  Maxb = NRTmax(1:15,3)
%
  Maxb=zeros(size(Sbar)); % allocate the output
  Maxb(1)=Sbar(1); % max along the trivial path
  for m=1:2^N-1   % all future times up to N-1
    Maxb(2*m)=max(Maxb(m),Sbar(2*m));   % down
    Maxb(2*m+1)=max(Maxb(m),Sbar(2*m+1)); % up
  end
  return
end
