Beispiel #1
0
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;*/
}
Beispiel #2
0
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;

}
Beispiel #3
0
int main() {
    AVL avl;
    avl.insert(5);
    avl.print();
    avl.insert(3);
    avl.print();
}
Beispiel #4
0
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();
      
}
Beispiel #5
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);
}
Beispiel #6
0
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();

}
Beispiel #7
0
//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;
		}
Beispiel #8
0
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;
}
Beispiel #9
0
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);
}
Beispiel #10
0
void AVLHash :: insert(int k, int v)
{
        int pos;
        AVL *tree;

        pos = hashCode(k);
        tree = hash_table[pos];
        tree->insert(k, v);
}
Beispiel #11
0
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;
}
Beispiel #12
0
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;
}
Beispiel #13
0
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;
}
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;
}
Beispiel #15
0
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;
}
Beispiel #16
0
int main()
{
    int values[N];

    AVL<int> avl;

    cout << "Populating the tree with " << N << " random values... ";
    for (unsigned i = 0; i < N; ++i) {
        values[i] = rand();
        avl.insert(values[i]);
    }
    cout << "Done" << endl;

    printTreeStatus(avl);

    for (unsigned i = 0; i < N; ++i) {
        unsigned idx = rand() % N;
        if (!avl.contains(values[idx])) 
	        cout << "ERROR: Value " << values[idx] << " was inserted and not found!" << endl;
    }

    cout << "Now removing a random element from the tree... ";
    unsigned idx = rand() % N;
    avl.erase(values[idx]);
    cout << "Done" << endl;

    printTreeStatus(avl);
    
    cout << "Now removing the root of the tree " << N_ELEMS_TO_REMOVE << " times... ";
    for (unsigned i = 0; i < N_ELEMS_TO_REMOVE; ++i) {
        avl.erase(avl.root());
    }
    cout << "Done" << endl;

    printTreeStatus(avl);

    // Outputting to cerr so the output can be redirected with ./avl_demo 2> <name>.gvz
    cout << "Do you want to output the GraphViz representation of the tree to the cerr stream (Y/n)? ";
    char usrInput;
    cin >> usrInput;
    if (usrInput == 'Y' || usrInput == 'y') avl.toGraphViz(cerr, "AVL");
    
    return 0;
}
Beispiel #17
0
int main()
{
    AVL b;
    int n, k;
    cin >> n;
    for (int i = 0; i < n; i++) {
       cin >> k;
       b.insert(k, &b.root);
    }
    b.print(b.root);
   // cout << endl << b.search(2, b.root) << endl;
    cout << "enter number of queries for del" << endl;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cout << "enter what to del\n";
        cin >> k;
        b.delete_it(k, &b.root);
        cout << endl;
        b.print(b.root);
    }

    return 0;
}
Beispiel #18
0
//I used a .txt filled with employees names and ID numbers to create the AVL binary tree.
int main() 
{
    std::string file_name = "data/employees.txt";
    std::ifstream in_file(file_name);
    
    AVL <std::string, std::string> EmployeeData;
    
    //Parse the employee names and idss out of the text file
    std::string line;
    while(std::getline(in_file,line)) 
    {
        // Gets the employees name
        std::string employee_name = line.substr(0,line.find(","));
        // Gets the employees id number
        std::string employee_id = line.substr(line.find(",")+1);
        EmployeeData.insert(employee_name, employee_id);
    }
    //EmployeeData.Delete(EmployeeData.root, "Zyon Newman");
    EmployeeData.print(EmployeeData.root);
    std::cout << "Testing Lookup" << std::endl;
    std::cout << "Zyler Whitney: " << EmployeeData.lookup(EmployeeData.root, "Zyler Whitney") << std::endl;

    return 0;
}
Beispiel #19
0
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();
	*/
}
Beispiel #20
0
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++) {
    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[SIZE-1-i]);
    cout << "After Removing " << v[SIZE-1-i] << endl << endl;
    avl->print();
  }

  delete avl;


/*
  AVL<int>* avl = new AVL<int>();
  avl->insert(1);
  avl->insert(2);
  avl->print();
  avl->insert(3);
  avl->print();
  avl->insert(4);
  avl->print();
  avl->insert(5);
  avl->print();
  avl->insert(6);
  avl->print();
  avl->insert(7);
  avl->print();
  avl->insert(8);
  avl->insert(9);
  avl->print();  
  avl->insert(10);
  avl->print();
  avl->inOrderTraversal();
  avl->postOrderTraversal();

  
  avl->remove(1);
  avl->print();  
  avl->remove(2);
  avl->print();  
  avl->remove(3);
  avl->print();  
  avl->remove(4);
  avl->print();
  avl->remove(5);
  avl->print();  
  avl->remove(6);
  avl->print();  
  avl->remove(7);
  avl->print();  
  avl->remove(8);
  avl->print();
  avl->remove(9);
  avl->print();
  avl->remove(10);
  avl->print();  

  avl->insert(5);
  avl->print();
  avl->insert(18);
  avl->print();
  avl->insert(25);
  avl->insert(2);
  avl->insert(1);
  avl->print();
  avl->remove(18);
  avl->print();
  delete avl;

  AVL<int>* a = new AVL<int>();
  a->insert(400);
  a->print();
  a->remove(5);
  a->print();
  a->remove(400);
  a->print();
  delete a;
*/

/*
  AVL<double>* d = new AVL<double>();
  d->insert(1);
  d->insert(2);
  d->insert(4);
  d->print();
  d->insert(3.5);
  d->insert(5);
  d->print();
  d->insert(3.25);
  d->print();

  delete d;

  AVL<int>* a1 = new AVL<int>();
  a1->insert(10);
  a1->insert(20);
  a1->insert(30);
  a1->insert(40);
  a1->print();
  a1->insert(35);
  a1->print();
  delete a1;

  AVL<int> a2;
  a2.insert(3);
  a2.insert(2);
  a2.insert(1);
  a2.insert(4);
  a2.print();
  a2.insert(5);
  a2.print();
  a2.insert(6);
  a2.print();
  a2.insert(7);
  a2.print();
  a2.insert(16);
  a2.print();
  a2.insert(15);
  a2.print();
  a2.insert(14);
  a2.print();
  a2.remove(7);
  a2.print();
  a2.remove(4);
  a2.print();
*/
/*
  AVL<double>* db=new AVL<double>();
  db->insert(7.5);
  db->insert(12.98);
  db->insert(3.45);
  db->insert(-2.58);
  db->insert(99.1);
  db->print();
  db->remove(12.98);
  db->remove(7.5);
  db->remove(5.34353);
  db->print();
  delete db;

  AVL<int>* b=new AVL<int>();
  b->insert(10);
  b->insert(5);
  b->insert(15);
  b->insert(1);
  b->insert(7);
  b->insert(11);
  b->insert(17);
  b->insert(18);
  b->insert(12);
  b->insert(14);
  b->insert(19);
  b->insert(8);
  b->insert(9);

  b->print();
 
  b->remove(15);
  b->remove(12);
  b->remove(10);
  b->print();
  b->remove(19);
  b->print();

  delete b;
*/

}
Beispiel #21
0
int main(){
    int T;
    scanf("%d", &T);
    
    const int size = 5 * 100000 + 2;
    
    int input[size];
    int preprocess[size];
    
    for(int t = 0; t < T; ++t){
        AVL tree;
        
        int n;
        scanf("%d", &n);
        //fgets(input, n + 2, stdin);
        for(int i = 0; i < n; ++i){
            scanf("%d", &input[i]);
        }
        
        preprocess[n - 1] = 1; // preprocessing z prawa na lewo
        int last = input[n - 1];
        int len = 1;
        for(int i = n - 2; i >= 0; --i){
            int current = input[i];
            if(current < last){
                preprocess[i] = ++len;
            }
            else{
                preprocess[i] = 1;
                len = 1;
            }
            last = current;
        }
        
        int max = preprocess[0];
        
        last = input[0];
        len = 1;
        tree.insert(last, 1);
        for(int i = 1; i < n; ++i){
            int current = input[i];
            if(current > last){
                ++len;
            }
            else{
                len = 1;
            }
            last = current;
            
            int m = tree.get_max(current - 1);
            if(m + preprocess[i] > max){
                max = m + preprocess[i];
            }
            if(m < len){
                tree.insert(current, len);
            }
        }
        
        printf("%d\n", max);
    }
    
    return 0;
}