Пример #1
0
pRBNode BSTdelete(pRBNode root, int val, char *col) {
    pRBNode tmp;
    if(root == NULL)
        return root;
    else if(val < root->val)
        root->left = BSTdelete(root->left, val, col);
    else if(val > root->val)
        root->right = BSTdelete(root->right, val, col);
    else if(root->left && root->right) {  // node with two children
        tmp = findMin(root->right);

        *col = tmp->color;  // remember the original color of successor

        root->val = tmp->val;
        root->right = BSTdelete(root->right, root->val, col);
    } else {  // node with 0 or 1 child
        tmp = root;

        *col = tmp->color;  // remember the original color of successor 

        if(root->left == NULL)
            root = root->right;
        else if(root->right == NULL)
            root = root->left;
        free(tmp);
    }
    return root;
}
Пример #2
0
void inserisci_a_caso(rbtree* p){
	int x,y;
	x = BSTGetRootX(adm_positions);
	y = BSTGetRootY(adm_positions);
	rbinsert(p,x,y);
    adm_positions = BSTdelete(adm_positions,x,y);
}
Пример #3
0
void deleteRBNode(ppRBNode root, int val) {
    // If z is the node to be deleted, then the original color of z's successor is: original_color
    char original_color;  

    // 1. Perform standard binary-search-tree deletion
    pRBNode x = BSTdelete(*root, val, &original_color);
    if(x == NULL)
        return ;

    // 2. Fix the RB-tree if necessary
    if(original_color == 'B')
        deleteFixup(root, x);
}
Пример #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;
}