char * heap_remove(heap * h) { int i, filho_maior; elemento * aux; char * ret; if (!h || h->tamanho_actual <= 0) return NULL; ret = h->elementos[RAIZ]->valor; free(h->elementos[RAIZ]); h->tamanho_actual--; h->elementos[RAIZ] = h->elementos[h->tamanho_actual]; h->elementos[h->tamanho_actual] = NULL; i = RAIZ; while(FILHO_ESQ(i) < h->tamanho_actual) { filho_maior = FILHO_ESQ(i); if (FILHO_DIR(i) < h->tamanho_actual && menor_que(h->elementos[FILHO_DIR(i)], h->elementos[FILHO_ESQ(i)])) filho_maior = FILHO_DIR(i); if (menor_que(h->elementos[filho_maior], h->elementos[i])) { aux = h->elementos[filho_maior]; h->elementos[filho_maior] = h->elementos[i]; h->elementos[i] = aux; i = filho_maior; } else break; } return ret; }
int heap_insere(heap * h, const char * texto, int prioridade) { elemento * aux, * elem; int i; /* se heap esta' cheia, nao insere elemento */ if (h->tamanho_atual >= h->tamanho_maximo) return 0; elem = elemento_cria(prioridade, texto); if (!elem) return 0; /* coloca elemento no fim da heap */ i = h->tamanho_atual; h->elementos[i] = elem; h->tamanho_atual++; /* enquanto elemento for mais prioritario do que o respetivo pai, troca-os */ while (i != RAIZ && menor_que(h->elementos[i], h->elementos[PAI(i)])) { aux = h->elementos[PAI(i)]; h->elementos[PAI(i)] = h->elementos[i]; h->elementos[i] = aux; i = PAI(i); } return 1; }
char * heap_remove(heap * h) { int i, filho_maior; elemento * aux; char * ret; /* se heap estiver vazia, nao remove elemento */ if (!h || h->tamanho_atual <= 0) return NULL; ret = h->elementos[RAIZ]->valor; free(h->elementos[RAIZ]); /* coloca ultimo elemento da heap na raiz */ h->tamanho_atual--; h->elementos[RAIZ] = h->elementos[h->tamanho_atual]; h->elementos[h->tamanho_atual] = NULL; i = RAIZ; /* enquanto nao chegar 'a base da heap */ while(FILHO_ESQ(i) < h->tamanho_atual) { filho_maior = FILHO_ESQ(i); /* verifica se existe filho 'a direita e se este e' mais prioritario do que 'a esquerda */ if (FILHO_DIR(i) < h->tamanho_atual && menor_que(h->elementos[FILHO_DIR(i)], h->elementos[FILHO_ESQ(i)])) filho_maior = FILHO_DIR(i); /* enquanto elemento for mais prioritario do que o respetivo pai, troca-os */ if (menor_que(h->elementos[filho_maior], h->elementos[i])) { aux = h->elementos[filho_maior]; h->elementos[filho_maior] = h->elementos[i]; h->elementos[i] = aux; i = filho_maior; } else break; } return ret; }
int heap_insere(heap * h, char * texto, int prioridade) { elemento * aux, * elem; int i; if (h->tamanho_actual >= h->tamanho_maximo) return 0; elem = elemento_cria(prioridade, texto); i = h->tamanho_actual; h->elementos[i] = elem; h->tamanho_actual++; while (i != RAIZ && menor_que(h->elementos[i], h->elementos[PAI(i)])) { aux = h->elementos[PAI(i)]; h->elementos[PAI(i)] = h->elementos[i]; h->elementos[i] = aux; i = PAI(i); } return 1; }
bool racional_t::operator<(racional_t& v) { return menor_que(v); }
bool racional_t::menorigual_que(racional_t rac) { return (igual_que(rac) || menor_que(rac)); }
bool racional_t::mayor_que(racional_t rac) { return (not(menor_que(rac))); }
bool real_t::operator<(REAL v) { return menor_que(v); }
bool real_t::operator<(real_t& v) { return menor_que(v.mostrar()); }
bool real_t::menorigual_que(const REAL val) { return (igual_que(val) || menor_que(val)); }
bool complejo_t::operator<(complejo_t& v) { return menor_que(v); }
bool complejo_t::menorigual_que(complejo_t c1) { return (igual_que(c1) || menor_que(c1)); }