int main()
{
    cout<< endl << "reb black tree test :"<<endl;
    RedBlackTree p;
    cout<<p.Find(4)<<endl;
    p.Insert(9);
    p.Insert(37);
    p.Insert(4);
    p.Insert(53);
    p.Insert(6);
    p.Insert(45);
    p.Insert(1);
    p.InOrderTraverse();
    p.Delete(9);
    p.InOrderTraverse();
    cout<<p.Find(4)<<endl;
    p.Delete(4);
    cout<<p.Find(4)<<endl;
    p.InOrderTraverse();
    p.Insert(9);
    p.Insert(37);
    p.InOrderTraverse();

	return(0);
}
Exemplo n.º 2
0
int main(int argc,char**argv)
{
    RedBlackTree p;
	cout<<p.Find(4)<<endl;
    p.Insert(9);
    p.Insert(37);
    p.Insert(4);
    p.Insert(53);
    p.Insert(6);
    p.Insert(45);
    p.Insert(1);
    p.InOrderTraverse();
    p.Delete(9);
    p.InOrderTraverse();
	cout<<p.Find(4)<<endl;
	p.Delete(4);
	cout<<p.Find(4)<<endl;
	p.InOrderTraverse();
	p.Insert(9);
	p.Insert(37);
	p.InOrderTraverse();
    return 0;
}
Exemplo n.º 3
0
void TestRedBlackTree()
{
	int *ptr;
	int *val, *ret;
	bool IsInsert;

	RedBlackTree<int, int>* RBTree;
	SimpleCompareNodesAlgorithm<int, int> CompareAlgorithm;

/**********************
* Example 
***********************/
	RBTree = new RedBlackTree<int, int>(&CompareAlgorithm);

	//initialize randomizer
	srand( (unsigned)time( NULL ) );

	for (int i = 0; i < 12; i++)
	{
		ptr = new int;
		*ptr = i;//rand()&0xff;
		val = RBTree->Search(ptr, &IsInsert);
		if(val != NULL)
		{
			cout << "ERROR" << endl;
		}
	}

	//display all values
	for(val=RBTree->Lookup(RBFIRST, NULL); val!=NULL; val=RBTree->Lookup(RBNEXT, val))
		cout << *val << endl;

	//delete tree
	for(val=RBTree->Lookup(RBFIRST, NULL); val!=NULL; val=RBTree->Lookup(RBFIRST, val))
	{
		ret = RBTree->Delete(val);
		delete (int*) ret;
	}
	
	delete RBTree;

/**********************
* Example 1
***********************/
	
	RBTree = new RedBlackTree<int, int>(&CompareAlgorithm);

	for (int j = 200; j > 0; j--)
	{
		ptr = new int;
		*ptr = j;
		val = RBTree->Search(ptr, &IsInsert);
		if(val != NULL)
		{
			cout << "ERROR" << endl;
		}
	}

	//display tree
	for(val=RBTree->Lookup(RBFIRST, NULL); val!=NULL; val=RBTree->Lookup(RBNEXT, val))
		cout << *val << endl;

	//delete tree
	for(val=RBTree->Lookup(RBFIRST, NULL); val!=NULL; val=RBTree->Lookup(RBFIRST, val))
	{
		ret = RBTree->Delete(val);
		delete (int*) ret;
	}
	
	delete RBTree;

/**********************
* Example 2
***********************/

	RBTree = new RedBlackTree<int, int>(&CompareAlgorithm);

	for (int k = 400; k > 0; k--)
	{
		ptr = new int;
		*ptr = k;
		val = RBTree->Search(ptr, &IsInsert);
		if(val != NULL)
		{
			cout << "ERROR" << endl;
		}
	}

/*
	printf("Minimum branch length: %d\n", minleaf);
	printf("Maximum branch length: %d\n", maxleaf);
*/	
	for (int k1 = 400; k1 > 0; k1--)
	{
		val = RBTree->Delete(&k1);
		if(val == NULL)
		{
			cout << "ERROR" << endl;
		}

		delete (int*) val;
	}

	delete RBTree;

/**********************
* Test Find
***********************/
	
	RBTree = new RedBlackTree<int, int>(&CompareAlgorithm);

	for (int m = 200; m > 0; m--)
	{
		ptr = new int;
		*ptr = m;
		val = RBTree->Find(ptr);
		if(val != NULL)
		{
			cout << "ERROR" << endl;
		}

		//ptr shouldn't be added to the tree, so just delete it
		delete ptr;
	}

	//display tree (shouldn't be one)
	for(val=RBTree->Lookup(RBFIRST, NULL); val!=NULL; val=RBTree->Lookup(RBFIRST, val))
	{
		cout << *val << endl;
		ret = RBTree->Delete(val);
		delete (int*) ret;
	}
	
	delete RBTree;
}