//-----------------------------------------------------------------------------
// 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_reverse
//
// purpose	Find the node with the previous lower key from the specified
//		binary tree.  The type of key is irrelevant.
//
// arguments	1 (AVL *) pointer to binary tree
//		2 (void *) optional pointer to node to start from
//
// returns	(void *) pointer to the next lower node
//		(void *) NULL if there is no lower node
//-----------------------------------------------------------------------------
void *
avl_reverse (
    AVL *	arg_tree
    ,
    void *	arg_node
    )
__PROTO_END__
{
    avl_link *		link_p		;

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

    //-------------------------------------------------
    // If no node is given, then use the selected node.
    // If no selected node, then select the last node.
    //-------------------------------------------------
    if ( arg_node ) {
	link_p = avl_node_to_link( arg_tree, arg_node );
    } else {
	if ( ! ( link_p = arg_tree->select ) ) return avl_last( arg_tree );
    }

    //------------------------------------------------
    // If there is a low branch, search lo, hi, hi ...
    //------------------------------------------------
    if ( link_p->lo ) {
	link_p = link_p->lo;
	while ( link_p->hi ) link_p = link_p->hi;
	arg_tree->select = link_p;
	return avl_link_to_node( arg_tree, link_p );
    }

    //---------------------------------------
    // Else go up to find the correct parent.
    //---------------------------------------
    for (;;) {

	//-- If we hit the root then there is no next node.
	if ( link_p->pa == (avl_link *) arg_tree ) {
	    arg_tree->select = NULL;
	    return NULL;
	}

	//-- If we came up the hi branch then this is the next node.
	if ( link_p->pa->hi == link_p ) {
	    link_p = link_p->pa;
	    arg_tree->select = link_p;
	    return avl_link_to_node( arg_tree, link_p );
	}

	//-- Continue further up the tree.
	link_p = link_p->pa;
    }
}

