IniziaInizia gratis

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:

Graphical representation of a binary search 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())

Questo esercizio fa parte del corso

Data Structures and Algorithms in Python

Visualizza il corso

Istruzioni dell'esercizio

  • Set current_node as the root.
  • Iterate over the nodes on the appropriate subtree.
  • Update the value for current_node.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

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())
Modifica ed esegui il codice