Repository navigation
Expand file tree
/
Copy path1200-full.cpp
More file actions
103 lines (88 loc) · 2.35 KB
/
Copy path1200-full.cpp
File metadata and controls
103 lines (88 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#define VARTYPE string
using namespace std;
struct nodo {
nodo *esquerda;
VARTYPE informacao;
nodo *direita;
nodo (VARTYPE info):
informacao(info),
esquerda(0),
direita (0) {}
};
string toshow = "";
struct nodo *insere (nodo *tree, VARTYPE informacao) {
if (tree == NULL){
tree = new nodo (informacao);
} else if (informacao < tree->informacao){
tree->esquerda = insere(tree->esquerda, informacao);
} else if ( informacao > tree->informacao ){
tree->direita = insere(tree->direita, informacao);
}
return tree;
};
bool busca(nodo *tree, VARTYPE k) {
if (tree == NULL) {
return 0;
} else if (tree->informacao == k) {
return 1;
}
if (tree->informacao > k) {
return busca (tree->esquerda, k);
} else {
return busca (tree->direita, k);
}
}
void infixa (nodo *tree) {
if (tree != NULL) {
infixa(tree->esquerda);
toshow += " " + tree->informacao;
infixa(tree->direita);
}
}
void posfixa(nodo *tree) {
if (tree != NULL) {
posfixa(tree->esquerda);
posfixa(tree->direita);
toshow += " " + tree->informacao;
}
}
void prefixa(nodo *tree) {
if (tree != NULL) {
toshow += " " + tree->informacao;
prefixa(tree->esquerda);
prefixa(tree->direita);
}
}
int main(){
string line;
nodo *tree = 0;
while (cin >> line) {
if (line == "INFIXA") {
infixa(tree);
cout << toshow.substr(1) << endl;
toshow = "";
} else if (line == "POSFIXA") {
posfixa(tree);
cout << toshow.substr(1) << endl;
toshow = "";
} else if (line == "PREFIXA") {
prefixa(tree);
cout << toshow.substr(1) << endl;
toshow = "";
} else {
VARTYPE ch;
cin >> ch;
if (line == "I") {
tree = insere(tree, ch);
} else if (line == "P") {
if (busca(tree, ch)) {
cout << ch << " existe\n";
} else {
cout << ch << " nao existe\n";
}
}
}
}
return 0;
}