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.
class Tree:
def __init__(self,val=None):
self.value = val
if self.value:
self.left = Tree()
self.right = Tree()
else:
self.left = None
self.right = None
#creating a node in the tree
t = Tree(5)
Time complexity: O(1)
Space complexity: O(1)
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.
Contact Us