python - n-ary tree


  • 0
    S
    class Directory(object):
        def __init__(self, path):
            self.path     = path
            self.children = dict()
        
        def mkdir(self,subdir):
            if subdir:
                subdirs = subdir.split("/")
                subdir = subdirs[0]
                if not subdir in self.children:
                    path   = self.path + subdir if self.path == "/" else self.path + "/" + subdir
                    self.children[subdir] = Directory(path)
                self.children[subdir].mkdir('/'.join(subdirs[1:]))
        
        def search(self,path):
            if path:
                subdirs = path.split("/")
                child   = self.children[subdirs[0]]
                if isinstance(child,Directory): 
                    return child.search('/'.join(subdirs[1:]))
                else:
                    return child
            return self
        
        def getchildren(self):
            return sorted(self.children.keys())
        
    class File(object):
        def __init__(self,name,content=""):
            self.name    = name
            self.content = content
        
        def getname(self):
            return self.name
        
        def getcontent(self):
            return self.content
        
        def addcontent(self,content):
            self.content += content
    
    class FileSystem(object):
    
        def __init__(self):
            self.root = Directory("/")
    
        def ls(self, path):
            """
            :type path: str
            :rtype: List[str]
            """
            obj = self.root.search(path[1:])
            if isinstance(obj,Directory):
                return obj.getchildren()
            else:
                return  [ obj.getname() ]
    
        def mkdir(self, path):
            """
            :type path: str
            :rtype: void
            """
            self.root.mkdir(path[1:])
            
    
        def addContentToFile(self, filePath, content):
            """
            :type filePath: str
            :type content: str
            :rtype: void
            """
            groups = filePath.split("/")
            dirPath = '/'.join(groups[:-1])
            obj = self.root.search(dirPath[1:])
            if isinstance(obj,Directory):
                if groups[-1] not in obj.children:
                    obj.children[groups[-1]] = File(groups[-1], content)
                else:
                    obj.children[groups[-1]].addcontent(content)
            else:
                raise ValueError('filePath ', filePath, ' is not directory')
            
            
    
        def readContentFromFile(self, filePath):
            """
            :type filePath: str
            :rtype: str
            """
            groups = filePath.split("/")
            dirPath = '/'.join(groups[:-1])
            obj = self.root.search(dirPath[1:])
            if isinstance(obj,Directory):
                if isinstance(obj.children[groups[-1]], File):
                    return obj.children[groups[-1]].getcontent()
                else:
                    
                    raise ValueError('Invalid filePath', filePath)
            else:
                raise ValueError('Invalid filePath', filePath)
    
    
    
    
    
    # Your FileSystem object will be instantiated and called as such:
    # obj = FileSystem()
    # param_1 = obj.ls(path)
    # obj.mkdir(path)
    # obj.addContentToFile(filePath,content)
    # param_4 = obj.readContentFromFile(filePath)

Log in to reply
 

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