Memory Efficient code


  • 0
    A

    struct node{
    string name;
    string content;
    bool type;
    node * next;
    node * child;
    node() : name(""),type(0),next(NULL),child(NULL) {}
    node(string nam) : name(nam),type(0),next(NULL),child(NULL){}

    };

    struct Tree{

       node *root;
    Tree(){
    	root = new node();
    }
    
    void insert(string filePath,string content,bool isFile){
    	unsigned int i = 0;
    	node * temp = root , *temp1,*prev;
    	bool flag = true;
    	while(i<filePath.length()){
    		flag = 1;
    		if(filePath[i]=='/'){
    			i++;
    			string tempfile = "";
    			while(i<filePath.length()&&filePath[i]!='/'){
    				tempfile += filePath[i];
    				i++;
    			}
    			if(tempfile.length()!=0){
    				if(temp->child == NULL){
    					temp->child = new node(tempfile);
    					temp = temp->child;
    				}else{
    					temp1 = temp->child;
    					prev = temp->child;
    					while(temp1!=NULL){
    						if(strcmp(temp1->name.c_str(),tempfile.c_str())==0){
    							temp = temp1;
    							flag = 0;
    							break;
    						}
    						prev = temp1;
    						temp1 = temp1->next;
    					}
    					if(flag){
    						prev->next = new node(tempfile);
    						temp = prev->next;
    					}
    				}
    			}
    		}else
    			i++;
    	}
    	if(isFile){
    		temp->type = 1;
    		temp->content += content;
    	}
    }
    
    node* read(string filePath){
    	string content = "";
    	unsigned int i = 0;
    	node * temp = root;
    	bool flag = 0;
    	while(i<filePath.length()){
    		flag = 0;
    		if(filePath[i]=='/'){
    			i++;
    			string tempfile = "";
    			while(i<filePath.length()&&filePath[i]!='/'){
    				tempfile += filePath[i];
    				i++;
    			}
    			if(tempfile.length()!=0){
    				temp = temp->child;
    				while(temp!=NULL){  //search for file name
    					if(strcmp(temp->name.c_str(),tempfile.c_str())==0){ //matching file names
    						flag = 1;
    						break;
    					}
    					temp = temp->next;
    				}
    				if(!flag){
    					break;
    				}
    			}
    		}else
    			i++;
    	}
    	return temp;
    }
    

    };

    class FileSystem {

    	Tree * tree;
    
    public:
    
    	FileSystem() {
    		tree = new Tree(); 
    	}
    	
    	vector<string> ls(string path) {
    		node* temp = tree->read(path);
    		vector<string> ans;
    		if(temp!=NULL){
    			if(temp->type==0){ // Directory
    				temp = temp->child;
    				while(temp!=NULL){
    					ans.push_back(temp->name);
    					temp = temp->next;
    				}
    				sort(ans.begin(),ans.end()); //sort
    			}else{  // file
    				ans.push_back(temp->name);
    			}
    		}
    		return ans;
    	}
    	
    	void mkdir(string path) {
    		tree->insert(path,"",0);
    	}
    	
    	void addContentToFile(string filePath, string content) {
    		tree->insert(filePath,content,1);
    	}
    	
    	string readContentFromFile(string filePath) {
    		node* temp = tree->read(filePath);
    		if(temp!=NULL) // file exists
    			return temp->content;
    		return "";
    	}
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.