function [ inode, jnode ] = tree_pruefer_to_arc ( code )

%*****************************************************************************80
%
%% tree_pruefer_to_arc() is given a Pruefer code, and computes the tree.
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    25 March 2023
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Dennis Stanton, Dennis White,
%    Constructive Combinatorics,
%    Springer Verlag, New York, 1986.
%
%  Input:
%
%    integer code(NNODE-2): the Pruefer code of the tree.
%
%  Output:
%
%    integer INODE(NNODE-1), JNODE(NNODE-1): the edge
%    array of the tree.  The I-th edge joins nodes INODE(I) and JNODE(I).
%
  nnode = size ( code, 1 ) + 2;
%
%  Initialize WORK(I) to count the number of neighbors of node I.
%  The Pruefer code uses each node one less time than its total
%  number of neighbors.
%
  work(1:nnode) = ones ( nnode, 1 );
  inode = zeros ( nnode - 1, 1 );
  jnode = zeros ( nnode - 1, 1 );
 
  for i = 1 : nnode - 2
    work(code(i)) = work(code(i)) + 1;
  end
%
%  Now process each entry in the Pruefer code.
%
  for i = 1 : nnode - 2
 
    ii = 0
    for j = 1 : nnode
      if ( work(j) == 1 )
        ii = j
      end
    end
 
    inode(i) = ii;
    jnode(i) = code(i);
    work(ii) = 0;
    work(code(i)) = work(code(i)) - 1;
 
  end
 
  inode(nnode-1) = code(nnode-2);
 
  if ( code(nnode-2) ~= 1 )
    jnode(nnode-1) = 1;
  else
    jnode(nnode-1) = 2;
  end
 
  return
end
