//-----------------------------------------------------------------------------
// 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_find
//
// purpose	Find a link in a binary tree based on a key in a given link
//		structure.
//
//		If the influential direction is none, return the first exact
//		match encountered, or return none.
//
//		If the influential direction is low, return the lowest exact
//		match encountered, or if there is no exact match: if the
//		near option is specified, choose the nearest node in the
//		low direction.
//
//		If the influential direction is high, return the lowest exact
//		match encountered, or if there is no exact match: if the
//		near option is specified, choose the nearest node in the
//		high direction.
//
// arguments	1 (AVL *) pointer to binary tree
//		2 (void *) pointer to argument key link
//		3 (int) direction and near option, -2, -1, 0, 1, 2
//			-2 = choose lowest exact, or nearest lower
//			-1 = choose lowest exact only
//			 0 = choose fastest exact only
//			 1 = choose highest exact only
//			 2 = choose highest exact, or nearest higher
//
// returns	(void *) pointer to node with matching key.
//		(void *) NULL if node not found
//-----------------------------------------------------------------------------
void *
avl_find (
    AVL *	arg_tree
    ,
    void *	arg_key
    ,
    int		arg_dir_near
    )
__PROTO_END__
{
    avl_link *		link_p		;
    avl_link *		exact_p		;
    avl_link *		near_p		;


#if AVL_CHECK_ARGS
    //---------------------------------------
    // Make sure we were given a binary tree.
    //---------------------------------------
    if ( ! arg_tree ) return NULL;
#endif

    //----------------------------------------------------------------
    // If we were not given a key, then return the last selected node.
    //----------------------------------------------------------------
    if ( ! arg_key ) {
	return ( arg_tree->select ) ? avl_link_to_node( arg_tree, arg_tree->select ) : NULL;
    }

    if ( arg_dir_near == 0 ) {

	//-------------------------------------
	// Search the tree for the exact match.
	//-------------------------------------
	link_p = (avl_link *) arg_tree->link.hi;
	while ( link_p ) {
	    int match;
	    match = avl_compare_keys( link_p, arg_key, arg_tree );
	    if ( match == 0 ) break;
	    link_p = ( match < 0 ) ? link_p->hi : link_p->lo;
	}

    } else {

	if ( arg_dir_near < 0 ) {

	    //----------------------------------------------------------
	    // Search the tree with an influence to the lower direction.
	    //----------------------------------------------------------
	    near_p = exact_p = (avl_link *) NULL;
	    link_p = (avl_link *) arg_tree->link.hi;
	    while ( link_p ) {
		int match;
		match = avl_compare_keys( link_p, arg_key, arg_tree );
		if ( match < 0 ) {
		    if ( ! link_p->hi ) break;
		    link_p = ( near_p = link_p )->hi;
		} else {	 
		    if ( match == 0 ) exact_p = link_p;
		    if ( ! link_p->lo ) break;
		    link_p = link_p->lo;
		}
	    }
	    link_p = NULL;
	    if ( arg_dir_near < -1 ) link_p = near_p;

	} else {

	    //-----------------------------------------------------------
	    // Search the tree with an influence to the higher direction.
	    //-----------------------------------------------------------
	    near_p = exact_p = (avl_link *) NULL;
	    link_p = (avl_link *) arg_tree->link.hi;
	    while ( link_p ) {
		int match;
		match = avl_compare_keys( link_p, arg_key, arg_tree );
		if ( match > 0 ) {
		    if ( ! link_p->lo ) break;
		    link_p = ( near_p = link_p )->lo;
		} else {	 
		    if ( match == 0 ) exact_p = link_p;
		    if ( ! link_p->hi ) break;
		    link_p = link_p->hi;
		}
	    }
	    link_p = NULL;
	    if ( arg_dir_near > 1 ) link_p = near_p;

	}

	//-------------------------------------------
	// If there is an exact match, always use it.
	//-------------------------------------------
	if ( exact_p ) link_p = exact_p;

    }

    //-- If there is no match at all, return NULL.
    if ( ! link_p ) return (avl_link *) NULL;

    //-- Make the found node be the selected node.
    arg_tree->select = link_p;

    //-- Return the found node.
    return avl_link_to_node( arg_tree, link_p );
}

