本文整理汇总了C++中AVL::remove方法的典型用法代码示例。如果您正苦于以下问题:C++ AVL::remove方法的具体用法?C++ AVL::remove怎么用?C++ AVL::remove使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AVL
的用法示例。
在下文中一共展示了AVL::remove方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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() {
AVL<int>* avl = new AVL<int>();
avl->insert(4); // 4
avl->insert(7); // 4
// 7
avl->insert(6); // 6
// 4 7
avl->insert(10); // 6
// 4 7
// 10
avl->insert(12); // 6
// 4 10
// 7 12
avl->print();
std::cout << "\n";
avl->remove(6);
avl->print();
std::cout << "\n";
avl->printInOrder();
std::cout << "\n";
avl->printPostOrder();
return 0;
}
示例3: main
int main()
{
while (true)
{
///////////// Read user input ////////////
std::string command, value;
size_t key;
utils::read_input<size_t, std::string>(std::cin, command, key, value);
///////////// Process ///////////
switch (utils::str2enum(command))
{
case options::LS:
{
g_avl.Print();
break;
}
case options::ADD:
{
g_avl.insert(key, value);
break;
}
case options::RM:
{
g_avl.remove(key);
break;
}
case options::GET:
{
try
{
std::cout << key << " = " << g_avl.lookup(key) << std::endl;
}
catch (const std::string& e)
{
std::cout << "error: " << e << std::endl;
}
break;
}
case options::Q: { exit(0); break; }
case options::UNKNOWN: { break; }
}
}
return 0;
}
示例4: main
int main() {
int SIZE=20;
vector<int> v(SIZE);
AVL<int>* avl = new AVL<int>();
srand(time(0));
for (int i=0;i<SIZE;i++) {
v[i] = 5*i;
}
random_shuffle(v.begin(),v.end());
cout << "Multiples of 5 from 0 to 95 have been shuffled." << endl;
cout << "They will be inserted into an AVL tree in the following order:" << endl << endl;
for (int i=0;i<SIZE;i++) {
cout << v[i] << endl;
}
cout << endl;
for (int i=0;i<SIZE;i++) {
avl->insert(v[i]);
cout << "After Inserting " << v[i] << endl << endl;
avl->print();
}
cout << "In Order Traversal: " << endl;
avl->inOrderTraversal();
cout << endl << "Post Order Traversal: " << endl;
avl->postOrderTraversal();
cout << endl;
cout << "Now the shuffled numbers will be removed from the AVL tree ";
cout << "in the order they were inserted." << endl << endl;
for (int i=0;i<SIZE;i++) {
avl->remove(v[i]);
cout << "After Removing " << v[i] << endl << endl;
avl->print();
}
avl->removeTree();
srand(time(0));
cout << "Now 20 random numbers between 0 and 99 will be inserted into an AVL tree in this order.";
cout << endl;
for (int i=0;i<SIZE;i++) {
v[i] = rand() % 100;
cout << v[i] << endl;
}
cout << endl;
for (int i=0;i<SIZE;i++) {
avl->insert(v[i]);
cout << "After Inserting " << v[i] << endl << endl;
avl->print();
}
cout << "In Order Traversal: " << endl;
avl->inOrderTraversal();
cout << endl << "Post Order Traversal: " << endl;
avl->postOrderTraversal();
cout << endl;
for (int i=0;i<SIZE;i++) {
avl->remove(v[i]);
cout << "After Removing " << v[i] << endl << endl;
avl->print();
}
avl->removeTree();
cout << "Now the numbers from 1 to 20 will be inserted into an AVL tree in order." << endl;
cout << "They will be removed in the same order." << endl;
for (int i=0;i<SIZE;i++) {
v[i] = i+1;
cout << v[i] << endl;
}
cout << endl;
for (int i=0;i<SIZE;i++) {
avl->insert(v[i]);
cout << "After Inserting " << v[i] << endl << endl;
avl->print();
}
cout << "In Order Traversal: " << endl;
avl->inOrderTraversal();
cout << endl << "Post Order Traversal: " << endl;
avl->postOrderTraversal();
cout << endl;
for (int i=0;i<SIZE;i++) {
avl->remove(v[i]);
cout << "After Removing " << v[i] << endl << endl;
avl->print();
}
avl->removeTree();
cout << "Now the numbers from 1 to 20 will be inserted into an AVL tree in order." << endl;
cout << "They will be removed in REVERSE order." << endl;
for (int i=0;i<SIZE;i++) {
//.........这里部分代码省略.........
示例5: main
int main()
{
cout << "TIME TESTS: " << endl << endl;
AVL<int> t;
set<int> s;
cout << "Tree insertion" << endl;
time_t start = clock();
for (int i{ 0 }; i != 10000000; ++i)
t.insert(i);
cout << "Time : " << double(clock() - start) / CLOCKS_PER_SEC << " seconds\n\n";
cout << "Set insortion" << endl;
start = clock();
for (int i{ 0 }; i != 10000000; ++i)
s.insert(i);
cout << "Time : " << double(clock() - start) / CLOCKS_PER_SEC << " seconds\n";
cout << endl << "=================================" << endl << endl;
cout << "Tree removing" << endl;
start = clock();
for (int i{ 0 }; i != 10000000; ++i)
t.remove(i);
cout << "Time : " << double(clock() - start) / CLOCKS_PER_SEC << " seconds\n\n";
cout << "Set removing" << endl;
start = clock();
for (int i{ 0 }; i != 10000000; ++i)
s.erase(i);
cout << "Time : " << double(clock() - start) / CLOCKS_PER_SEC << " seconds\n";
cout << endl << "=================================" << endl << endl;
cout << "Tree searching" << endl;
start = clock();
for (int i{ 0 }; i != 100000000; ++i)
t.find(1 + rand() % 1000000);
cout << "Time : " << double(clock() - start) / CLOCKS_PER_SEC << " seconds\n\n";
cout << "Set searching" << endl;
start = clock();
for (int i{ 0 }; i != 100000000; ++i)
s.find(1 + rand() % 1000000);
cout << "Time : " << double(clock() - start) / CLOCKS_PER_SEC << " seconds\n";
/*
AVL<int> tree;
tree.insert(5);
tree.insert(15);
tree.insert(3);
tree.insert(2);
tree.insert(1);
tree.insert(6);
tree.insert(9);
tree.print();
cout << "Depth of the tree is : " << tree.getDepth() << '\n';
cout << "begin() is " << *(tree.begin()) << endl << endl;
tree.remove(5);
tree.remove(15);
tree.remove(2);
cout << "Elements 5, 15, 2 were removed. " << endl;
tree.print();
cout << endl << "Lets find elem 6 in tree and make an iterator " << endl;
AVL<int>::iterator<int> it{ tree.find(6) };
++it;
std::cout << "Next element is " << *it << '\n';
--it;
--it;
std::cout << "Preveous element is " << *it << '\n' << '\n';
tree.saveInFile("tree.txt");
AVL<int> tree1;
tree1.loadFromFile("tree.txt");
cout << endl << "New tree was loaded from file : " << endl << endl;
tree1.print();
*/
}