Chapter 5: 이진 탐색 트리
내용 정리
이진 탐색 트리 구조
이진 탐색 트리에서 탐색하기
```python from __future__ import annotations from typing import Optional class TreeNode: def __init__(self, value: int) -> None: self.value = value self.left: Optional[TreeNode] = None self.right: Optional[TreeNode] = None def insert_node_iterative(root: Optional[TreeNode], value: int) -> TreeNode: if root is None: return TreeNode(value) current = root while True: if value < current.value: if current.left is None: current.left = TreeNode(value) break current = current.left else: if current.right is None: current.right = TreeNode(value) break current = current.right return root def iterative_search(root: Optional[TreeNode], value: int) -> bool: current = root while current: if current.value == value: return True current = current.left if value < current.value else current.right return False ```
```python def insert_node_recursive(root: Optional[TreeNode], value: int) -> TreeNode: if root is None: return TreeNode(value) if value < root.value: root.left = insert_node_recursive(root.left, value) else: root.right = insert_node_recursive(root.right, value) return root def recursive_search(root: Optional[TreeNode], value: int) -> bool: if root is None: return False if root.value == value: return True if value < root.value: return recursive_search(root.left, value) else: return recursive_search(root.right, value) ```
이진 탐색 트리 변경하기
균형이 맞지 않는 트리
이진 탐색 트리 대량 구축
Last updated