GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask
  • Introduction1 min read
  • Input & Output2 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Trees in Python - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask

Trees in Python

Last Updated : 11 Aug, 2026

A tree is a hierarchical, non-linear data structure made up of nodes connected by edges. It starts with a single root node, and every child node has exactly one parent, creating a parent-child relationship. Trees are widely used to represent hierarchical data such as file systems, organizational structures, XML/HTML documents and decision trees.

420046786
Representation of Trees

Basic Tree Terminologies

Before learning different types of trees, let's understand some important terms used in a tree data structure.

  • Root Node: The topmost node of a tree that has no parent. Every non-empty tree has exactly one root node. (Example: A)
  • Parent Node: A node that has one or more child nodes. (Example: B is the parent of D and E.)
  • Child Node: A node directly connected below a parent node. (Example: D and E are the children of B.)
  • Leaf Node (External Node): A node that has no children. (Example: F, G, H, I, J, and K are leaf nodes.)
  • Internal Node: A node that has at least one child. (Example: A, B, C, and E are internal nodes.)
  • Ancestor: Any node on the path from the root to a given node. (Example: A and B are ancestors of E.)
  • Descendant: Any node that lies below another node in the tree. (Example: D and E are descendants of B.)
  • Sibling: Nodes that share the same parent. (Example: D and E are siblings because both have parent B.)
  • Level of a Node: The number of edges from the root node to that node. The root node is always at level 0.
  • Neighbor: The parent or child of a node is called its neighbor.
  • Subtree: A tree formed by any node along with all of its descendants.

Representation of a Node

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.

Example: Here, we create a simple Node class to represent a node in a tree.

Python
class Node:
    def __init__(self, data):
        self.data = data
        self.children = []

Explanation:

  • data stores the value of the node.
  • children is a list that stores references to all child nodes.
  • Each object created from the Node class represents one node in the tree.

Types of Tree Data Structures

Trees come in different forms, each designed for specific use cases. Below are some of the most commonly used tree data structures.

1. Binary Tree: a tree in which each node can have at most two children, called the left child and right child. It is commonly used to represent hierarchical data and forms the basis for many other tree structures.

To know more, refer to: Binary Tree in Python

2. Binary Search Tree (BST): a special type of binary tree where:

  • The left subtree contains values smaller than the parent node.
  • The right subtree contains values greater than the parent node.

This ordering makes searching, insertion and deletion more efficient.

To know more, refer to: Binary Search Tree in Python

3. AVL Tree: a self-balancing Binary Search Tree. After every insertion or deletion, it automatically balances itself so that the height difference between the left and right subtrees of any node is at most 1.

To know more, refer to: AVL Tree in Python

4. Red Black Tree: another self-balancing Binary Search Tree. It uses color-based balancing rules to maintain an approximately balanced structure, ensuring efficient search, insertion and deletion operations.

To know more, refer to: Red Black Tree in Python

5. B-Tree: a balanced tree in which each node can contain multiple keys and multiple child nodes. It is widely used in databases and file systems because it minimizes disk access and efficiently handles large amounts of data.

To know more, refer to: B Tree in Python

6. B+ Tree: a variation of the B-Tree in which all data is stored in the leaf nodes, while internal nodes are used only for searching. The leaf nodes are linked together, making B+ Trees ideal for fast sequential access and database indexing.

To know more, refer to: B+ Tree in Python

Properties of a Tree Data Structure

Some important properties of a tree data structure are:

  1. Number of Edges: A tree with N nodes always has N − 1 edges. There is exactly one unique path between any two nodes in a tree.
  2. Depth of a Node: The depth of a node is the number of edges from the root node to that node. The root node always has a depth of 0.
  3. Height of a Node: The height of a node is the number of edges on the longest path from that node to any leaf node.
  4. Height of a Tree: The height of a tree is the height of its root node, which is equal to the number of edges on the longest path from the root to a leaf node.
  5. Degree of a Node: The degree of a node is the number of children it has. A leaf node always has a degree of 0.
  6. Degree of a Tree: The degree of a tree is the highest degree among all the nodes in the tree.

Implementation of Tree

A way to represent a tree in Python is by creating a Node class. Each node stores its own data and a list of child nodes. We can then connect multiple nodes to form a tree.

Python
class Node:
    def __init__(self, data):
        self.data = data
        self.children = []

root = Node("A")
node_b = Node("B")
node_c = Node("C")
node_d = Node("D")

root.children.append(node_b)
root.children.append(node_c)
node_b.children.append(node_d)
print("Root:", root.data)

for child in root.children:
    print("Child:", child.data)

print("Child of B:", node_b.children[0].data)

Output
Root: A
Child: B
Child: C
Child of B: D

Explanation:

  • A Node class is created with data and children attributes.
  • Four nodes (A, B, C and D) are created.
  • B and C are added as children of A.
  • D is added as a child of B, forming a simple tree.
  • Finally, the tree structure is displayed by accessing the child nodes.

Tree Traversal Techniques

Tree traversal is the process of visiting every node in a tree exactly once in a specific order. Different traversal techniques are used depending on how the tree needs to be processed. Three most common depth-first traversal techniques are:

  1. Inorder Traversal: Visits the left subtree, then the root node, and finally the right subtree.
  2. Preorder Traversal: Visits the root node first, followed by the left subtree and then the right subtree.
  3. Postorder Traversal: Visits the left subtree, then the right subtree, and finally the root node.

To know more, refer to: Tree Traversal Techniques in Python

Comment

Explore

    Python Fundamentals
    • Introduction1 min read
    • Input & Output2 min read
    • Variables4 min read
    • Operators4 min read
    • Keywords2 min read
    • Data Types4 min read
    • Conditional Statements3 min read
    • Loops3 min read
    • Functions4 min read
    Python Data Structures
    • String4 min read
    • List3 min read
    • Tuples3 min read
    • Dictionary3 min read
    • Set2 min read
    • Arrays5 min read
    Advanced Python
    • OOP Concepts3 min read
    • Exception Handling5 min read
    • File Handling4 min read
    • Python Database2 min read
    • MongoDB3 min read
    • MySQL7 min read
    • Packages4 min read
    • Modules3 min read
    • DSA Libraries5 min read
    • Python GUI2 min read
    Data Science with Python
    • Numpy3 min read
    • Pandas4 min read
    • Matplotlib3 min read
    • Seaborn3 min read
    • StatsModel2 min read
    • Model Building6 min read
    • TensorFlow2 min read
    • PyTorch5 min read
    Web Development with Python
    • Flask3 min read
    • Django5 min read
    • Django ORM4 min read
    • Jinja2 Templating5 min read
    • Django Templates5 min read
    • REST API3 min read
    • Build API with DRF4 min read
    Python Practice
    • Quiz1 min read
    • Practice Problems2 min read
    • Interview Q & A15+ min read
    Courses
    • Placement 360 Course2 min read
    • Data Science 360 Course2 min read
    • AI Engg Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.