Ejemplo n.º 1
0
int main(){
    BST bst;
    bst.insert(1);
    bst.insert(3);
    bst.insert(2);
    for (BST::iterator i = bst.begin(); i != bst.end(); ++i){
        cout << *i << endl;
    }
}
Ejemplo n.º 2
0
int main(){
    BST<int> bst;
    bst.add(2);
    bst.add(1);
    bst.add(3);
    bst.add(13);
    bst.add(31);
    for (BST<int>::iterator i = bst.begin(); i != bst.end(); ++i){
        cout << *i << endl;
    }
}
void BTSort(arrayItems D[])
{
   BST<arrayItems> sortBin;
   // insert data to BST
   for (int i = 0; i < NUMITEMS; i++)
      sortBin.insert(D[i]);
   
   int count = 0;
   BSTiterator<arrayItems> it;
   // copy back to the array
   for (BSTiterator<arrayItems> it = sortBin.begin(); it != sortBin.end(); it++)
   {
      D[count] = * it;
      count++;
   }
}
Ejemplo n.º 4
0
/**
 * A simple test driver for the BST class template.
 * P1 CSE 100 2012
 * Author: P. Kube (c) 2012
 */
int main() {

  /* Create an STL vector of some ints */
  vector<int> v;
  v.push_back(3);
  v.push_back(4);
  v.push_back(1);
  v.push_back(100);
  v.push_back(-33);
  v.push_back(2);
  v.push_back(10);
  v.push_back(-3);

  /* Create an instance of BST holding int */
  BST<int> b;

  /* Insert a few data items. */
  vector<int>::iterator vit = v.begin();
  vector<int>::iterator ven = v.end();
  for(; vit != ven; ++vit) {
    // all these inserts are unique, so should return true
    if(! b.insert(*vit) ) {
      cout << "Incorrect return value when inserting " << *vit << endl;
      return -1;
    }
  }

  vit = v.begin();
  for(; vit != ven; ++vit) {
    // all these inserts are duplicates, so should return false
    if( b.insert(*vit) ) {
      cout << "Incorrect return value when re-inserting " << *vit << endl;
      return -1;
    }
  }

  /* Test size. */
  cout << "Size is: " << b.size() << endl;
  if(b.size() != v.size()) {
    cout << "... which is incorrect." << endl;
    return -1;
  }

  /* Test find return value. */
  vit = v.begin();
  for(; vit != ven; ++vit) {
    if(*(b.find(*vit)) != *vit) {
      cout << "Incorrect return value when finding " << *vit << endl;
      return -1;
    }
  }


  /* Sort the vector, to compare with inorder iteration on the BST */
  sort(v.begin(),v.end());


  /* Test BST iterator; should iterate inorder */
  cout << "traversal using iterator:" << endl;
  vit = v.begin();
  BST<int>::iterator en = b.end();
  BST<int>::iterator it = b.begin();
  for(; vit != ven; ++vit) {
    cout << *it << endl;
    if(*it != *vit) {
      cout << *it << "," << *vit << ": Incorrect inorder iteration of BST." << endl;
      return -1;
    }
    ++it;
  }
  cout << "OK." << endl;
}
Ejemplo n.º 5
0
/**
 * A simple test driver for the BST class template.
 * P1 CSE 100 2010
 * Author: P. Kube (c) 2010
 */
int main() {

  /* Create an STL vector of some ints */
  vector<int> v;
  map<int, bool> m;
  
  for (int i = 0; i < 50; ++i) {
	  int n = rand() % 1000;
	  if (!m[n]) {
		  m[n] = true;
		  v.push_back(n);
	  }
  }
  v.erase(unique(v.begin(), v.end()), v.end());
  
  std::cout << "v is size: " << v.size() << std::endl;
  for (size_t i = 0; i < v.size(); ++i) {
	  //std::cout << v[i] << std::endl;
  }

  /* Create an instance of BST holding int */
  BST<int> b;

  /* Insert a few data items. */
  vector<int>::iterator vit = v.begin();
  vector<int>::iterator ven = v.end();
  for(; vit != ven; ++vit) {
    // all these inserts are unique, so should return true
    if(! b.insert(*vit) ) {
      cout << "Incorrect return value when inserting " << *vit << endl;
      return -1;
    }
  }

  for(; vit != ven; ++vit) {
    // all these inserts are duplicates, so should return false
    if( b.insert(*vit) ) {
      cout << "Incorrect return value when re-inserting " << *vit << endl;
      return -1;
    }
  }

  /* Test size. */
  cout << "Size is: " << b.size() << endl;
  if(b.size() != v.size()) {
    cout << "... which is incorrect." << endl;
    return -1;
  }

  /* Test find return value. */
  vit = v.begin();
  for(; vit != ven; ++vit) {
    if(*(b.find(*vit)) != *vit) {
      cout << "Incorrect return value when finding " << *vit << endl;
      return -1;
    }
  }

  

  /* Sort the vector, to compare with inorder iteration on the BST */
  sort(v.begin(),v.end());


  /* Test BST iterator; should iterate inorder */
  cout << "traversal using iterator:" << endl;
  vit = v.begin();
  BST<int>::iterator en = b.end();
  BST<int>::iterator it = b.begin();
  for(; it != en; ++it) {
    cout << *it << endl;
    if(*it != *vit) {
      cout << *it << "," << *vit << ": Incorrect inorder iteration of BST." << endl;
      return -1;
    }
    ++vit;
  }

}
Ejemplo n.º 6
0
/**
 * A simple test driver for the BST class template.
 * P1 CSE 100 2013
 * Author: P. Kube (c) 2013
 */
int main() {

  /* Create an STL vector of some ints */
  
  vector<int> v;
  v.push_back(3);
  v.push_back(4);
  v.push_back(1);
  v.push_back(33);
  v.push_back(66);
  v.push_back(144);
  v.push_back(1984);
  v.push_back(-92393);
  v.push_back(46);
  v.push_back(22);
  v.push_back(0);
  v.push_back(123445);
  v.push_back(100);
  v.push_back(-33);
/*
  vector<string> v;
  v.push_back("hi");
  v.push_back("hi no");
  v.push_back("hi yes");
  v.push_back("hi you");
  v.push_back("hi fly");
  v.push_back("hi w");
*/

  /* Create an instance of BST holding int */
  BST<int> b;
 //   BST<string> b;

  /* Insert a few data items. */
  vector<int>::iterator vit = v.begin();
  vector<int>::iterator ven = v.end();
  for(; vit != ven; ++vit) {
    // all these inserts are unique, so should return a std::pair
    // with second part true
    std::pair<BST<int>::iterator,bool> pr = b.insert(*vit);
 //   if(! pr.second ) {
  //    cout << "Incorrect bool return value when inserting " << *vit << endl;
  //    return -1;
   // }
    if(*(pr.first) != *vit) {
      cout << "Incorrect iterator return value when inserting " << *vit << endl;
      return -1;
    }  
  }

  /* Test size. */
  cout << "" << endl;
  cout << "Size is: " << b.size() << endl;
  cout << "" << endl;
  if(b.size() != 14) {
    cout << "... which is incorrect." << endl;
    return -1;
  }

  /* Test find return value. */
  vit = v.begin();
  for(; vit != ven; ++vit) {
    if(*(b.find(*vit)) != *vit) {
      cout << "Incorrect return value when finding " << *vit << endl;
      return -1;
    }
  }

  /* This is for testing find on something not in the BST
   * Causes a seg fault so it is taken out
  BST<int>::iterator it3 = b.find(5);; 
  cout << *it3 << endl;
  */
  
  /* Sort the vector, to compare with inorder iteration on the BST */
  sort(v.begin(),v.end());

  /* Test BST iterator; should iterate inorder */
  cout << "traversal using iterator:" << endl;
  cout << "" << endl;
  vit = v.begin();
  BST<int>::iterator en = b.end();
  BST<int>::iterator it = b.begin();
  for(; vit != ven; ++vit) {
    cout << *it << endl;
    if(*it != *vit) {
      cout << *it << "," << *vit << ": Incorrect inorder iteration of BST." << endl;
      return -1;
    }
    ++it;
  }
  cout << "" << endl;
  cout << "OK, order is correct" << endl;
  cout << "" << endl;

   /* Testing == and !=*/
  cout << "Testing equals and not equals:" << endl;
  cout << "" << endl;
  BST<int>::iterator test1 = b.begin();
  BST<int>::iterator test2 = b.begin();
  if(*test1 != *test2) {
      cout << *test1 << " and " << *test2 << ": Should be equal." << endl;
      return -1;
    }
  cout << "Correct! " << *test1 << " and " << *test2 << ": Are equal." << endl;
  cout << "" << endl;
  ++test1;
  if(*test1 == *test2) {
      cout << *test1 << " and " << *test2 << ": Should NOT be equal." << endl;
      return -1;
    }
  cout << "Correct! " <<*test1 << " and " << *test2 << ": Are NOT equal." << endl;
  cout << "" << endl;

  cout << "== and != tests passed!" << endl;
  cout << "" << endl;

  cout << "Testing find...Printing all with find" << endl;
  cout << "" << endl;


  BST<int>::iterator test100 = b.find(3);
  cout << *test100 << endl;

  test100 = b.find(4);
  cout << *test100 << endl;
  test100 = b.find(1);
  cout << *test100 << endl;
  test100 = b.find(33);
  cout << *test100 << endl;
  test100 = b.find(66);
  cout << *test100 << endl;
  test100 = b.find(144);
  cout << *test100 << endl;
  test100 = b.find(1984);
  cout << *test100 << endl;
  test100 = b.find(-92393);
  cout << *test100 << endl;
  test100 = b.find(46);
  cout << *test100 << endl;
  test100 = b.find(22);
  cout << *test100 << endl;
  test100 = b.find(0);
  cout << *test100 << endl;
  test100 = b.find(123445);
  cout << *test100 << endl;
  test100 = b.find(100);
  cout << *test100 << endl;
  test100 = b.find(-33);
  cout << *test100 << endl;
  test100 = b.find(666);
  BST<int>::iterator test101 = b.end();
	BST<int>::iterator test102 = b.begin();
  if(test100 == test101) {
    cout << "666 returns null" << endl;
    cout << "" << endl;
  } 
  if(test100 != test102) {
    cout << "Passes another check for !=" << endl;
    cout << "" << endl;
  } 

  cout << "Testing Clear..." << endl;
  cout << "" << endl;
  b.clear();

  cout << "Clear Ran, Now Testing Size." << endl;
  cout << "" << endl;

  /* Test size. */
  cout << "Size is: " << b.size() << endl;
  cout << "" << endl;
  if(b.size() != 0) {
    cout << "BST WAS NOT CLEARED!!!!" << endl;
    return -1;
  } 
  
  cout << "Correct! YAY! Super Great!" << endl;
  cout << "" << endl;
  
  

  /* Test BST iterator; should iterate inorder */
  cout << "This should cause a seg fault if BST is empty:" << endl;
  cout << "" << endl;
  BST<int>::iterator it2 = b.begin(); 
  cout << *it2 << endl;
}
Ejemplo n.º 7
0
/**
 * A simple test driver for the BST class template.
 * P1 CSE 100 2013
 * Author: P. Kube (c) 2013
 */
int main() {

  /* Create an STL vector of some ints */
  vector<int> v;
  v.push_back(3);
  v.push_back(4);
  v.push_back(1);
  v.push_back(100);
  v.push_back(-33);

  /* Create an instance of BST holding int */
  BST<int> b;

  /* Insert a few data items. */
  vector<int>::iterator vit = v.begin();
  vector<int>::iterator ven = v.end();
  for(; vit != ven; ++vit) {
    // all these inserts are unique, so should return a std::pair
    // with second part true
    std::pair<BST<int>::iterator,bool> pr = b.insert(*vit);
    cout << "vit = " << *vit << endl;
    if(! pr.second ) {
      cout << "Incorrect bool return value when inserting " << *vit << endl;
      return -1;
    }
    if(*(pr.first) != *vit) {
      cout << "Incorrect iterator return value when inserting " << *vit << endl;
      return -1;
    }  
  }

  /* Test size. */
  cout << "Size is: " << b.size() << endl;
  if(b.size() != v.size()) {
    cout<< "bsize is" << b.size() << endl;
    cout<< "vsize is" << v.size() << endl;
    cout << "... which is incorrect." << endl;
    return -1;
  }
    cout<<"simple test" << endl;
    for(BST<int>::iterator it = b.begin(); it != b.end(); ++it){
	cout<<*it<< endl;
	}



  /* Test find return value. */
  vit = v.begin();
  for(; vit != ven; ++vit) {
    if(*(b.find(*vit)) != *vit) {
      cout << "Incorrect return value when finding " << *vit << endl;
      return -1;
    }
  }
  
  /* Sort the vector, to compare with inorder iteration on the BST */
  sort(v.begin(),v.end());

  /* Test BST iterator; should iterate inorder */
  cout << "traversal using iterator:" << endl;
  vit = v.begin();
  BST<int>::iterator en = b.end();
  BST<int>::iterator it = b.begin();
  for(; vit != ven; ++vit) {
    if(! (it != en) ) {
      cout << *it << "," << *vit << ": Early termination of BST iteration." << endl;
      return -1;
    }
    cout << *it << endl;
    if(*it != *vit) {
      cout << *it << "," << *vit << ": Incorrect inorder iteration of BST." << endl;
      return -1;
    }
    ++it;
  }
  cout << "OK." << endl;

/* Testing clear and checking clear size afterwards*/
 BST<int> c;
 vit = v.begin();
 ven = v.end();
  for(vit = v.begin(); vit != ven; ++vit) {
    // all these inserts are unique, so should return a std::pair
    // with second part true
    std::pair<BST<int>::iterator,bool> pr = c.insert(*vit);
    }
  c.clear();
  cout<< "clear size is" << c.size() << endl;
  if(c.size() != 0){
	cout << "fail" << endl;
	return -1;  
  }
/* Testing BST destructor */
BST<int>* d = new BST<int>();
d->insert(3);
delete d;

/*Test Clear then add and size after */
 BST<int> e;
 vit = v.begin();
 ven = v.end();
  for(vit = v.begin(); vit != ven; ++vit) {
    // all these inserts are unique, so should return a std::pair
    // with second part true
    std::pair<BST<int>::iterator,bool> pr = e.insert(*vit);
    }
  e.clear();
  e.insert(5);
  cout<< "e size is " << e.size() << endl;
/* Test insert of same item */
 BST<int> f;
 vit = v.begin();
 ven = v.end();
  for(; vit != ven; ++vit) {
    // all these inserts are unique, so should return a std::pair
    // with second part true
    std::pair<BST<int>::iterator,bool> pk = f.insert(*vit);
    }
  std::pair<BST<int>::iterator,bool> pi = f.insert(3);
  if(pi.second){
	cout << "you f****d up" << endl;
        cout << "value is " << *(pi.first) << endl;
	return -1;
  }
/* Clear an empty tree */
BST<int> q;
q.clear();
}
Ejemplo n.º 8
0
/**
 * A simple test driver for the BST class template.
 * P1 CSE 100 2013
 * Author: P. Kube (c) 2013
 */
int main() {

  /* Create an STL vector of some ints */
  vector<int> v;
  vector<int> random;
  
    v.push_back(50);
    v.push_back(60);
    v.push_back(30);
    v.push_back(33);
    v.push_back(38);
    v.push_back(35);
    v.push_back(37);
    v.push_back(20);
   // v.push_back(15);
    
  // Overriding for more extensive tests */

 /* 
  srand(time(NULL));
  
  for (int i = 0; i < 10000; i++ )
  {
      int r = rand() % 10000;
      bool dupe = false;

      vector<int>::iterator ranit = random.begin();
      vector<int>::iterator ranen = random.end();
      for(; ranit != ranen; ++ranit) 
      {
        if (*ranit == r) 
        {
          //cout << "dupe" << endl;
          dupe = true;
          // return -1;
        }
      }
      if (!dupe)
      {
          cout << r << endl;
            random.push_back(r);
            v.push_back(r);
      }
  } 
  */

  /* Create an instance of BST holding int */
  BST<int> b;

  /* Insert a few data items. */
  vector<int>::iterator vit = v.begin();
  vector<int>::iterator ven = v.end();
  for(; vit != ven; ++vit) {
    // all these inserts are unique, so should return a std::pair
    // with second part true
    std::pair<BST<int>::iterator,bool> pr = b.insert(*vit);
    if(! pr.second ) {
      cout << "Incorrect bool return value when inserting " << *vit << endl;
      return -1;
    }
    if(*(pr.first) != *vit) {
      cout << "Incorrect iterator return value when inserting " << *vit << endl;
      return -1;
    }  
  }

  /* Test inserting repeated values - expected false result */
  
  /*
  vector<int> z;
  z.push_back(1);
  z.push_back(3);

  vector<int>::iterator bit = z.begin();
  vector<int>::iterator ben = z.end();
  for(; bit !=ben; ++bit) {
      std::pair<BST<int>::iterator,bool> pr = b.insert(*bit);
      if( pr.second ) {
        cout << "Incorrect bool return value when inserting " << *bit << endl;
        return -1;
      }
      if(*(pr.first) != *bit) {
      cout << "Incorrect iterator return value when inserting " << *bit << endl;
      return -1;
    }
  }
  // Make sure size hasn't changed for failed inputs.
  cout << "Size is: " << b.size() << endl;
  if(b.size() == z.size()) {
    cout << "... which is incorrect." << endl;
    return -1;
  }
 */

  /* Test size. */
  cout << "Size is: " << b.size() << endl;
  if(b.size() != v.size()) {
    cout << "... which is incorrect." << endl;
    return -1;
  }

  /* Test find return value. */
  vit = v.begin();
  for(; vit != ven; ++vit) {
    if(*(b.find(*vit)) != *vit) {
      cout << "Incorrect return value when finding " << *vit << endl;
      return -1;
    }
  }
  
  /* Sort the vector, to compare with inorder iteration on the BST */
  sort(v.begin(),v.end());

  /* Test BST iterator; should iterate inorder */
  cout << "traversal using iterator:" << endl;
  vit = v.begin();
  BST<int>::iterator en = b.end();
  BST<int>::iterator it = b.begin();
  for(; vit != ven; ++vit) {
    if(! (it != en) ) {
      cout << *it << "," << *vit << ": Early termination of BST iteration." << endl;
      return -1;
    }
    cout << *it << endl;
    if(*it != *vit) {
      cout << *it << "," << *vit << ": Incorrect inorder iteration of BST." << endl;
      return -1;
    }
    ++it;
  }
  cout << "OK." << endl;

}
Ejemplo n.º 9
0
/**
 * A simple test driver for the BST class template.
 * P1 CSE 100 2013
 * Author: P. Kube (c) 2013
 */
int main() {

  /* Create an STL vector of some ints */
  vector<int> v;
  /*
  v.push_back(3);
  v.push_back(4);
  v.push_back(1);
  v.push_back(100);
  v.push_back(-33);
  v.push_back(-20);
  v.push_back(6);
  v.push_back(0);
  v.push_back(-1);
  v.push_back(-2);
  v.push_back(-3);
  v.push_back(-4);
  */

  /* Create an instance of BST holding int */
  BST<int> b;

  /* Insert a few data items. */
  vector<int>::iterator vit = v.begin();
  vector<int>::iterator ven = v.end();
  for(; vit != ven; ++vit) {
    // all these inserts are unique, so should return a std::pair
    // with second part true
    std::pair<BST<int>::iterator,bool> pr = b.insert(*vit);
    if(! pr.second ) {
      cout << "Incorrect bool return value when inserting " << *vit << endl;
      return -1;
    }
    if(*(pr.first) != *vit) {
      cout << "Incorrect iterator return value when inserting " << *vit << endl;
      return -1;
    }  
  }

  /* Test size. */
  cout << "Size is: " << b.size() << endl;
  if(b.size() != v.size()) {
    cout << "... which is incorrect." << endl;
    return -1;
  }

  /* Test find return value. */
  vit = v.begin();
  for(; vit != ven; ++vit) { 
    if(*(b.find(*vit)) != *vit) {
      cout << "Incorrect return value when finding " << *vit << endl;
      return -1;
    }
  }
  
  /* Sort the vector, to compare with inorder iteration on the BST */
  sort(v.begin(),v.end());

  /* Test BST iterator; should iterate inorder */
  cout << "traversal using iterator:" << endl;
  vit = v.begin();
  BST<int>::iterator en = b.end();
  BST<int>::iterator it = b.begin();
  for(; vit < ven; ++vit) {
    if(! (it != en) ) {
      cout << *it << "," << *vit << ": Early termination of BST iteration." << endl;
      return -1;
    }
    cout << *it << endl;
    if(*it != *vit) {
      cout << *it << "," << *vit << ": Incorrect inorder iteration of BST." << endl;
      return -1;
    }
    ++it;
  }
  /** Added a basic clear tests that checks for correct size after delete */
  cout << "OK." << endl;
  b.clear();
  cout << ((b.size() == 0) 
          ? "PASS: clear() size of tree: "
          : "FAIL: clear() Size of tree: ") << b.size()  << endl;
  

  /** ================================ Added test Cases =====================*/ 

  /* Test fixtures */
  vector<int> v2;
  srand(unsigned(time(NULL)));
  BST<int> b2;
  BST<int>* treeDestructorTest = new BST<int>();
  unsigned int numInspected = 0;
  unsigned int sizeOfInput = 0;

  for(int i = 1000; i >= -1000; i--){
    v2.push_back(i);
    sizeOfInput++;
  }

  /* randomize the ordering of -1000 - 1000 before inserting 
   * so we don't just test a linked list
   */
  std::random_shuffle(v2.begin(), v2.end());

  vector<int>::iterator vit2 = v2.begin();
  vector<int>::iterator ven2 = v2.end();

  /* insert all the items in the randomized vector into the bst 
   * and check the boolean return
   */
  while( vit2 != ven2 ){
    if( !(b2.insert(*vit2)).second ){
      cout << "incorrect boolean return value of insert for: "<< *vit2 <<endl;
      return -1;
    }
    ++vit2;    
  }

  cout << "PASS: insert() unique value, boolean return. " << endl;

  /* try to insert the values again. checking for correct return 
   * boolean on duplicate insertion.
   */
  for( vit2 = v2.begin(); vit2 != ven2; ++vit2 ){
    if( (b2.insert(*vit2)).second ){
      cout << "incorrect boolean return value of duplicate " 
           << "insert for: "<< *vit2 << endl;
      return -1;
    }
  } 

  cout << "PASS: insert() duplicate value, boolean return. " << endl;

  /** sort the vector for inorder traversal */
  sort(v2.begin(), v2.end());

  vector<int>::iterator vit3 = v2.begin();
  vector<int>::iterator ven3 = v2.end();
  
  BST<int>::iterator front = b2.begin();
  BST<int>::iterator back = b2.end();

  /* check that each returned element is the correct successor */
  for( ; vit3 != ven3; ++front, ++vit3 ){
    if( *front !=  *vit3 ){
      cout << "FAIL: inorder traversal failed on : " << *front 
           << " " << *vit3 << endl;
      return -1;
    }
    ++numInspected;
  }

  cout << "PASS: iterator correct element ordering in traversal. " << endl;

  /* check we traversed all the elements in the tree */
  if( numInspected != sizeOfInput ){
    cout << "FAIL: incomplete tree traversal. Read " << numInspected
         << " of " << sizeOfInput << " elements." << endl;
    return -1;
  }else{
    cout << "PASS: iterator number of elements read Expected: " 
         << sizeOfInput << " Result: " 
         << numInspected << endl;
  }

  /* randomize the vector again */
  std::random_shuffle(v2.begin(), v2.end());

  /* test find on all the items in the vector & bst */
  for(vit3 = v2.begin(); vit3 != ven3; ++vit3){
    if( *(b2.find(*vit3)) != *vit3 ){
      cout << "FAIL: find return value did not match: " << *vit3 << endl;  
      return -1;
    }
  }

  cout << "PASS: find() returned all correct values." << endl;
  cout << ( (b2.size() == sizeOfInput) 
            ? "PASS: size() returned expected tree size. " 
            : "FAIL: size() returned non-matching tree size. " ) << endl;
  cout << ( (b2.empty() == false)
            ? "PASS: empty() returned false when not empty. "
            : "FAIL: empty() returned true when not empty. " ) << endl;
  b2.clear();
  cout << ( (b2.empty() == true)
            ? "PASS: empty() returned true when empty. "
            : "FAIL: empty() returned false when empty. " ) << endl;
  
  /** Test for memory leaks when allocating a tree on the heap 
   *  using valgrind will tell you if this passes or not
   */
  treeDestructorTest->insert(1);
  treeDestructorTest->insert(2);
  treeDestructorTest->insert(0);

  treeDestructorTest->clear();

  treeDestructorTest->insert(3);
  treeDestructorTest->insert(5);
  treeDestructorTest->insert(0);
  delete treeDestructorTest;

  cout << "PASS: No errors when calling destructor after clear(). i" << endl;
  cout << "      Run valgrind for memory leak analysis. " << endl;


/*****************************START Testing for RST***********************/
    RST<int> r;

  /* Insert a few data items. */
  vit = v.begin();
  //vector<int>::iterator ven = v.end();
  for(; vit != ven; ++vit) {
    // all these inserts are unique, so should return a std::pair
    // with second part true
    std::pair<RST<int>::iterator,bool> pr = r.insert(*vit);
    if(! pr.second ) {
      cout << "Incorrect bool return value when inserting into RST " << *vit << endl;
      return -1;
    }
    if(*(pr.first) != *vit) {
      cout << "Incorrect iterator return value when inserting into RST " << *vit << endl;
      return -1;
    }  
  }

  //PASS insert rst test
  cout << "PASS Insert into RST" << endl;

   /* Test size. */
  cout << "Size is: " << r.size() << endl;
  if(r.size() != v.size()) {
    cout << "... which is incorrect." << endl;
    return -1;
  }

  /* Test find return value. */
  vit = v.begin();
  for(; vit != ven; ++vit) { 
    if(*(r.find(*vit)) != *vit) {
      cout << "Incorrect return value when finding " << *vit << endl;
      return -1;
    }
  }
  
  /* Sort the vector, to compare with inorder iteration on the BST */
  sort(v.begin(),v.end());

  /* Test BST iterator; should iterate inorder */
  cout << "traversal using iterator:" << endl;
  vit = v.begin();
  //BST<int>::iterator en = r.end();
  //BST<int>::iterator it = r.begin();
  for(; vit < ven; ++vit) {
    if(! (it != en) ) {
      cout << *it << "," << *vit << ": Early termination of BST iteration." << endl;
      return -1;
    }
    cout << *it << endl;
    if(*it != *vit) {
      cout << *it << "," << *vit << ": Incorrect inorder iteration of BST." << endl;
      return -1;
    }
    ++it;
  }
  /** Added a basic clear tests that checks for correct size after delete */
  cout << "OK." << endl;
  r.clear();
  cout << ((r.size() == 0) 
          ? "PASS: clear() size of tree: "
          : "FAIL: clear() Size of tree: ") << r.size()  << endl;
  




//END OF FILE
}
/**
 * A simple test driver for the BST class template.
 * P1 CSE 100 2013
 * Author: P. Kube (c) 2013
 */
int main() {

  /* Create an STL vector of some ints */
  vector<int> v;
  v.push_back(3);
  v.push_back(4);
  v.push_back(1);
  v.push_back(100);
  v.push_back(-33);

  /* Create an instance of BST holding int */
  BST<int> b;

  /* Insert a few data items. */
  vector<int>::iterator vit = v.begin();
  vector<int>::iterator ven = v.end();
  for(; vit != ven; ++vit) {
    // all these inserts are unique, so should return a std::pair
    // with second part true
    std::pair<BST<int>::iterator,bool> pr = b.insert(*vit);
    if(! pr.second ) {
      cout << "Incorrect bool return value when inserting " << *vit << endl;
      return -1;
    }
    if(*(pr.first) != *vit) {
      cout << "Incorrect iterator return value when inserting " << *vit << endl;
      return -1;
    }  
  }

  /* Test size. */
  cout << "Size is: " << b.size() << endl;
  if(b.size() != v.size()) {
    cout << "... which is incorrect." << endl;
    return -1;
  }

  /* Test find return value. */
  vit = v.begin();
  for(; vit != ven; ++vit) {
    if(*(b.find(*vit)) != *vit) {
      cout << "Incorrect return value when finding " << *vit << endl;
      return -1;
    }
  }
  
  /* Sort the vector, to compare with inorder iteration on the BST */
  sort(v.begin(),v.end());

  /* Test BST iterator; should iterate inorder */
  cout << "traversal using iterator:" << endl;
  vit = v.begin();
  BST<int>::iterator en = b.end();
  BST<int>::iterator it = b.begin();
  for(; vit != ven; ++vit) {
    if(! (it != en) ) {
      cout << *it << "," << *vit << ": Early termination of BST iteration." << endl;
      return -1;
    }
    cout << *it << endl;
    if(*it != *vit) {
      cout << *it << "," << *vit << ": Incorrect inorder iteration of BST." << endl;
      return -1;
    }
    ++it;
  }
  cout << "OK." << endl;

}
Ejemplo n.º 11
0
/**
 * A simple partial test driver for the RST class template.
 * P2 CSE 100
 * Author: P. Kube (c) 2010, 2013
 */
int main(int argc, char** argv) {

  int N = 1000;
  if(argc > 1) N = atoi(argv[1]);
  // a good RST implementation should need no more than this number
  // of key comparisons when inserting N keys
  double  maxcompsperkey =  (log(N) * 2.5); 
  cout << "Log(n) is: " << log(N) << endl;
  /* Create an STL vector of some countints, in sorted order */
  vector<countint> v;
  for(int i=0; i<N; i++) {
    //    v.push_back(i);
    v.push_back(i);
  }

  /* Create an empty instance of RST holding countint */
  BST<countint>* r = new RST<countint>();

  /* Clear the comparison counter */
  countint::clearcount();

  /* Insert the data items, in order */
  cout << "Inserting " << N << " sorted keys in initially empty RST...";
  vector<countint>::iterator vit = v.begin();
  vector<countint>::iterator ven = v.end();
  for(; vit != ven; ++vit) {
     // all these inserts are unique, so should return a std::pair
    // with second part true
    std::pair<BST<countint>::iterator,bool> pr = r->insert(*vit);
    if(! pr.second ) {
      cout << "Incorrect bool return value when inserting " << *vit << endl;
      return -1;
    }
    // countint rc = *(pr.first);
    // countint bc = *vit;
    // if(rc < bc || bc < rc) {
    //   cout << "Incorrect iterator return value when inserting " << *vit << endl;
    //   return -1;
    // }  
  }
  
  cout << " done." << endl;
  
  //  r->inorder();

  //r->print_structure();

  /* How many comparisons did it take to do the inserts, avg per key? */
  double compsperkey =  countint::getcount() / (double) N;
  cout << "That took " << compsperkey << " average comparisons per key, " << endl;
  cout << "Max compare key is: " << maxcompsperkey << endl;
  if(compsperkey <= maxcompsperkey) cout << "OK. " << endl;
  else if (compsperkey <= maxcompsperkey * 2) cout << "could be better... " << endl;
  else {
    cout << "way too many!" << endl;
    return -1;
  }

  /* Test iterator; should iterate the entire tree inorder */
  cout << "Checking traversal using iterator...";
  vit = v.begin();
  BST<countint>::iterator en = r->end();
  BST<countint>::iterator it = r->begin();
  int i = 0;
  for(; it != en; ++it) {
    //    cout << *it << endl;
    countint rc = *it;
    countint bc = *vit;
    if(rc < bc || bc < rc) {
      cout << endl << "Incorrect inorder iteration of RST." << endl;
      return -1;
    }
    ++i;
    ++vit;
  }
  if(i!=N) {
    cout << endl << "Early termination during inorder iteration of RST." << endl;
    return -1;
  }
  cout << " OK." << endl;

  return 0;
}