//-----------------------------------------------------------------------------
// 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"

//-----------------------------------------------------------------------------
// internal function	avl_check_link_branch
//
// purpose	Perform a diagnostic check on the correct structure of a
//		binary tree starting at a given link address.  This function
//		uses link pointers instead of node pointers, and so is
//		intended for internal use only.
//
// arguments	1 (AVL *) pointer to binary tree
//		2 (avl_link *) pointer to link in node to check
//
// returns	(int) 0 for no error
//		(int) or-ed flags for detected errors
//
// note		While the AVL package is implemented without recursion, this
//		function does use recursion in order to keep its code simple.
//-----------------------------------------------------------------------------
static
int
avl_check_link_branch (
    AVL *	arg_tree
    ,
    avl_link *	arg_link
    )
{
    int		errors_here	;
    int		errors_deep	;
    int		depth		;

    errors_here = 0;
    errors_deep = 0;
    depth = 0;


    //-- Check parent link.
    if ( arg_link->pa ) {
	if ( arg_link->pa->lo != arg_link && arg_link->pa->hi != arg_link ) {
	    if ( errors_here == 0 ) fputc( '\n', stderr );
	    fprintf( stderr, "ERROR: node->pa->lo != node && node->pa->hi != node\n" );
	    errors_here |= AVL_ERR_CORRUPT_PA_NOBACK;
	}
    } else {
	if ( errors_here == 0 ) fputc( '\n', stderr );
	fprintf( stderr, "ERROR: node->pa == NULL\n" );
	errors_here |= AVL_ERR_CORRUPT_PA_NULL;
    }

    //-- Check lo link.
    if ( arg_link->lo ) {
	if ( arg_link->lo == arg_link->hi ) {
	    if ( errors_here == 0 ) fputc( '\n', stderr );
	    fprintf( stderr, "ERROR: node->lo == node->hi [%p]\n", arg_link->lo );
	    errors_here |= AVL_ERR_CORRUPT_LOHI_CROSSED;
	}
	if ( arg_link->lo->pa != arg_link ) {
	    if ( errors_here == 0 ) fputc( '\n', stderr );
	    fprintf( stderr, "ERROR: node->lo->pa [%p] != node [%p]\n", arg_link->lo->pa, arg_link );
	    errors_here |= AVL_ERR_CORRUPT_LO_NOBACK;
	} else {
	    errors_deep |= avl_check_link_branch( arg_tree, arg_link->lo );
	}
	if ( depth < arg_link->lo->depth ) depth = arg_link->lo->depth;
    }

    //-- Check hi link.
    if ( arg_link->hi ) {
	if ( arg_link->hi->pa != arg_link ) {
	    if ( errors_here == 0 ) fputc( '\n', stderr );
	    fprintf( stderr, "ERROR: node->hi->pa [%p] != node [%p]\n", arg_link->hi->pa, arg_link );
	    errors_here |= AVL_ERR_CORRUPT_HI_NOBACK;
	} else {
	    errors_deep |= avl_check_link_branch( arg_tree, arg_link->hi );
	}
	if ( depth < arg_link->hi->depth ) depth = arg_link->hi->depth;
    }

    //-- Check depth.
    if ( arg_link->depth != 1+depth ) {
	fprintf( stderr,
		 "ERROR: node->depth[%d] != 1+max( node->lo->depth[%d] , node->hi->depth[%d] )[%d]\n",
		 arg_link->depth,
		 arg_link->lo ? arg_link->lo->depth : 0,
		 arg_link->hi ? arg_link->hi->depth : 0,
		 depth );
    }

    //-- Output the node link details if there is an error at this node.
    if ( errors_here ) {
	avl_print_link_detail( stderr, arg_tree, arg_link );
    }

    //-- Return combined errors.
    return errors_here | errors_deep;
}

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	avl_check_tree
//
// purpose	Perform a diagnostic check on the correct structure of a
//		whole binary tree.  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
//
// returns	(int) 0 for no error
//		(int) or-ed flags for detected errors
//-----------------------------------------------------------------------------
int
avl_check_tree (
    AVL *		arg_tree
    )
__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;
    }

    //-- Run checks starting at apex of this tree.
    errors = ( arg_tree->link.hi )
	? avl_check_link_branch( arg_tree, arg_tree->link.hi )
	: 0;

    //-- 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;
}

