//----------------------------------------------------------------------------- // 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_link // // purpose Perform a diagnostic check on the correct structure of a // binary tree around a given link. 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 //----------------------------------------------------------------------------- int avl_check_link ( AVL * arg_tree , avl_link * arg_link ) __PROTO_END__ { int errors_here ; int depth ; errors_here = 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; } 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; } 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 errors. return errors_here; }