-
Notifications
You must be signed in to change notification settings - Fork 0
/
1200-bst_operations_i.cpp
110 lines (96 loc) · 2.33 KB
/
1200-bst_operations_i.cpp
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
104
105
106
107
108
109
110
#include <bits/stdc++.h>
using namespace std;
template <class T>
struct Node {
T data;
Node<T>* left = nullptr;
Node<T>* right = nullptr;
Node(T data) : data(data) {}
~Node() {
if (left) delete left;
if (right) delete right;
}
};
void insert(Node<char>* tree, char c) {
if (tree->data < c) {
if (tree->right) {
insert(tree->right, c);
} else {
tree->right = new Node<char>(c);
}
} else {
if (tree->left) {
insert(tree->left, c);
} else {
tree->left = new Node<char>(c);
}
}
}
vector<char> traversal;
bool search(Node<char>* tree, char c) {
if (tree == nullptr) return false;
if (tree->data == c) return true;
bool search_left = search(tree->left, c);
bool search_right = search(tree->right, c);
return search_left || search_right;
}
void inorder(Node<char>* tree) {
if (tree == nullptr) return;
inorder(tree->left);
traversal.push_back(tree->data);
inorder(tree->right);
}
void preorder(Node<char>* tree) {
if (tree == nullptr) return;
traversal.push_back(tree->data);
preorder(tree->left);
preorder(tree->right);
}
void postorder(Node<char>* tree) {
if (tree == nullptr) return;
postorder(tree->left);
postorder(tree->right);
traversal.push_back(tree->data);
}
void print_traversal() {
for (int i = 0; i < (int)traversal.size(); i++) {
printf("%c%c", traversal[i], i < ((int)traversal.size() - 1) ? ' ' : '\n');
}
}
int main() {
Node<char> tree((char)0);
string line;
stringstream ss;
while (getline(cin, line)) {
ss = stringstream(line);
while (!ss.eof()) {
string first, second;
ss >> first;
if (first == "I") {
ss >> second;
if (tree.data == (char)0)
tree.data = second[0];
else
insert(&tree, second[0]);
} else if (first == "INFIXA") {
traversal.clear();
inorder(&tree);
print_traversal();
} else if (first == "PREFIXA") {
traversal.clear();
preorder(&tree);
print_traversal();
} else if (first == "POSFIXA") {
traversal.clear();
postorder(&tree);
print_traversal();
} else if (first == "P") {
ss >> second;
if (search(&tree, second[0]))
printf("%c existe\n", second[0]);
else
printf("%c nao existe\n", second[0]);
}
}
}
}