Hi you may give itertree a try [I'm the author].
The package goes in the direction of anytree package but with a bit different focus. The performance on huge trees [>100000 items] is much better and it deals with iterators to have effective filter mechanism.
>>>from itertree import *
>>>root=iTree['root']
>>># add some children:
>>>root.append[iTree['Africa',data={'surface':30200000,'inhabitants':1257000000}]]
>>>root.append[iTree['Asia', data={'surface': 44600000, 'inhabitants': 4000000000}]]
>>>root.append[iTree['America', data={'surface': 42549000, 'inhabitants': 1009000000}]]
>>>root.append[iTree['Australia&Oceania', data={'surface': 8600000, 'inhabitants': 36000000}]]
>>>root.append[iTree['Europe', data={'surface': 10523000 , 'inhabitants': 746000000}]]
>>># you might use __iadd__ operator for adding too:
>>>root+=iTree['Antarktika', data={'surface': 14000000, 'inhabitants': 1100}]
>>># for building next level we select per index:
>>>root[0]+=iTree['Ghana',data={'surface':238537,'inhabitants':30950000}]
>>>root[0]+=iTree['Niger', data={'surface': 1267000, 'inhabitants': 23300000}]
>>>root[1]+=iTree['China', data={'surface': 9596961, 'inhabitants': 1411780000}]
>>>root[1]+=iTree['India', data={'surface': 3287263, 'inhabitants': 1380004000}]
>>>root[2]+=iTree['Canada', data={'type': 'country', 'surface': 9984670, 'inhabitants': 38008005}]
>>>root[2]+=iTree['Mexico', data={'surface': 1972550, 'inhabitants': 127600000 }]
>>># extend multiple items:
>>>root[3].extend[[iTree['Australia', data={'surface': 7688287, 'inhabitants': 25700000 }], iTree['New Zealand', data={'surface': 269652, 'inhabitants': 4900000 }]]]
>>>root[4]+=iTree['France', data={'surface': 632733, 'inhabitants': 67400000 }]]
>>># select parent per TagIdx - remember in itertree you might put items with same tag multiple times:
>>>root[TagIdx['Europe'0]]+=iTree['Finland', data={'surface': 338465, 'inhabitants': 5536146 }]
The created tree can be rendered:
>>>root.render[]
iTree['root']
└──iTree['Africa', data=iTData[{'surface': 30200000, 'inhabitants': 1257000000}]]
└──iTree['Ghana', data=iTData[{'surface': 238537, 'inhabitants': 30950000}]]
└──iTree['Niger', data=iTData[{'surface': 1267000, 'inhabitants': 23300000}]]
└──iTree['Asia', data=iTData[{'surface': 44600000, 'inhabitants': 4000000000}]]
└──iTree['China', data=iTData[{'surface': 9596961, 'inhabitants': 1411780000}]]
└──iTree['India', data=iTData[{'surface': 3287263, 'inhabitants': 1380004000}]]
└──iTree['America', data=iTData[{'surface': 42549000, 'inhabitants': 1009000000}]]
└──iTree['Canada', data=iTData[{'surface': 9984670, 'inhabitants': 38008005}]]
└──iTree['Mexico', data=iTData[{'surface': 1972550, 'inhabitants': 127600000}]]
└──iTree['Australia&Oceania', data=iTData[{'surface': 8600000, 'inhabitants': 36000000}]]
└──iTree['Australia', data=iTData[{'surface': 7688287, 'inhabitants': 25700000}]]
└──iTree['New Zealand', data=iTData[{'surface': 269652, 'inhabitants': 4900000}]]
└──iTree['Europe', data=iTData[{'surface': 10523000, 'inhabitants': 746000000}]]
└──iTree['France', data=iTData[{'surface': 632733, 'inhabitants': 67400000}]]
└──iTree['Finland', data=iTData[{'surface': 338465, 'inhabitants': 5536146}]]
└──iTree['Antarktika', data=iTData[{'surface': 14000000, 'inhabitants': 1100}]]
E.g. Filtering can be done like this:
>>>item_filter = Filter.iTFilterData[data_key='inhabitants', data_value=iTInterval[0, 20000000]]
>>>iterator=root.iter_all[item_filter=item_filter]
>>>for i in iterator:
>>> print[i]
iTree["'New Zealand'", data=iTData[{'surface': 269652, 'inhabitants': 4900000}], subtree=[]]
iTree["'Finland'", data=iTData[{'surface': 338465, 'inhabitants': 5536146}], subtree=[]]
iTree["'Antarktika'", data=iTData[{'surface': 14000000, 'inhabitants': 1100}], subtree=[]]
Created: March-04, 2021 | Updated: January-22, 2022
- Implement a Tree From Scratch in Python
- Traverse a Binary Tree in Python
- Implement a Tree Using a Python Library
A Tree is one of the data structures. A data structure is nothing but how we organize the data in memory. A Tree is a combination of nodes [also known as vertices] and edges. A tree can have any number of nodes and edges. A node is where we store the data, and an edge is a path between 2
nodes. There are various types of trees available like a binary tree, ternary tree,
binary search tree, AVL tree, etc.