Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3.  
  4. typedef int Dado;
  5.  
  6. enum Posicao {DIR, ESQ};
  7.  
  8. class Noh {
  9.     friend class ABB;
  10.     public:
  11.         Noh (Dado d = Dado());
  12.         ~Noh();
  13.         // Verifica se um nó é filho da direita do pai.
  14.         bool FilhoDaDireita();
  15.         // Retorna a menor chave de uma sub-árvore.
  16.         Dado MenorRecursivo();
  17.         // Verifica se existe sucessor e retorna o valor sucessor, se existir.
  18.         bool Sucessor(Dado* ptResultado);
  19.     protected:
  20.         Dado valor;
  21.         Noh* esq;
  22.         Noh* dir;
  23.         Noh* pai;
  24. };
  25.  
  26. class ABB {
  27.     public:
  28.         ABB() { raiz = NULL; }
  29.         ~ABB();
  30.         // Retorna um ponteiro para o nó de uma chave, ou NULL.
  31.         Noh* Busca(Dado d);
  32.         // Escreve uma árvore nível a nível.
  33.         void EscreverNivelANivel(std::ostream& saida);
  34.         // Insere um dado na árvore.
  35.         void Inserir(Dado d);
  36.         // Verifica se um dado tem sucessor e o retorna.
  37.         bool Sucessor(Dado d, Dado* ptResultado);
  38.     protected:
  39.         Noh* raiz;
  40. };
  41.  
  42. using namespace std;
  43. // === Classe Noh ==============================================================
  44. Noh::Noh(Dado d) // d tem valor default
  45.     : valor(d), esq(NULL), dir(NULL), pai(NULL) {
  46. }
  47.  
  48. Noh::~Noh() {
  49.     // Destrutor recursivo!
  50.     delete esq;
  51.     delete dir;
  52. }
  53.  
  54. bool Noh::FilhoDaDireita() {
  55.     // -----------------------------------implementar  (Certo ?)
  56.     //~ if(this->pai == NULL){
  57.         //~ return false;
  58.     //~ }else
  59.         return (this == pai->dir);
  60. }
  61.  
  62. Dado Noh::MenorRecursivo() {
  63.     //---------------------------------------------implementar    (Certo ?)
  64.     Noh* raizSubArvore = this;
  65.     Dado menor = raizSubArvore->valor;
  66.     if(raizSubArvore->esq != NULL){
  67.         menor = raizSubArvore->esq->MenorRecursivo();
  68.     }
  69.     return menor;
  70. }
  71.  
  72. bool Noh::Sucessor(Dado* ptResultado) {
  73.     // ----------------------------------------------------implementar
  74.     if(dir != NULL){
  75.         *ptResultado = dir->MenorRecursivo();
  76.         return true;
  77.     }
  78.     if(pai != NULL){
  79.         Noh* iter = this;
  80.         while(iter->pai != NULL && iter->FilhoDaDireita()){
  81.             iter = iter->pai;
  82.         }
  83.         if(iter->pai == NULL){
  84.             return false;
  85.         }else{
  86.             iter = iter->pai;
  87.             *ptResultado = iter->valor;
  88.             return true;
  89.         }
  90.     }else{
  91.         return false;
  92.     }
  93. }
  94.  
  95. // === Classe ABB ==============================================================
  96. ABB::~ABB(){
  97.     delete raiz;
  98. }
  99.  
  100. void ABB::Inserir(Dado d) {
  101.     Noh* novo = new Noh(d);
  102.     if (raiz == NULL) {
  103.         raiz = novo;
  104.     } else {
  105.         Posicao posInsercao;
  106.         Noh* atual = raiz;
  107.         Noh* anterior;
  108.         while (atual != NULL) {
  109.             anterior = atual;
  110.             if (atual->valor > d) {
  111.                 atual = atual->esq;
  112.                 posInsercao = ESQ;
  113.             } else {
  114.                 atual = atual->dir;
  115.                 posInsercao = DIR;
  116.             }
  117.         }
  118.         novo->pai = anterior;
  119.         if (posInsercao == DIR) {
  120.             anterior->dir = novo;
  121.         } else {
  122.             anterior->esq = novo;
  123.         }
  124.     }
  125. }
  126.  
  127. bool ABB::Sucessor(Dado d, Dado* ptResultado) {
  128.     return Busca(d)->Sucessor(ptResultado);
  129. }
  130.  
  131. Noh* ABB::Busca(Dado d) {
  132.     Noh* atual = raiz;
  133.     while (atual != NULL) {
  134.         if (atual->valor == d) {
  135.             return atual;
  136.         } else if (atual->valor > d) {
  137.             atual = atual->esq;
  138.         } else {
  139.             atual = atual->dir;
  140.         }
  141.     }
  142.     return atual;
  143. }
  144.  
  145. void ABB::EscreverNivelANivel(ostream& saida) {
  146.     queue<Noh*> filhos;
  147.     Noh aux;
  148.     Noh* fimDeNivel = &aux; // marcador especial para fim de nivel
  149.     filhos.push(raiz);
  150.     filhos.push(fimDeNivel);
  151.     while (not filhos.empty()) {
  152.         Noh* ptNoh = filhos.front();
  153.         filhos.pop();
  154.         if (ptNoh == fimDeNivel) {
  155.             saida << "\n";
  156.             if (not filhos.empty())
  157.                 filhos.push(fimDeNivel);
  158.         }
  159.         else {
  160.             saida << '[';
  161.             if (ptNoh != NULL) {
  162.                 saida << ptNoh->valor;
  163.                 filhos.push(ptNoh->esq);
  164.                 filhos.push(ptNoh->dir);
  165.             }
  166.             saida << ']';
  167.         }
  168.     }
  169. }
  170.  
  171. // === Programa ================================================================
  172. int main() {
  173.     ABB arvore;
  174.     Dado chave;
  175.     char operacao;
  176.     do {
  177.         cin >> operacao;
  178.         switch (operacao) {
  179.             case 'i': // Inserir
  180.                 cin >> chave;
  181.                 arvore.Inserir(chave);
  182.                 break;
  183.             case 'e': // Escrever
  184.                 arvore.EscreverNivelANivel(cout);
  185.                 break;
  186.             case 's': { // Sucessor
  187.                 cin >> chave;
  188.                 Dado sucessor;
  189.                 while (arvore.Sucessor(chave, &sucessor)) {
  190.                     cout << sucessor << ' ';
  191.                     chave = sucessor;
  192.                 }
  193.                 cout << endl;
  194.                 break;
  195.             }
  196.         }
  197.     } while (operacao != 'f');
  198.     return 0;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement