Baixe o app para aproveitar ainda mais
Prévia do material em texto
1 #include <iostream> 2 #include <sstream> 3 #include <cstdio> 4 #include <cctype> 5 6 using namespace std; 7 8 bool existe, enter = false; 9 10 struct nodo { 11 nodo *esquerdo; 12 string informacao; 13 nodo *direito; 14 nodo (string esclarecer): 15 informacao (esclarecer), 16 esquerdo (0), 17 direito (0) {} 18 }; 19 20 struct nodo *adiciona ( nodo *tree, string informacao ){ 21 if (tree == NULL) { 22 tree = new nodo (informacao); 23 } else { 24 int i = 0; 25 while (i > -1) { 26 if (informacao[i] < tree->informacao[i]) { 27 tree->esquerdo = adiciona(tree->esquerdo, informacao); 28 break; 29 } 30 else if (informacao[i] > tree->informacao[i]) { 31 tree->direito = adiciona(tree->direito, informacao); 32 break; 33 } 34 else i++; 35 } 36 } 37 return tree; 38 }; 39 40 struct nodo *nodobusca (nodo *tree, string informacao) { 41 if (tree != NULL) { 42 if (informacao == tree->informacao) { 43 existe = true; 44 return tree; 45 } 46 else { 47 int i = 0; 48 while (i > -1) { 49 if (informacao[i] < tree->informacao[i]) { 50 tree->esquerdo = nodobusca(tree->esquerdo, informacao); 51 break; 52 } 53 else if (informacao[i] > tree->informacao[i]){ 54 tree->direito = nodobusca(tree->direito, informacao); 55 break; 56 } 57 else i++; 58 } 59 } 60 } 61 return tree; 62 }; 63 64 bool busca (nodo *tree, string informacao){ 65 if (tree != NULL) { 66 tree = nodobusca(tree, informacao); 67 if (existe || informacao == tree->informacao) return true; 68 else return false; 69 } else return false; 70 } 71 72 void mostrar (nodo *tree){ 73 if (tree != NULL) { 74 mostrar(tree->esquerdo); 75 cout << tree->informacao << endl; 76 mostrar(tree->direito); 77 } 78 } 79 80 int main(){ 81 nodo *raiz = 0; 82 string linha, leitura = "", palavra = ""; 83 char letra; 84 existe = false; 85 86 while (getline(cin, linha)){ 87 stringstream streamlinha(linha); 88 while (streamlinha >> leitura){ 89 int i = 0; 90 while (i <= leitura.length()){ 91 if (isalpha(leitura[i])){ 92 letra = tolower(leitura[i]); 93 palavra += letra; 94 } 95 else if (palavra.length() > 0 || (palavra.length() == i && palavra.length() > 0)) { 96 if (!busca(raiz, palavra)){ 97 raiz = adiciona(raiz, palavra); 98 palavra = ""; 99 } else { 100 palavra = ""; 101 existe = false; 102 } 103 } 104 i++; 105 } 106 } 107 } 108 mostrar(raiz); 109 return 0; 110 }
Compartilhar