Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class No
  5. {
  6. private:
  7.     No *esq, *dir;
  8.     int chave;
  9.  
  10. public:
  11.     No(int chave)
  12.     {
  13.         this->chave = chave;
  14.         esq = NULL;
  15.         dir = NULL;
  16.     }
  17.  
  18.     int getChave()
  19.     {
  20.         return chave;
  21.     }
  22.    
  23.     // funções getters e setters
  24.  
  25.     No* getEsq()
  26.     {
  27.         return esq;
  28.     }
  29.  
  30.     No* getDir()
  31.     {
  32.         return dir;
  33.     }
  34.  
  35.     void setEsq(No *no)
  36.     {
  37.         esq = no;
  38.     }
  39.  
  40.     void setDir(No *no)
  41.     {
  42.         dir = no;
  43.     }
  44. };
  45.  
  46. class Arvore
  47. {
  48. private:
  49.     No *raiz;
  50.  
  51. public:
  52.     Arvore()
  53.     {
  54.         raiz = NULL;
  55.     }
  56.  
  57.     void inserir(int chave)
  58.     {
  59.         if(raiz == NULL) // verifica se a árvore está vazia
  60.             raiz = new No(chave); // cria um novo nó
  61.         else
  62.             inserirAux(raiz, chave);
  63.     }
  64.  
  65.     void inserirAux(No *no, int chave)
  66.     {
  67.         // se for menor, então insere à esquerda
  68.         if(chave < no->getChave())
  69.         {
  70.             // verifica se a esquerda é nulo
  71.             if(no->getEsq() == NULL)
  72.             {
  73.                 No *novo_no = new No(chave);
  74.                 no->setEsq(novo_no); // seta o novo_no à esquerda
  75.             }
  76.             else
  77.             {
  78.                 // senão, continua percorrendo recursivamente
  79.                 inserirAux(no->getEsq(), chave);
  80.             }
  81.         }
  82.         // se for maior, então insere à direita
  83.         else if(chave > no->getChave())
  84.         {
  85.             // verifica se a direita é nulo
  86.             if(no->getDir() == NULL)
  87.             {
  88.                 No *novo_no = new No(chave);
  89.                 no->setDir(novo_no); // seta o novo_no à direita
  90.             }
  91.             else
  92.             {
  93.                 // senão, continua percorrendo recursivamente
  94.                 inserirAux(no->getDir(), chave);
  95.             }
  96.         }
  97.         // se for igual, não vai inserir
  98.         // não pode existir 2 chaves iguais
  99.     }
  100.  
  101.     No* getRaiz()
  102.     {
  103.         return raiz;
  104.     }
  105.  
  106.     void emOrdem(No* no)
  107.     {
  108.         if(no != NULL)
  109.         {
  110.             emOrdem(no->getEsq());
  111.             cout << no->getChave() << " ";
  112.             emOrdem(no->getDir());
  113.         }
  114.     }
  115. };
  116.  
  117. int main(int argc, char *argv[])
  118. {
  119.     Arvore arv;
  120.     int vl=0;
  121.    
  122.  
  123.     // insere as chaves
  124.     while(1){
  125.     cin>>vl;
  126.     arv.inserir(vl);
  127.     cout << "Percorrendo em ordem...\n";
  128.     arv.emOrdem(arv.getRaiz());
  129.     }
  130.  
  131.     // percorre em ordem iniciando da raiz
  132.     cout << "Percorrendo em ordem...\n";
  133.     arv.emOrdem(arv.getRaiz());
  134.  
  135.     return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement