Tree and List
Tree class
When instantiating a class, you can use some settings
id_key
Id of the node.
parent_key
Parent node id. This id has to match the id_key that identifies a parent node.
child_key
When you create a tree from a list, this value will be the name of the key which will be a list of child nodes. If used when creating a list from a tree, this value will be used to identify the key that contains the list with the child nodes.
parent_start
This is the value that defines a parent node. Ex: "0", "None".
flow_key
when a tree is created from a list, will automatically be created in the key in the dictionary called "flow" that contains the current node sequence. Ex: "flow": "1-1-2".
flow
Receives a boolean value that generates the flow_key or not.
The default values are:
1 2 3 4 5 6 7 8 9 10 11 |
|
Tree from List
Default values
In this example contains 2 trees in the list.
- The value "0" will be used to identify the parent node.
- The value of the key that contains the id of the node is "id"
- The value of the key that contains the id of the parent node is "parent"
- The key value that will be created with the list of child nodes will be "children"
- A key named "flow" will be automatically created with the current sequence of each node
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Output:
[
{
"id": 1,
"parent": 0,
"flow": "1",
"children": [
{
"id": 2,
"parent": 1,
"flow": "1-1",
"children": [
{
"id": 4,
"parent": 2,
"flow": "1-1-1"
}
]
},
{
"id": 3,
"parent": 1,
"flow": "1-2"
}
]
},
{
"id": 5,
"parent": 0,
"flow": "2",
"children": [
{
"id": 6,
"parent": 5,
"flow": "2-1"
},
{
"id": 7,
"parent": 5,
"flow": "2-2"
}
]
}
]
Flow Key
In this example the value of the key flow will be changed to sequence.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Output:
[
{
"id": 1,
"parent": 0,
"sequence": "1",
"children": [
{
"id": 2,
"parent": 1,
"sequence": "1-1"
},
{
"id": 3,
"parent": 1,
"sequence": "1-2"
}
]
},
{
"id": 4,
"parent": 0,
"sequence": "2"
}
]
You can disable the "flow_key".
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Output:
[
{
"id": 1,
"parent": 0,
"children": [
{
"id": 2,
"parent": 1
},
{
"id": 3,
"parent": 1
}
]
},
{
"id": 4,
"parent": 0
}
]
List from Tree
Maybe you already have a tree and want to turn it into a list with all the nodes. Then you can pass a list of trees to turn into a list of nodes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
Output:
[
{
"id": 1,
"parent": 0
},
{
"id": 2,
"parent": 1
},
{
"id": 3,
"parent": 1
},
{
"id": 4,
"parent": 0
}
]