//-----------------------------------------------------------------------------
// 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_init_alike
//
// purpose	Initialize the root struct of a binary tree to an initial
//		empty state, but like that of another given binary tree,
//		made to be 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 (AVL *) pointer to peer root struct to be copied from
//
// returns	(AVL *) pointer to same root struct now initialized
//		(AVL *) NULL if there is an error
//-----------------------------------------------------------------------------
AVL *
avl_init_alike(
    AVL *		arg_tree
    ,
    AVL *		arg_peer
)
__PROTO_END__
{
    //---------------------------------
    // Make sure we have a binary tree.
    //---------------------------------
    if ( ! arg_tree ) return NULL;
    if ( ! arg_peer ) 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_peer->offset;
    arg_tree->compare_f		= arg_peer->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;
}

