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.

Traversals in BST in Python

Python3
class Tree:
    def __init__(self, val=None):
        # Initialize the node with a value and left and right subtrees
        self.value = val
        if self.value:
            self.left = Tree()
            self.right = Tree()
        else:
            self.left = None
            self.right = None
    
    def isempty(self):
        # Check if the tree is empty
        return self.value is None
    
    def insert(self, data):
        # Insert a value into the tree
        if self.isempty():
            self.value = data
            self.left = Tree()
            self.right = Tree()
        elif self.value == data:
            return
        elif data < self.value:
            self.left.insert(data)
        elif data > self.value:
            self.right.insert(data)
    
    def isleaf(self):
        # Check if the node is a leaf node
        return self.left is None and self.right is None
    
    def inorder(self):
        # Perform inorder traversal
        if self.isempty():
            return []
        else:
            return self.left.inorder() + [self.value] + self.right.inorder()

# Example usage
t = Tree(10)
t.insert(8)
t.insert(6)
t.insert(12)

print("Inorder Traversal : ")
print(t.inorder())

Output
Inorder Traversal : 
[6, 8, 10, 12]

Time complexity: O(log n)
Space complexity: O(n)

Binary Search Tree In Python

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.

Similar Reads

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....

Creation of Binary Search Tree (BST) in Python

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

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(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:...

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....

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....

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....

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....

Contact Us