//----------------------------------------------------------------------------- // 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_initialize // // purpose Initialize the root struct of a binary tree to the initial // empty state, ready for operations, regardless of the original // contents. The original contents will be lost, and if any // pointers are the only pointers to allocated memory, then that // memory is also lost. // // arguments 1 (AVL *) pointer to root struct to be initialized // 2 (size_t) offset from node struct to links struct // 3 (int(*)(avl_link*,avl_link*,AVL*)) function to compare keys // // returns (AVL *) pointer to same root struct now initialized // (AVL *) NULL if there is an error //----------------------------------------------------------------------------- AVL * avl_initialize( AVL * arg_tree , size_t arg_offset , int (* arg_compare_f)(avl_link *,avl_link *,AVL *) ) __PROTO_END__ { //--------------------------------- // Make sure we have a binary tree. //--------------------------------- if ( ! arg_tree ) return NULL; //------------------------- // Fill in the root struct. //------------------------- arg_tree->link.lo = NULL; arg_tree->link.hi = NULL; arg_tree->link.pa = NULL; arg_tree->link.depth = -1; arg_tree->select = NULL; arg_tree->offset = arg_offset; arg_tree->compare_f = arg_compare_f; #if AVL_COUNT_ENABLE //---------------------- // Initialize the count. //---------------------- avl_count_set_nodes( arg_tree, 0 ); #endif /* AVL_COUNT_ENABLE */ //-- Clear last error. arg_tree->err_num = 0; //-- The tree is now ready for nodes. return arg_tree; }