/*<html><pre>*/
#ifndef   V_TREE_H
#define   V_TREE_H

/*
**	Binary tree node
*/
typedef struct btree_s
{
	void			*data;
	struct btree_s	*parent;
	struct btree_s	*children[2];
}	btree_t, *btree_p;

/*
**	N-way tree node
*/
typedef struct ntree_s
{
	void			*data;
	struct ntree_s	*parent;
	vp_stack_t		children;
}	ntree_t, *ntree_p;


#ifdef	__cplusplus
extern "C" {
#endif	/* __cplusplus */
	/*	Allocates new tree node and optionally connects it with
	 *	parent. Input:
	 *		data	- pointer to store in ->data field;
	 *		parent	- parent node for new node (can be NULL);
	 *		child_index - if 0, connect new node at left of parent.
	 */
	btree_p	V_GLOBAL	btAllocNode(void *data,btree_t *parent,
		int child_index);
	/*	Same as above, but allocates additional space and copies
	 *	'len' bytes from 'data' to node->data.
	 */
	btree_p	V_GLOBAL	btStore(void *data,u32_t len,btree_t *parent,
		int child_index);
	/*
	 *	Returns root of tree with given node.
	 */
	btree_p	V_GLOBAL	btRootNode(btree_t *node);
	/*
	**	Removes all nodes from leftmost to rightmost, optionally
	**	calling 'destructor' for data in each node.
	*/
	void	V_GLOBAL	btDeleteAllNodes(btree_t *root,
		void (V_CALL *destructor)(void*));
	/*	Allocates new tree node */
	ntree_p	V_GLOBAL	treeAllocNode(void *data,ntree_t *root);
	/*	Same as above, but allocates additional space and copies
	 *	'len' bytes from 'data' to node->data.
	 */
	ntree_p	V_GLOBAL	treeStore(void *data,u32_t len,ntree_t *root);
	/*
	 *	Returns root of tree with given node.
	 */
	ntree_p	V_GLOBAL	treeRootNode(ntree_t *node);
	/*
	**	Removes all nodes from leftmost to rightmost, optionally
	**	calling 'destructor' for data in each node.
	*/
	void	V_GLOBAL	treeDeleteAllNodes(ntree_t *root,
		void (V_CALL *destructor)(void*));
	/*
	**	SET implementation using binary trees.
	**	compar() must return: -1 if less, 0 ifeq, 1 ifgt.
	*/
	btree_p	V_GLOBAL	btInsertNode(btree_p root,void *data,
		int (V_CALL *compar)(void *,void *,void *),void *arg);
	btree_p	V_GLOBAL	btFindNode(btree_p root,void *data,
		int (V_CALL *compar)(void *,void *,void *),void *arg);
	/*	'destructor' is optional */
	vboolean	V_GLOBAL	btDeleteNode(btree_p *root,void *data,
		int (V_CALL *compar)(void *,void *,void *),void *arg,
		void (V_CALL *destructor)(void*));
#ifdef	__cplusplus
}
#endif	/* __cplusplus */

#endif	/* V_TREE_H */
/*</pre></html>*/
/*EOF*/
