function r8btree_bracket_level_test ( )

%*****************************************************************************80
%
%% R8BTREE_BRACKET_LEVEL_TEST seeks to bracket a given value.
%
%  Discussion:
%
%    The BTREE contains indices that implicitly sort a real array.
%    Given a value X, we seek indices L and R so that
%    X(L) <= X <= X(R).
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    30 November 2009
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'R8BTREE_BRACKET_LEVEL_TEST\n' );
  fprintf ( 1, '  Given a BTREE of data and a value X, we seek to find\n' );
  fprintf ( 1, '  "bracket values" so that X(L) <= X <= X(R).\n' );
  fprintf ( 1, '  However, the value of LEVEL limits how deep we may search.\n' ); 
%
%  Initialize the tree.
%
  node_num = 33;

  tree(1:4,1:node_num) = [ ...
       1,       2,       3,      -1; ...
       2,      -1,       4,       1; ...
       3,       5,      -1,       1; ...
       4,       6,       7,       2; ...
       5,       8,       9,       3; ...
       6,      10,      11,       4; ...
       7,      12,      13,       4; ...
       8,      14,      15,       5; ...
       9,      16,      17,       5; ...
      10,      18,      19,       6; ...
      11,      20,      21,       6; ...
      12,      22,      23,       7; ...
      13,      24,      25,       7; ...
      14,      26,      27,       8; ...
      15,      28,      29,       8; ...
      16,      30,      31,       9; ...
      17,      32,      33,       9; ...
      18,      -1,      -1,      10; ...
      19,      -1,      -1,      10; ...
      20,      -1,      -1,      11; ...
      21,      -1,      -1,      11; ...
      22,      -1,      -1,      12; ...
      23,      -1,      -1,      12; ...
      24,      -1,      -1,      13; ...
      25,      -1,      -1,      13; ...
      26,      -1,      -1,      14; ...
      27,      -1,      -1,      14; ...
      28,      -1,      -1,      15; ...
      29,      -1,      -1,      15; ...
      30,      -1,      -1,      16; ...
      31,      -1,      -1,      16; ...
      32,      -1,      -1,      17; ...
      33,      -1,      -1,      17 ]';

  data_num = 2;

  tree_data(1:data_num,1:node_num) = [ ...
      0.5000,      0.2500; ...
      0.0000,      0.0000; ...
      1.0000,      1.0000; ...
      0.2500,      0.0625; ...
      0.7500,      0.5625; ...
      0.1250,      0.0156; ...
      0.3750,      0.1406; ...
      0.6250,      0.3906; ...
      0.8750,      0.7656; ...
      0.0625,      0.0039; ...
      0.1875,      0.0352; ...
      0.3125,      0.0977; ...
      0.4375,      0.1914; ...
      0.5625,      0.3164; ...
      0.6875,      0.4727; ...
      0.8125,      0.6602; ...
      0.9375,      0.8789; ...
      0.0312,      0.0010; ...
      0.0938,      0.0088; ...
      0.1562,      0.0244; ...
      0.2188,      0.0479; ...
      0.2812,      0.0791; ...
      0.3438,      0.1182; ...
      0.4062,      0.1650; ...
      0.4688,      0.2197; ...
      0.5312,      0.2822; ...
      0.5938,      0.3525; ...
      0.6562,      0.4307; ...
      0.7188,      0.5166; ...
      0.7812,      0.6104; ...
      0.8438,      0.7119; ...
      0.9062,      0.8213; ...
      0.9688,      0.9385 ]';
%
%  Print the BTREE.
%
  r8btree_print_ordered ( node_num, tree, data_num, tree_data );
%
%  Choose LEVEL = 0, 1, 2, 3
%
  for level = 0 : 3
    fprintf ( 1, '\n' );
    fprintf ( 1, '  LEVEL = %d\n', level );
    seed = 123456789;
    rand ( 'twister', seed );
%
%  Choose random values both inside and outside the range.
%
    for i = 1 : 15
      x = 1.5 * rand ( ) - 0.25;
      [ l, r ] = r8btree_bracket_level ( node_num, tree, data_num, tree_data, x, level );
      fprintf ( 1, '\n' );
      if ( 0 < l )
        fprintf ( 1, '  X(%d) = %f  F(X) = %f\n', l, tree_data(1,l), tree_data(2,l) );
      else
        fprintf ( 1, '  No left bracket.\n' );
      end
      fprintf ( 1, '  X     = %f\n', x );
      if ( 0 < r )
        fprintf ( 1, '  X(%d) = %f  F(X) = %f\n', r, tree_data(1,r), tree_data(2,r) );
      else
        fprintf ( 1, '  No right bracket.\n' );
      end
    end

  end

  fprintf ( 1, '\n' );
  fprintf ( 1, 'R8BTREE_BRACKET_LEVEL_TEST\n' );
  fprintf ( 1, '  Normal end of execution.\n' );

  return
end
