Binary Search Tree Traversals

Mohammad M Rahman
4 min readOct 14, 2022


# Implement binary search tree insert method
# Creating a class of node
class Node(object):
def __init__(self):
self.left = None
self.right = None = None

# Inserting a node in BST
def insertion(val):
# if first node it will be considered as a root
if ( == None):
print(val, " Inserted as root") = val
# else executed for all the other insertions
# Pointer to move around tree to search for a place for new node
p = root

# Creating a new node
n = Node() = val

# Iterating to search for an appropriate place in the new node
while (1):
# if val is less than the current node p indicates that new node will be inserted on left subtree
if (val <
if (p.left == None):
print(val, " Inserted on left of ",
p.left = n
p = p.left
# if val is greater than the current node p indicates that new node will be inserted on right subtree
if (p.right == None):
print(val, " Inserted on right of",
p.right = n
p = p.right

def inorder(node):
if node:
# Traverse left subtree
# Visit node
# Traverse right subtree

# A function to do preorder tree traversal
def printPreorder(root):
if root:
# First print the data of node

# Then recur on left child

# Finally recur on right child

def printPostorder(root):
if root:
# First recur on left child

# the recur on right child

# now print the data of node

root = Node()
#print("Value ", deletion(25), "deleted successfully")
#print("Value ", deletion(10), "deleted successfully")
root = Node()

print("In order:")
print("Pre order:")
print("Post order:")

Time complexity

i. Best case scenario: In the event that the root node is selected as the node
to be searched, just one comparison will need to be made, resulting in aconstant duration. In the best situation, time complexity would be O. (1).

ii. The average case: When a binary search tree is balanced
(a binary search tree is considered balanced if the height difference between nodes onthe left and right subtrees is not greater than one), height is logN, where N is thenumber of nodes in the tree.The time it takes to search for a key in a binary search tree is equal to the height ofthe tree, which is logN, so the time complexity for searching is O(logN) in the averagecase. During the search operation, we will continue to traverse through nodes one at atime. Suppose we find the element in the second level, in which case we have performedtwo comparisons. If we get the element in the third level, we will be performing threecomparisons.Worst-case scenario: If the binary search tree is imbalanced or distorted(skewed binary search tree is a tree in which there are no nodes available in leftsubtree or right subtree see the image below to get better understanding)Space complexity: Since there are a maximum of n stack frames that may be held inmemory at any given moment, the space cost of searching a node in a BST would be O(n),where n is the depth of the tree (number of nodes present in a tree).A single exploration of every node in the tree is known as tree traversal.In-order traversal can be implemented either iteratively or recursively. Recursion isthe method we're interested in using, though. When traversing a tree in order, theleft child is explored first, followed by the root then the right child.In light of this, the following stages will be used to implement a recursive procedure:

Visit the left subtree repeatedly
Go to the node.
Continually go to the appropriate subtree

A point between the recursive traversals of the left and right subtrees is visited
during an inorder traversal. A binary tree T's inorder traversal can be thought of asgoing "from left to right" across its nodes.Pre-order traversal involves the following tree exploration:

Go to the node.
Investigate the left subtree repeatedly
Investigate the appropriate subtree repeatedly

We only need to reverse the visit order in postorder traversal; the method is
illustrated as follows:

Visit the left subtree repeatedly
Continually go to the appropriate subtree
Go to the node.

(Time and Space Complexity of Binary Search Tree (BST), 2021; Tree Traversals (Inorder, Preorder and Postorder), 2009)


Time and Space complexity of Binary Search Tree (BST). (2021, December 29). OpenGenus IQ: Computing Expertise & Legacy.

Tree Traversals (Inorder, Preorder and Postorder). (2009, June 23). GeeksforGeeks.