[算法100天 day12]复杂数据结构入门(二叉树,排序二叉树,优先队列和散列表)

二叉树遍历(PKU上机题)

给出先序序列,给出中序序列。P155

#include<bits/stdc++.h>
using namespace std;
int pos = 0;
string str;

struct Node{
    char data;
    Node* leftchild;
    Node* rightchild;
    Node(char c): data(c),leftchild(NULL),rightchild(NULL){}//构造函数

};

Node* constructTree(){
    //注意这里千万不要去遍历str,因为是递归建树的
    char x = str[pos];pos++;
    if(x == '#') return NULL;
    else{
        Node* node = new Node(x);
        node->leftchild = constructTree();
        node->rightchild = constructTree();
        return node;
    }
}

void midOrder(Node* node){
    if(node == NULL) return;
    else{
        midOrder(node->leftchild);
        cout<<node->data;
        midOrder(node->rightchild);
    }
}

int main(){
    while(cin>>str){
        pos = 0;
        Node* tree = constructTree();
        midOrder(tree);
    }
    return 0;
}

/*
abc##de#g##f###
*/

由于不知道我的优快云抽什么风,我就将第13天的内容贴在这里了。

补充编程知识:
string类型可以直接用find去寻找某一个字符的位置,返回为带0编号的字符的位置。
string的substr是用来截断一个字串的,string.str(startposition,length);即第一个参数为开始的索引,第二个参数为截取的字符串的长度。

题面:由先序和中序写出后序遍历。

#include<bits/stdc++.h>
using namespace std;
string str;

struct Node{
    char data;
    Node* left;
    Node* right;
    Node(char c): data(c),left(NULL),right(NULL){}
};

//str1为先序序列,str2为中序序列
Node* constructTree(string str1,string str2){
    if(str1.size() == 0){return NULL;}
    else{
        int x = str1[0];
        int pos = str2.find(x);
        Node* node = new Node(x);
        node->left = constructTree(str1.substr(1,pos),str2.substr(0,pos));
        //只需要记录开始的位置即可,stbstr的用法
        node->right = constructTree(str1.substr(pos+1),str2.substr(pos+1));
        return node;
    }
}

void postOrder(Node* node){
    if(node == NULL) return ;
    else{
        postOrder(node->left);
        postOrder(node->right);
        cout<<node->data;
    }
}

int main(){
    string str1,str2;
    while(cin>>str1>>str2){
        Node* tree = constructTree(str1,str2);
        postOrder(tree);
        cout<<endl;
    }
    return 0;
}

/*
abc##de#g##f###
*/

优先队列(有STL库可以直接用)

优先队列的应用①:复杂数据结构,一定要把重载数据结构记清楚

对于简单数据结构如int的排序只要用队列内部的排序即可,但是如果是一个复杂数据结构(比如结构体)就需要重载运算符。

#include<bits/stdc++.h>
using namespace std;

struct Complex{
    int x;
    int y;
    Complex(int i,int j): x(i),y(j){}
    bool operator < (const Complex& e)const{
        return x * x + y * y < e.x * e.x + e.y * e.y;
    }
};
priority_queue<Complex> MyQueue;
int main(){
    int n;
    while(n--){
        string str;
        cin>>str;
        if(str == "Pop"){
            if(MyQueue.empty()){
                cout<<"empty"<<endl;
            }else{
                Complex com = MyQueue.top();
                MyQueue.pop();
                cout<<com.x<<"+i"<<com.y<<endl;
                cout<<"SIZE="<<MyQueue.size()<<endl;
            }
        }else if(str == "Insert"){
            int x,y;
            scanf("%d+i%d",&x,&y);
            Complex* com1 = new Complex(x,y);
            MyQueue.push(*com1);
            cout<<"SIZE="<<MyQueue.size()<<endl;
        }
    }
    return 0;
}


/*
3
Pop
Insert 1+i2
Pop
*/

优先队列的应用②:哈夫曼树

哈夫曼树对于数据结构是非常重要的部分,必须要会的!

注意优先队列的重新初始化:按小的优先输出
priority_queue<int , vector, greater > myQueue;

#include<bits/stdc++.h>
using namespace std;

int main(){

    int n;
    while(scanf("%d", &n) != EOF){
        int ans = 0;
        priority_queue<int , vector<int>, greater<int>  > myQueue;
        while(n--){
            int num;
            cin >> num;
            myQueue.push(num);
        }
        while(myQueue.size() != 1){
            int x = myQueue.top();
            myQueue.pop();
            int y = myQueue.top();
            myQueue.pop();
            ans = ans + x + y;
            myQueue.push(x+y);
        }
        cout<<ans<<endl;
    }
    return 0;
}
/*
3
Pop
Insert 1+i2
Pop
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值