+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

Could someone working with tree structure?

发布于2021-03-08 20:34     阅读(193)     评论(0)     点赞(26)     收藏(5)


0

1

2

3

4

5

6

7

8

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.

enter image description here

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.


解决方案


暂无回答

0

1

2

3

4

5

6

7

8



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接: https://www.pythonheidong.com/blog/article/880000/e31dd829b895d273e022/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

26 0
收藏该文
已收藏

评论内容:(最多支持255个字符)