山东大学数据结构实验10 最小堆 霍夫曼树

山东大学数据结构实验10 最小堆 霍夫曼树

联系邮箱 tor108@outlook.com

概述

  • 不要直接复制粘贴 2023秋季做的话千万不要复制粘贴

源代码

  • 最小堆
#include<iostream>
using namespace std;
class SmallRootTree{
    public:
    int list[10000];
    int size=0;
    void in(bool if_in=1,int si=0){
        if(if_in)
        cin>>size;
        else{
            size=si;
        }
        for(int i=0;i<size;i++){
            cin>>list[i+1];
        }
    }
    int top(){
        return list[1];
    }
    void push(int a){
        list[size+1]=a;
        int pos=(size+1);
        size++;
        while(pos!=1&&a<list[pos/2]){
            int tmp=list[pos];
            list[pos]=list[pos/2];
            list[pos/2]=tmp;
            pos/=2;
        }
    }
    void pop(){
        int val=list[size];
        size--;
        list[1]=val;
        int pos=2;
        while(pos<=size){
            if(pos<size&&list[pos]>list[pos+1]){
                pos++;
            }
            if(list[pos]<val){
                list[pos/2]=list[pos];
                list[pos]=val;
                pos*=2;
            }
            else{
                break;
            }
        }
    }
    void init(){//init from the last node that have a son
        int current_root=size/2;
        for(;current_root>=1;current_root--){
            int pos=2*current_root;
            while(pos<=size){//same logic as the pop
                if(pos<size&&list[pos]>list[pos+1]){
                    pos++;
                }
                if(list[pos]<list[pos/2]){
                    int tmp=list[pos];
                    list[pos]=list[pos/2];
                    list[pos/2]=tmp;
                }
                pos*=2;
            }
        }
    }
    void out(){
        for(int i=1;i<=size;i++){
            cout<<list[i]<<' ';
        }
        cout<<endl;
    }
    void sort(int size){
        SmallRootTree b=SmallRootTree();
        b.in(0,size);
        b.init();
        int r[b.size];
        int r_size=b.size;
        while(b.size>0){
        r[b.size-1]=b.top();
        b.pop();
    }
    for(int i=r_size-1;i>=0;i--){
        cout<<r[i]<<' ';
    }
    cout<<endl;
    }
};
int main(){
    SmallRootTree brp=SmallRootTree();
    brp.in();
    brp.init();
    cout<<brp.top()<<endl;
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        int a,b;
        cin>>a;
        if(a==2){
            brp.pop();
            cout<<brp.top()<<endl;
        }
        else{
            cin>>b;
            switch(a)
            {
            case 1:
                brp.push(b);
                cout<<brp.top()<<endl;
                break;
            case 3:
                brp.sort(b);
            default:
                break;
            }
        }
    }
}
  • 霍夫曼编码
#include<iostream>
using namespace std;
struct Node{
    int val=0;
    char c;
    Node(int v,int _c):val(v),c(_c){};
    Node(){c=' ';val=0;};
};
class SamllRootTree{
    public:
    Node*list[10000];
    int size=0;
    SamllRootTree(){
        for(int i=1;i<=31;i++){
            list[i]=new Node();
            size++;
        }
    }
    Node* top(){
        return list[1];
    }
    void push(Node*a){
        list[size+1]=a;
        int pos=(size+1);
        size++;
        while(pos!=1&&a->val<list[pos/2]->val){
            Node tmp=*list[pos];
            list[pos]->val=list[pos/2]->val;
            list[pos/2]->val=tmp.val;
            pos/=2;
        }
    }
    void pop(){
        Node tmp=*list[size];
        size--;
        list[1]->val=tmp.val;
        int pos=2;
        while(pos<=size){
            if(pos<size&&list[pos]->val>list[pos+1]->val){
                pos++;
            }
            if(list[pos]->val<tmp.val){
                list[pos/2]->val=list[pos]->val;
                list[pos]->val=tmp.val;
                pos*=2;
            }
            else{
                break;
            }
        }
    }
    void init(){//init from the last node that have a son
        int current_root=size/2;
        for(;current_root>=1;current_root--){
            int pos=2*current_root;
            while(pos<=size){//same logic as the pop
                if(pos<size&&list[pos]->val>list[pos+1]->val){
                    pos++;
                }
                if(list[pos]->val<list[pos/2]->val){
                    int tmp=list[pos]->val;
                    list[pos]->val=list[pos/2]->val;
                    list[pos/2]->val=tmp;
                }
                pos*=2;
            }
        }
    }
};
int main(){
    SamllRootTree brp=SamllRootTree();
    char s[1000000];
    cin>>s;
    char c=s[0];
    Node* letter_set[27];
    for(int i=0;i<=26;i++){
        letter_set[i]=new Node();
    }
    cout<<(letter_set[1]->c);
    for(int i=1;i<=26;i++){
        letter_set[i]->c=96+i;
    }
    int i=0;
    while(c!='\0'){
        letter_set[c-96]->val++;
        i++;
        c=s[i];
    }
    for(int i=1;i<=26;i++){
        brp.push(letter_set[i]);
    }
    for(int i=1;i<brp.size;i++){
        cout<<brp.list[i]->c<<' '<<brp.list[i]->val<<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值