POJ2003 RE到shi ,树存储的数据结构

1 题意

2 分析

3 跟别人学的、

#include <iostream>
#include <string>
#include <map>
#include <list>
#include <algorithm>


using namespace std;

string str1,str2;
struct Node{
    string name;
    //char name[60];
    Node* parent;
    list<Node*> son;
    Node(){
        parent=NULL;
    }
};
map<string,Node*> mmap;
Node* root;
void Ini(){
    cin>>str1;
    root=new Node();
    root->name=str1;
    mmap[str1]=root;
}
void Hires(string p1,string s1){
    Node* n1=mmap[p1];
    Node* n2=new Node();
    n2->name=s1;
    n2->parent=n1;
    n1->son.push_back(n2);
    mmap[s1]=n2;
    ///delete n1,n2;  it is wrong.
}
void Fire(string s1){
    Node* n2=mmap[s1];//son
    Node* n1=n2->parent;//father
    mmap.erase(s1);
    while(n2->son.size()!=0){
        n2->name=n2->son.front()->name;
        mmap[n2->name]=n2;
        n2=n2->son.front();
    }
    n2->parent->son.remove(n2);
    delete n2;/// it is true
}
void Print(Node* cur,long cou ){
    for(long i=0;i<cou;i++){
        cout<<"+";
    }
    cout<<cur->name<<endl;
    for(list<Node*>::iterator it=cur->son.begin();it!=cur->son.end();it++){
        Print(*it,cou+1);
    }
}
int main()
{
    Ini();
    while(cin>>str1){
        if(str1=="print"){
            Print(root,0);
            for(int i=0;i<60;i++){
                cout<<"-";
            }
            cout<<endl;
        }
        else if(str1=="fire"){
            cin>>str2;
            Fire(str2);
        }
        else{
            cin>>str2>>str2;
            Hires(str1,str2);
        }

    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值