Aan de slagGa gratis aan de slag

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())

Deze oefening maakt deel uit van de cursus

Data Structures and Algorithms in Python

Cursus bekijken

Oefeninstructies

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

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

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())
Code bewerken en uitvoeren