TreeLib
TreeLib provides a Python implementation for creating, manipulating, and visualizing tree data structures. It allows users to build hierarchical data, traverse nodes, and render trees to various outputs like console text or Graphviz. It is actively maintained with a moderate release cadence, with the current version being 1.8.0.
Warnings
- breaking treelib v1.8.0 and later no longer support Python 3.6 due to its End-of-Life. Attempting to install or run treelib 1.8.0+ on Python 3.6 will result in errors.
- breaking The `treelib.plugin` module, including `BaseConnector` and `NodeMixin`, was removed in v1.7.0. Code relying on these deprecated components will break.
- gotcha Methods like `Tree.to_graphviz()` require the `graphviz` Python package (and the Graphviz system library) to be installed separately. Without these, such methods will raise an `ImportError` or similar error.
- gotcha Each node in a `treelib.Tree` must have a unique `identifier`. Reusing identifiers for different nodes will lead to unexpected behavior, as `treelib` uses them internally for lookup and maintaining structure. This is often confused with the `tag` attribute, which can be duplicated.
Install
-
pip install treelib -
pip install treelib[graphviz]
Imports
- Tree
from treelib import Tree
Quickstart
from treelib import Tree
tree = Tree()
tree.create_node("Harry", "harry") # Root node
tree.create_node("Jane", "jane", parent="harry")
tree.create_node("Bill", "bill", parent="harry")
tree.create_node("Diane", "diane", parent="jane")
tree.create_node("George", "george", parent="jane")
tree.create_node("Mark", "mark", parent="bill")
tree.show()