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);
}
Example #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;
}
Example #3
0
int main()
{
	RedBlackTree rb;
	srand(time(NULL));


	/*
	insert delete는 매번 호출 후에 redblack tree가 유지되고 있는지 검사합니다.
	*/

	//insert 임의의 숫자를 insert.

	std::cout << "insert start\n" << std::endl;
	for (int i = 0; i < 20; i++)
	{
		rb.Insert(rand() %20);
	}

	//search. delete안에서 매번 호출하기 때문에 10만 search해봤습니다. 
	std::cout << "\n\n\nsearch start\n" << std::endl;
	Node* result = rb.SearchData(10);
	if(result)
		std::cout << "search 10: " << (result->data) << std::endl;
	else
		std::cout << "No Such Data" << std::endl;


	//delete 임의의 숫자를 delete. 없는 경우 no matching data가 뜹니다.
	std::cout << "\n\n\ndelete start" << std::endl;
	for (int i = 0; i < 20; i++)
	{
		rb.Delete(rand()%20);
	}

	getchar();
}
Example #4
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;
}