0

I have a string of tab-indented lines that looks like this:

Node A
    Node B
    Node C
        Node D
    Node E
Node F

And I hope to get a data structure where I can traverse the tree by the following:

print data[Node A][Node B][Node C] => Gives me all children under Node C

How can I do this with Python? I don't know what to search for this! Any help would be really appreciated.

High schooler
  • 1,664
  • 3
  • 28
  • 45

2 Answers2

3

You should look at networkx. It is a great library for graph / tree Data Structures.

And for your question on how to parse the tab separated data into a tree structure check this SO Question to help you get started.

Community
  • 1
  • 1
Raghav RV
  • 3,788
  • 2
  • 20
  • 26
0

As a start, nested dictionaries could do the job:

tree = {
    "a": 5,
    "b": {
        "c": 6
    }
}

node = tree["b"]["c"]
Douglas
  • 35,066
  • 8
  • 72
  • 89