本文整理汇总了C++中AVL::size方法的典型用法代码示例。如果您正苦于以下问题:C++ AVL::size方法的具体用法?C++ AVL::size怎么用?C++ AVL::size使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AVL
的用法示例。
在下文中一共展示了AVL::size方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: testAVL
/******************************************************************************************
* 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);
}
示例2: main
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;
}
示例3: printTreeStatus
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;
}