|
| 1 | +#include <iostream> |
| 2 | +#include <string> |
| 3 | +#include <vector> |
| 4 | +#include <memory.h> |
| 5 | + |
| 6 | +using namespace std; |
| 7 | + |
| 8 | +struct node { |
| 9 | + node *left, *right; |
| 10 | + char value; |
| 11 | + static node *sentinel; |
| 12 | + |
| 13 | + node() { |
| 14 | + memset(this, 0, sizeof *this); |
| 15 | + } |
| 16 | + |
| 17 | + node(char value) { |
| 18 | + left = right = sentinel; |
| 19 | + this->value = value; |
| 20 | + } |
| 21 | +}; |
| 22 | + |
| 23 | +node *node::sentinel = new node; |
| 24 | + |
| 25 | +vector<char> trav; |
| 26 | + |
| 27 | +node * insert(node *cur, char &value) { |
| 28 | + if(cur == node::sentinel) return new node(value); |
| 29 | + if(cur->value == value) return cur; |
| 30 | + cur->value > value ? cur->left = insert(cur->left, value) : cur->right = insert(cur->right, value); |
| 31 | + return cur; |
| 32 | +} |
| 33 | + |
| 34 | +bool search(node *cur, char value) { |
| 35 | + if(cur == node::sentinel) return false; |
| 36 | + if(cur->value == value) return true; |
| 37 | + return cur->value > value ? search(cur->left, value) : search(cur->right, value); |
| 38 | +} |
| 39 | + |
| 40 | +void pre_order(node *cur) { |
| 41 | + if(cur == node::sentinel) return; |
| 42 | + trav.push_back(cur->value); |
| 43 | + pre_order(cur->left); |
| 44 | + pre_order(cur->right); |
| 45 | +} |
| 46 | + |
| 47 | +void in_order(node *cur) { |
| 48 | + if(cur == node::sentinel) return; |
| 49 | + in_order(cur->left); |
| 50 | + trav.push_back(cur->value); |
| 51 | + in_order(cur->right); |
| 52 | +} |
| 53 | + |
| 54 | +void post_order(node *cur) { |
| 55 | + if(cur == node::sentinel) return; |
| 56 | + post_order(cur->left); |
| 57 | + post_order(cur->right); |
| 58 | + trav.push_back(cur->value); |
| 59 | +} |
| 60 | + |
| 61 | +void print() { |
| 62 | + for(int i = 0; i < (int)trav.size(); ++i) |
| 63 | + cout << (i ? " " : "") << trav[i]; |
| 64 | + cout << endl; |
| 65 | +} |
| 66 | + |
| 67 | +void free_bst(node *cur) { |
| 68 | + if(cur == node::sentinel) |
| 69 | + return; |
| 70 | + |
| 71 | + free_bst(cur->left); |
| 72 | + free_bst(cur->right); |
| 73 | + |
| 74 | + delete cur; |
| 75 | +} |
| 76 | + |
| 77 | +int main() { |
| 78 | + node *root = NULL; |
| 79 | + |
| 80 | + string s; |
| 81 | + char c; |
| 82 | + while(cin >> s) { |
| 83 | + trav.clear(); |
| 84 | + |
| 85 | + if(s == "PREFIXA") { |
| 86 | + pre_order(root); |
| 87 | + print(); |
| 88 | + } else if(s == "INFIXA") { |
| 89 | + in_order(root); |
| 90 | + print(); |
| 91 | + } else if(s == "POSFIXA") { |
| 92 | + post_order(root); |
| 93 | + print(); |
| 94 | + } else if(s == "I") { |
| 95 | + cin >> c; |
| 96 | + if(root == NULL) |
| 97 | + root = new node(c); |
| 98 | + else |
| 99 | + insert(root, c); |
| 100 | + } else { |
| 101 | + cin >> c; |
| 102 | + if(search(root, c)) |
| 103 | + cout << c << " existe" << endl; |
| 104 | + else |
| 105 | + cout << c << " nao existe" << endl; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + free_bst(root); |
| 110 | + |
| 111 | + return 0; |
| 112 | +} |
| 113 | + |
0 commit comments