BUPT OJ 文件系統

孩子兄弟表示法的多叉樹遍歷

 

#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);
			}
			
			
			
		}
		
		
	}
	
	
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章