Ejemplo n.º 1
0
TEST(AVLTree, Insert) {
  {
  AVLTree<int, int> a;

  for (int i = 0; i < 1000; i++) {
    a.Insert(i, i * i);
    for (int k = 0; k <= i; k++) {
      ASSERT_EQ(a.Find(k).first, k * k);
      ASSERT_TRUE(a.Find(k).second);
    }
  }
  }
  {
  AVLTree<int, int> a;

  for (int i = 100; i > 0; i--) {
    a.Insert(i, i * i);
    for (int k = i; k <= 100; k++)
      ASSERT_EQ(a.Find(k).first, k * k);
  }
  }
  AVLTree<int, int> b;
  mt19937_64 _e1(chrono::system_clock::now().time_since_epoch().count());
  std::uniform_int_distribution<int> dist(0, 1000000);
  for (int i = 0; i < 1000; i++) {
    int t = dist(_e1);
    b[t] = i * i;
    ASSERT_EQ(b.Find(t).first, i * i);
  }
}
Ejemplo n.º 2
0
TEST(AVLTree, FuzzyDeletion) {
  AVLTree<int, int> b;
  map<int, int> rb;
  mt19937_64 _e1(chrono::system_clock::now().time_since_epoch().count());
  std::uniform_int_distribution<int> dist(0, 1000);
  for (int i = 0; i < 5000; i++) {
    int t = dist(_e1);
    if (i % 3 == 0) {
      auto it = rb.find(t);
      if (it == rb.end()) {
        ASSERT_EQ(b.Erase(t), false);
      } else {
        rb.erase(t);
        ASSERT_EQ(b.Erase(t), true);
        ASSERT_EQ(b.Find(t).second, false);
      }
    }
    b[t] = i * i;
    rb[t] = i * i;
    ASSERT_EQ(b.Find(t).first, i * i);
  }
  ASSERT_EQ(b.Size(), rb.size());
  for (int i = 0; i < 1000; i++) {
    auto it = rb.find(i);
    if (it == rb.end()) {
      ASSERT_EQ(b.Find(i).second, false);
      ASSERT_EQ(b.Erase(i), false);
      continue;
    }
    ASSERT_EQ(b.Find(i).second, true);
    ASSERT_EQ(b.Find(i).first, rb[i]);

    rb.erase(i);
    ASSERT_EQ(b.Erase(i), true);
    ASSERT_EQ(b.Find(i).second, false);
  }
  ASSERT_EQ(b.Size(), rb.size());
}
Ejemplo n.º 3
0
int main(int argc,char*argv[])
{
	AVLTree<string> avlTree;
	BSTree<string> bsTree;
	string str, last;
	char c;

	if (argc != 2)
	{
		cerr << "Parameters: <filename>.  use quotations around long path names." << endl;
		return 1;
	}

	ifstream ifsInput(argv[1],ios::in | ios::binary);

	if (!ifsInput)
	{
		cerr << "Couldn't open input file." << endl;
		return 2;
	}

	
	while (ifsInput.get(c) && !ifsInput.eof())
	{
		switch (c)
		{
			case '.':
			case ',':
			case '\'':
			case '\"':
			case ':':
			case ';':
			case '<':
			case '>':
			case '/':
			case '?':
			case '[':
			case ']':
			case '{':
			case '}':
			case '\\':
			case '|':
			case '!':
			case '#':
			case '$':
			case '%':
			case '^':
			case '&':
			case '*':
			case '(':
			case ')':
			case '_':
			case '=':
			case '`':
			case ' ':
			case '\r':
			case '\n':
			case '\t':
				if (str.size())
				{
					avlTree.Insert(str);
					bsTree.Insert(str);
					last = str;
				}
				
				str = "";
				continue;
			default:
				str += c;
				continue;
		}

	}
	ifsInput.close();
	//x.Output(Print);
	cout << "AVL Height: " << avlTree.Height() << endl;
	cout << "BST Height: " << bsTree.Height() << endl;
	cout << "AVL Size:   " << avlTree.Size() << endl;
	cout << "BST Size:   " << bsTree.Size() << endl;


	clock_t stime, etime;
	double elapse; 
	int i;
	
	cout << "Searching for \"" << last << "\"..." << endl;
	stime = clock();
	for (i = 0; i < 100000; i++)
		avlTree.Find(last);
	etime = clock();
	elapse =(double)(etime-stime)/CLOCKS_PER_SEC;
	cout << "Search in AVL tree took " << elapse << " seconds " << endl;


	stime = clock();
	for (i = 0; i < 100000; i++)
		bsTree.Find(last);
	etime = clock();
	elapse =(double)(etime-stime)/CLOCKS_PER_SEC;
	cout << "Search in BST tree took " << elapse << " seconds " << endl;

	avlTree.Output(Print);

	return 0;
}