I am trying to find the left most leaf and right most leaf in the bigtree node , here is my code
from bigtree import nested_dict_to_tree, print_tree
path_dict = {
"name": "a",
"points": 100,
"side":'H',
"children": [
{
"name": "b",
"points": 50,
"side":'L',
"children": [
{"name": "d", "points": 40,"side":'L'},
{"name": "e", "points": 20,"side":'R'},
],
},
{"name": "c", "points": 60,"side":'R' ,
"children": [
{"name": "f", "points": 40,"side":'L'},
{"name": "g", "points": 20,"side":'R'},
]},
],
}
tree_root = nested_dict_to_tree(path_dict)
print_tree(tree_root)
for below output i need to get ‘d’ as right most node and ‘g’ as left most node
a
├── b
│ ├── d
│ └── e
└── c
├── f
└── g
2
Answers
You could use the iteration methods in the
bigtree.utils
module:Note that you got left and right mixed up. The tree is like this:
So "d" is the left most leaf and "g" the right most leaf. (See also the "side" keys in your initial dictionary).
The leftmost node is referring to the first child of every node, starting from root node, while the rightmost node is referring to the last child of every node, starting from root node.
The children of a node can be directly accessed as such,
This will result in
Source: I’m the author of
bigtree
🙂