Ejemplo n.º 1
0
int BSTsearch( bst b, int x, int y ) {
  if ( b == NULL )
    return 0; 
  if ( b->x == x && b->y == y)
	return 1;
	
    if ( is_lower_than(x,y, b->x,b->y) < 0) {	
		return BSTsearch( b->sx, x,y );
    }else{
	 	return BSTsearch( b->dx, x,y );
	
	}
}
Ejemplo n.º 2
0
NodePtr BST :: BSTsearch( NodePtr x, KeyType k )
{
    if((x == NIL) || (k == x->key))
    {
        return x;
    }
    if(k< x->key)
    {
        return BSTsearch(x->left,k);
    }
    else
    {
        return BSTsearch(x->right,k);
    }
   // You write this
}
Ejemplo n.º 3
0
int main()
{
	int arr[] = {10,3,5,2,9,100};
	int i, len, key;
	TreePointer bst, ret;
	
	len = arr_length(arr,int);
    
	//create tree using ADT
	bst = createBST(arr[0]);
	for(i=1; i <  len ; i ++)
		insert(bst,arr[i]);
    
	
	key = 2;
	ret = BSTsearch(bst,key);
	assert(ret!=NULL);
	assert(key==getBSTData(ret));
    
	//print all by order
	printSorted(bst);
    
	return 0;
}
Ejemplo n.º 4
0
/* Inserimento del singolo nodo, con valorizzazione di tutti i campi del nodo */
rbnode *simpleinsert(rbtree *tree, int x, int y)
{
	// controlla se la tessera nelle posizioni x, y è ammessa
	if (BSTsearch(adm_positions,x,y)!= 1){
		return NULL;
	}
	// cancella la tessera dalle posizioni ammesse
	adm_positions = BSTdelete(adm_positions,x,y);	   
	rbnode *q = (rbnode*) malloc(sizeof(rbnode));
	rbnode *r = tree->root;
	rbnode *s = tree->nil;

	if(!q) { 
		fprintf(stderr,"Errore di allocazione C\n");
        	exit(-4);
	}
	
	q->x = x;
	q->y = y;

	q->left = q->right = tree->nil;
	q->c = red;
	
	while(r != tree->nil) {                /* Controllo dove va inserito il nuovo nodo */
		
		s = r;
		if (is_lower_than(x,y, r->x, r->y) < 0){
		   r = r->left;
        }
		else{
			if (is_lower_than(x,y, r->x, r->y) == 0)
				return NULL;
             r = r->right;
        }                  
      }
	   q->up = s;
  

	if(s == tree->nil)
		tree->root = q;
	else{
		
       if(is_lower_than(x,y, s->x, s->y) < 0)
           s->left = q;
		else{
			if (is_lower_than(x,y, s->x, s->y) == 0)
				return NULL;
   	       s->right = q;
		}
	}
	// aumenta il numero nodi presenti
	tree->count=tree->count +1;
	// ottiene le posizioni ammissibili del nuovo nodo
	int* positions = get_admitted_positions(x,y);
	int collisions = 0;
	for(size_t i = 0; i < 7; i = i+2 )
	{
		rbnode *rnode = search(tree,positions[i],positions[i+1]);
		if (rnode){
			collisions = collisions +1;
		}else{
			adm_positions = BSTinsert( adm_positions, positions[i],positions[i+1] );
		}
	}
	
	/// caricamento variabili di utilità
	if (x<0 && x< tree->xdown) tree->xdown = x;
	if (x>0 && x> tree->xup) tree->xup = x;
	if (y<0 && y< tree->ydown) tree->ydown = y;
	if (y>0 && y> tree->yup) tree->yup = y;
	// generazione del perimetro e dell'ordine
	tree->ordine = 1+ MAX(tree->xup - tree->xdown,tree->yup - tree->ydown);
	tree->perimetro= (tree->perimetro+4) -(collisions*2);
	return q;
}