Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Binary Search Tree
Next article icon

Binary Search Tree In Python

Last Updated : 10 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A Binary search tree is a binary tree where the values of the left sub-tree are less than the root node and the values of the right sub-tree are greater than the value of the root node. In this article, we will discuss the binary search tree in Python.

What is a Binary Search Tree(BST)?

A Binary Search Tree is a data structure used in computer science for organizing and storing data in a sorted manner. Each node in a Binary Search Tree has at most two children, a left child and a right child.

Properties of BST

  • The left side of a node only has smaller values.
  • The right side of a node only has bigger values.
  • Both the left and right sides follow the same rule, forming a smaller binary search tree.
Binary Search Tree

In this binary tree

  • Root node: 8
  • Left subtree (3): values < 8
  • Right subtree (10): values > 8
  • Each subtree maintains the same rule, with values relative to their respective roots.

Creation of Binary Search Tree (BST) in Python

Below, are the steps to create a Binary Search Tree (BST).

  • Create a class Tree
  • In the parameterized constructor, we have the default parameter val = None
  • When the Tree class is instantiated without any parameter then it creates an empty node with left and right pointers
  • If we pass the value while instantiating then it creates a node having that value and left, right pointers are created with None Types.
Python
class Node:     def __init__(self, key):         self.value = key           self.left = None           self.right = None      # Creating the root node root = Node(5) 

Time complexity: O(1)
Space complexity: O(1)

Basic Operations on Binary Search Tree (BST)

Below, are the some basic Operations of Binary Search Tree(BST) in Python.

  • Insertion in Binary Search Tree
  • Searching in Binary Search Tree
  • Deletion in Binary Search Tree
  • Binary Search Tree (BST) Traversals

Insertion in Binary Search Tree(BST) in Python

Inserting a node in a Binary search tree involves adding a new node to the tree while maintaining the binary search tree (BST) property. So we need to traverse through all the nodes till we find a leaf node and insert the node as the left or right child based on the value of that leaf node. So the three conditions that we need to check at the leaf node are listed below:

Illustrations

Let us insert 13 into the below Binary search tree


Untitled-Diagramdrawio-(1)
Binary Search Tree


  • When we call the insert method then 13 is checked with the root node which is 15 , Since 13 < 15 we need to insert 13 to the left sub tree. So we recursively call left_sub_tree.insert(13)
  • Now the left child of 15 is 10 so 13 is checked with 10 and since 13>10 we need to insert 13 to the right sub tree. So we recursively call right_sub_tree.insert(13)
  • Here, the right child of 10 is 11 so 13 is checked with 11 and since 13>11 and we need to insert 13 to the right child of 11. Again we recursively call right_sub_tree.insert(13)
  • In this recursion call, we found there is no node which means we reached till the leaf node, so we create a node and insert the data in the node

After inserting 13 , the BST looks like this :


Untitled-Diagramdrawio-(2)
Inserted 13 Node in Binary Search Tree


Insertion in Binary Search Tree (BST) in Python

Python
# Python program to demonstrate # insert operation in binary search tree class Node:     def __init__(self, key):         self.left = None         self.right = None         self.val = key   # A utility function to insert # a new node with the given key def insert(root, key):     if root is None:         return Node(key)     if root.val == key:             return root     if root.val < key:             root.right = insert(root.right, key)     else:             root.left = insert(root.left, key)     return root   # A utility function to do inorder tree traversal def inorder(root):     if root:         inorder(root.left)         print(root.val, end=" ")         inorder(root.right)   r = Node(15) r = insert(r, 10) r = insert(r, 18) r = insert(r, 4) r = insert(r, 11) r = insert(r, 16) r = insert(r, 20) r = insert(r, 13)  # Print inorder traversal of the BST inorder(r) 

Output
4 10 11 13 15 16 18 20 

Time Complexity: 

  • The worst-case time complexity of insert operations is O(h) where h is the height of the Binary Search Tree. 
  • In the worst case, we may have to travel from the root to the deepest leaf node. The height of a skewed tree may become n and the time complexity of insertion operation may become O(n). 

Auxiliary Space: The auxiliary space complexity of insertion into a binary search tree is O(1)

Searching in Binary Search Tree in Python

Searching in Binary Search Tree is very easy because we don't need to traverse all the nodes. It is based on the value of nodes,which means if the value is less than root then we search in the left sub tree or else we search in right sub tree.

Illustrations

Steps to search a value 'v' in BST:

  • The value is checked with the root node, if it is equal to the root node then it returns the value
  • If the value is less than root node then it recursively calls the search operation in the left sub tree
  • If the value is greater than root node then it recursively calls the search operation in the right sub tree


Untitled-Diagramdrawio-(3)
Binary Search Tree


  • In above example BST search for value '8' begins from the root node.
  • If the value is not equal to the current node's value, the search continues recursively on the left subtree if the value is less than the current node's value.
  • Once the value matches a node, the search terminates, returning the node containing the value.

Searching in Binary Search Tree (BST) in Python

Python
class Node:     def __init__(self, key):         self.key = key         self.left = None         self.right = None  # function to search a key in a BST def search(root, key):        # Base Cases: root is null or key      # is present at root     if root is None or root.key == key:         return root          # Key is greater than root's key     if root.key < key:         return search(root.right, key)          # Key is smaller than root's key     return search(root.left, key)    # Creating a hard coded tree for keeping   # the length of the code small. We need   # to make sure that BST properties are   # maintained if we try some other cases. root = Node(50) root.left = Node(30) root.right = Node(70) root.left.left = Node(20) root.left.right = Node(40) root.right.left = Node(60) root.right.right = Node(80)  # Searching for keys in the BST print("Found" if search(root, 19) else "Not Found") print("Found" if search(root, 80) else "Not Found") 

Output
Not Found Found 

Time complexity: O(h), where h is the height of the BST.
Auxiliary Space: O(h) This is because of the space needed to store the recursion stack.

Deletion in Binary Search Tree in Python

Deleting a node in Binary search tree involves deleting an existing node while maintaining the properties of Binary Search Tree(BST). we need to search the node which needs to be deleted and then delete it.

Case 1 : While deleting a node having both left child and right child

  • After finding the node to be deleted, copy the value of right child to v and copy the right child's left pointer to left of v and right pointer


Untitled-Diagramdrawio-(6)
Delete node 18 in BST


  • In the above example, suppose we need to delete 18 then we need to replace 18 with the maximum value of its left or right sub tree
  • Let us replace it with 20 which is maximum value of right sub tree
  • So after deleting 18, the Binary search tree looks like this
Testdrawio-(1)

Case 2 : While deleting a node having either left child or right child

  • After finding the node to be deleted, we need to replace the value in the node with its left node it has left child or right node if it has right child.


Untitled-Diagramdrawio-(8)
Delete 16 in BST


  • In the above example, suppose we need to delete 16 then we need to replace 16 with the value of its right child node
  • So it will be replaced with 17 which is the value of its right child node
  • So after deleting 16, the Binary search tree looks like this


Untitled-Diagram-(4)
Deleted 16 in BST


Case 3 : While deleting a leaf node(a node which has no child)


Untitled-Diagramdrawio-(9)
Delete 4 in BST
  • In the above example, suppose we need to delete 4 which is a leaf node.
  • Simply we change value of the node, left and right pointers to None.
  • After deleting 4 the BST looks like this:
Untitled-Diagram-(6)
Deleted 4 in BST

Deletion in Binary Search Tree in Python

Python
class Node:     def __init__(self, key):         self.key = key         self.left = None         self.right = None  # Note that it is not a generic inorder successor  # function. It mainly works when the right child # is not empty, which is  the case we need in BST # delete. def get_successor(curr):     curr = curr.right     while curr is not None and curr.left is not None:         curr = curr.left     return curr  # This function deletes a given key x from the # given BST and returns the modified root of the  # BST (if it is modified). def del_node(root, x):        # Base case     if root is None:         return root      # If key to be searched is in a subtree     if root.key > x:         root.left = del_node(root.left, x)     elif root.key < x:         root.right = del_node(root.right, x)              else:         # If root matches with the given key          # Cases when root has 0 children or          # only right child         if root.left is None:             return root.right          # When root has only left child         if root.right is None:             return root.left          # When both children are present         succ = get_successor(root)         root.key = succ.key         root.right = del_node(root.right, succ.key)              return root  # Utility function to do inorder traversal def inorder(root):     if root is not None:         inorder(root.left)         print(root.key, end=" ")         inorder(root.right)  # Driver code if __name__ == "__main__":     root = Node(10)     root.left = Node(5)     root.right = Node(15)     root.right.left = Node(12)     root.right.right = Node(18)     x = 15      root = del_node(root, x)     inorder(root)     print() 

Output
5 10 12 18  

Time Complexity: O(h), where h is the height of the BST. 
Auxiliary Space: O(h).

Traversals in Binary Search Tree in Python

Below, are the traversals of Binary Search Tree (BST) in Python which we can perform as SImilar to Binary Tree.

  • Inorder Traversal
  • Preorder Traversal
  • Postorder Traversal

Traversals in BST in Python

Python
# Python3 code to implement the approach  # Class describing a node of tree class Node:     def __init__(self, v):         self.left = None         self.right = None         self.data = v  # Inorder Traversal def printInorder(root):     if root:         # Traverse left subtree         printInorder(root.left)                  # Visit node         print(root.data,end=" ")                  # Traverse right subtree         printInorder(root.right)  # Driver code if __name__ == "__main__":     # Build the tree     root = Node(100)     root.left = Node(20)     root.right = Node(200)     root.left.left = Node(10)     root.left.right = Node(30)     root.right.left = Node(150)     root.right.right = Node(300)      # Function call     print("Inorder Traversal:",end=" ")     printInorder(root)      # This code is contributed by ajaymakvana. 

Output
Inorder Traversal: 10 20 30 100 150 200 300 

Time complexity: O(N), Where N is the number of nodes.
Auxiliary Space: O(h), Where h is the height of tree

Applications of BST:

  • Graph algorithms: BSTs can be used to implement graph algorithms, such as in minimum spanning tree algorithms.
  • Priority Queues: BSTs can be used to implement priority queues, where the element with the highest priority is at the root of the tree, and elements with lower priority are stored in the subtrees.
  • Self-balancing binary search tree: BSTs can be used as a self-balancing data structures such as AVL tree and Red-black tree.
  • Data storage and retrieval: BSTs can be used to store and retrieve data quickly, such as in databases, where searching for a specific record can be done in logarithmic time.

Advantages:

  • Fast search: Searching for a specific value in a BST has an average time complexity of O(log n), where n is the number of nodes in the tree. This is much faster than searching for an element in an array or linked list, which have a time complexity of O(n) in the worst case.
  • In-order traversal: BSTs can be traversed in-order, which visits the left subtree, the root, and the right subtree. This can be used to sort a dataset.
  • Space efficient: BSTs are space efficient as they do not store any redundant information, unlike arrays and linked lists.

Disadvantages:

  • Skewed trees: If a tree becomes skewed, the time complexity of search, insertion, and deletion operations will be O(n) instead of O(log n), which can make the tree inefficient.
  • Additional time required: Self-balancing trees require additional time to maintain balance during insertion and deletion operations.
  •  Efficiency: BSTs are not efficient for datasets with many duplicates as they will waste space.



Next Article
Binary Search Tree

M

mohithsharmajf3o
Improve
Article Tags :
  • Python
  • Data Structures
  • Data Structures
  • Python-DSA
Practice Tags :
  • Data Structures
  • Data Structures
  • python

Similar Reads

  • Ceil in a Binary Search Tree Python
    In a Binary Search Tree (BST), the ceiling of a given value is the smallest element in the tree that is greater than or equal to the given value. Finding the ceiling in a BST can be a useful operation when working with ordered data. In this article, we will explore how to find the ceiling in a Binar
    3 min read
  • Binary Tree in Python
    Binary Tree is a non-linear and hierarchical data structure where each node has at most two children referred to as the left child and the right child. The topmost node in a binary tree is called the root, and the bottom-most nodes are called leaves. Representation of Binary TreeEach node in a Binar
    9 min read
  • Binary Search Tree
    A Binary Search Tree (or BST) is a data structure used in computer science for organizing and storing data in a sorted manner. Each node in a Binary Search Tree has at most two children, a left child and a right child, with the left child containing values less than the parent node and the right chi
    3 min read
  • What is Binary Search Tree
    A binary search tree (BST) is a binary tree in which the left subtree of a node contains only nodes with less value and the right subtree of a node contains only nodes with values greater than it. Characteristics of Binary Search Tree: The properties of a binary search tree are as follows: Ordering
    3 min read
  • B Tree in Python
    A B-tree is a self-balancing tree data structure that maintains sorted data and allows searches, sequential access, insertions, and deletions in logarithmic time. Table of Content Characteristics of B-TreesTraversal of B-Tree in PythonSearch operation in B Tree in PythonInsert operation in B Tree in
    14 min read
  • B+ Tree in Python
    In computer science, data structures are crucial in efficiently managing and organizing data. Among these, the B+ tree is a powerful and important data structure, widely used in databases and file systems. In this article, we will discuss the concept of B+ trees, exploring their structure, operation
    12 min read
  • Red Black Tree in Python
    Red Black Tree is a self-balancing binary search tree where each node has an extra bit representing its color, either red or black. By constraining how nodes are colored during insertions and deletions, red-black trees ensure the tree remains approximately balanced during all operations, allowing fo
    11 min read
  • Tree Sort in Python
    Tree sort is a sorting algorithm that builds a Binary Search Tree (BST) from the elements of the array to be sorted and then performs an in-order traversal of the BST to get the elements in sorted order. In this article, we will learn about the basics of Tree Sort along with its implementation in Py
    3 min read
  • AVL Tree in Python
    The AVL tree in Python is a self–balancing binary search tree that guarantees the difference of the heights of the left and right subtrees of a node is at most 1. The algorithm is named after its inventors, Georgy Adelson-Velsky, and Evgenii Landis who published their paper in 1962. The AVL tree kee
    6 min read
  • Implement Binary Search Tree(BST) Iterator
    Binary Search Trees (BSTs) are data structures, in computer science. They are commonly used for searching, insertion and deletion operations. In situations it becomes necessary to traverse a BST in an order. For example an in order traversal visits nodes in ascending order based on their values. Thi
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences