//----------------------------------------------------------------------------- // 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_iterate // // purpose Execute a given function once for each node in the given // binary tree. Iteration is done in forward order. // // arguments 1 (AVL *) pointer to binary tree // 2 (void (*fun)(void *)) pointer to function to execute // // returns (long) sum of return values or error code. // // callback The function specified in argument 2 is called with: // arguments 1 (void *) pointer to node // returns (void) ignored // // warning Do NOT delete the current node from the tree during the // function being called with it. //----------------------------------------------------------------------------- long avl_iterate( AVL * arg_tree , int ( (* arg_fun)(void *) ) ) __PROTO_END__ { avl_link * link_p ; avl_link * prev_p ; long count ; #ifdef AVL_CHECK_ARGS //--------------------------------- // Make sure we have a binary tree. //--------------------------------- if ( ! arg_tree ) return AVL_ERR_BAD_ROOT; //-------------------------------------- // Make sure we have a function to call. //-------------------------------------- if ( ! arg_fun ) return 0; #endif count = 0; if ( ! ( link_p = ( prev_p = & ( arg_tree->link ) )->hi ) ) return 0; while ( link_p != & ( arg_tree->link ) ) { if ( prev_p == link_p->pa ) { if ( link_p->lo ) { prev_p = link_p; link_p = link_p->lo; continue; } prev_p = link_p->lo; } if ( prev_p == link_p->lo ) { count += (* arg_fun)( (void *) avl_link_to_node( arg_tree, link_p ) ); if ( link_p->hi ) { prev_p = link_p; link_p = link_p->hi; continue; } prev_p = link_p->hi; } prev_p = link_p; link_p = link_p->pa; continue; } return count; }