๐ณ๐
Binary Search Tree: every left child is smaller, every right child is larger. This one rule makes searching as fast as flipping to the middle of an ordered list โ O(log n) time!
Mode:
Speed:
Nodes: 0
Height: 0
Min: โ
Max: โ
๐ฑ
Tree is empty
Insert a value to start!
In-order:
Insert
Quick presets:
Search
Delete
Traversals
๐ณ
Ready
Insert a value or click a preset to begin.
๐ BST Property
All left descendants
<
Node
<
All right descendants
This property makes search, insert, and delete all O(log n) in a balanced tree.
๐ณ
Binary Search Tree (BST)
A self-organizing data structure that keeps everything sorted
๐ณ
A Binary Tree is a tree where each node has at most 2 children: a left child and a right child.
๐
In a BST, every node's left subtree contains only values smaller than it, and its right subtree contains only values larger than it.
๐
This ordering rule makes it possible to search in O(log n) โ like binary search, but on a tree!
โ ๏ธ
A BST can become skewed (like a linked list) if you insert values in sorted order. Then search is O(n) worst case. Self-balancing trees (AVL, Red-Black) fix this.
โ
Insert: Compare with root โ go left if smaller, right if larger โ repeat until you find an empty spot. Place the new node there.
๐
Search: Same comparison path as insert. If you reach a null, the value doesn't exist.
๐๏ธ
Delete (3 cases):
โ Leaf node โ simply remove
โก One child โ replace node with its child
โข Two children โ replace with in-order successor (smallest in right subtree)
โ Leaf node โ simply remove
โก One child โ replace node with its child
โข Two children โ replace with in-order successor (smallest in right subtree)
โฌ
โก
In-order (LโRootโR): Visits nodes in sorted ascending order. Used to get a sorted list from a BST.
โฌ
Pre-order (RootโLโR): Visits root first. Used to copy or serialize the tree.
โฌ
Post-order (LโRโRoot): Visits root last. Used to delete a tree or evaluate expression trees.
๐ต
Level-order (BFS): Visits level by level using a queue. Gives breadth-first view of the tree.
๐
File systems use tree structures to organize folders โ finding a file is essentially a tree search.
๐๏ธ
Database indexes (B-trees, which are generalized BSTs) make SQL queries like WHERE id = 42 run in milliseconds on millions of rows.
๐
Auto-complete and spell-checking use tries (prefix trees), a variant of binary trees.
๐ฎ
Game AI uses game trees (binary or N-ary) and the Minimax algorithm to decide the best move.