//-----------------------------------------------------------------------------
// 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_check_node
//
// purpose	Perform a diagnostic check on the correct structure of a
//		binary tree around a given node.  If there is an error,
//		abort program execution unless the environment variable
//		AVL_ERROR_CONTINUE is defined non-zero or empty.
//
// arguments	1 (AVL *) pointer to binary tree to check
//		2 (void *) pointer to node (not link)
//
// returns	(int) 0 for no error
//		(int) or-ed flags for detected errors
//-----------------------------------------------------------------------------
int
avl_check_node (
    AVL *		arg_tree
    ,
    void *		arg_node
    )
__PROTO_END__
{
    char *	env_var		;
    int		errors		;

    //-- If checking has been disabled, just return now.
    env_var = getenv( "AVL_CHECK_DISABLE" );
    if ( env_var && ( env_var[0] == 0 || strtol( env_var, NULL, 0 ) ) ) {
	return 0;
    }

    //-- Check the root pointer.
    if ( ! arg_tree ) {
	fprintf( stderr, "ERROR: root is NULL\n" );
	return AVL_ERR_ROOT_NULL;
    }

    //-- Check the node pointer.
    if ( ! arg_node ) {
	fprintf( stderr, "ERROR: node is NULL\n" );
	return AVL_ERR_NODE_NULL;
    }

    //-- Run checks on the link in this node.
    errors = avl_check_link( arg_tree, avl_node_to_link( arg_tree, arg_node ) );

    //-- If there was an error, either abort or continue.
    if ( errors != 0 ) {
	env_var = getenv( "AVL_ERROR_CONTINUE" );
	if ( ! env_var || ( env_var[0] != 0 && ! strtol( env_var, NULL, 0 ) ) ) {
	    fprintf( stderr, "\nAVL abort due to errors (%04x)\n", errors );
	    exit( 1 );
	}
    }

    //-- Return error status.
    return errors;
}

