-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1201.cpp
More file actions
175 lines (138 loc) · 3.54 KB
/
1201.cpp
File metadata and controls
175 lines (138 loc) · 3.54 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <iostream>
using namespace std;
struct node {
int valor;
node *left, *right;
};
node* create_node(int num) {
node *root = new node;
root->valor = num;
root->left = nullptr;
root->right = nullptr;
return root;
}
void insert_node(node*& root, node* new_node) {
if (!root) {
root = new_node;
return;
}
if (new_node->valor > root->valor) {
insert_node(root->right, new_node);
} else {
insert_node(root->left, new_node);
}
}
node *procurar(node *root, const int &num){
if(!root || root->valor == num){
return root;
} else if (root->valor > num){
return procurar(root->left, num);
} else{
return procurar(root->right, num);
}
return nullptr;
}
node* find_node(node* root, int value, node*& parent) {
node* aux = root;
parent = nullptr;
while (aux) {
if (aux->valor == value) return aux;
parent = aux;
if(value < aux->valor) aux = aux->left;
else aux = aux->right;
}
return nullptr;
}
void delete_node(node*& root, int deleted_value) {
if(!root) return;
node* pai;
node* aux_pai;
node* aux_node;
node* n = find_node(root, deleted_value, pai);
if (!n) return;
if (!n->left || !n->right) {
if (!n->left) aux_node = n->right;
else aux_node = n->left;
} else {
aux_pai = n;
aux_node = n->left;
while (aux_node->right) {
aux_pai = aux_node;
aux_node = aux_node->right;
}
if (aux_pai != n) {
aux_pai->right = aux_node->left;
aux_node->left = n->left;
}
aux_node->right = n->right;
}
if (!pai) {
root = aux_node;
delete n;
return;
}
if (deleted_value < pai->valor) pai->left = aux_node;
else pai->right = aux_node;
delete n;
}
void pre_ordem(node *root, bool &isFirst){
if (!root) return;
if (!isFirst) cout << " ";
cout << root->valor;
isFirst = false;
pre_ordem(root->left, isFirst);
pre_ordem(root->right, isFirst);
}
void in_ordem(node *root, bool &isFirst) {
if (!root) return;
in_ordem(root->left, isFirst);
if (!isFirst) cout << " ";
cout << root->valor;
isFirst = false;
in_ordem(root->right, isFirst);
}
void pos_ordem(node *root, bool &isFirst){
if (!root) return;
pos_ordem(root->left, isFirst);
pos_ordem(root->right, isFirst);
if (!isFirst) cout << " ";
cout << root->valor;
isFirst = false;
}
int main() {
string entrada;
int num;
node *root = nullptr;
while(cin >> entrada) {
if(entrada == "I"){
cin >> num;
node* new_node = create_node(num);
insert_node(root, new_node);
} else if (entrada == "P"){
cin >> num;
if(procurar(root, num)){
cout << num << " existe\n";
} else {
cout << num << " nao existe\n";
}
} else if (entrada == "R"){
cin >> num;
delete_node(root, num);
} else if (entrada == "INFIXA"){
bool first = true;
in_ordem(root, first);
cout << endl;
} else if (entrada == "PREFIXA"){
bool first = true;
pre_ordem(root, first);
cout << endl;
} else if (entrada == "POSFIXA"){
bool first = true;
pos_ordem(root, first);
cout << endl;
} else {
break;
}
}
return 0;
}