시작하기무료로 시작하기

BST에서 최소 노드 찾기

이번 연습에서는 BST에서 최소 노드를 찾는 방법을 연습해 보겠습니다.

다음 트리를 사용해 코드를 테스트할 수 있어요:

Graphical representation of a binary search tree.

이 트리는 bst 변수에 미리 로드되어 있어요(14행):

bst = CreateTree()

find_min() 메서드가 반환하는 결과는 다음 코드로 출력할 수 있어요(15행):

print(bst.find_min())

이 연습은 강의의 일부입니다

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())
코드 편집 및 실행