Skip to content
UPEEC

Binary Search Trees

Left subtree smaller, right subtree larger — search, insert and delete in O(log n) when balanced.

Intermediate
Comments 0

For every node: keys in the left subtree << node << keys in the right subtree. Searching follows one path from the root, so it takes O(h)O(h) where hh is the height.

A balanced tree has h≈log⁡2nh \approx \log_2 n; inserting sorted data into a plain BST makes it a linked list (h=nh = n). Self-balancing trees — AVL and red-black trees (used by C++ std::map and Java TreeMap) — keep operations at O(log⁡n)O(\log n).

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.