Cs Notes Binary Search Trees 23 09b346c3
Cs Notes Binary Search Trees 23 09b346c3
Introduction
A Binary Search Tree (BST) is a tree data structure where each node has at most two children, with left
subtree values smaller and right subtree values larger.
1. Node Structure
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
2. Diagram
[10]
/ \
[5] [15]
/ /
[3] [12]
3. Operations
Insertion and search are O(h) where h is the tree height, ideally O(log n) for balanced trees.
Summary Table
Operation Complexity
Insert O(h)
Search O(h)
Delete O(h)
Exercises
1. Implement BST insertion. 2. Find the height of a BST. 3. Check if a tree is a valid BST.