//-----------------------------------------------------------------------------
// Copyright © 2003 - Philip Howard - All rights reserved
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//-----------------------------------------------------------------------------
// package	libh/avl
// homepage	http://libh.slashusr.org/
//-----------------------------------------------------------------------------
// author	Philip Howard
// email	libh at ipal dot org
// homepage	http://phil.ipal.org/
//-----------------------------------------------------------------------------
// This file is best viewed using a fixed spaced font such as Courier
// and in a display at least 120 columns wide.
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// G. M. Adelson-Velskii and E. M. Landis are the inventors of the algorithm
// bearing their initials, AVL.  Their algorithm for height-balanced binary
// search trees is the basis of the code in this package.  Some modification
// is made to the balancing operations, and features are added for practical
// purposes.
//-----------------------------------------------------------------------------

#include "avl_lib.h"

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	avl_select
//
// purpose	Set the current selection to a node previously obtained from
//		avl_find.  The node must still be in the tree or the results
//		are unpredictable and likely disasterous.
//
// arguments	1 (AVL *) pointer to root of the binary tree
//		2 (void *) pointer to node to be restored as current
//
// returns	(int) -1 : error
//		(int)  0 : OK
//-----------------------------------------------------------------------------
int
avl_select (
    AVL *		arg_tree
    ,
    void *		arg_node
    )
__PROTO_END__
{
#if AVL_CHECK_ARGS
    //---------------------------------
    // Make sure we have a binary tree.
    //---------------------------------
    if ( ! arg_tree ) return -1;
#endif

#if AVL_PARANOID_LOGIC
#if AVL_COUNT_ENABLE
    //------------------------------------------------------
    // Make sure there are supposed to be nodes in the tree.
    //------------------------------------------------------
    if ( avl_count_get_nodes( arg_tree ) < 1 ) return -1;
#endif /* AVL_COUNT_ENABLE */
#endif /* AVL_PARANOID_LOGIC */

    //-------------------------------------------
    // Restore the node as the current selection.
    //-------------------------------------------
    arg_tree->select = ( arg_node ) ? avl_node_to_link( arg_tree, arg_node ) : NULL;

    return 0;
}

