问题
I would love to someone who understands concept of nodes and could make some easy explanation on one program I am trying to write, basicaly a "file structure" which would be like tree.
I understand that nodes work the way that they are linked to each other (ergo: Node has prev/next attribute which links to other nodes)
This is the class that would describe each node.
class Node:
def __init__(self, nid: int, name: str, owner: str, is_dir: bool, size: int, parent: Optional["Node"],
children: List["Node"]):
self.nid = nid
self.name = name
self.owner = owner
self.is_dir = is_dir
self.size = size
self.parent = parent
self.children = children
This is basicaly what i wanna achieve.
Thing that is troubling me the most is how i would build such structure.
For e.g. if i get dictionary that has node.nid as key and ID of its children as array so it would be like this.
metadata = {1:[2,4],2:[3]}
I want to build the entire structure from this dictionary with (probably) for loop.
def build_filestructure(metadata: Dict[int, List[int]]) -> Optional[Node]:
for key in node_list:
# Create node and add its children
# Add children for its children
return Node?
What would be the right way to build a file structure as described above from this dictionary?
Thanks for all your ideas in advance.
来源:https://stackoverflow.com/questions/65364781/could-someone-working-with-tree-structure