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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值