Day 15
Day 15: Binary Search Trees
15/60 Days
Binary Search Trees#
Welcome to Day 15 of our 60 Days of Coding Algorithm Challenge! Today, we’ll dive into Binary Search Trees (BST), a powerful data structure that combines the efficiency of binary search with the flexibility of a linked data structure.
What is a Binary Search Tree?#
A Binary Search Tree is a binary tree with the following properties:
- The left subtree of a node contains only nodes with keys less than the node’s key.
- The right subtree of a node contains only nodes with keys greater than the node’s key.
- Both the left and right subtrees must also be binary search trees.
These properties make BSTs efficient for search, insertion, and deletion operations.
Implementing a Binary Search Tree in Python#
Let’s implement a basic Binary Search Tree:
1class BSTNode:
2 def __init__(self, key):
3 self.key = key
4 self.left = None
5 self.right = None
6
7class BinarySearchTree:
8 def __init__(self):
9 self.root = None
10
11 def insert(self, key):
12 self.root = self._insert_recursive(self.root, key)
13
14 def _insert_recursive(self, root, key):
15 if root is None:
16 return …Keep your momentum going
Create a free account to unlock the full lesson, all 60 days, and track your progress as you go.
- Full access to all 60 daily lessons
- Track completion and build a daily streak
- Get interview-ready, one algorithm at a time
Join 2,900+ learners · 4.8/5 average rating