Esempio n. 1
0
void TestRBTree() {
    ::printf("PrintRBTree\n");

    DataNode_t a, b, c;
    a.m_value = 1;

    b.m_value = 3;

    c.m_value = 2;

    RBTree<DataNode_t, Compare_t> rbTree;
    rbTree.InsertUnique(&a);
    rbTree.InsertUnique(&b);
    rbTree.InsertUnique(&c);

    PrintRBTree(rbTree);

    auto first = rbTree.First();
    auto last = rbTree.Last();

    auto target = rbTree.Search(2);
    auto backward = rbTree.Backward(target);
    auto forward = rbTree.Forward(target);

    rbTree.Remove(&c);
    first = rbTree.First();
    last = rbTree.Last();

    PrintRBTree(rbTree);
}
Esempio 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;
        }
    }
}
Esempio n. 3
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;
}