Esempio n. 1
0
Tree *dtree(Tree *root, char key){
    Tree *p, *p2;
    if(!root)return root;   /*não encontrado*/
    if(root->info==key){   /* apagar a raiz */
        /*isso significa uma arvore vazia*/
        if(root->left==root->right){
            free(root);
            return NULL;
        }
        /*ou se uma subarvore e nula*/
        else if(root->left==NULL){
            p = root->right;
            free(root);
            return p;
        }
        else if(root->right==NULL){
            p = root->right;
            free(root);
            return p;
        }
        else {
            p2 = root->right;
            p = root->right;
            while(p->left)p = p->left;
            p->left = root->left;
            free(root);
            return p2;
        }
    }
    if(root->info<key)root->right = dtree(root->right, key);
    else root->left = dtree(root->left, key);
    return root;
}
Esempio n. 2
0
int main()
{
	int i,j;
	x = malloc(SIZE*sizeof(double*));
	for(i=0;i<SIZE;i++)
		x[i] = malloc(3*sizeof(double));
	t_x = malloc(T_SIZE*sizeof(double*));
	for(i=0;i<T_SIZE;i++)
		t_x[i] = malloc(3*sizeof(double));
	for(i=0;i<SIZE;i++)
	{
		for(j=0;j<3;j++)
			scanf("%lf",&x[i][j]);
	}
	for(i=0;i<T_SIZE;i++)
	{
		for(j=0;j<3;j++)
			scanf("%lf",&t_x[i][j]);
	}
	for(i=0;i<SIZE;i++)
	{
		for(j=0;j<3;j++)
			printf("%lf ",x[i][j]);
		printf("\n");
	}

	struct Node * root;
	root = dtree(x,SIZE,root);

	fprintf(stderr,"END, s,t,d,b = %d %lf %d %d\n",root->s,root->theta,root->dim,root->b);
	/*double ds[3];
	ds[0]=0.502864; ds[1]=0.721677; ds[2]=-1.000000;
	
	printf("======%d\n",predict(root,ds));*/
	double Ein = 0;
	for(i=0;i<SIZE;i++)
	{
		if(predict(root,x[i]) != x[i][2])
			Ein++;
	}
	Ein = Ein / SIZE;
	double Eout = 0;
	for(i=0;i<T_SIZE;i++)
	{
		if(predict(root,t_x[i]) != t_x[i][2])
			Eout++;
	}
	Eout = Eout / T_SIZE;
	printf("Ein = %lf, Eout = %lf\n",Ein,Eout);

	return 0;

}
Esempio n. 3
0
void TestCase_CDHashAVLTree_TestCase1(void)
{
	CDHashAVLTree	dtree(2, HashInt, IntCompare, NULL, IntCopy);

	int i;
	for ( i = 400; i > 0 ; i-- )
	{
		if ( i == 32 )
		{
			printf( "i = %d\n", i);
		}
		dtree.Insert((void *)i);
	}
	int a = 3;
	int ret = -1;
	ret = (int)dtree.Find((void *)a);

	assertTrue( ret == a);

	for ( i = 1; i <= 400; i++ )
	{
		ret = (int)dtree.Find((void *)i);
		if ( ret != i )
		{
			printf("ret = %d, i = %d\n", ret, i);
		}
	}


	dtree.Delete((void *)a);

	ret = (int)dtree.Find((void *)a);

	assertTrue(ret == 0);

	dtree.Insert((void *)a);

	for ( i = 1; i <= 400; i++ )
	{
		ret = (int)dtree.Find((void *)i);
		if ( ret != i )
		{
			printf("ret = %d, i = %d\n", ret, i);
		}
	}
	printf("TestCase1 Finished.\n");

}
Esempio n. 4
0
void TestCase_CDHashAVLTree_TestCase2(void)
{
	CDHashAVLTree	dtree(32768, HashInt, IntCompare, NULL, IntCopy);

	int i;
	clock_t  t1, t2;
	t1 = clock();
//#pragma omp parallel for num_threads(4) schedule(static, 16)
	for ( i = 1; i <= 1000000; i++ )
	{
		dtree.Insert((void *)i);
	}
	t2 = clock();
	printf("CDHashAVLTree::Insert, time = %ld\n", t2-t1);

	int data = 1001;
	MCapi_CreateThread(CDHashAVLTree_Find_Thread, (void *)&dtree,
        MCAPI_THREAD_RUNNING);
	for ( i = 0; i < 100000; i++ )
	{
		dtree.Delete((void *)data);
		dtree.Insert((void *)data);
	}
	t1 = clock();
#pragma omp parallel for //schedule(static, 4096)
	for ( i = 1; i <= 1000000; i++ )
	{
		int Key;
		Key = (int)dtree.Find((void *)i);
	}
	t2 = clock();
	printf("CDHashAVLTree::Find 1000000, time = %ld\n", t2-t1);

	t1 = clock();
#pragma omp parallel for //schedule(static, 4096)
	for ( i = 1; i <= 1000000; i++ )
	{
		int Key;
		Key = (int)dtree.Find((void *)i);
	}
	t2 = clock();
	printf("CDHashAVLTree::Find 1000000, time = %ld\n", t2-t1);

	Sleep(100);
}
Esempio n. 5
0
struct Node * dtree(double **ins, int size, struct Node *node)
{
	int i,j;
	if(node == NULL)
	{
		printf(" is NULL\n");
		node = malloc(sizeof(struct Node));
		node->p = NULL;
		node->n = NULL;
		node->b = 0;
		if(size == 0)
		{
			return NULL;
		}
		if(size == 1)
		{
			node->label = ins[0][2];
			return node;
		}
			
		struct REC * result = decision_stump(ins, size);
		printf("r: s,d,t,c = %d, %d, %lf, %lf\n",result->s, result->dim,result->theta,result->count);
		
		node->s = result->s;
		node->dim = result->dim;
		node->theta = result->theta;
		node->count = result->count;
		node->b = 0;
		if(node->count == 0 || node->count == size)
		{
			node->label = ins[0][2];
			for(i=0;i<size;i++)
				printf("%lf ",ins[i][2]);
			printf("\n");
			return node;
		}
			

		double **insp = malloc((result->count)*sizeof(double*));
		for(i=0;i<result->count;i++)
			insp[i] = malloc(3*sizeof(double));
		double **insn = malloc((size - result->count)*sizeof(double*));
		for(i=0;i<size - result->count;i++)
			insn[i] = malloc(3*sizeof(double));
		int inspc = 0, insnc = 0;
		for(i=0;i<size;i++)
		{
			if ((node->s) * (ins[i][node->dim]- node->theta)>0)
			{
				for(j=0;j<3;j++)
					insp[inspc][j] = ins[i][j];
				inspc++;
			}
			else
			{
				for(j=0;j<3;j++)
					insn[insnc][j] = ins[i][j];
				insnc++;
			}
		}
		node->p = dtree(insp, node->count, node->p);
		node->n = dtree(insn, size - node->count, node->n);
		//node->b = node->p->b+node->n->b;
		node->b = 1;
		if(node->p !=NULL)
			node->b +=node->p->b;
		if(node->n != NULL)
			node->b += node->n->b;
		return node;
	}
	else
		fprintf(stderr, "node != NULL\n");
	return NULL;
}