Ejemplo n.º 1
0
		void TestRbTree::testCase1() {

			RBTree<int, int> tree;
			vector<int> v;
			int i =0;
			for (int i = 0; i < 20; ++i) {
				v.push_back(i);
			}
			random_shuffle(v.begin(), v.end());
			copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
			cout << endl;
			stringstream sstr;
			for (i = 0; i < v.size(); ++i) {
				tree.Insert(makePair(v[i], i));
				cout << "insert:" << v[i] << endl;   //添加结点
			}
			for (i = 0; i < v.size(); ++i) {
				cout << "Delete:" << v[i] << endl;
				tree.Delete(v[i]);             //删除结点
				tree.InOrderTraverse();
			}
			cout << endl;
			tree.InOrderTraverse();
			std::cout << "case1 passed" << std::endl;
		}
Ejemplo n.º 2
0
int main(int argc, const char * argv[]) {
    RBTree<int> tree;
    Helper::print("Welcome to the int RB Tree tester. Jonathan Ginsburg (C) September 2015.");
    while (true) {
        Helper::print("\t1 for inserting in tree.");
        Helper::print("\t2 for deleting from tree.");
        Helper::print("\t3 for searching in tree.");
        Helper::print("\t4 for printing tree in ascending order.");
        Helper::print("\t5 for printing tree in descending order.");
//        Helper::print("\t6 for verifying legality of tree according to AVL rules.");
        Helper::print("\t0 for terminating program.");
        int input = Helper::read<int>(">> Enter your choice: ");
        switch (input) {
            case 0:
                return 0;
                break;
                
            case 1:
                try {
                    tree.Insert(Helper::read<int>(">> Enter value to insert: "));
                } catch (const char * e) {
                    Helper::print(e);
                }
                break;
                
            case 2:
                try {
                    tree.Erase(Helper::read<int>(">> Enter value to erase: "));
                } catch (const char * e) {
                    Helper::print(e);
                }
                break;
                
            case 3:
                try {
                    std::string wasFound = tree.Search(Helper::read<int>("Enter value to search for: ")) ? " " : " not ";
                    std::cout << ">> Value is" << wasFound << "in the tree." << std::endl;
                } catch (const char * e) {
                    Helper::print(e);
                }
                break;
                
            case 4:
                tree.PrintAscending();
                break;
                
            case 5:
                tree.PrintDescending();
                break;
                
//            case 6:
//                std::string isLegal = tree.IsLegal() ? "Tree is legal." : "Tree is not legal.";
//                Helper::print(isLegal);
//                break;
        }
    }
}
Ejemplo n.º 3
0
int main()
{
	RBTree<int, int> rb;
	int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
	for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
	{
		rb.Insert(a[i], a[i]);
	}
	rb.InOrder();
	cout<<rb.IsBalanceTree();
	return 0;
}
Ejemplo n.º 4
0
void TestRBTree()
{
	int arr[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
	int len = sizeof(arr) / sizeof(arr[0]);
	RBTree<int, int> t;
	for (int i = 0; i < len; i++)
	{
		t.Insert(arr[i], i);
		t.InOrder_NonR();
		cout << "Is?" << t.Check() << endl;
	}
	
}
Ejemplo n.º 5
0
int main()
{
	RBTree<int> a;
	for (int i = 0; i < 10; ++i)
		a.Insert(i);
	a.show();
	for (int i = 2; i < 5; ++i)
		a.Del(i);
	a.show();
	cin.get();
	return 0;

}
Ejemplo n.º 6
0
void TestRBTree()
{
	int array[] = { 4, 5, 13, 28, 5, 19, 18, 37, 10, 20 };
	RBTree<int, int> t;
	for (size_t i = 0; i < 10; ++i)
	{
		t.Insert(array[i], array[i]);
	}

	t.InOrder();

	cout << endl;
	cout << "IsBlance?" << " " << t.IsBlance() << endl;
}
Ejemplo n.º 7
0
void Test()
{
	RBTree<int,int> rbt;
	//int arr[] = {16,3,7,11,9,26,18,14,15};
	int arr[] = {48,12,56,32,99,11,10,78,22};
	int size = sizeof(arr)/sizeof(arr[0]);

	for (int i = 0;i < size;i++)
	{
		rbt.Insert(arr[i],i);
		cout<<"IsBlance? "<<rbt.IsBlance()<<endl;
	}
	rbt.InOrder();
	cout<<"IsBlance? "<<rbt.IsBlance()<<endl;
}
Ejemplo n.º 8
0
int main(int argc,char* argv[])
{
    RBTree<int> tree;
    int array[] = {12,1,9,2,0,11,7,19,4,15,18,5,14,13,10,16,6,3,8,17};
    for(int i=0;i<sizeof(array)/sizeof(array[0]);i++)
        tree.Insert(array[i]);
    std::cout<<"preOrder:"<<std::endl;
    tree.PreOrderTraver();
    std::cout<<"delete 6 num,preOrder:"<<std::endl;
    tree.Delete(12);
    tree.Delete(1);
    tree.Delete(9);
    tree.Delete(2);
    tree.Delete(0);
    tree.Delete(11);
    tree.PreOrderTraver();
}
Ejemplo n.º 9
0
int main()
{
    RBTree<int> tree;
    int i;
	int t[]={0,1,2,3,4,5,6,7,8,-1,-2,-3,-4,-5,-6,-7,-8};
    for(i=0;i<17;i++)
        tree.Insert(t[i]);

	cout<<"InROrder:"<<endl;
    tree.InROrder();

    cout<<"\nOutLevelROrder:"<<endl;
    tree.OutLevelROrder();

    cout<<"\nOutLevelROrder:"<<endl;
    tree.Delete(3);
    tree.OutLevelROrder();

    return 0;
}
Ejemplo n.º 10
0
int main(int argc, const char * argv[]) {

    using namespace std;
    using namespace chrono;

    int numberOfElements;
    int numberOfElementsToLookFor;
    int numberOfRepetitions;
    int order = 0;

    cout << endl << "Searching In Trees Time Meter 1.0 by Jonathan Ginsburg and Enrique Gonzales. (c) September 30, 2015. All rights reserved." << endl;

    if (argc == 1) {
        cout << "Type <<TimingOfSearchingTreeAlgorithms help>>" << endl;
        cout << endl;
        return 0;
    }
    else {
        if (WhereIs(argv, argc, "help") != -1) {
            cout << "This is a tool for timing searching algorithms in AVL, B, Red-Black and 2-3 trees written in C++. Use any of the following commands:" << endl;
            cout << "\t1. TimingOfSearchingTreeAlgorithms <arg1> <arg2> <arg3> <arg4>, where <arg1> is the population of elements in each tree, <arg2> is the number of elements to search for in each tree, <arg3> is the number of repetitions to make all measurements with different populations, and <arg4> is the order of the B Tree." << endl;

            cout << endl;
            return 0;
        }
        else if (argc == 5) {
            numberOfElements = atoi(argv[1]);
            numberOfElementsToLookFor = atoi(argv[2]);
            numberOfRepetitions = atoi(argv[3]);
            order = atoi(argv[4]);
            if (numberOfElements > 0 && numberOfElementsToLookFor > 0 && numberOfRepetitions > 0 && order > 0) {
                if (numberOfElements > numberOfElementsToLookFor) {
                }
                else return 0;
            }
            else return 0;
        }
    }

    cout << endl;

    cout << "Number of elements in each tree = " << numberOfElements << ". Number of measurements to make = " << numberOfRepetitions << ". Number of elements to search for = " << numberOfElementsToLookFor << ":" << endl << endl;

    for (int i = 0; i < numberOfRepetitions; ++i) {

        vector<int> data;
        FillIntVector(data, numberOfElements);

        vector<int> dataToLookFor;
        for (int i = 0; i < numberOfElementsToLookFor; ++i) {
            dataToLookFor.push_back(data[i]);
        }

        cout << "\tFilling BTree of order " << order << ": ";
        std::cout.flush();
        BTreeInDisk<int> btree(".", "BTreeSpace", order);
        high_resolution_clock::time_point begin = high_resolution_clock::now();
        for (int i = 0; i < data.size(); ++i) {
            btree.Insert(data[i]);
        }
        high_resolution_clock::time_point end = high_resolution_clock::now();
        auto duration = std::chrono::duration_cast<std::chrono::microseconds>( end - begin ).count();// Time in microseconds
        cout << "\t" << duration << " micro s" << " = " << duration / 1000000 << " s." << endl;

        cout << "\tSearching elements in BTree" << ": ";
        std::cout.flush();
        begin = high_resolution_clock::now();
        for (int i = 0; i < dataToLookFor.size(); ++i) {
            btree.Search(dataToLookFor[i]);
        }
        end = high_resolution_clock::now();
        duration = std::chrono::duration_cast<std::chrono::microseconds>( end - begin ).count();// Time in microseconds
        cout << "\t" << duration << " micro s" << " = " << duration / 1000000 << " s." << endl;

        cout << "\tFilling AVLTree:";
        std::cout.flush();
        jgn::AVLTree<int> avltree;
        begin = high_resolution_clock::now();
        for (int i = 0; i < data.size(); ++i) {
            try {
                avltree.Insert(data[i]);
            }
            catch (const char * exception) {

            }
        }
        end = high_resolution_clock::now();
        duration = std::chrono::duration_cast<std::chrono::microseconds>( end - begin ).count();// Time in microseconds
        cout << "\t" << "\t" << duration << " micro s" << " = " << duration / 1000000 << " s." << endl;

        cout << "\tSearching elements in AVLTree" << ": ";
        std::cout.flush();
        begin = high_resolution_clock::now();
        for (int i = 0; i < dataToLookFor.size(); ++i) {
            try {
                avltree.Search(dataToLookFor[i]);
            } catch (const char * exception) {

            }
        }
        end = high_resolution_clock::now();
        duration = std::chrono::duration_cast<std::chrono::microseconds>( end - begin ).count();// Time in microseconds
        cout << "\t" << duration << " micro s" << " = " << duration / 1000000 << " s." << endl;

        cout << "\tFilling 2-3Tree:";
        std::cout.flush();
        TwoThreeTree<int> _23tree;
        begin = high_resolution_clock::now();
        for (int i = 0; i < data.size(); ++i) {
            _23tree.insert(data[i]);
        }
        end = high_resolution_clock::now();
        duration = std::chrono::duration_cast<std::chrono::microseconds>( end - begin ).count();// Time in microseconds
        cout << "\t" << "\t" << duration << " micro s" << " = " << duration / 1000000 << " s." << endl;

        cout << "\tSearching elements in 2-3Tree" << ": ";
        std::cout.flush();
        begin = high_resolution_clock::now();
        for (int i = 0; i < dataToLookFor.size(); ++i) {
            _23tree.search(dataToLookFor[i]);
        }
        end = high_resolution_clock::now();
        duration = std::chrono::duration_cast<std::chrono::microseconds>( end - begin ).count();// Time in microseconds
        cout << "\t" << duration << " micro s" << " = " << duration / 1000000 << " s." << endl;

        cout << "\tFilling RBTree:";
        std::cout.flush();
        RBTree<int> rbtree;
        begin = high_resolution_clock::now();
        for (int i = 0; i < data.size(); ++i) {
            rbtree.Insert(data[i]);
        }
        end = high_resolution_clock::now();
        duration = std::chrono::duration_cast<std::chrono::microseconds>( end - begin ).count();// Time in microseconds
        cout << "\t" << "\t" << duration << " micro s" << " = " << duration / 1000000 << " s." << endl;

        cout << "\tSearching elements in RBTree" << ": ";
        std::cout.flush();
        begin = high_resolution_clock::now();
        for (int i = 0; i < dataToLookFor.size(); ++i) {
            rbtree.Search(dataToLookFor[i]);
        }
        end = high_resolution_clock::now();
        duration = std::chrono::duration_cast<std::chrono::microseconds>( end - begin ).count();// Time in microseconds
        cout << "\t" << duration << " micro s" << " = " << duration / 1000000 << " s." << endl;

        cout << endl;
    }

    cout << endl;
    return 0;
}