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
Answer from c0fec0de on Stack Overflow
🌐
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.
🌐
W3Schools
w3schools.com › python › python_dsa_trees.asp
Python Trees
Python Examples Python Compiler ... Plan Python Interview Q&A Python Training ... A tree is a hierarchical data structure consisting of nodes connected by edges....
🌐
Built In
builtin.com › articles › tree-python
Python Tree Implementation: A Guide
May 10, 2024 - A tree is a data structure used to show a hierarchical relationship, such as an organizational chart. It can be created in Python using the bigtree package,
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.
🌐
Medium
medium.com › pythoneers › getting-started-with-trees-in-python-a-beginners-guide-4e68818e7c05
Getting Started with Trees in Python: A Beginner’s Guide | by Rajat Sharma | The Pythoneers | Medium
April 15, 2024 - Height: The length of the longest path from a node to a leaf node, indicating the depth of the tree. In Python, trees can be implemented using classes and objects.
🌐
Pythonspot
pythonspot.com › home › beginner › python tree
Python Tree — Tutorial with Examples
January 1, 2026 - A tree with eight nodes. The root of the tree (5) is on top. Python does not have built-in support for trees.
Find elsewhere
🌐
Bacancy Technology
bacancytechnology.com › qanda › python › tree-implementation-in-python
Step-by-Step Guide for Tree Implementation in Python
April 24, 2024 - The TreeNode class is defined with an __init__ method to initialize each node with a key and set its left and right children to None.
🌐
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.
🌐
PythonForBeginners
pythonforbeginners.com › home › tree data structure in python
Tree Data Structure in Python - PythonForBeginners.com
June 9, 2023 - A Python tree is a data structure in which data items are connected using references in a hierarchical manner in the form of edges and nodes. Each tree consists of a root node from which we can access the elements of the tree.
🌐
Towards Data Science
towardsdatascience.com › home › latest › python tree implementation with bigtree
Python Tree Implementation with BigTree | Towards Data Science
November 8, 2022 - This article will introduce basic tree concepts, how to construct trees with the bigtree Python package, tree traversal, search, modification, and export methods.
🌐
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).
🌐
Smithsonian's National Zoo
nationalzoo.si.edu › animals › green-tree-python
Green tree python | Smithsonian's National Zoo and Conservation Biology Institute
Green tree pythons are named for their vibrant green color. These nonvenomous snakes and found in the tropical rainforests of New Guinea, eastern Indonesia, and the northeast Cape York Peninsula of Australia.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › binary-tree-in-python
Binary Tree in Python - GeeksforGeeks
July 23, 2025 - The topmost node in a binary tree is called the root, and the bottom-most nodes are called leaves. ... # A Python class that represents # an individual node in a Binary Tree class Node: def __init__(self, key): self.left = None self.right = None self.val = key