Пример #1
0
int main()
{
	{
		BinarySearchTree<int> t;
		t.insert(8);t.insert(3);t.insert(10);t.insert(1);
		t.remove(1);t.remove(3);
		t.insert(6);t.insert(14);t.insert(4);t.insert(7);t.insert(13);

        assert( t.find(13)->val == 13);
        assert( t.findMax()->val == 14);
        assert( t.findMin()->val == 4);

		t.traverse(printNode);
	}

	{
		BinarySearchTree<int> t;
		t.insert(8);t.insert(3);t.insert(10);t.insert(1);
		t.insert(6);t.insert(14);t.insert(4);t.insert(7);t.insert(13);

		t.traverse(printNode);
	}

	return 0;
}
Пример #2
0
    // Test program
int main( )
{
    BinarySearchTree<int> t;
    int NUMS = 400000;
    const int GAP  =   3711;
    int i;

    cout << "Checking... (no more output means success)" << endl;

    for( i = GAP; i != 0; i = ( i + GAP ) % NUMS )
        t.insert( i );

    for( i = 1; i < NUMS; i+= 2 )
        t.remove( i );

    if( NUMS < 40 )
        t.printTree( );
    if( t.findMin( ) != 2 || t.findMax( ) != NUMS - 2 )
        cout << "FindMin or FindMax error!" << endl;

    for( i = 2; i < NUMS; i+=2 )
        if( !t.contains( i ) )
            cout << "Find error1!" << endl;

    for( i = 1; i < NUMS; i+=2 )
    {
        if( t.contains( i ) )
            cout << "Find error2!" << endl;
    }

    BinarySearchTree<int> t2;
    t2 = t;

    for( i = 2; i < NUMS; i+=2 )
        if( !t2.contains( i ) )
            cout << "Find error1!" << endl;

    for( i = 1; i < NUMS; i+=2 )
    {
        if( t2.contains( i ) )
            cout << "Find error2!" << endl;
    }

    cout << "Finished testing" << endl;

    return 0;
}
Пример #3
0
int main() {
    int n, v;
    char cmd[32];

    while (scanf("%d", &n) != EOF) {
        BinarySearchTree bst;
        for (int i = 0; i < n; ++i) {
            scanf("%s", cmd);
            if (!strcmp(cmd, "find")) {
                scanf("%d", &v);
                TreeNode *ptr = bst.find(v);
                if (!ptr) {
                    puts("null");
                } else {
                    printf("%x : %d\n", (unsigned)ptr, ptr->v);
                }
            } else if (!strcmp(cmd, "findMin")) {
                TreeNode *mini = bst.findMin();
                if (!mini) {
                    puts("null");
                } else {
                    printf("%x : %d\n", (unsigned)mini, mini->v);
                }
            } else if (!strcmp(cmd, "findMax")) {
                TreeNode *maxi = bst.findMax();
                if (!maxi) {
                    puts("null");
                } else {
                    printf("%x : %d\n", (unsigned)maxi, maxi->v);
                }
            } else if (!strcmp(cmd, "insert")) {
                scanf("%d", &v);
                bst.insert(v);
            } else if (!strcmp(cmd, "remove")) {
                scanf("%d", &v);
                bst.remove(v);
            } else if (!strcmp(cmd, "show")) {
                bst.show();
            }
        }
    }
    return 0;
}
Пример #4
0
int main(int argc, char **argv)
{
    BinarySearchTree bst;
    bst.insert(12);
    bst.insert(34);
    bst.insert(9);
    bst.insert(54);
    bst.insert(134);
    bst.insert(4);
    bst.insert(19);
    bst.insert(5);


    bst.printTree();

    cout << "-------------------------" << endl;
    cout<< bst.findMin(bst.thisone())->element << endl;
    cout<< bst.findMax(bst.thisone())->element << endl;

	return 0;
}
Пример #5
0
 //Test program 3: BiIterator
int main( )
{
    BinarySearchTree<string> T;
    ifstream file("words.txt");

    if (!file)
    {
        cout << "couldn't open file words.txt" << endl;
        return 1;
    }

    vector<string> V1 = { istream_iterator<string>{file}, istream_iterator<string>{} };
    file.close();

    for(auto j: V1)
        T.insert( j );

    /**************************************/
    cout << "\nPHASE 1: contains\n\n";
    /**************************************/

    vector<string> V2 = { "airborne", "stop", "yelp", "Sweden", "obligations", "unbridled" };

    for(auto w: V2)
    {
        if( T.contains( w ) != T.end() )
            cout << "\""<< w << "\"" << " in the tree" << endl;
        else
            cout << "\""<< w << "\"" << " not in the tree" << endl;
    }

    /**************************************/
    cout << "\nPHASE 2: BiIterator, operator++\n\n";
    /**************************************/

    for(BinarySearchTree<string>::BiIterator it = T.begin(); it != T.end(); ++it)
    {
        cout << *it << endl;
    }


    cout << endl;

    /**************************************/
    cout << "PHASE 3: BiIterator, operator--\n\n";
    /**************************************/

    string largest = T.findMax( );

    for(auto it = T.contains( largest ); it != T.end(); --it)
    {
        cout << *it << endl;
    }

    cout << "\nFinished testing" << endl;


    /**************************************/
    cout << "PHASE 4: Frequency Table" << endl;
    /**************************************/

    BinarySearchTree<Word> T2;

    BinarySearchTree<Word>::BiIterator word_it;
    for(auto j: V1)
    {
        Word temp_word(j,-1);
        word_it = T2.contains(temp_word);
        if(word_it != BinarySearchTree<Word>::BiIterator())
            word_it->counter++;
        else
            T2.insert( Word(j,1) );

    }

    Word smallest = T2.findMin( );
    for(auto it = T2.contains( smallest ); it != T2.end(); ++it)
    {
        cout << setw(14) << it->key <<  "\t";
        cout << it->counter << endl;
    }


    return 0;
}