本文整理汇总了C++中AVL::insert方法的典型用法代码示例。如果您正苦于以下问题:C++ AVL::insert方法的具体用法?C++ AVL::insert怎么用?C++ AVL::insert使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AVL
的用法示例。
在下文中一共展示了AVL::insert方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main() {
AVL<int>* avl = new AVL<int>();
//AVL<double>* avlD = new AVL<double>();
//AVL<std::string>* avlS = new AVL<std::string>();
cout << "Pre Order Traversal Print" << endl;
cout << endl;
avl->insert(4);
avl->insert(2);
avl->insert(3);
avl->insert(7);
avl->insert(1);
avl->insert(17);
avl->insert(16);
avl->insert(5);
avl->print();
cout << endl;
/*
cout << "Post Order Traversal" << endl;
avl->postOrderTraversal();
cout << endl;
cout << "In Order Traversal" << endl;
avl->inOrderTraversal();
cout << endl;
cout << "End!" << endl;*/
}
示例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() {
AVL avl;
avl.insert(5);
avl.print();
avl.insert(3);
avl.print();
}
示例4: main
int main(void){
AVL tree;
tree.insert(10);
tree.insert(30);
tree.insert(20);
tree.insert(15);
tree.insert(18);
tree.insert(16);
cout << "*************************" << endl;
tree.inOrderPrint();
}
示例5: 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);
}
示例6: main
int main(void){
AVL tree;
tree.insert(10);
tree.insert(20);
tree.print();
tree.insert(30);
cout << "*********************" << endl;
tree.print();
for(int i=40;i< 70;i+=10){
tree.insert(i);
cout << "*********************" << endl;
tree.print();
}
// tree.depthFirstPrint();
}
示例7: main
//Main function
int main()
{
AVL *a = new AVL();
string str;
int val;
cin >> str;
clock_t start, end;
start = clock();
while(!cin.eof())
{
if(str.compare("insert") == 0)
{
cin >> val;
if((a->access(val)) == true)
cout << "Element already inserted" << endl;
else
{
a->insert(val);
cout << "Element inserted" << endl;
}
}
if(str.compare("access") == 0)
{
cin >> val;
if((a->access(val)) == true)
cout << "Element accessed" << endl;
else
cout << "Element not found" << endl;
}
示例8: ARC028_B
signed ARC028_B(){
int n,k;
cin>>n>>k;
int x[n];
for(int i=0;i<n;i++) cin>>x[i];
map<int,int> m;
for(int i=0;i<n;i++) m[x[i]]=i+1;
AVL<Int> avl;
for(int i=0;i<k-1;i++) avl.insert(x[i]);
for(int i=k-1;i<n;i++){
avl.insert(x[i]);
Int key=avl.rank(k-1)->key;
cout<<m[key]<<endl;
assert(avl.index(key)==k-1);
}
return 0;
}
示例9: main
int main(void){
AVL avl;
avl.insert(10);
avl.insert(5);
avl.insert(20);
avl.insert(3);
avl.insert(15);
avl.insert(8);
avl.insert(25);
avl.insert(7);
avl.insert(2);
avl.insert(9);
avl.insert(6);
}
示例10: insert
void AVLHash :: insert(int k, int v)
{
int pos;
AVL *tree;
pos = hashCode(k);
tree = hash_table[pos];
tree->insert(k, v);
}
示例11: 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;
}
示例12: main
int main()
{
int n,x;
AVL <int> avl;
cout<<"input the number of elements:";
cin>>n;
for(int i=0;i<n;i++)
cin>>x,avl.insert(x);
avl.print();
return 0;
}
示例13: 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;
}
示例14: main
int main(){
FileIO * file = new FileIO("tube.txt");
vector<Station *> station_list = file->read_file_to_station_list();
vector<Station *> sorted_list = file->read_file_to_station_list();
std::sort(sorted_list.begin(), sorted_list.end(), sort_by_name);
// print_all(sorted_list, station_list);
// search_by_keyword(sorted_list, station_list);
// mode_selection(sorted_list, station_list);
//test section
node<int> *_node = new node<int>();
// cout<<sorted_list[0]->get_station_index()<<endl;
_node->data = sorted_list[0]->get_station_index();
AVL<int> *tree = new AVL<int>(_node); //new binary_tree<int>(_node);
for(vector<Station *>::iterator it = sorted_list.begin() + 1;it != sorted_list.end();++it){
bool b_return = tree->insert((*it)->get_station_index());
// cout<<(*it)->get_station_index()<<" "<<b_return<<endl;
}
cout<<"done!"<<endl;
// tree->traverse_inorder(tree->root);
cout<<"Tree height is "<<tree->tree_height<<endl;
node<int> *returned_node = tree->search(0);
cout<<returned_node->data<<" "<<returned_node->height<<endl;
node<int> *rhs = tree->root;
// cout<<rhs->data<<" "<<rhs->height<<endl;
rhs = rhs->rhs;
node<int> min, max;
// cout<<"d 1"<<endl;
tree->find_min_from(rhs, min);
// cout<<"d 2"<<endl;
tree->find_max_from((tree->root)->lhs, max);
cout<<"min in right branch is "<<min.data<<" "<<min.height<<endl;
cout<<"max in left branch is "<<max.data<<" "<<max.height<<endl;
cout<<"depth is "<<tree->find_depth(tree->root)<<endl;
cout<<"left branch depth is "<<tree->find_depth((tree->root)->lhs)<<endl;
cout<<"right branch depth is "<<tree->find_depth((tree->root)->rhs)<<endl;
cout<<"balance factor is "<<tree->balance_factor(tree->root)<<endl;
cout<<"tree is balanced? "<<tree->is_balanced(tree->root)<<endl;
cout<<"tree root "<<(tree->root)->data<<endl;
// delete[] tree->root;
delete tree;
return 0;
}
示例15: ARC033_C
signed ARC033_C(){
int q;
cin>>q;
AVL<Int> avl;
for(int i=0;i<q;i++){
int t,x;
cin>>t>>x;
if(t==1) avl.insert(x);
else{
Int key=avl.rank(x-1)->key;
cout<<key<<endl;
avl.erase(key);
}
}
return 0;
}