BUPT OJ 文件系统

来源:互联网 发布:心电图工作站软件下载 编辑:程序博客网 时间:2024/06/08 12:38

孩子兄弟表示法的多叉树遍历

 

#include<iostream>#include<string>using namespace std;enum type{file,dir};class node{public:string name;type mytype;node*child;node*brother;//孩子兄弟表示法; };void CreateFile(node*pt,string filename){node*temp=new node;temp->mytype=file;temp->child=temp->brother=NULL;temp->name=filename;if(pt->child!=NULL){  node* tempnode=pt->child;while(tempnode->brother!=NULL){tempnode=tempnode->brother;}tempnode->brother=temp;}else{pt->child=temp;}}void CreateDir(node*pt,string dirname){node*temp=new node;temp->mytype=dir;temp->child=temp->brother=NULL;temp->name=dirname;if(pt->child!=NULL){  node* tempnode=pt->child;while(tempnode->brother!=NULL){tempnode=tempnode->brother;}tempnode->brother=temp;}else{pt->child=temp;}}void ListFile(node*pt){node*now=pt->child;while(now){if(now->mytype==file){cout<<now->name<<endl;}now=now->brother;}}void ListDir(node*pt){node*now=pt->child;while(now){if(now->mytype==dir){cout<<now->name<<endl;}now=now->brother;}}node* travel(node*root,string name){if(root){if(root->name==name)return root;node*p=NULL;p=travel(root->child,name);if(p)return p;return travel(root->brother,name);}}int main(){int t,op;string task;cin>>t;node *root=new node;;root->name="root";root->mytype=dir;while(t--){root->child=root->brother=NULL;cin>>op;while(op--){cin>>task;if(task=="CREATEFILE"){string FILENAME,DIRNAME;cin>>FILENAME>>DIRNAME;node *fa=travel(root,DIRNAME);CreateFile(fa,FILENAME); }else if(task=="CREATEDIR"){string DIRNAME1,DIRNAME2;cin>> DIRNAME1 >> DIRNAME2;node *fa=travel(root,DIRNAME2);CreateDir(fa,DIRNAME1);}else if(task=="LISTFILE"){string DIRNAME;cin>>DIRNAME;node *fa=travel(root,DIRNAME);ListFile(fa);}else{string DIRNAME;cin>>DIRNAME;node *fa=travel(root,DIRNAME);ListDir(fa);}}}}

0 0