#include <v_lib.h>

int V_CALL
compar(void *p1,void *p2,void *arg)
{
	if(p1 == p2)
		return 0;
	return (unsigned)p1 < (unsigned)p2 ? -1 : 1;
}

#define		size	10
unsigned 	add_data[size]=
{
	1,2,4,0,5,6,3,7,8,9
};

#define		find_size	4
unsigned 	find_data[find_size]=
{
	0,4,3,size
};

void V_CALL destr(void *data)
{
	printf("Destructor: node data %X\n",data);
}

int main()
{
	int i;
	btree_p root, node;
	void *data;
	logStartConsoleLog();
    logUpperLevel(LOG_TRACE);
    
	root=btAllocNode(NULL,NULL,0); /* also can be btInsert() */

	for(i=0;i<size;i++)
	{
		data=(void*)(add_data[i]);
		node=btInsertNode(root,data,compar,NULL);
		printf("btInsert(0x%08X): ",data);
		if(!node)
			printf("already exists.\n");
		else
			printf("allocated node %X\n",node);
	}

	/* Remove one node */
	data=(void*)4;
	if(btDeleteNode(&root,data,compar,NULL,destr))
		printf("\nNode with %u removed.\n\n",data);
	else
		printf("\nNode with %u NOT removed.\n\n",data);
	
	for(i=0;i<find_size;i++)
	{
		data=(void*)(find_data[i]);
		node=btFindNode(root,data,compar,NULL);
		printf("btFind(0x%0000000X): ",data);
		if(!node)
			printf("not exists.\n");
		else
			printf("found in node %X\n",node);
	}

	puts("");
		
	/*
	btDeleteAllNodes(root,destr);
	*/
	memPrintStats();

	while(root)
		btDeleteNode(&root,root->data,compar,NULL,destr);

	memPrintStats();

	return 0;
}
