int removerArvore(Arvore **pRaiz, int matricula){ Arvore *aux; if(*pRaiz == NULL) return 0; if(matricula > (*pRaiz)->cliente->matricula ) removerArvore( &((*pRaiz) -> dir), matricula); else if(matricula < (*pRaiz) -> cliente -> matricula ) removerArvore( &((*pRaiz)-> esq), matricula); else{ if( (*pRaiz) -> esq == NULL){ aux = *pRaiz; *pRaiz = (*pRaiz)->dir; free(aux); } else if( (*pRaiz)-> dir == NULL){ aux = *pRaiz; *pRaiz = (*pRaiz)->esq; free(aux); } else if( (*pRaiz) -> dir != NULL) (*pRaiz) -> dir = sucessor((*pRaiz), (*pRaiz) -> dir); else (*pRaiz) -> dir = antecessor((*pRaiz), (*pRaiz) -> esq); } balancearArvoreAVL(pRaiz); return 1; }
Arvore *sucessor(Arvore *pRaiz, Arvore *sus){ Arvore *aux; if(sus -> esq == NULL){ pRaiz->cliente -> matricula = sus -> cliente -> matricula ; aux = sus; sus = sus -> dir; free(aux); } sus -> esq = sucessor(pRaiz, sus -> esq); return sus; }
/* * Remove um no da arvore */ char *rn_remove(arv *t, char *chave) { no *p = malloc(sizeof(no*)); no *y = malloc(sizeof(no*)); no *x = malloc(sizeof(no*)); p = busca_remove(t->raiz, chave); if(!p) return NULL; if((p->esq = t->nil) || (p->dir == t->nil)) y = p; else y = sucessor(p); if(y->esq != t->nil) x = y->esq; else x = y->dir; x->pai = y->pai; if(y->pai == t->nil){ t->raiz = x; } else if(y == y->pai->esq) y->pai->esq = x; else y->pai->dir = x; if(y != p){ strcpy(p->chave, y->chave); strcpy(p->conteudo, y->conteudo); p->pai = y->pai; p->dir = y->dir; p->esq = y->esq; } if(y->cor == BLACK) rn_remove_fixup(t, x); return p->conteudo; }
struct _sthread *rbt_remove(struct rbt *tree, int vruntime) { struct node *delete_node = rbt_find(tree, vruntime); struct node *y; struct node *x; struct _sthread *thread; if(delete_node == NULL){ printf("Node with vruntime = %d doesn't exist\n", vruntime); return NULL; } if (delete_node->queue->first->next != NULL) return queue_remove(delete_node->queue); if(delete_node->left == tree->nil || delete_node->right == tree->nil) y = delete_node; else { y = sucessor(tree, delete_node); if(!(y->color == RED || !black_leef(tree, y))) y = predecessor(tree, delete_node); } if (y->left != tree->nil) x = y->left; else x = y->right; x->parent = y->parent; if (y->parent == tree->nil) tree->root = x; else { if (y == y->parent->left) y->parent->left = x; else y->parent->right = x; } if(y != delete_node){ substitute(tree, delete_node, y); if (isRoot(tree, y)) tree->root = y; } if (y == tree->first){ if (y->parent == tree->nil && y->right != tree->nil) tree->first = minimum(tree, y->right); else tree->first = x->parent; } if(y->color == BLACK) delete_fixup(tree, x); treeRoot(tree); lower(tree); thread = queue_remove(y->queue); destroy_node(y); return thread; }