北京化工大学数据结构第十二次作业(计算机类)(2022.12.1)

 A 二叉排序树 - 文本输出

构造二叉排序树之后先序遍历输出,要注意如何处理特殊的输出格式。

#include <bits/stdc++.h>
using namespace std;
typedef struct {
    int key;
    int otherinfo;
}ElemType;
typedef struct BSTNode {
    ElemType data;
    struct BSTNode* lchild, * rchild;
}BSTNode,*BSTree;

void InsertBST(BSTree& T, ElemType e) {
    if (!T) {
        BSTNode* S = new BSTNode;
        S->data = e;
        S->lchild = S->rchild = NULL;
        T = S;
    }
    else if (e.key < T->data.key)
    InsertBST(T->lchild, e);
    else if (e.key > T->data.key)
    InsertBST(T->rchild, e);
}
void CreatBST(BSTree& T) 
{
    T = NULL;
    ElemType e;
    int n;
    cin >> n;
    while(n--){
        cin >> e.key;
        InsertBST(T, e);
    }
}
string s="";
void InorderTree(BSTree T) {
    cout<<s;
    if (T)
    {
        cout << T->data.key << " ";
        cout << endl;
        if(T->lchild || T->rchild)
        {
            s+="    ";
            InorderTree(T->lchild);
            InorderTree(T->rchild);
            for(int i=0;i<4;i++) s.pop_back();
        }
    }
    else
    {
        cout<<"#"<<endl;
    }
    
}
int main() {
    BSTree T = NULL;
    CreatBST(T);
    InorderTree(T);
    return 0;
}

销售排行榜

结构体把数据都储存进去,然后直接排序即可。第一遍按name排序把相同name的合并,第二遍按题意排序得到结果。

#include <bits/stdc++.h>
using namespace std;
struct shopg
{
    string name;
    long long num;
    long long price;
}goods[100010];
bool pd1(shopg shopg1,shopg shopg2)
{
    if(shopg1.name<shopg2.name) return true;
    else return false;
}
bool pd2(shopg shopg1,shopg shopg2)
{
    if(shopg1.num>shopg2.num) return true;
    else if(shopg1.num==shopg2.num && shopg1.name<shopg2.name) return true;
    else return false;
}
int main()
{
    int i=0;
    string s;
    while(cin>>s)
    {
        goods[i].name=s;
        cin>>goods[i].num;
        cin>>goods[i].price;
        goods[i].price*=goods[i].num;
        cin>>s;
        i++;
    }
    sort(goods,goods+i,pd1);
    for(int j=0;j<i-1;j++)
    {
        if(goods[j].name==goods[j+1].name)
        {
            goods[j+1].num+=goods[j].num;
            goods[j+1].price+=goods[j].price;
            goods[j].num=0;
            goods[j].price=0;
        }
    }
    sort(goods,goods+i,pd2);
    for(int j=0;j<i;j++)
    {
        if(goods[j].num!=0)
        {
            cout<<goods[j].name<<" "<<goods[j].num<<" "<<goods[j].price<<endl;
        }
    }
    return 0;
}

C 二叉排序树-平衡因子

在A题的基础上多维护每个节点的左最大深度和右最大深度。

平衡因子为左最大深度-右最大深度。

#include <bits/stdc++.h>
using namespace std;
typedef struct {
    int key;
    int leftbal;
    int rightbal;
}ElemType;
int cc;
typedef struct BSTNode {
    ElemType data;
    struct BSTNode* lchild, * rchild;
}BSTNode,*BSTree;
void InsertBST(BSTree& T, ElemType e) {
    if (!T)
    {    
        BSTNode* S = new BSTNode;
        e.leftbal=cc;
        e.rightbal=cc;
        S->data = e;
        S->lchild = S->rchild = NULL;
        T = S;
    }
    else if (e.key < T->data.key)
    {
        cc++;
        InsertBST(T->lchild, e);
        T->data.leftbal=max(T->data.leftbal,cc);
    }
    else if (e.key > T->data.key)
    {
        cc++;
        InsertBST(T->rchild, e);
        T->data.rightbal=max(T->data.rightbal,cc);
    }
}
void CreatBST(BSTree& T) {
   
    T = NULL;
    ElemType e;
    int n;
    cin >> n;
    while(n--){
        cin >> e.key;
        e.leftbal=0;
        e.rightbal=0;
        cc=0;
        InsertBST(T, e);
    }
}
string s="";
void InorderTree(BSTree T) {
    cout<<s;
    if (T)
    {
        cout << T->data.key << "("<<T->data.leftbal-T->data.rightbal<<")";
        cout << endl;
        if(T->lchild || T->rchild)
        {
            s+="    ";
            InorderTree(T->lchild);
            InorderTree(T->rchild);
            for(int i=0;i<4;i++) s.pop_back();
        }
    }
    else
    {
        cout<<"#"<<endl;
    }
    
}
int main() {
    BSTree T = NULL;
    CreatBST(T);
    InorderTree(T);
    return 0;
}

D 案例 1-1.1 二分查找

建议使用C++的二分查找函数lower_bound()

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,t;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++) cin>>a[i];
    cin>>t;
    auto g=lower_bound(a,a+n,t);
    if(*g!=t) cout<<-1<<endl;
    else cout<<g-a+1<<endl;
    return 0;
}

E 进阶实验 1-3.1:两个有序序列的中位数

不知道这题出在这一次作业的意义((((

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int a[2*n];
    for(int i=0;i<2*n;i++) cin>>a[i];
    sort(a,a+2*n);
    cout<<a[(2*n+1)/2-1]<<endl;
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

五百场cf灰名

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

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

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

打赏作者

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

抵扣说明:

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

余额充值