L3-016 二叉搜索树的结构 (30 分) 数组形式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiang_6/article/details/88735644

让每个数对应一个id,然后树上的结点ans[] 和左孩子结点ls[] ,右孩子结点rs[] :存这些id,

遍历记录深度

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxn = 100 + 7;

map<int, int> mp;

int ls[maxn], rs[maxn]; // 左右孩子
int f[maxn];
int ans[maxn]; // 结点值
int id = 1;
int d[maxn]; // 深度

int n;

void ins(int x, int t) {
//    cout << x << " +++ " << t << endl;
    if(x < ans[t]) {
        if(ls[t] == -1) {
            ls[t] = id;
            ans[id] = x;
            mp[x] = id;
            f[id] = t;
            id++;
            return;
        }
        else {
            ins(x, ls[t]);
        }
    }
    else {
        if(rs[t] == -1) {
            rs[t] = id;
            ans[id] = x;
            mp[x] = id;
            f[id] = t;
            id++;
            return;
        }
        else {
            ins(x, rs[t]);
        }
    }
}
void dfs(int id, int c) {
    d[id] = c;
    if(ls[id] != -1) {
        dfs(ls[id], c+1);
    }
    if(rs[id] != -1) {
        dfs(rs[id], c+1);
    }
}
int main() {
    memset(ans, -1, sizeof ans);
    memset(f, -1, sizeof f);
    memset(ls, -1, sizeof ls);
    memset(rs, -1, sizeof rs);

    scanf("%d", &n);
    int x;
    scanf("%d", &x);
    ans[1] = x; mp[x] = 1; id = 2; f[1] = 0;
    for(int i = 1; i < n; ++i) {
        scanf("%d", &x);
        ins(x, 1);
    }
//    for(int i = 1; i <= 6; ++i) {
//        cout << ans[i] << " = " << ls[i] << " = " << rs[i] << endl;
//    }
    int q; scanf("%d", &q);
    dfs(1, 1);

    string s;
    int x1;

    while(q--) {
        scanf("%d", &x);
        cin >> s;
        if(s == "is") {
            cin >> s; cin >> s;
            if(s == "root") {
                if(ans[1] == x) {
                    puts("Yes");
                }
                else {
                    puts("No");
                }
            }
            else if(s == "parent") {
                cin >> s; cin >> x1;
                x = mp[x]; x1 = mp[x1];
                if(x == 0 || x1 == 0) {
                    puts("No");
                    continue;
                }
                if(f[x1] == x) {
                    puts("Yes");
                }
                else {
                    puts("No");
                }
            }
            else if(s == "left") {
                cin >> s; cin >> s; cin >> x1;
                x = mp[x]; x1 = mp[x1];
                if(x == 0 || x1 == 0) {
                    puts("No");
                    continue;
                }
//                cout << x1 << " ++++++++ " << x << endl;
                if(ls[x1] == x) {
                    puts("Yes");
                }
                else {
                    puts("No");
                }
            }
            else if(s == "right") {
                cin >> s; cin >> s; cin >> x1;
                x = mp[x]; x1 = mp[x1];
                if(x == 0 || x1 == 0) {
                    puts("No");
                    continue;
                }
                if(x == 0 || x1 == 0) {
                    puts("No");
                    continue;
                }
//                cout << x1 << " ++++++++ " << x << endl;
                if(rs[x1] == x) {
                    puts("Yes");
                }
                else {
                    puts("No");
                }
            }
        }
        else {
            scanf("%d", &x1);
            cin >> s; cin >> s;
            if(s == "siblings") {
                x = mp[x]; x1 = mp[x1];
                if(f[x] == f[x1]) {
                    puts("Yes");
                }
                else {
                    puts("No");
                }
            }
            else {
                cin >> s; cin >> s; cin >> s;
                x = mp[x]; x1 = mp[x1];
                if(d[x] == d[x1]) {
                    puts("Yes");
                }
                else {
                    puts("No");
                }
            }
        }
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/xiang_6/article/details/88735644
今日推荐