Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int MAX = 550;
- struct node {
- int val;
- int esq, dir;
- node () {
- esq = -1;
- dir = -1;
- }
- };
- int n, a[MAX];
- vector<node> st;
- void insert(int no, int val) {
- if (val < st[no].val) {
- if (st[no].esq == -1) {
- st.push_back(node());
- st[no].esq = st.size()-1;
- st[st[no].esq].val = val;
- } else {
- insert(st[no].esq, val);
- }
- } else {
- if (st[no].dir == -1) {
- st.push_back(node());
- st[no].dir = st.size()-1;
- st[st[no].dir].val = val;
- } else {
- insert(st[no].dir, val);
- }
- }
- }
- void pre(int no) {
- cout << ' ' << st[no].val;
- if (st[no].esq != -1) pre(st[no].esq);
- if (st[no].dir != -1) pre(st[no].dir);
- }
- void in(int no) {
- if (st[no].esq != -1) in(st[no].esq);
- cout << ' ' << st[no].val;
- if (st[no].dir != -1) in(st[no].dir);
- }
- void post(int no) {
- if (st[no].esq != -1) post(st[no].esq);
- if (st[no].dir != -1) post(st[no].dir);
- cout << ' ' << st[no].val;
- }
- int main() {
- cin.tie(0)->sync_with_stdio(0);
- int tc; cin >> tc;
- int C = 1;
- while (tc--) {
- cout << "Case " << C++ << ":\n";
- cin >> n;
- for (int i = 0; i < n; ++i) {
- cin >> a[i];
- }
- st.clear();
- st.push_back(node());
- st.back().val = a[0];
- for (int i = 1; i < n; ++i) {
- insert(0, a[i]);
- }
- cout << "Pre.:";
- pre(0);
- cout << '\n';
- cout << "In..:";
- in(0);
- cout << '\n';
- cout << "Post:";
- post(0);
- cout << '\n';
- cout << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment