SDUT PTA 22级数据结构与算法实验5——树和二叉树

7-1 还原二叉树

#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
char s1[N], s2[N];
int dfs(int n1, int n2, int l, int h)
{
    if(l == 0) return h - 1;
    int len = 0;
    while(1)
    {
        if(s2[n2 + len] == s1[n1]) break;
        len ++;
    }
    int h1 = dfs(n1 + 1, n2, len, h + 1);
    int h2 = dfs(n1 + len + 1, n2 + len + 1, l - len - 1, h + 1);
    return h1 > h2 ? h1 : h2; 
}

int main()
{
    int n;
    cin >> n >> s1 >> s2;
    cout << dfs(0, 0, n, 1);
}

7-2 朋友圈

#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
int p[N], s[N];
int maxs;
int find(int x)
{
    if(p[x] != x) p[x] = find(p[x]);
    return p[x];
}

int main()
{
    int n, m;
    cin >> n >> m;
    for(int i = 1; i <= n; i ++)
    {
        p[i] = i;
        s[i] = 1;
    }
    while(m --)
    {
        int x, y;
        cin >> x >> y;
        for(int i = 1; i < x; i ++)
        {
            int z;
            cin >> z;
            int px = find(y), py = find(z);
            if(px != py)
            {
                p[py] = px;
                s[px] += s[py];
            }
        }
    }
    for(int i = 1; i <= n; i ++)
    {
        if(p[i] == i)
            maxs = max(maxs, s[i]);
    }
    cout << maxs;
}

7-3 修理牧场

#include <bits/stdc++.h>
using namespace std;
int main()
{
    priority_queue<int, vector<int>, greater<int>> q;
    int n, ans = 0;
    cin >> n;
    for(int i = 0; i < n; i ++)
    {
        int m;
        cin >> m;
        q.push(m);
    }
    while(q.size() > 1)
    {
        int a = q.top();
        q.pop();
        int b = q.top();
        q.pop();
        q.push(a + b);
        ans += a + b;
    }
    cout << ans;
}

7-4 根据后序和中序遍历输出先序遍历

#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int b[N], c[N];
void dfs(int *c, int *b, int n)
{
    if(n == 0) return;
    int x = c[n - 1];
    int i;
    for(i = 0; i < n; i ++)
        if(b[i] == x)
            break;
    cout << " " << x;
    dfs(c, b, i);
    dfs(c + i, b + i + 1, n - i - 1);
}

int main()
{
    int n;
    cin >> n;
    for(int i = 0; i < n; i ++) cin >> c[i];
    for(int i = 0; i < n; i ++) cin >> b[i];
    cout << "Preorder:";
    dfs(c, b, n);
}

7-5 玩转二叉树

#include <bits/stdc++.h>
using namespace std;
const int N = 35;
int a[N], b[N];
vector<int> ve(100010, -1);
void f(int x, int y, int root, int step)//x,y为左子树或右子树的区间,root为根节点,step为当前根节点的位置(满二叉树)
{
    if(x > y) return;
    ve[step] = a[root];
    int i;
    for(i = x; i <= y; i ++)
        if(a[root] == b[i])
            break;
    f(i + 1, y, root + i - x + 1, step * 2 + 1);
    f(x, i - 1, root + 1, step * 2 + 2);
    //镜面这个其实就只要在递归的时候先递归右树,再递归左树就好
}

int main()
{
    int n;
    cin >> n;
    for(int i = 1; i <= n; i ++) cin >> b[i];
    for(int i = 1; i <= n; i ++) cin >> a[i];
    f(1, n, 1, 0);
    int cnt = 0;
    for(int i = 0; i < ve.size() && cnt <= n; i ++)
    {
        if(ve[i] != -1)
        {
            if(i) cout << " ";
            cout << ve[i];
            cnt ++;
        }
    }
}

7-6 完全二叉树的层序遍历

#include <bits/stdc++.h>
using namespace std;
const int N = 35;
int n, m;
int a[N], ans[N];
void dfs(int u)
{
    if(u > n) return;
    dfs(2 * u);
    dfs(2 * u + 1);
    ans[u] = a[m ++];
}

int main()
{
    cin >> n;
    for(int i = 1; i <= n; i ++) cin >> a[i];
    m = 1;
    dfs(1);
    for (int i = 1; i <= n; i++) 
    {
        cout << ans[i];
        if(i != n) cout << " ";
    }
}

7-7 部落

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
set<int> s1, s2;
int p[N];
int find(int x)
{
    if(p[x] != x) p[x] = find(p[x]);
    return p[x];
}

int main()
{
    int t;
    cin >> t;
    for(int i = 1; i <= 10000; i ++)
        p[i] = i;
    while(t --)
    {
        int n, m;
        cin >> n >> m;
        s1.insert(m);
        for(int i = 1; i < n; i ++)
        {
            int k;
            cin >> k;
            s1.insert(k);
            p[find(k)] = find(m);
        }
    }
    cout << s1.size() << " ";
    for(auto it = s1.begin(); it != s1.end(); it ++)
    {
        int x = find(*it);
        s2.insert(x);
    }
    cout << s2.size() << endl;
    int nn;
    cin >> nn;
    while(nn --)
    {
        int a, b;
        cin >> a >> b;
        if(find(a) == find(b)) cout << "Y" << endl;
        else cout << "N" << endl;
    }
}

7-8 列出叶结点

#include <bits/stdc++.h>
using namespace std;
int a[15];
struct node {
    int l, r;
} t[15];

int main()
{
    char l, r;
    int n;
    cin >> n;
    for(int i = 0; i < n; i ++)
    {
        cin >> l >> r;
        if(l == '-') t[i].l = -1;
        else 
        {
            t[i].l = l - '0';
            a[t[i].l] = 1;
        }
        if(r == '-') t[i].r = -1;
        else 
        {
            t[i].r = r - '0';
            a[t[i].r] = 1;
        }
    }
    int root;
    for(int i = 0; i < n; i ++)
    {
        if(!a[i])
        {
            root = i;
            break;
        }
    }
    queue<int> q;
    q.push(root);
    int flag = 0;
    while(q.size())
    {
        root = q.front();
        q.pop();
        if(t[root].l == -1 && t[root].r == -1)
        {
            if(flag) cout << " ";
            cout << root;
            flag = 1;
        }
        else 
        {
            if(t[root].l != -1) q.push(t[root].l);
            if(t[root].r != -1) q.push(t[root].r);
        }
    }
}

7-9 交换二叉树中每个结点的左孩子和右孩子

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
char a[N];
int cnt;
void dfs()
{
    char c;
    cin >> c;
    if(c == '#') return;
    dfs();
    a[cnt ++] = c;
    dfs();
}

int main()
{
    dfs();
    for(int i = 0; i < cnt; i ++)
        cout << a[i];
    cout << endl;
    for(int i = cnt - 1; i >= 0; i --)
        cout << a[i];
}

7-10 建立与遍历二叉树

#include <bits/stdc++.h>
using namespace std;
void dfs()
{
    char c;
    cin >> c;
    if(c == '#') return;
    dfs();
    cout << c;
    dfs();
}

int main()
{
    dfs();
}

7-11 树的遍历

#include <bits/stdc++.h>
using namespace std;
vector<int> post(35), in(35), level(10000, -1);
//后序中的最后一个结点是根结点root,在中序中从start到end移动i找到这个根结点的位置,i以左是左子树,以右是右子树
void pre(int start, int end, int root, int index)//index是根节点在数组level中存放的位置
{
    if(start > end) return ;//当一个结点就是一个树时,start==end;将这个最底层的叶子结点存入level后就应该结束递归了。
    int i = start;
    while(i < end && in[i] != post[root]) i ++;
    level[index] = post[root];
    pre(start, i - 1, root - (end - i) - 1, index * 2 + 1);
    pre(i + 1, end, root - 1, index * 2 + 2);
}

int main()
{
    int n;
    cin >> n;
    for(int i = 0; i < n; i ++)
        cin >> post[i];
    for(int i = 0; i < n; i ++)
        cin >> in[i];
    pre(0, n - 1, n - 1, 0);
    int cnt = 0;
    for(int i = 0; i < 10000; i ++)
    {
        if(level[i] != -1 && cnt < n - 1)
        {
            cout << level[i] << " ";
            cnt ++;
        }
        else if(level[i] != -1)
        {
            cout << level[i];
            break;
        }
    } 
}

7-12 树层次遍历

#include <bits/stdc++.h>
using namespace std;
const int N = 250;
int a[N];
vector<vector<int>> ve(110);
stack<int> s;
int main()
{
    int len = 0;
    int t;
    while(cin >> t) a[len ++] = t;
    s.push(a[0]);
    int i = 1;
    while(!s.empty())
    {
        if(a[i]) s.push(a[i ++]);
        else
        {
            int t = s.top();
            ve[s.size()].push_back(t);
            s.pop();
            i ++;
        }
    }
    for(int i = 0; i < ve.size(); i ++)
        for(int j = 0; j < ve[i].size(); j ++)
            cout << ve[i][j] << " ";
}

7-13 哈夫曼树

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin >> n;
    priority_queue<int, vector<int>, greater<int>> q;
    for(int i = 0; i < n; i ++)
    {
        int x;
        cin >> x;
        q.push(x);
    }
    int ans = 0;
    while(q.size() > 1)
    {
        int t1 = q.top();
        q.pop();
        int t2 = q.top();
        q.pop();
        q.push(t1 + t2);
        ans += t1 + t2;
    }
    cout << ans << endl;
}

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

村长二甫弘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值