目前有如下数据:A1 B1 C1 D1
A1 B2 C2 D3
A2 B1 C3 D4
A2 B1 C3 D6
A3 B3 C4 D5
等等。。生成下面如下树形结构:
A1 B1 C1 D1
   B2 C2 D3
A2 B1 C3 D4
         D6
A3 B3 C4 D5请问如何来做?

解决方案 »

  1.   

    给你一个C++的代码
    PKU的一个题目,方法基本类似。
    能看懂这个就能做好你的问题。
    题目链接:http://acm.pku.edu.cn/JudgeOnline/problem?id=1760//pku 1760
    #include<iostream>
    #include<algorithm>
    #include<string>
    #include<fstream>
    using namespace std;#define N 510
    #define null NULL
    struct NODE{
    string name;
    NODE *next;
    NODE *fchd;
    };
    NODE *root=null;bool cmp(string s1,string s2){
    return s1>s2;
    }void proc(NODE **node,string str){
    string s=str.substr(0,str.find(' '));
    NODE *t;
    if(s.empty())return;
    if(*node==null){
    t=new NODE();
    t->name=s;
    t->next=null;
    t->fchd=null;
    *node=t;
    }
    if((*node)->name==s)
    t=*node;
    else{
    t=new NODE();
    t->name=s;
    t->next=null;
    t->fchd=null;
    t->next=(*node);
    (*node)=t;
    }
    proc(&(t->fchd),str.substr(str.find(' ')+1));
    }
    void disp(NODE *node,int n){
    while(node){
    for(int i=0;i<n;i++)
    cout<<' ';
    cout<<node->name<<endl;
    if(node->fchd)disp(node->fchd,n+1);
    node=node->next;
    }
    }
    int main(){
    //ifstream fin("1760.txt");
    //cin=fin; int w,i,j;
    string str[N];
    cin>>w;
    for(i=0;i<w;i++){
    cin>>str[i];
    while(true){
    j=str[i].find('\\');
    if(j==-1)break;
    str[i].replace(j,1," ");
    }
    str[i]+=" ";
    }
    sort(str,str+w,cmp);
    for(i=0;i<w;i++)
    proc(&root,str[i]);
    disp(root,0);
    return 0;
    }
      

  2.   

    int n=100,m=100;
    int tree[n,m];
    int newtree[n,m];
    for(int i=0;i<n;i++)
    {
    newtree[0,i]=tree[0,i];
    }for(int i=1;i<n;i++)
    {
       for(int j=0;j<m;j++)
       {
           if(tree[i,j]!=tree[i-1,j])
           {
               newtree[i,j]=tree[i-1,j];
           }
             
       }
    }
    }
      

  3.   

    目前有如下数据: A1 B1 C1 D1 
    A1 B2 C2 D3 
    A2 B1 C3 D4 
    A2 B1 C3 D6 
    A3 B3 C4 D5 
    等等。。 生成下面如下树形结构: 
    A1 B1 C1 D1 
      B2 C2 D3 
    A2 B1 C3 D4 
            D6 
    A3 B3 C4 D5 请问如何来做? 这棵树看不懂,上面给代码或方法的人纯属瞎闹,哪有这种树啊