当前位置: 首页>>代码示例>>C++>>正文


C++ RedBlackTree类代码示例

本文整理汇总了C++中RedBlackTree的典型用法代码示例。如果您正苦于以下问题:C++ RedBlackTree类的具体用法?C++ RedBlackTree怎么用?C++ RedBlackTree使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了RedBlackTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: DestructorTest

void DestructorTest()
{
	cout << "--------------------------DestructorTest---------------------------" << endl;

	RedBlackTree<int> tree;
	cout << "Create Tree..." << endl;
	tree.Insert(6);
	tree.Insert(7);
	tree.Insert(8);
	tree.Insert(9);
	cout << "Remove All of Tree..." << endl << endl;
	tree.RemoveAll();

	RedBlackTree<int> tree2;
	cout << "Create Tree2..." << endl << endl;
	tree2.Insert(0);
	tree2.Insert(1);
	tree2.Insert(2);
	tree2.Insert(3);

	RedBlackTree<int> tree3(tree2);
	cout << "Create Tree3 (Copy of Tree2)..." << endl;
	cout << "Insert into Tree3..." << endl << endl;
	tree3.Insert(10);
	tree3.Insert(55);
	tree3.Insert(96);
	tree3.Insert(777);

	cout << "Destory Empty Tree1..." << endl;
	cout << "Destory Tree2..." << endl;
	cout << "Destory Copy Tree3..." << endl << endl;
}
开发者ID:liuyal,项目名称:Cpp-Projects,代码行数:32,代码来源:main.cpp

示例2: TEST_F

TEST_F(TestRBTree, RedBlackTree)
{
    RedBlackTree<int32> tree;
    
    tree.insert(1);
    tree.insert(2);
    tree.insert(3);
    
    tree.dump();
}
开发者ID:VitoZhang,项目名称:rime,代码行数:10,代码来源:TestRBTree.hpp

示例3: main

int main()
{
	RedBlackTree t;
	for (unsigned i = 1000; i > 0; --i)
	{
		int k = static_cast<int>(rand());
		cout << "inserting " << k << endl;
		t.insert(k);
	}
}
开发者ID:Danghor,项目名称:Algorithms,代码行数:10,代码来源:red-black.c

示例4: CopyTest

void CopyTest()
{
	cout << "--------------------------CopyTest---------------------------" << endl;

	RedBlackTree<int> tree;
	cout << "Create Tree..." << endl;
	tree.Insert(6);
	tree.Insert(77);
	tree.Insert(1);
	tree.Insert(9);
	tree.Insert(11);
	tree.Insert(5);

	cout << endl << "Verify Red Black Tree..." << endl;
	tree.verify(tree);

	cout << "Size: " << tree.Size() << endl;
	RedBlackTree<int> treeC(tree);
	cout << "Perform Copy Tree..." << endl;
	cout << "Size of Copy: " << treeC.Size() << endl;

	cout << endl << "Verify Copy Red Black Tree..." << endl;
	treeC.verify(treeC);

	cout << "Remove 2 Elements of Copy" << endl; 
	treeC.Remove(5);
	treeC.Remove(1);
	cout << "Size of Copy: " << treeC.Size() << endl;

	//Verify order of Tree
	cout << endl << "Verify Copy Red Black Tree..." << endl;
	treeC.verify(treeC);

	cout << endl;
}
开发者ID:liuyal,项目名称:Cpp-Projects,代码行数:35,代码来源:main.cpp

示例5: main

int main(){
    int i;
    RedBlackTree t;
    RedBlackNode * ptr;
    RedBlackNode * tpr;
    
    i = 3;
    ptr = t.BSTinsert(i);
    cout << "****insert " << i << "************************************" << endl;
    cout << t;
    
    i = 22;
    tpr = t.BSTinsert(i);
    cout << "****insert " << i << "************************************" << endl;
    cout << t;
    
    i = 15;
    t.BSTinsert(i);
    cout << "****insert " << i << "************************************" << endl;
    cout << t;

    i = 2;
    t.BSTinsert(i);
    cout << "****insert " << i << "************************************" << endl;
    cout << t;
    
    i = 12;
    t.BSTinsert(i);
    cout << "****insert " << i << "************************************" << endl;
    cout << t;
    
    i = 25;
    t.BSTinsert(i);
    cout << "****insert " << i << "************************************" << endl;
    cout << t;

    cout << "****right rotate at 3************************************" << endl;
    t.rightrotate(ptr);
    cout << t;
    
    cout<< "****left rotate at 22*************************************" <<endl;
    t.leftrotate(tpr);
    cout << t;
    
    cout<< "****right rotate at 22*************************************" <<endl;
    t.rightrotate(tpr);
    cout << t;
}
开发者ID:snayski,项目名称:School-Work,代码行数:48,代码来源:rbtreetest0.cpp

示例6: InsertTestB

void InsertTestB()
{
	cout << "--------------------------Insert TestB---------------------------" << endl;

	RedBlackTree<int> tree;
	tree.Insert(20);
	tree.Insert(3);
	tree.Insert(7);
	tree.Insert(5);
	tree.Insert(6);
	tree.Insert(10);
	tree.Insert(21);
	tree.Insert(23);
	cout << "New Tree..." << endl;
	cout << "Verify Red Black Tree..." << endl;
	tree.verify(tree);

	RedBlackTree<int> treeC(tree);
	cout <<  "Copy And Insert..." << endl;
	treeC.Insert(55);
	treeC.Insert(9);
	treeC.Insert(11);
	treeC.Insert(777);
	cout << "Verify Copy and Inserted Red Black Tree..." << endl;
	treeC.verify(treeC);

	cout << endl;
}
开发者ID:liuyal,项目名称:Cpp-Projects,代码行数:28,代码来源:main.cpp

示例7: test_redblack

bool test_redblack()
{
	RedBlackTree<int> rbt;
	int lim = 1000;
	srand(time(NULL));
	for(int i = 0; i < lim; i++)
	{
		rbt.insert((i + lim ) % lim);
	}

	int *sorted = rbt.inorder();
	printf("\n");
	for(int i = 0; i < 15; i++)
	{
		printf("%d", sorted[i]);
	}
	printf("\n");
	return true;
}
开发者ID:jacobcalvert,项目名称:GenericDataStructures,代码行数:19,代码来源:tests.cpp

示例8: main

int main()
{
	srand(time(0));

	BinarySearchTree BST;
	RedBlackTree RBT;

	cout << "Inserting 1000 ordered elements into a Binary Search Tree and a Red Black Tree:" << endl << endl;

	for (int i = 0; i < 1000; i++)
	{
		BST.insert(i);
		RBT.insert(i);
	}

	cout << "Height of the Binary Search Tree: " << BST.height() << endl;
	cout << "Height of the Red Black Tree: "     << RBT.height() << endl;

	cout << endl;
	
	BinarySearchTree BST2;
	RedBlackTree RBT2;

	cout << "Inserting 1000 random elements into a Binary Search Tree and a Red Black Tree:" << endl << endl;

	int r;

	for (int i = 0; i < 1000; i++)
	{
		r = rand() % 1000;

		BST2.insert(r);
		RBT2.insert(r);
	}

	cout << "Height of the Binary Search Tree: " << BST2.height() << endl;
	cout << "Height of the Red Black Tree: "     << RBT2.height() << endl;

	cout << endl;
	
}
开发者ID:CarlosPlusPlus,项目名称:rpi-undergrad,代码行数:41,代码来源:TreeTest.cpp

示例9: main

int main(int argc, char **argv) {
    RedBlackTree *rbt = new RedBlackTree(1);
    rbt->Insert(1);

    int numElements = (argc > 1 ? std::atoi(argv[1]) : 1000000);

    std::clock_t start = std::clock();
    for (int i = 2; i <= numElements; i++) {
        rbt->Insert(std::rand() % (numElements * 10));
        std::cout << rbt->ToString() << std::endl;
        std::cout << "---------------------------" << std::endl;
    }
    // double duration = (std::clock() - start) / (double)CLOCKS_PER_SEC;
    // std::cout << "Done inserting " << numElements << " elements. Insertion took
    // "
    //           << duration << " seconds. That is equal to "
    //           << numElements / duration << " insertions per second.\n"
    //           << std::endl;
    //
    // bool foundAll = true;
    // start = std::clock();
    // for (int i = 2; i <= numElements; i++) {
    //   foundAll = foundAll && (rbt->Search(i) != nullptr);
    // }
    // duration = (std::clock() - start) / (double)CLOCKS_PER_SEC;
    //
    // std::cout << "Done searching " << numElements << " elements, "
    //           << (foundAll ? "found all" : "did not find all")
    //           << " elements. Search took " << duration
    //           << " seconds. That is equal to " << numElements / duration
    //           << " searches per second.\n" << std::endl;
    std::cout << rbt->Deepest() << std::endl;
    std::cout << rbt->Shallowest() << std::endl;
}
开发者ID:KyleStach1678,项目名称:RedBlack,代码行数:34,代码来源:main.cpp

示例10: main

int main(int argc, char * argv[]){
	if(argc != 2){
		cout << "USAGE ./trees <input-file>" << endl;
		return 1;
	}
	
	string file(argv[1]);
	ifstream infile(file);	
	string line;
	RedBlackTree RBT;
	while(getline(infile, line)){
		stringstream ss(line);
		int input;
		ss >> input;	
		RBT.insertNode(input);
	}
	RBT.printContents();
	RBT.showTree();
	infile.close();
//	RBT.test_search(file);
	return 0;
}
开发者ID:riptyde4,项目名称:Self-Balancing-Trees,代码行数:22,代码来源:Driver.cpp

示例11: main

int main(int argc, char const *argv[])
{
	RedBlackTree<int, string> sample;
	sample.insert(make_pair(10,"haha"));
	sample.insert(make_pair(20,"haha"));
	sample.insert(make_pair(30,"haha"));
	sample.insert(make_pair(15,"haha"));
	sample.insert(make_pair(25,"haha"));
	sample.insert(make_pair(12,"haha"));
	sample.insert(make_pair(5,"haha"));
	sample.insert(make_pair(3,"haha"));
	sample.insert(make_pair(8,"haha"));
	sample.insert(make_pair(27,"haha"));
	sample.insert(make_pair(40,"haha"));
	sample.insert(make_pair(50,"haha"));
	sample.insert(make_pair(45,"haha"));
	sample.insert(make_pair(9,"haha"));


	sample.print();
	return 0;
}
开发者ID:LCyson,项目名称:Data-Structure,代码行数:22,代码来源:rbbst_test.cpp

示例12: main

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();
}
开发者ID:kimsin3003,项目名称:Red-Black_Tree,代码行数:36,代码来源:Main.cpp

示例13: AssignmentTest

void AssignmentTest()
{
	cout << "--------------------------AssignmentTest---------------------------" << endl;

	cout << endl << "Basic Tree Size of Four" << endl;
	cout  << "Insert.." << endl;
	RedBlackTree<int> tree;
	tree.Insert(5);
	tree.Insert(4);
	tree.Insert(3);
	tree.Insert(2);

	cout << "Verify Red Black Tree 1..." << endl;
	tree.verify(tree);

	RedBlackTree<int> tree2;
	cout << "Assign Tree 1 to Tree 2..." << endl << endl;
	tree2 = tree;

	cout << "Verify Red Black Tree 2..." << endl;
	tree2.verify(tree2);

	cout << "Creat Blank Tree..." << endl ;
	RedBlackTree<int> treeEmpty;
	cout << "Size: " << treeEmpty.Size() << endl << endl;

	RedBlackTree<int> tree3(tree);
	cout << "Creat Tree3 Copy of Tree1..." << endl ;
	cout << "Insert..." << endl;
	tree3.Insert(3);
	tree3.Insert(2);
	cout << "Verify Red Black Tree 3..." << endl;
	tree3.verify(tree3);

	cout << "Assign Tree3 to Empty Tree..." << endl << endl;
	cout << "Verify Red Black Tree 3..." << endl;
	tree3 = treeEmpty;
	tree3.verify(tree3);

	cout << endl;
}
开发者ID:liuyal,项目名称:Cpp-Projects,代码行数:41,代码来源:main.cpp

示例14: TestRedBlackTree_String

int TestRedBlackTree_String()
{
	RedBlackTree<std::string, std::string> *rbtree = new RedBlackTree<std::string, std::string>();

	TEST_ASSERT(rbtree->size() == 0);

	rbtree->insert("first", "one");
	rbtree->insert("second", "two");
	rbtree->insert("third", "three");
	rbtree->insert("fourth", "four");

	std::string                             res;

	TEST_ASSERT(rbtree->size() == 4);

	TEST_ASSERT(rbtree->find("first", res));
	TEST_ASSERT(res == "one");

	TEST_ASSERT(!rbtree->exists("fifth"));

	TEST_ASSERT(rbtree->exists("second"));
	TEST_ASSERT(rbtree->find("second", res));

	TEST_ASSERT(res == "two");

	TEST_ASSERT(!rbtree->erase("fifth"));
	TEST_ASSERT(rbtree->size() == 4);

	TEST_ASSERT(rbtree->exists("first"));
	TEST_ASSERT(rbtree->erase("first"));
	TEST_ASSERT(rbtree->size() == 3);

	TEST_ASSERT(rbtree->exists("second"));
	TEST_ASSERT(rbtree->erase("second"));
	TEST_ASSERT(rbtree->size() == 2);

	TEST_ASSERT(rbtree->exists("third"));
	TEST_ASSERT(rbtree->erase("third"));
	TEST_ASSERT(rbtree->size() == 1);

	TEST_ASSERT(rbtree->exists("fourth"));
	TEST_ASSERT(rbtree->erase("fourth"));
	TEST_ASSERT(rbtree->size() == 0);

	delete rbtree;
	return 0;
}
开发者ID:skydevgit,项目名称:crisscross,代码行数:47,代码来源:rbtree.cpp

示例15: main


//.........这里部分代码省略.........
    // CLOCKS_PER_SEC is a macro from the time library
    cout << "Net time taken for insert:(in seconds) " << time_diff / CLOCKS_PER_SEC << endl;


    // deleting values in the same order:

    cout << "\nDeleting values from 1 to 1,000,000 serially in ascending order\n";

    t1 = clock();

    for (int i = 1; i <= 1000000; i++ ){
    	bst.remove(i);
    }

    t2 = clock();         // end clock

    time_diff = ((float)t2-(float)t1);

    // converting the time to seconds before console printing
    // CLOCKS_PER_SEC is a macro from the time library
    cout << "Net time taken for delete:(in seconds) " << time_diff / CLOCKS_PER_SEC << endl;


    cout << "\n**********************************************************************\n";

     /* For an AVL Tree */    

    cout << "For a Red Black Tree\n\n";
    cout << "Inserting unique values from 1 to 1,000,000 randomly\n";

    // start clock
    t1 = clock();

	RedBlackTree<int> rbt;
   
   for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) {
    rbt.insert_key(*it);
}
    t2 = clock();         // end clock
 
    time_diff = ((float)t2-(float)t1);

    // converting the time to seconds before console printing
    // CLOCKS_PER_SEC is a macro from the time library
    cout << "Net time taken for insert:(in seconds) " << time_diff / CLOCKS_PER_SEC << endl;


    // deleting values in the same order:

    cout << "\nDeleting values from 1 to 1,000,000 serially in ascending order\n";

    t1 = clock();

    for (int i = 1; i <= 1000000; i++ ){
    	rbt.delete_key(i);
    }

    t2 = clock();         // end clock

    time_diff = ((float)t2-(float)t1);

    // converting the time to seconds before console printing
    // CLOCKS_PER_SEC is a macro from the time library
    cout << "Net time taken for delete:(in seconds) " << time_diff / CLOCKS_PER_SEC << endl;

开发者ID:Saurav-K-Aryal,项目名称:CS3-Trees-Sorts,代码行数:65,代码来源:main.cpp


注:本文中的RedBlackTree类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。