Binary Search Tree
A binary search tree (BST) is a node-based data structure in which each node has at most two children, and every node's left subtree contains only values less than the node while its right subtree contains only values greater, enabling…
Definition
A binary search tree (BST) is a node-based data structure in which each node has at most two children, and every node's left subtree contains only values less than the node while its right subtree contains only values greater, enabling fast lookup, insertion, and deletion.
Overview
A binary search tree is one of the foundational data structures taught alongside linked lists and hash tables, because it demonstrates how ordering data hierarchically can make search dramatically faster than scanning a flat list. Each node stores a key, an optional value, and pointers to a left and right child. The invariant that left descendants are smaller and right descendants are larger means an in-order traversal visits nodes in sorted order, and a lookup can discard half the remaining tree at each step, similar in spirit to binary search over a sorted array. On a balanced tree, search, insertion, and deletion all run in O(log n) time, which is why understanding Big O Notation matters when reasoning about BST performance. The catch is that a plain BST is not self-balancing: inserting already-sorted data can degrade it into a linked list with O(n) operations. This is why real-world systems typically use self-balancing variants (AVL trees, red-black trees) or B-trees for on-disk indexes, which apply the same ordering principle with extra bookkeeping to keep the tree shallow. BSTs underpin many higher-level structures: ordered maps and sets in standard libraries, database indexes, and expression parsers. They're also a frequent whiteboard interview topic because implementing insert, search, delete, and traversal correctly requires careful handling of recursion and edge cases like deleting a node with two children. Because pointer-chasing through tree nodes involves scattered memory access, BSTs can be slower in practice than array-based structures for small datasets despite better asymptotic complexity — a nuance worth understanding before defaulting to a tree for every ordered-data problem.
Key Concepts
- Left-subtree-smaller, right-subtree-larger ordering invariant at every node
- O(log n) average-case search, insert, and delete on a balanced tree
- In-order traversal yields keys in sorted order
- Degrades to O(n) worst case when inserted data is already sorted (unbalanced)
- Foundation for self-balancing variants like AVL and red-black trees
- Recursive definition makes insert/search/delete naturally recursive algorithms
- Supports efficient range queries (min, max, successor, predecessor)
Use Cases
Frequently Asked Questions
From the Blog
Python File I/O: Reading and Writing Files
Almost every real Python program reads or writes files — logs, configs, CSVs, JSON, reports. This guide covers text files, CSV, JSON, binary files, and the modern pathlib approach, with best practices for safe file handling.
Read More Career GrowthHow to Build a Developer Portfolio That Gets You Hired
A developer portfolio is your most powerful job-search tool — more important than your degree, and often more persuasive than your resume. This guide explains what to build, how to present it, and how to make recruiters stop scrolling.
Read More Career GrowthLinkedIn Tips for Developers: Turn Your Profile Into an Inbound Machine
Most developers treat LinkedIn as an online CV and wonder why recruiters don't reach out. This guide explains how to optimise your profile for recruiter search, what to post to build visibility, and how to use LinkedIn to land interviews without cold-applying.
Read More AI & TechnologyRAG Explained: Retrieval-Augmented Generation
RAG is how you give an LLM access to your own private data without training a new model. This guide explains the full pipeline — chunking, embeddings, vector search, and augmented generation — with a working Python example using open-source tools.
Read More