找出 BST 的最小節點
在這個練習中,你會在一棵 BST 上練習找出最小節點。
你可以使用下列的樹來測試程式碼:

這棵樹已經預先載入在變數 bst 中(第 14 行):
bst = CreateTree()
你可以用下列程式碼(第 15 行)列印 find_min() 方法的回傳結果:
print(bst.find_min())
本練習屬於課程
Data Structures and Algorithms in Python
練習說明
- 將
current_node設為根節點。 - 在適當的子樹上逐一走訪節點。
- 更新
current_node的值。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
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())