Ejemplo n.º 1
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;
}
int main(){

	cout << "HOMEWORK EXAMPLE:" << endl;
	BinarySearchTree<int, int> bst;
	bst.insert(make_pair(10,0));
	bst.insert(make_pair(4,0));
	bst.insert(make_pair(20,0));
	bst.insert(make_pair(2,0));
	bst.insert(make_pair(8,0));
	bst.insert(make_pair(14,0));
	bst.insert(make_pair(24,0));
	bst.insert(make_pair(6,0));
	bst.insert(make_pair(12,0));
	bst.insert(make_pair(18,0));
	bst.insert(make_pair(22,0));
	bst.insert(make_pair(16,0));

	bst.print();

	bst.insert(make_pair(15,0));
	bst.print();

	bst.insert(make_pair(17,0));
	bst.print();

	bst.insert(make_pair(11,0));
	bst.print();

	bst.remove(20);
	bst.print();

	bst.remove(16);
	bst.print();

	bst.remove(4);
	bst.print();
	
	cout << "TESTING ITERATOR:" << endl;
	for(BinarySearchTree<int, int>::iterator it = bst.begin(); it != bst.end(); ++it)
	{
		cout << it->first << ", ";
	}
	cout << endl << endl;

	cout << "TESTING INCREASING INSERT:" << endl;
	BinarySearchTree<int, int> bst2;
	for(int i=0; i<5; i++)
	{
		bst2.insert(make_pair(i, 0));
	}
	bst2.print();

	cout << "TESTING DECREASING INSERT:" << endl;
	for(int i=9; i>=5; i--)
	{
		bst2.insert(make_pair(i, 0));
	}
	bst2.print();



	cout << "TESTING INCREASING DELETION:" << endl;
	for(int i=0; i<10; i=i+2)
	{
		bst2.remove(i);
	}
	bst2.print();

	cout << "TESTING WEIRD DELETIONS:" << endl;
	bst2.remove(7);
	bst2.remove(3);
	bst2.remove(5);

	bst2.print();

	return 0;
}