Binary Search Tree (BST)
June 19, 2025 · View on GitHub
A Binary Search Tree (BST) is a hierarchical data structure where each node has at most two children, and the values are organized in a specific order: all values in the left subtree are less than the root, and all values in the right subtree are greater than the root.
Properties
- Left Subtree: All nodes have values less than the parent node
- Right Subtree: All nodes have values greater than the parent node
- No Duplicates: Each value appears only once (in this implementation)
- Recursive Structure: Each subtree is also a BST
Types of BSTs
- Full Binary Search Tree: Each node has either 0 or 2 children
- Complete Binary Search Tree: All levels are filled except possibly the last level, which is filled from left to right
- Perfect Binary Search Tree: Both full and complete, with all leaf nodes at the same depth
Structure

Methods
1. Insert
Description: Inserts a new value into the BST while maintaining the BST property.
Algorithm:
- If the tree is empty, create a new root node
- Compare the new value with the current node
- If the value is less than the current node, go to the left subtree
- If the value is greater than the current node, go to the right subtree
- If the value is equal, do nothing (duplicate handling)
- Repeat until finding an empty position and insert the new node
Time Complexity: O(h) where h is the height of the tree
- Best case: O(log n) for balanced trees
- Worst case: O(n) for skewed trees
2. Lookup
Description: Searches for a value in the BST and returns true if found, false otherwise.
Algorithm:
- Start from the root
- Compare the target value with the current node
- If the value is less than the current node, go to the left subtree
- If the value is greater than the current node, go to the right subtree
- If the value is equal, return true
- If we reach a null node, return false
Time Complexity: O(h) where h is the height of the tree
- Best case: O(log n) for balanced trees
- Worst case: O(n) for skewed trees
3. Min Value Node
Description: Finds the node with the minimum value in a subtree.
Algorithm:
- Start from the given node
- Keep traversing to the left child until reaching a node with no left child
- Return that node
Time Complexity: O(h) where h is the height of the subtree
4. Max Value Node
Description: Finds the node with the maximum value in a subtree.
Algorithm:
- Start from the given node
- Keep traversing to the right child until reaching a node with no right child
- Return that node
Time Complexity: O(h) where h is the height of the subtree
5. Breadth-First Search (BFS)
Description: Traverses the tree level by level, visiting all nodes at the current level before moving to the next level.

Algorithm:
- Use a queue to keep track of nodes to visit
- Start with the root node in the queue
- While the queue is not empty:
- Remove the first node from the queue
- Add its value to the result
- Add its left and right children to the queue (if they exist)
Time Complexity: O(n) where n is the number of nodes
Space Complexity: O(w) where w is the maximum width of the tree
Example Output: [20, 15, 25, 12, 16, 22, 30, 10, 14, 28, 35]
6. Depth-First Search (DFS)
Pre-Order Traversal
Description: Visits nodes in the order: Root → Left → Right

Algorithm:
- Visit the current node
- Recursively traverse the left subtree
- Recursively traverse the right subtree
Time Complexity: O(n)
Example Output: [20, 15, 12, 10, 14, 16, 25, 22, 30, 28, 35]
In-Order Traversal
Description: Visits nodes in the order: Left → Root → Right

Algorithm:
- Recursively traverse the left subtree
- Visit the current node
- Recursively traverse the right subtree
Time Complexity: O(n)
Example Output: [10, 12, 14, 15, 16, 20, 22, 25, 28, 30, 35]
Post-Order Traversal
Description: Visits nodes in the order: Left → Right → Root

Algorithm:
- Recursively traverse the left subtree
- Recursively traverse the right subtree
- Visit the current node
Time Complexity: O(n)
Example Output: [10, 14, 12, 16, 15, 22, 28, 35, 30, 25, 20]
Advantages
- Fast search, insert, and delete operations (O(log n) average case)
- Maintains sorted order
- Efficient for range queries
- Memory efficient
Disadvantages
- Performance degrades to O(n) for skewed trees
- No built-in balancing mechanism
- Complex deletion operation (not implemented in this version)
Applications
- Database indexing
- File system organization
- Symbol tables in compilers
- Priority queues
- Range queries