I recommend anytree (I am the author).
Example:
from anytree import Node, RenderTree
udo = Node("Udo")
marc = Node("Marc", parent=udo)
lian = Node("Lian", parent=marc)
dan = Node("Dan", parent=udo)
jet = Node("Jet", parent=dan)
jan = Node("Jan", parent=dan)
joe = Node("Joe", parent=dan)
print(udo)
Node('/Udo')
print(joe)
Node('/Udo/Dan/Joe')
for pre, fill, node in RenderTree(udo):
print("%s%s" % (pre, node.name))
Udo
├── Marc
│ └── Lian
└── Dan
├── Jet
├── Jan
└── Joe
print(dan.children)
(Node('/Udo/Dan/Jet'), Node('/Udo/Dan/Jan'), Node('/Udo/Dan/Joe'))
anytree has also a powerful API with:
- simple tree creation
- simple tree modification
- pre-order tree iteration
- post-order tree iteration
- resolve relative and absolute node paths
- walking from one node to an other.
- tree rendering (see example above)
- node attach/detach hookups
GeeksforGeeks
geeksforgeeks.org › python › trees-in-python
Trees in Python - GeeksforGeeks
August 11, 2026 - A tree is built using nodes, where each node stores some data and references to its child nodes. In Python, a node can be represented by creating a class with a data field and a list to store its children.
27:33
Tree Data Structure: Simple Tutorial for Beginners in Python - YouTube
39:39
Binary Trees & Binary Search Trees - DSA Course in Python Lecture ...
05:35
Part 1 - Defining a Binary Tree Class in Python - YouTube
11:27
Introduction to Trees (Binary Tree) in Python - A Simplified Tutorial ...
03:57
Green Tree Python - YouTube
15:55
Next Level Designer Green Tree Python Collection Tour - YouTube
Top answer 1 of 16
400
I recommend anytree (I am the author).
Example:
from anytree import Node, RenderTree
udo = Node("Udo")
marc = Node("Marc", parent=udo)
lian = Node("Lian", parent=marc)
dan = Node("Dan", parent=udo)
jet = Node("Jet", parent=dan)
jan = Node("Jan", parent=dan)
joe = Node("Joe", parent=dan)
print(udo)
Node('/Udo')
print(joe)
Node('/Udo/Dan/Joe')
for pre, fill, node in RenderTree(udo):
print("%s%s" % (pre, node.name))
Udo
├── Marc
│ └── Lian
└── Dan
├── Jet
├── Jan
└── Joe
print(dan.children)
(Node('/Udo/Dan/Jet'), Node('/Udo/Dan/Jan'), Node('/Udo/Dan/Joe'))
anytree has also a powerful API with:
- simple tree creation
- simple tree modification
- pre-order tree iteration
- post-order tree iteration
- resolve relative and absolute node paths
- walking from one node to an other.
- tree rendering (see example above)
- node attach/detach hookups
2 of 16
159
Python doesn't have the quite the extensive range of "built-in" data structures as Java does. However, because Python is dynamic, a general tree is easy to create. For example, a binary tree might be:
class Tree:
def __init__(self):
self.left = None
self.right = None
self.data = None
You can use it like this:
root = Tree()
root.data = "root"
root.left = Tree()
root.left.data = "left"
root.right = Tree()
root.right.data = "right"
If you need an arbitrary number of children per node, then use a list of children:
class Tree:
def __init__(self, data):
self.children = []
self.data = data
left = Tree("left")
middle = Tree("middle")
right = Tree("right")
root = Tree("root")
root.children = [left, middle, right]
TutorialsPoint
tutorialspoint.com › python_data_structure › python_binary_tree.htm
Python Data Structure - Binary Tree
We create a tree data structure in python by using the concept os node discussed earlier. We designate one node as root node and then add more nodes as child nodes.
Codecademy
codecademy.com › learn › learn-data-structures-and-algorithms-with-python › modules › trees › cheatsheet
Learn Data Structures and Algorithms with Python: Trees Cheatsheet | Codecademy
The topmost node of a tree is called the “root”, and each node (with the exception of the root node) is associated with one parent node. Likewise, each node can have an arbitrary number of child nodes. An implementation of a TreeNode class in Python should have functions to add nodes, remove nodes, and traverse nodes within the tree.
Open Book Project
openbookproject.net › thinkcs › python › english2e › ch21.html
21. Trees — How to Think Like a Computer Scientist: Learning with Python 2nd Edition documentation
For example, the expression (3 + 7) * 9 yields the following tree: Notice that we have simplified the diagram by leaving out the names of the attributes. The parser we will write handles expressions that include numbers, parentheses, and the operators + and *. We assume that the input string has already been tokenized into a Python list (producing this list is left as an exercise).
Mga
comp.mga.edu › learning › python › module › 21
21. Trees | Learning Python | School of Computing | Middle Georgia State University
Implement basic tree operations (creating, traversing, searching, inserting, and deleting) using Python.
Open Book Project
openbookproject.net › thinkcs › python › english3e › trees.html
27. Trees — How to Think Like a Computer Scientist: Learning with Python 3
For example, the expression (3 + 7) * 9 yields the following tree: Notice that we have simplified the diagram by leaving out the names of the attributes. The parser we will write handles expressions that include numbers, parentheses, and the operators + and *. We assume that the input string has already been tokenized into a Python list (producing this list is left as an exercise).
Readthedocs
treelib.readthedocs.io › en › latest
Welcome to treelib’s documentation! — treelib 1.8.0 documentation
from treelib import Tree # Create a new tree tree = Tree() # Add root node tree.create_node("Company", "company") # Add departments tree.create_node("Engineering", "eng", parent="company") tree.create_node("Sales", "sales", parent="company") tree.create_node("HR", "hr", parent="company") # Add team members tree.create_node("Alice (CTO)", "alice", parent="eng") tree.create_node("Bob (Developer)", "bob", parent="eng") tree.create_node("Carol (Sales Manager)", "carol", parent="sales") tree.create_node("Dave (HR Manager)", "dave", parent="hr") # Display the tree tree.show()
Carbon Coffee
carboncoffee.hashnode.dev › implementing-general-tree-using-python
Implementing General Tree using Python - Carbon Coffee
February 10, 2024 - class TreeNode: def __init__(self,data): self.data = data self.children = [] self. parent = None def add_child(self,child): self.child = child child.parent = self self.children.append(child) def get_level(self): level = 0 p = self.parent while p : p = p.parent level += 1 return level def print_tree(self): print(' '*self.get_level() + '|--', end = '') print(self.data) if self.children: for each in self.children: each.print_tree() def run(): root = TreeNode('Eletronics') laptop = TreeNode('Laptop') root.add_child(laptop) laptop.add_child(TreeNode('Mac')) laptop.add_child(TreeNode('Windows')) lap
Medium
medium.com › swlh › making-data-trees-in-python-3a3ceb050cfd
Making Data Trees in Python. Learn about trees and how to implement… | by Keno Leon | Medium
June 30, 2026 - The key thing here is that these children have only one parent, if they had more this wouldn’t strictly be a tree ( it would be some sort of graph ), some examples: Dad -> Son, Daughter Boss -> Manager_1, Manager_2, Manager_3 Favorite Foods -> Chinese, Pizza, Tacos 1 -> Gold Medal,$10000,New Car,Sponsorship · Implementing these on Python should be straightforward since they are just expansions of the previous examples:
Pythonread
pythonread.github.io › dsa › trees.html
Tree Data Structure
learn about data types, variables, lists, tuples, dictionaries, decision-making statements, and loops,user-defined functions, object-oriented programming, threading, and Python scripting.