Exemple #1
0
/******************************************************************************************
 * Test an AVL
 ******************************************************************************************/
template <typename T> void  testAVL(int n) {
   AVL<T>* avl = new AVL<T>;
   while (avl->size() < n) {
      T e = dice((T)n*3); //[0, 3n)范围内的e
      switch (dice(3)) {
         case 0: { //查找,成功率 <= 33.3%
            printf("Searching for "); print(e); printf(" ...\n");
            BinNodePosi(T) & p = avl->search(e);
            p ?
               printf("Found with"), print(p), printf("\n") :
               printf("Not found\n");
            break;
         }
         case 1: { //删除,成功率 <= 33.3%
            printf("Removing "); print(e); printf(" ...\n");
            avl->remove(e) ? printf("Done\n"), print(avl) : printf("Not exists\n");
            break;
         }
         default: {//插入,成功率 == 100%
            printf("Inserting "); print(e); printf(" ...\n");
            BinNodePosi(T) p = avl->insert(e);
            printf("Done with"), print(p), printf("\n"), print(avl);
            break;
         }
      }
   }
   while (avl->size() > 0) {
      T e = dice((T)n*3); //[0, 3n)范围内的e
      printf("Removing "); print(e); printf(" ...\n");
      avl->remove(e) ? printf("Done\n"), print(avl) : printf("Not exists\n");
   }
   release(avl);
}
int main(int argc, char **argv)
{
	int i, j, n;
	double t;
	AVL<int> tree;
	if (argc > 3) return EXIT_FAILURE;
	i = time(0);
	if (argc == 1) n = 20;
	else {
		n = atoi(argv[1]);
		if (argc == 3) i = atoi(argv[2]);
	}
	srand((unsigned int)i);
	cout << "Size is " << n << endl;
	cout << "Seed is " << i << endl;
	cout << "Inserting..." << endl;
	t = ((double)clock())/CLOCKS_PER_SEC;
	for (i = 1 ; i <= n ; i++) {
		j = rand()%n+1;
	//	cout << j << ' ';
		tree.insert(j);
	}
	t = ((double)clock())/CLOCKS_PER_SEC-t;
	cout << t << " secs" << endl;
//	cout << "\nPrinting tree..." << endl;
//	tree.print();
	cout << "Size of tree is: " << tree.size() << endl;
	ofstream out("avl.dot");
	tree.display(out);
	out.close();
	system("dot avl.dot -Tpng -o avl.png");
	cout << "Created tree image at avl.png!" << endl;
	cout << "Extracting..." << endl;
	t = ((double)clock())/CLOCKS_PER_SEC;
	for (i = 1 ; i <= n ; i++) {
		j = rand()%n+1;
	//	cout << j << ' ';
		tree.extract(j);
	}
	t = ((double)clock())/CLOCKS_PER_SEC-t;
	cout << t << " secs" << endl;
//	cout << "\nPrinting tree..." << endl;
//	tree.print();
	cout << "Size of tree is: " << tree.size() << endl;
	cout << "Clearing..." << endl;
	t = ((double)clock())/CLOCKS_PER_SEC;
	tree.clear();
	t = ((double)clock())/CLOCKS_PER_SEC-t;
	cout << t << " secs" << endl;
	return EXIT_SUCCESS;
}
Exemple #3
0
void printTreeStatus(const AVL<T> &t) {
    cout << "----------------------------------------" << endl;
    if (t.isEmpty()) cout << "The tree is empty" << endl;
    else {
        cout << "Tree root is: " << t.root() << endl;
        cout << "Tree height is: " << t.height() << endl;
        cout << "Tree contains " << t.size() << " elements" << endl;
    }
    cout << "----------------------------------------" << endl;
}