void multi_random_insert_RBTree(){
	RBTree rbt;
	std::vector<UInt32> array(10);
	array[0] = 4;
	array[1] = 43;
	array[2] = 23;
	array[3] = 6;
	array[4] = 57;
	array[5] = 5;
	array[6] = 8;
	array[7] = 1018;
	array[8] = 54;
	array[9] = 67;

	for(UInt32 i = 0; i < 10; ++i){

		Test_Sorted_Order_Key tk(array, array[i]);
	
		BOOST_REQUIRE(rbt.insert(tk, i) == true);

		UInt32 searched;
		BOOST_REQUIRE(rbt.find(tk, searched) == true);

		BOOST_REQUIRE(searched == i);
	}

	for(UInt32 i = 0; i < 10; ++i){
		Test_Sorted_Order_Key tk(array, array[i]);
		
		UInt32 searched;
		BOOST_REQUIRE(rbt.find(tk, searched) == true);

		BOOST_REQUIRE(searched == i);
	}
}
void delete_2_RBTree(){
        RBTree rbt;
        std::vector<UInt32> array(3);
        array[0] = 2;
        array[1] = 1;
        array[2] = 3;

        Test_Sorted_Order_Key tk0(array, 2);
        BOOST_REQUIRE(rbt.insert(tk0, 0) == true);

        Test_Sorted_Order_Key tk1(array, 1);
        BOOST_REQUIRE(rbt.insert(tk1, 1) == true);

        Test_Sorted_Order_Key tk2(array, 3);
        BOOST_REQUIRE(rbt.insert(tk1, 2) == true);

        UInt32 searched;

	BOOST_REQUIRE(rbt.remove(tk0) == true);
        BOOST_REQUIRE(rbt.find(tk0, searched) == false);

	BOOST_REQUIRE(rbt.remove(tk1) == true);
        BOOST_REQUIRE(rbt.find(tk1, searched) == false);

	BOOST_REQUIRE(rbt.remove(tk2) == true);
        BOOST_REQUIRE(rbt.find(tk2, searched) == false);
}
void multi_insert_delete_RBTree(const std::vector<UInt32>& order_of_removal){
        RBTree rbt;
        std::vector<UInt32> array(order_of_removal.size());
	UInt32 sz = order_of_removal.size();
	UInt32 filled = 0;
        while(filled < sz){
                array[filled] = rand();
                Test_Sorted_Order_Key tk( array, array[filled] );

                if(rbt.insert(tk, filled) == false){
			continue;
		}
		++filled;
        }

        for(UInt32 i = 0; i < order_of_removal.size(); ++i){
                Test_Sorted_Order_Key tk(array, array[ order_of_removal[i] ] );

                UInt32 searched;
                BOOST_REQUIRE(rbt.find(tk, searched) == true);

		BOOST_REQUIRE(rbt.remove(tk) == true);

                BOOST_REQUIRE(rbt.find(tk, searched) == false);
        }
}
void multi_delete_RBTree(const std::vector<UInt32>& order_of_removal){
        RBTree rbt;
        std::vector<UInt32> array(order_of_removal.size());
        for(UInt32 i = 0; i < order_of_removal.size(); ++i){
                array[i] = i;
                Test_Sorted_Order_Key tk(array, array[i]);

                BOOST_REQUIRE(rbt.insert(tk, i) == true);

                UInt32 searched;
                BOOST_REQUIRE(rbt.find(tk, searched) == true);

                BOOST_REQUIRE(searched == i);
        }

        for(UInt32 i = 0; i < order_of_removal.size(); ++i){
                Test_Sorted_Order_Key tk( array, array[ order_of_removal[i] ] );

                UInt32 searched;
                BOOST_REQUIRE(rbt.find(tk, searched) == true);

		BOOST_REQUIRE(rbt.remove(tk) == true);

                BOOST_REQUIRE(rbt.find(tk, searched) == false);
        }
}
void simple_delete_RBTree(){
	RBTree rbt;
	std::vector<UInt32> array(10);
	array[0] = 2;
	Test_Sorted_Order_Key tk(array, 2);

	BOOST_REQUIRE(rbt.insert(tk, 0) == true);

	UInt32 searched;
	BOOST_REQUIRE(rbt.find(tk, searched) == true);

	BOOST_REQUIRE(searched == 0);

	BOOST_REQUIRE(rbt.remove(tk) == true);

	BOOST_REQUIRE(rbt.find(tk, searched) == false);
}
void multi_insert_delete_Objects_RBTree(const std::vector<UInt32>& order_of_removal){

	UInt32 sz = order_of_removal.size();

	std::vector<Compound_Test> array(sz);

	BOOST_REQUIRE(sz == 8);

	array[0].m_name="Gargi";array[0].m_age=4;
	array[1].m_name="Mithun";array[0].m_age=76;
	array[2].m_name="Abhay";array[0].m_age=56;
	array[3].m_name="Jamie";array[0].m_age=9;
	array[4].m_name="Yang";array[0].m_age=19;
	array[5].m_name="Jerry";array[0].m_age=52;
	array[6].m_name="Sherry";array[0].m_age=80;
	array[7].m_name="Gargi";array[0].m_age=26;

        RBTree rbt;

	for(UInt32 i = 0; i < sz; ++i){
		Test_Compound_Object_Key<Compound_Test> tco(array, array[i]);

		BOOST_REQUIRE(rbt.insert(tco, i) == true);

                UInt32 searched;
                BOOST_REQUIRE(rbt.find(tco, searched) == true);
	}

        for(UInt32 i = 0; i < order_of_removal.size(); ++i){
                Test_Compound_Object_Key<Compound_Test> tco(array, array[ order_of_removal[i] ] );

                UInt32 searched;
                BOOST_REQUIRE(rbt.find(tco, searched) == true);

		BOOST_REQUIRE(rbt.remove(tco) == true);

                BOOST_REQUIRE(rbt.find(tco, searched) == false);
        }
}
Exemple #7
0
int main()
{ RBTree<int, string> rbtree;
  for (int i=0; i<1000; i++)
    rbtree.insertItem((i*71)%1000, "ABC");
  RBTree<int, string>::Position 
                            f = rbtree.find(10*71%1000);
  cout << f.element() << " " << f.key() << endl;
  
  f = rbtree.find(1001);
  if (f.isNull()) cout << "NULL" << endl;
  else  cout << f.element() << " " << f.key() << endl;
  
  try {
     rbtree.removeElement(555*71%1000);
     rbtree.removeElement(1001);
  }
  catch(NonexistentElementException e)
  { cout << e.getMessage() << endl;
    exit(1);  
  }
  return 0;   
}    
void multi_insert_RBTree(){
	RBTree rbt;
	std::vector<UInt32> array(10);
	for(UInt32 i = 0; i < 10; ++i){
		array[i] = i;
		Test_Sorted_Order_Key tk(array, array[i]);
	
		BOOST_REQUIRE(rbt.insert(tk, i) == true);

		UInt32 searched;
		BOOST_REQUIRE(rbt.find(tk, searched) == true);

		BOOST_REQUIRE(searched == i);
	}

	for(UInt32 i = 0; i < 10; ++i){
		Test_Sorted_Order_Key tk(array, array[i]);
		
		UInt32 searched;
		BOOST_REQUIRE(rbt.find(tk, searched) == true);

		BOOST_REQUIRE(searched == i);
	}
}
Exemple #9
0
int main()
{
//    LinkedList<int> *l = new LinkedList<int>;
//    l->prependElement(3);
//    l->prependElement(5);
//    l->prependElement(6);
//    l->insertElement(5, new LinkedListElement<int>(12));
//    l->print();
//    l->deleteElement(5);
//    l->print();
//    std::cout << "============" << std::endl;
//    RBTree<int> t(5);
    RBTree<int> t;
    srand (1);
    for (int i = 0; i < 18; i++) {
//        t.insert(rand());
        t.insert(i);
    }
//    t.insert(40);
//    t.insert(60);
//    t.insert(-10);
//    t.insert(2);
//    t.insert(91);
//    t.insert(4);
//    t.insert(6);
//    t.insert(18);
//    t.insert(17);
//    t.insert(50);
    printPretty(&t, 1, 0, std::cout);
//    std::cout << t.min()->data() << std::endl;
//    std::cout << t.max()->data() << std::endl;
    t.deleteNode(t.find(11));
    printPretty(&t, 1, 0, std::cout);
//    t.print();
//    t.deleteNode(t.find(40), 0);
//    std::cout << "==" << std::endl;
//    t.print();

    return 0;
}
Exemple #10
0
///== /////////////////////////////////////////////
int main() {
    printf("Test of Red-Black Tree class\n");
    printHelp();
    FILE* f = NULL;

    // Define a random tree with 20 nodes
    time_t t = time(0); // Current time in seconds since 1 Jan 1970
    srand(t);

    RBTree tree;
    char line[256];
    while (true)
    {
        printf("Command>");
        if (fgets(line, 254, stdin) == NULL) break;

        // Parse a command
        line[254] = 0;
        int len = strlen(line);
        // Remove "\r\n" at the end of line
        if (len > 0 && line[len-1] == '\n') { line[len-1] = 0; --len; }
        if (len > 0 && line[len-1] == '\r') { line[len-1] = 0; --len; }

        int i = 0;
        // Skip a space in beginning of line
        while (i < len && isspace(line[i])) ++i;
        int commandBeg = i;
        while (i < len && isalpha(line[i])) ++i;
        int commandEnd = i;
        int commandLen = commandEnd - commandBeg;

        if (strncmp("gentree", line+commandBeg, commandLen) == 0 || strncmp("gt", line+commandBeg, commandLen) == 0)
        {
            while (i < len && isspace(line[i])) ++i; // Skip a space
            if (i >= len || !isdigit(line[i])) { printf("Incorrect command.\n"); printHelp(); continue; }
            int n = atoi(line + i);

            // Generate a random tree
            tree.clear();
            int j = 0; if (n > 100)  n = 100;        // Maximum 100 nodes
            while (j < n)
            {
                Integer num(rand() % 100 + 1); RBTreeNode* node;
                if (!tree.find(&num, tree.root(), &node)) { Integer* v = new Integer(num); tree.insert(node, v); ++j; }
            }

            // Print a tree to stdout
            writeIntegerTree(tree.root(), stdout);

        }
        else if (strncmp("readtree", line+commandBeg, commandLen) == 0)
        {

            while (i < len && isspace(line[i])) ++i; // Skip a space
            if ((f = fopen(line+i, "r")) == NULL) { perror("Could not open a file for reading"); continue; }
            // Read a tree from a file
            if (readIntegerTree(tree, f)) writeIntegerTree(tree.root(), stdout); else  printf("Incorrect format.\n"); 
            fclose(f); f = NULL;

        }
        else if (strncmp("writetree", line+commandBeg, commandLen) == 0)
        {
            while (i < len && isspace(line[i])) ++i; // Skip a space
            if (i >= len) { printf("Incorrect command.\n"); printHelp(); continue; }
            if ((f = fopen(line+i, "w")) == NULL) { perror("Could not open a file for writing"); continue; }
            writeIntegerTree(tree.root(), f);
            fclose(f); f = NULL;

        }
        ///////////////////// ================================================
        else if (strncmp("md", line+commandBeg, commandLen) == 0)
        {
            RBTreeNode*node = tree.root();
            printf("Max depth = %d.\n", GetDepth(node));
            printf("Black depth = %d.\n", GetBlackDepth(node));
        }
        ///////////////////// ================================================
        else if (strncmp("quit", line+commandBeg, commandLen) == 0) break; // end if
    } // end while

    return 0;
}
void simple_ctor_RBTree(){
	RBTree rbt;
	Test_Key tk;
	UInt32 ox;
	BOOST_REQUIRE(rbt.find(tk, ox) == false);
}