数据结构学习

DS二叉树–二叉树构建与遍历(含代码框架)

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
struct Tr
{
    char ch;
    int l, r;
}tr[N];
int cnt = 0, n, idx = 0;
char s[N];
int dfs() // i表示s的某个位置 返回当前节点在树上的编号
{
    idx ++;
    if(idx == n || s[idx] == '0') return 0;
    int cur = ++ cnt;
    tr[cur].ch = s[idx];
    tr[cur].l = dfs(), tr[cur].r = dfs();
    return cur;
}
void pre(int i)
{
    if(!i) return;
    cout << tr[i].ch; 
    pre(tr[i].l);
    pre(tr[i].r);
}
void mid(int i)
{
    if(!i) return;
    mid(tr[i].l);
    cout << tr[i].ch;
    mid(tr[i].r);
}
void post(int i)
{
    if(!i) return;
    post(tr[i].l);
    post(tr[i].r);
    cout << tr[i].ch;
}
int main()
{
    #ifdef ZY
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    #endif
    int t; cin >> t;
    while(t --)
    {
        cin >> s;
        n = strlen(s), cnt = 0, idx = -1;
        dfs();
        pre(1); puts(""); mid(1); puts(""); post(1); puts("");
    }
    return 0;
}

DS二叉树——二叉树之父子结点

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
struct Tr
{
    char ch;
    int l, r, fa;
}tr[N];
int cnt = 0, n, idx = 0;
char s[N];
int dfs() // i表示s的某个位置 返回当前节点在树上的编号
{
    idx ++;
    if(idx == n || s[idx] == '0') return 0;
    int cur = ++ cnt;
    tr[cur].ch = s[idx];
    tr[cur].l = dfs(), tr[cur].r = dfs();
    tr[tr[cur].l].fa = cur, tr[tr[cur].r].fa = cur;
    return cur;
}
void pre(int i)
{
    if(!i) return;
    if(!tr[i].l && !tr[i].r) cout << tr[i].ch << ' ';
    pre(tr[i].l), pre(tr[i].r);
}
void pre_fa(int i)
{
    if(!i) return;
    if(!tr[i].l && !tr[i].r) cout << tr[tr[i].fa].ch << ' ';
    pre_fa(tr[i].l), pre_fa(tr[i].r);
}
int main()
{
    #ifdef ZY
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    #endif
    int t; cin >> t;
    while(t --)
    {
        cin >> s;
        n = strlen(s), cnt = 0, idx = -1;
        dfs();
        pre(1); puts(""); pre_fa(1); puts("");
    }
    return 0;
}

DS树–二叉树高度

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
struct Tr
{
    int l, r, height;
}tr[N];
int cnt = 0, n, idx = 0, height = 0;
char s[N];
int dfs() // i表示s的某个位置 返回当前节点在树上的编号
{
    idx ++;
    if(idx == n || s[idx] == '0') return 0;
    int cur = ++ cnt;
    tr[cur].l = dfs(), tr[cur].r = dfs();
    if(tr[cur].l) tr[cur].height = tr[tr[cur].l].height + 1;
    if(tr[cur].r) tr[cur].height = max(tr[cur].height, tr[tr[cur].r].height + 1);
    if(!tr[cur].l && !tr[cur].r) tr[cur].height = 1;
    height = max(tr[cur].height, height);
    return cur;
}

int main()
{
    #ifdef ZY
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    #endif
    int t; cin >> t;
    while(t --)
    {
        cin >> s;
        n = strlen(s), cnt = 0, idx = -1, height = 0;
        dfs();
        cout << height << endl;
    }
    return 0;
}

DS二叉树——二叉树之数组存储

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int a[N], n;
void dfs(int i) // i表示s的某个位置 返回当前节点在树上的编号
{
    if(i > n || !a[i]) return;
    cout <<  a[i] << ' ';
    dfs(i * 2), dfs(i * 2 + 1);
}
int main()
{
    #ifdef ZY
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    #endif
    int t; cin >> t;
    while(t --)
    {
        cin >> n;
        for(int i = 1; i <= n; i ++) cin >> a[i];
        dfs(1); puts("");
    }
    return 0;
}

DS二叉树–叶子数量

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int idx, leaves, n;
char s[N];
int dfs() // i表示s的某个位置 返回当前节点在树上的编号
{
    idx ++;
    if(idx == n || s[idx] == '0') return 0;
    int l = dfs(), r = dfs();
    if(!l && !r) leaves ++;
    return 1;
}

int main()
{
    #ifdef ZY
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    #endif
    int t; cin >> t;
    while(t --)
    {
        cin >> s;
        n = strlen(s), idx = -1, leaves = 0;
        dfs();
        cout << leaves << endl;
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
char s[N];
int height, leaves, sum, n, idx, num[N], leaves_idx;
int dfs() // i表示s的某个位置 返回当前节点在树上的编号
{
    idx ++;
    if(idx == n || s[idx] == '0') return 0;
    height ++;
    int cur_height = height;
    bool l = dfs(), r = dfs();
    if(!l && !r) sum += num[++ leaves_idx] * (cur_height - 1);
    height --;
    return 1;
}
int main()
{
    #ifdef ZY
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    #endif
    int t; cin >> t;
    while(t --)
    {
        cin >> s;
        n = strlen(s), idx = -1, height = 0, leaves = 0, sum = 0, leaves_idx = 0;
        cin >> leaves;
        for(int i = 1; i <= leaves; i ++) cin >> num[i];
        dfs();
        cout << sum << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值