当前位置: 首页>>代码示例>>C++>>正文


C++ AVL::insert方法代码示例

本文整理汇总了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;*/
}
开发者ID:GregGay,项目名称:csci333-avl,代码行数:27,代码来源:avl_test.cpp

示例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;

}
开发者ID:jmblanchard,项目名称:csci333-avl,代码行数:33,代码来源:tree_test.cpp

示例3: main

int main() {
    AVL avl;
    avl.insert(5);
    avl.print();
    avl.insert(3);
    avl.print();
}
开发者ID:richard-liang,项目名称:leetcode,代码行数:7,代码来源:avl.cpp

示例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();
      
}
开发者ID:cathyatseneca,项目名称:dsa555.f12,代码行数:12,代码来源:avl.cpp

示例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);
}
开发者ID:tantantzp,项目名称:datastructure,代码行数:36,代码来源:main.cpp

示例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();

}
开发者ID:bpan2,项目名称:dsa555-w15,代码行数:16,代码来源:avl.cpp

示例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;
		}
开发者ID:exyrion,项目名称:bst_avl_splay,代码行数:31,代码来源:avl.cpp

示例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;
}
开发者ID:beet-uoa,项目名称:library,代码行数:17,代码来源:avltree.cpp

示例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);
}
开发者ID:vu2srk,项目名称:Problem-solving-and-DS,代码行数:14,代码来源:avl.cpp

示例10: insert

void AVLHash :: insert(int k, int v)
{
        int pos;
        AVL *tree;

        pos = hashCode(k);
        tree = hash_table[pos];
        tree->insert(k, v);
}
开发者ID:Mengqi,项目名称:Cpp,代码行数:9,代码来源:avlhash.cpp

示例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;
}
开发者ID:mboghiu,项目名称:try,代码行数:55,代码来源:main.cpp

示例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;
}
开发者ID:eecrazy,项目名称:acmcode,代码行数:11,代码来源:AVL平衡树.cpp

示例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;
}
开发者ID:mmdsharifi,项目名称:trees,代码行数:51,代码来源:recursive-avl-tree.cpp

示例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;
}
开发者ID:TedCAI,项目名称:London-Tube-Path-Search,代码行数:48,代码来源:binary_tree.cpp

示例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;
}
开发者ID:beet-uoa,项目名称:library,代码行数:16,代码来源:avltree.cpp


注:本文中的AVL::insert方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。