8.6

#include<string.h>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e3 + 10;
const int inf = 0x3f3f3f3f;
const int Base = 131;
const ll INF = 1ll << 62;
//const double PI = acos(-1);
const double eps = 1e-7;
const int mod = 1e9 + 9;
#define mem(a,b) memset(a,b,sizeof(a))
#define speed {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); }
int Heap[maxn];
int n, m;
void buildheap(int deep) {//建立小顶堆
    int son = deep;
    int fa = deep >> 1;
    while (fa >= 1) {
        if (Heap[son] < Heap[fa]) {
            swap(Heap[fa], Heap[son]);
            son = fa;
            fa =fa>>1;
        }else {
            break;
        }
    }
}
int Search(int x) {//查询值的下标
    for (int i = 1; i <= n; i++) {
        if (Heap[i] == x) {
            return i;
        }
    }
}

int main() {

    scanf("%d %d",&n,&m);
    for (int i = 1; i <= n; i++) {
        scanf("%d",&Heap[i]);
        buildheap(i);
    }
    getchar();
    while(m--){
        int a, b;
        char s[50];
        scanf("%d %s",&a,s);
        if(s[0]=='a'){//判断是否是兄弟节点
            scanf("%d",&b);
            scanf("%s",s);
            scanf("%s",s);
            int p=Search(a)>>1;
            if(Heap[p<<1]==b||Heap[p<<1|1]==b)puts("T");
            else puts("F");
        }else{
            scanf("%s",s);
            scanf("%s",s);
            if(s[0]=='r'){//判断是否是根节点
                if(a==Heap[1])puts("T");
                else puts("F");
            }
            if(s[0]=='p'){//判断父亲节点
                scanf("%s",s);
                scanf("%d",&b);
                int p=Search(b)>>1;
                if(Heap[p]==a)puts("T");
                else puts("F");
            }
            if(s[0]=='c'){//判断子节点
                scanf("%s",s);
                scanf("%d",&b);
                int p=Search(a)>>1;
                if(Heap[p]==b)puts("T");
                else puts("F");
            }
        }
    }
    system("pause");
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
//二叉树结点结构体
struct node
{
    int data;
    node *l, *r;
};
node* Build(int len, int a[], int b[])  //建树(模板)
{
    node *root = new node;
    if (len == 0)
        return NULL;
    int i;
    root->data = a[0];
    //判断是否相等进行交换
    for (i = 0; i < len; i ++)
        if (b[i] == a[0])
            break;
    root->l = Build(i, a + 1, b);
    root->r = Build(len - i - 1, a + i + 1, b + i + 1);
    return root;
}
//输出
void f(node *tree)
{
    queue<node*> q;
    if (tree)
    {
        cout << tree->data;
        q.push(tree);
    }

    while (!q.empty())
    {
        node *root = q.front();
        q.pop();
        if (root->r)   //本来是先root-<l,这里交换一下位置
        {
            cout << ' ' << root->r->data;
            q.push(root->r);
        }
        if (root->l)
        {
            cout << ' ' << root->l->data;
            q.push(root->l);
        }
    }
}

int main()
{
    int n;
    int a[111], b[111];
    cin >> n;
//输入二叉树
    for (int i = 0; i < n; i ++)
        cin >> a[i];
    for (int i = 0; i < n; i ++)
        cin >> b[i];

    node *tree = new node;
    tree = Build(n, b, a);

    f(tree);    
    return 0;
}

 

posted @ 2023-08-06 19:55  徐星凯  阅读(58)  评论(0编辑  收藏  举报