Advertisement
Guest User

UVa #122

a guest
Jun 25th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #define NUM_NODES 256
  5.  
  6. template <class T>
  7. class Tree
  8. {
  9. private:
  10.     T heap[NUM_NODES];
  11.     int hs[NUM_NODES];  // HeapStatus - 0: Não Setado; 1: Setado; 2: Não setado com filho
  12.     bool errflag;
  13.     int iCount;
  14.     int eCount;
  15. public:
  16.     Tree()
  17.     {
  18.         this->errflag = false;
  19.         this->iCount = 0;
  20.         memset(this->heap, 0, NUM_NODES);
  21.         memset(this->hs, 0, NUM_NODES);
  22.     }
  23.  
  24.     void insert(T content, char* dir);
  25.     void readTree();
  26.     bool isEmpty();
  27.     void setInvalid();
  28. };
  29.  
  30. template <class T>
  31. void Tree<T>::insert(T content, char* dir)
  32. {
  33.     int aux = 1;
  34.     for(int i = 0; dir[i] != '\0'; i++)
  35.     {
  36.         // Nó da direita, posição 2n + 1
  37.         if(dir[i] == 'R')
  38.         {
  39.             aux *= 2;
  40.             aux += 1;
  41.         }
  42.         // Nó da esquerda, posição 2n
  43.         else if(dir[i] == 'L')
  44.         {
  45.             aux *= 2;
  46.         }
  47.     }
  48.  
  49.     // Verifica se o nó inserido é filho de um nó não setado.
  50.     if(aux%2 && (aux - 1) && !this->hs[(aux - 1)/2 - 1])
  51.     {
  52.         this->hs[(aux - 1)/2 - 1] = 2;
  53.         this->iCount++;
  54.     } else if(!(aux%2) && !this->hs[aux/2 - 1])
  55.     {
  56.         this->hs[aux/2 - 1] = 2;
  57.         this->iCount++;
  58.     }
  59.  
  60.     // Se o nó não foi setado e não tem filhos setados.
  61.     if(!this->hs[aux - 1])
  62.     {
  63.         this->heap[aux - 1] = content;
  64.         this->hs[aux - 1] = 1;
  65.         this->eCount++;      
  66.     }
  67.    
  68.     // Se o nó não foi setado mas tem filho setado.
  69.     else if(this->hs[aux - 1] == 2)
  70.     {
  71.         this->heap[aux - 1] = content;
  72.         this->hs[aux - 1] = 1;
  73.         this->iCount--;
  74.         this->eCount++;
  75.     } else this->errflag = true;    // Se o nó já havia sido setado.
  76. }
  77.  
  78. template <class T>
  79. void Tree<T>::readTree()
  80. {
  81.     if(!this->errflag && !this->iCount)
  82.     {
  83.         for(int i = 0; i < 256; i++)
  84.         {
  85.             if(this->hs[i])
  86.             {
  87.                 this->eCount--;
  88.                 if(this->eCount)
  89.                     printf("%d ", (int) this->heap[i]);
  90.                 else
  91.                     printf("%d", (int) this->heap[i]);
  92.             }
  93.         }
  94.         printf("\n");
  95.     } else printf("not complete\n");
  96. }
  97.  
  98. template <class T>
  99. bool Tree<T>::isEmpty()
  100. {
  101.     return !((bool) this->eCount);
  102. }
  103.  
  104. template <class T>
  105. void Tree<T>::setInvalid()
  106. {
  107.     this->errflag = true;
  108. }
  109.  
  110. int main()
  111. {
  112.     Tree<unsigned long> *tree;
  113.     char buf[32];
  114.     unsigned long i_buf;
  115.  
  116.     while(scanf("%s", buf) > 0)
  117.     {
  118.         tree = new Tree<unsigned long>();
  119.         while(strcmp(buf, "()"))
  120.         {
  121.             i_buf = 0;
  122.             for(int i = 1; buf[i] != ','; i++)
  123.             {
  124.                 if(buf[i] == '-')
  125.                 {
  126.                     tree->setInvalid();
  127.                     break;
  128.                 }
  129.                 if((buf[i] >= '0')&&(buf[i] <= '9'))
  130.                 {
  131.                     i_buf = i_buf*10 + (buf[i] - '0');
  132.                 }
  133.             }
  134.             if(!i_buf) tree->setInvalid();
  135.             else tree->insert(i_buf, buf);
  136.             scanf("%s", buf);
  137.         }
  138.         if(!tree->isEmpty()) tree->readTree();
  139.         else printf("not complete\n");
  140.         delete tree;
  141.     }
  142.  
  143.     return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement