int main() {
  std::vector<int> inpSequence = { 10, 2, 3, 65, 3, 34, 5, 76, 3, 3, 2, 4, 6, 78, 8, 34, 3, 67 };
  BinarySearchTree bst;
  for (int i : inpSequence) {
    bst.insert(i);
    bst.printPreOrder();
  }
  for (int i : inpSequence) {
    cout << bst.contains(i) << "\n";
  }
  cout << bst.contains(1000) << "\n";
  return 0;
}
Beispiel #2
0
int main(int argc, char** argv) {
    BinarySearchTree<int, int> intTree;
    
    intTree.add(40);
    intTree.add(5);
    intTree.add(10);
    intTree.add(30);
    intTree.add(20);
    intTree.add(35);
    intTree.add(25);
    intTree.add(15);

    cout << intTree.getHeight() << endl;
    cout << intTree.getNumberOfNodes() << endl;

    int item;

    cout << "Inorder\n";
    intTree.inorderTraverse(print);

    cout << "Get entry for 5 " << intTree.getEntry(5) << endl;
    cout << "Get entry for 25 " << intTree.getEntry(25) << endl;
    try {
        cout << "Get entry for 65 " << intTree.getEntry(65) << endl;
    } catch(NotFoundException e) {
        cout << "Error " << e.what() << endl;
    }

    cout << "Does it contain 5 " << intTree.contains(5) << endl;
    cout << "Does it contain 25 " << intTree.contains(25) << endl;
    cout << "Does it contain 65 " << intTree.contains(65) << endl;

    try {
        intTree.remove(45);
    } catch(NotFoundException e) {
        cout << "Error " << e.what() << endl;
    }
    intTree.inorderTraverse(print);
    cout << endl;
    intTree.remove(30);
    intTree.inorderTraverse(print);
    cout << endl;
    intTree.remove(5);
    intTree.inorderTraverse(print);
    cout << endl;
    intTree.remove(15);
    intTree.inorderTraverse(print);
    cout << endl;
    cout << intTree.getHeight() << endl;
    cout << intTree.getNumberOfNodes() << endl;
}
    // 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;
}
Beispiel #4
0
int main() {
	int values[14] = {4,2,11,15,9,1,-6,5,3,15,2,5,13,14};
	BinarySearchTree bst;
	for (int i=0;i<14;i++) {
		cout << "Inserting " << values[i] << " into the tree.\n";
		bst.insert(values[i]);
	}
	cout << "Original tree " <<
			"(asterisks denote a count of more than 1):\n";
	print_tree_details(bst);

	// make a copy with copy constructor
	BinarySearchTree bst_copy_constructor = bst;
	//bst_copy_constructor.print_tree();

	// make a copy with assignment overload
	BinarySearchTree bst_copy_1;
	bst_copy_1 = bst;

	cout << "Removing 9 from original tree:\n";
	bst.remove(9); // remove a node with no children
	print_tree_details(bst);

	bst = bst_copy_1;

	cout << "Removing 1 from original tree:\n";
	bst.remove(1); // remove a node with one child
	print_tree_details(bst);

	bst = bst_copy_1;

	cout << "Removing 11 from original tree:\n";
	bst.remove(11); // remove a node with one child
	print_tree_details(bst);

	bst = bst_copy_1;

	cout << "Removing 5 from original tree " <<
			"(should still have one 5):\n";
	bst.remove(5); // remove a node with one child
	print_tree_details(bst);

	// check if the tree contains values
	bst = bst_copy_1;
	for (int i=-10;i<20;i++) {
		cout << "Tree " << (bst.contains(i) ?
				"contains " :
				"does not contain ")
				<< "the value " << i << "\n";
	}

	cout << "\nFinished!\n";
	return 0;
}
Beispiel #5
0
void test_contains() {
	BinarySearchTree * tree = new BinarySearchTree;
	test_insert(tree);
	cout << "Contains -2? (false): " << tree->contains(-2) << "\n";
	cout << "Contains 7? (true): " << tree->contains(7) << "\n";
}
Beispiel #6
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;
}