Exemple #1
0
void treap()
{
    Treap<int> treap;

#define TREAP_LOOP   for (int i = 0; i < 1000; i ++)

	TREAP_LOOP
	{
		treap.Insert(i);
		assert(treap.GetMin() == 0);
		assert(treap.GetMax() == i);
	}

	std::cout<<"Treap constructed"<<std::endl;
	// print keys in sorted order
	treap.InOrderTraversal();

	TREAP_LOOP
	{
		assert(treap.Exists(i));
	}

	TREAP_LOOP
	{
		treap.Delete(i);
	}

	TREAP_LOOP
	{
		assert(!treap.Exists(i));
	}
}
Exemple #2
0
TEST(Treap , Min)
{
    Treap* tr = new Treap(false);

    tr->Insert(1);
    tr->Insert(5);
    tr->Insert(2);
    tr->Insert(61);
    tr->Insert(61);
    tr->Insert(24);
    tr->Insert(51);
    tr->Insert(61);
    tr->Insert(57);
    tr->Insert(32);
    tr->Insert(24);

    CHECK(tr->Min() == 1);
    tr->Remove(1);
    tr->Remove(2);
    CHECK(tr->Min() == 5);
    tr->Remove(5);
    CHECK(tr->Min() == 24);
    tr->Remove(24);
    CHECK(tr->Min() == 32);
    tr->Remove(32);
    CHECK(tr->Min() == 51);
    tr->Remove(51);
    CHECK(tr->Min() == 57);
    tr->Remove(57);
    CHECK(tr->Min() == 61);
    tr->Remove(61);
    CHECK(tr->Min() == -1);

    delete tr;
}
Exemple #3
0
TEST(Treap , Successor)
{
    Treap* tr = new Treap(false);

    CHECK(tr->Successor(5) == -1);

    tr->Insert(1);
    tr->Insert(5);
    tr->Insert(2);

    CHECK(tr->Successor(5) == -1);

    tr->Insert(61);
    tr->Insert(61);
    tr->Insert(24);
    tr->Insert(51);
    tr->Insert(61);
    tr->Insert(57);
    tr->Insert(32);
    tr->Insert(24);

    CHECK(tr->Successor(1) == 2);
    CHECK(tr->Successor(2) == 5);
    CHECK(tr->Successor(5) == 24);
    CHECK(tr->Successor(24) == 32);
    CHECK(tr->Successor(32) == 51);
    CHECK(tr->Successor(51) == 57);
    CHECK(tr->Successor(57) == 61);
    CHECK(tr->Successor(61) == -1);
    CHECK(tr->Successor(6) == -1);

    delete tr;
}
int main()
{
     freopen("Text/ORDERSET.txt","r",stdin);

    int Q; scanf("%d",&Q);

    Treap oTreap;

    while(Q--)
    {
        char t[5];
        int p;
        scanf("%s%d",t,&p);

        if(t[0]=='I')
        {
            oTreap.Insert(p);
        }
        else if(t[0]=='D')
        {
            oTreap.Delete(p);
        }
        else if(t[0]=='K')
        {
            int v = oTreap.FindKth(p);

            if(v > -INF)
            {
                printf("%d\n",v);
            }
            else
                puts("invalid");
        }
        else
        {
            int v = oTreap.Count(p);

            printf("%d\n",v);
        }

    }

    return 0;
}
Exemple #5
0
TEST(Treap , Remove)
{
    Treap* tr = new Treap(false);
    size_t n=5000;
    uint32_t* A = (uint32_t*)malloc(n*sizeof(uint32_t));
    for (size_t i=0; i<n; i++) A[i] = rand() % 5001;

    for (size_t i=0; i<n; i++) tr->Insert(A[i]);
    for (size_t i=1; i<n; i+=2) tr->Remove(A[i]);

    for (size_t i=0; i<n; i++) {
        if (i%2==1) {
            CHECK(tr->Find(A[i]) == false);
        }
    }

    free(A);
    delete tr;
}
Exemple #6
0
TEST(Treap , InsertFind)
{
    Treap* tr = new Treap(false);
    BinarySearchTree* bst = new BinarySearchTree(false);

    size_t n=5000;
    uint32_t* A = (uint32_t*)malloc(n*sizeof(uint32_t));
    for (size_t i=0; i<n; i++) A[i] = rand() % 5001;
    for (size_t i=0; i<n; i++) {
        tr->Insert(A[i]);
        bst->Insert(A[i]);
    }
    for (size_t i=0; i<n; i++) {
        CHECK(tr->Find(A[i]) == bst->Find(A[i]));
        CHECK(tr->Find(A[i]+1) == bst->Find(A[i]+1));
    }
    delete tr;
    delete bst;
    free(A);
}