š³ Trees¶
A hierarchical data structure with nodes connected by edges, starting from a root node.
Tree Terminology¶
āāāāā
ā A ā ā Root
āāā¬āā
āāāāāā“āāāāā
āāā“āā āāāā“āāā
ā B ā ā C ā ā Children of A
āāā¬āā āāāā¬āāā
āāāā“āāā āāāā“āāā
āā“ā āāā“ā āā“āā āāā“ā
āDā ā Eā ā Fā ā Gā ā Leaves
āāā āāāā āāāā āāāā
- Root: Top node (A)
- Parent/Child: A is parent of B, B is child of A
- Leaf: Node with no children (D, E, F, G)
- Height: Longest path from root to leaf
- Depth: Distance from root
Binary Tree¶
Each node has at most 2 children (left and right).
Tree Traversals¶
# Inorder (left, root, right)
def inorder(node):
if node:
inorder(node.left)
print(node.val)
inorder(node.right)
# Preorder (root, left, right)
def preorder(node):
if node:
print(node.val)
preorder(node.left)
preorder(node.right)
# Postorder (left, right, root)
def postorder(node):
if node:
postorder(node.left)
postorder(node.right)
print(node.val)
Binary Search Tree (BST)¶
Left child < parent < right child
Search, Insert, Delete: O(log n) average, O(n) worst
Common Operations¶
| Operation | Time (balanced) |
|---|---|
| Search | O(log n) |
| Insert | O(log n) |
| Delete | O(log n) |
| Traversal | O(n) |
Next Steps¶
Practice with examples.py!