Finding the minimum node of a BST
In this exercise, you will practice on a BST to find the minimum node.
To test your code, you can use the following tree:
It has been preloaded in the bst
variable (line 14):
bst = CreateTree()
You can print the result that returns the find_min()
method with this code (line 15):
print(bst.find_min())
This exercise is part of the course
Data Structures and Algorithms in Python
Exercise instructions
- Set
current_node
as the root. - Iterate over the nodes on the appropriate subtree.
- Update the value for
current_node
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
class BinarySearchTree:
def __init__(self):
self.root = None
def find_min(self):
# Set current_node as the root
current_node = ____
# Iterate over the nodes of the appropriate subtree
while current_node.____:
# Update current_node
current_node = current_node.____
return current_node.data
bst = CreateTree()
print(bst.find_min())