Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if an array represents Inorder of Binary Search tree or not in Python
The Binary Search Tree (BST) is a widely used data structure that maintains elements in a sorted hierarchical order. Each node in the BST follows a specific property:
- The values in the left subtree are always less than the current node value.
- The values in the right subtree are always greater than the current node value.
In this article, we will learn how to check if an array represents the inorder traversal of a BST in Python.
Understanding BST Inorder Traversal
Inorder traversal is a common way to access BST elements, which processes the left subtree, then the current node, and finally the right subtree. The inorder traversal of a BST always produces elements in strictly ascending order without duplicates.
To check if an array represents a valid BST inorder traversal, we need to verify that the array is sorted in strictly ascending order (no duplicates allowed).
Algorithm
The algorithm is straightforward:
- Iterate through the array from index 1 to the end
- Compare each element with its previous element
- If any element is less than or equal to the previous element, return False
- If all elements are in strictly ascending order, return True
Example 1: Valid BST Inorder
Let's check if the array [2, 5, 7, 10, 20, 30] represents a valid BST inorder traversal ?
def is_bst_inorder(array):
for i in range(1, len(array)):
if array[i] <= array[i - 1]:
return False
return True
array = [2, 5, 7, 10, 20, 30]
result = is_bst_inorder(array)
print(f"Array {array} represents BST inorder: {result}")
The output of the above code is ?
Array [2, 5, 7, 10, 20, 30] represents BST inorder: True
Example 2: Invalid BST Inorder (Duplicates)
Let's test an array with duplicate values, which cannot represent a valid BST inorder ?
def is_bst_inorder(array):
for i in range(1, len(array)):
if array[i] <= array[i - 1]:
return False
return True
array = [1, 22, 3, 4, 1]
result = is_bst_inorder(array)
print(f"Array {array} represents BST inorder: {result}")
The output of the above code is ?
Array [1, 22, 3, 4, 1] represents BST inorder: False
Example 3: Invalid BST Inorder (Decreasing Order)
Here's another example with elements in decreasing order ?
def is_bst_inorder(array):
for i in range(1, len(array)):
if array[i] <= array[i - 1]:
return False
return True
array = [10, 8, 6, 4, 2]
result = is_bst_inorder(array)
print(f"Array {array} represents BST inorder: {result}")
The output of the above code is ?
Array [10, 8, 6, 4, 2] represents BST inorder: False
Key Points
- Time Complexity: O(n) where n is the array length
- Space Complexity: O(1) as we only use constant extra space
- BST inorder traversal must be strictly ascending (no duplicates)
- Empty arrays and single-element arrays are considered valid
Conclusion
Checking if an array represents a BST inorder traversal is simply verifying that the array is in strictly ascending order. This approach leverages the fundamental property that BST inorder traversal always produces sorted elements without duplicates.
