A Binary Search Tree (BST) is a node-based data structure in which every node stores a value and maintains two child pointers —Documentation Index
Fetch the complete documentation index at: https://mintlify.com/tutosrive/ed/llms.txt
Use this file to discover all available pages before exploring further.
left and right — together with a parent back-pointer. Because values are arranged so that every key in a node’s left subtree is strictly smaller than the node’s key, and every key in the right subtree is greater or equal, the tree can locate, insert, or remove any value in O(log n) time on average. This ordering property makes BSTs one of the most fundamental structures in computer science, powering everything from database indexes to symbol tables in compilers.
BST Properties
The central invariant that defines a BST is the ordering property:For every nodeThis property holds recursively for every subtree, not just the root. As a result:Nin the tree:
- All values in the left subtree of
Nare strictly less thanN.value.- All values in the right subtree of
Nare greater than or equal toN.value.
| Property | Detail |
|---|---|
| Search complexity | O(log n) average, O(n) worst case (degenerate tree) |
| Insert complexity | O(log n) average |
| Delete complexity | O(log n) average |
| Inorder traversal | Visits all nodes in sorted (ascending) order |
| Space complexity | O(n) |
The Node Class
Each element in the tree is represented by aNode object. The UCALDAS implementation tracks both children and the parent, which simplifies deletion without requiring an explicit stack.
Node class
| Field | Type | Description |
|---|---|---|
value | any comparable | The data stored in this node |
left | Node | None | Pointer to the left child (smaller values) |
right | Node | None | Pointer to the right child (larger or equal values) |
parent | Node | None | Pointer to the parent node; None for the root |
Node(value), its left, right, and parent fields all default to None. The parent field is wired up by _insert and changeNodePosition as the tree is mutated.
The BST Class
TheBST class wraps the root node and exposes all tree operations through a clean public interface. Its initializer is intentionally minimal — it simply sets the root to None, representing an empty tree.
BST initializer
Quick Usage Example
The following snippet builds the BST shown in the diagram below, then prints it to the console:Building and printing a BST
50) is printed in the middle; nodes above it belong to the right subtree, nodes below to the left subtree. The ┌── connector indicates a right child and └── indicates a left child. Vertical bars (│) show where a branch continues past the current level.
This BST implementation uses the inorder predecessor — the largest node in the left subtree — as the replacement when deleting a node with two children. Some textbooks use the inorder successor instead; both strategies preserve the BST ordering invariant. See BST Operations for the full deletion logic.