コード例 #1
0
/*!
* Remove um determinado dado da árvore
*
* @param dado Dado a ser removido da árvore
* @param arv Árvore a qual será iniciada a busca pelo dado a ser removido
*/
    NoAVL<T>* remover(NoAVL<T>* arv, const T& dado) {
        NoAVL<T>* temporario;
        NoAVL<T>* filho;
        if (arv == NULL) {
            return arv;
        } else if (dado < *(arv->getDado())) {
                arv->esquerda = this->remover(arv->getEsquerda(), dado);
                return balancear(arv);
            } else if (dado > *(arv->getDado())) {
                    arv->direita = this->remover(arv->getDireita(), dado);
                    return balancear(arv);
                } else if (arv->getDireita() != NULL &&
                            arv->getEsquerda() != NULL) {
                        temporario = this->minimo(arv->getDireita());
                        arv->dado = new T(static_cast<T const&>(*(temporario->
                                                                   getDado())));
                        arv->direita = remover(arv->getDireita(),
                                                *(temporario->getDado()));
                        return balancear(arv);
                    } else if (arv->getDireita() != NULL) {
                        filho = arv->getDireita();
                        return balancear(filho);
                        } else if (arv->esquerda != NULL) {
                                filho = arv->getEsquerda();
                                return balancear(filho);
                            }
        delete arv;
        return NULL;
    }
コード例 #2
0
 NoAVL<T>* inserir(const T& dado, NoAVL<T>* arv) {
   NoAVL<T>* aux;
   if (dado < *arv->getDado()) {
     if (arv->getEsquerda() == NULL) {
       NoAVL<T>* novo = new NoAVL<T>(dado);
       if (novo == NULL)
         throw 20;
       arv->esquerda = novo;
     }
     else {
       aux = arv->inserir(dado, arv->getEsquerda());
       arv = verificarBalanceamento(arv, dado);
       if (arv->getDado() != aux->getDado()) {
         arv->esquerda = aux;
       }
     }
   }
   else {
     if (dado == *arv->getDado())
       throw 20;
     if (arv->getDireita() == NULL) {
       NoAVL<T>* novo = new NoAVL<T>(dado);
       if (novo == NULL)
         throw 20;
       arv->direita = novo;
     }
     else {
       aux = arv->inserir(dado, arv->getDireita());
       arv = verificarBalanceamento(arv, dado);
       if (arv->getDado() != aux->getDado()) {
         arv->direita = aux;
       }
     }
   }
   atualizarAltura(arv);
   return arv;
 }