遍历树数据结构

时间:2018-12-11 07:28:52

标签: python data-structures tree

数据结构

occupation
├── Artisans
│   ├── 197
│   └── 202

├── Entrepreneur
│   ├── Category?
│   │   ├── SC
│   │   │   └── 191
│   │   └── ST
│   │       └── 191
│   └── Industry?
│       └── Dairy
│           └── 180

├── Farmer
│   ├── 145
│   ├── 180
│   ├── 182
│   └── Crop?
│       └── Wheat
│           └── 144

├── Fisherman
│   ├── 182
│   └── Gender ?
│       └── Female
│           └── 184

└── Weavers
    └── 194

代码:

leafs=[]

R=[t.root]

while t.all_nodes():  

    l =len(R)

    c = t.is_branch(R[l-1])
    print(c)

    user_inp = input('Enter ')

    if len(t.is_branch(user_inp))==0:
        leafs.append(user_inp)
        print(leafs)

    else:
        R.append(user_inp)

我已经使用Python中的treelib库编写上述代码。

以上代码遍历叶子并将叶子节点的值存储在列表中。但是我不明白我该怎么回头?

0 个答案:

没有答案
相关问题