BST Find Maximum Element in DSA Python - Time & Space Complexity
We want to understand how long it takes to find the largest number in a Binary Search Tree (BST).
How does the time needed grow as the tree gets bigger?
Analyze the time complexity of the following code snippet.
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def find_max(root):
current = root
while current.right is not None:
current = current.right
return current.val
This code finds the maximum value by moving right until no more right child exists.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Moving from one node to its right child.
- How many times: At most once per level down the right side of the tree.
As the tree grows, the number of steps depends on the height of the tree, especially the rightmost path.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 4 steps (if tree is balanced) |
| 100 | Up to 7 steps (balanced tree height) |
| 1000 | Up to 10 steps (balanced tree height) |
Pattern observation: Steps grow with tree height, which is about log of number of nodes if balanced.
Time Complexity: O(h) where h is the tree height
This means the time depends on how tall the tree is, not just how many nodes it has.
[X] Wrong: "Finding max always takes time proportional to the number of nodes (O(n))."
[OK] Correct: The search only follows the right side, so it depends on tree height, not total nodes.
Understanding how tree height affects search time helps you explain why balanced trees are faster and shows your grasp of data structure efficiency.
"What if the tree is not balanced and looks like a linked list going right? How would the time complexity change?"
Practice
Solution
Step 1: Understand BST property
In a BST, all values in the right subtree are greater than the root, so the largest value is towards the right.Step 2: Locate the maximum element
The maximum element is found by moving to the right child repeatedly until no more right child exists.Final Answer:
At the rightmost node -> Option CQuick Check:
Maximum element = rightmost node [OK]
- Thinking maximum is at the root
- Looking at left subtree for maximum
- Assuming maximum can be any leaf
left and right attributes?Solution
Step 1: Check traversal direction for max
Maximum is found by moving right until no right child exists, so loop should checkroot.right.Step 2: Verify code correctness
def find_max(root): while root.right: root = root.right return root.data correctly loops whileroot.rightexists, then returnsroot.data. Others either go left or incorrectly use max function.Final Answer:
while root.right: root = root.right; return root.data -> Option AQuick Check:
Loop right child until none = correct max finder [OK]
- Looping on root.left instead of root.right
- Returning root.data without traversal
- Using max() on children without recursion
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def find_max(root):
current = root
while current.right:
current = current.right
return current.data
root = Node(10)
root.left = Node(5)
root.right = Node(20)
root.right.right = Node(30)
print(find_max(root))Solution
Step 1: Trace the BST structure
Root is 10, right child 20, right of 20 is 30. Maximum is the rightmost node 30.Step 2: Follow the find_max function
Function moves right from 10 to 20, then to 30, then stops as 30 has no right child, returning 30.Final Answer:
30 -> Option BQuick Check:
Rightmost node data = 30 [OK]
- Returning root data instead of rightmost
- Confusing left and right children
- Stopping traversal too early
def find_max(root):
if root is None:
return None
while root.left:
root = root.left
return root.dataSolution
Step 1: Analyze traversal direction
The code loops whileroot.leftexists, moving left, but maximum is on the right side in BST.Step 2: Identify correct traversal
To find maximum, loop should be onroot.rightuntil no right child remains.Final Answer:
It incorrectly traverses left child instead of right child -> Option DQuick Check:
Max element found by going right, not left [OK]
- Looping on left child for max
- Ignoring empty tree check
- Returning wrong node data
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def find_max(root):
# Your code here
pass
Choose the correct implementation to find the maximum element.Solution
Step 1: Understand duplicates in BST
Duplicates allowed only on right means maximum is still the rightmost node.Step 2: Choose correct traversal
def find_max(root): current = root while current.right: current = current.right return current.data correctly moves right until no right child exists, returning the maximum value.Step 3: Analyze other options
def find_max(root): if root.right is None: return root.data return find_max(root.left) incorrectly recurses left; def find_max(root): max_val = root.data max_val = max(max_val, find_max(root.left)) max_val = max(max_val, find_max(root.right)) return max_val lacks None checks before recursing, causing AttributeError on None children; def find_max(root): if root is None: return float('-inf') return max(root.data, find_max(root.left)) ignores right subtree.Final Answer:
while current.right: current = current.right; return current.data -> Option AQuick Check:
Rightmost node = max even with duplicates [OK]
- Searching left subtree for max
- Checking both subtrees unnecessarily
- Ignoring None checks causing errors
Ready to prove it?
Pass the test and this pattern moves unknown
