A binary tree is a hierarchy in which each node has at most two children, called the left and right child. A binary search tree (BST) adds one ordering rule — every value in a node's left subtree is smaller than the node, and every value in its right subtree is larger — and that single rule is what makes searching fast.

The headline numbers: on a balanced BST, search, insert, and delete all run in O(logn)O(\log n) time, because the tree's height stays close to log2n\log_2 n. On an unbalanced tree, the same operations can degrade to O(n)O(n).

Binary tree vs binary search tree

Every BST is a binary tree, but not every binary tree is a BST. The BST property must hold at every node:

(all keys in left subtree)<node key<(all keys in right subtree)\text{(all keys in left subtree)} < \text{node key} < \text{(all keys in right subtree)}

Note the wording: the rule applies to the entire subtree, not just the immediate children. A tree can have every parent–child pair correctly ordered and still violate the BST property deeper down — this is the classic trap in BST validation problems.

The four traversals

A traversal is a systematic way to visit every node exactly once. Three are depth-first and differ only in when the node itself is visited; the fourth is breadth-first.

Traversal Visit order Typical use
Inorder left, node, right sorted output from a BST
Preorder node, left, right copying or serializing a tree
Postorder left, right, node deleting nodes, evaluating expression trees
Level-order depth by depth (BFS) printing levels, shortest paths in trees

Worked example: four traversals of one tree

Use this BST:

        8
       / \
      3   10
     / \    \
    1   6    14

Walking each traversal by hand:

Traversal Result
Inorder 1,3,6,8,10,141, 3, 6, 8, 10, 14
Preorder 8,3,1,6,10,148, 3, 1, 6, 10, 14
Postorder 1,6,3,14,10,81, 6, 3, 14, 10, 8
Level-order 8,3,10,1,6,148, 3, 10, 1, 6, 14

The inorder result comes out sorted. That is not a coincidence — inorder traversal of any valid BST always produces the keys in ascending order, which makes it a quick correctness check.

Worked example: search and insert in a BST

Search for 66 in the tree above. Start at the root:

  1. At 88: since 6<86 < 8, go left.
  2. At 33: since 6>36 > 3, go right.
  3. At 66: found, after 33 comparisons.

A linear scan of the same six values could need up to 66 checks; the BST needed 33 because each comparison discarded an entire subtree.

Insert 77: follow the same path — 8368 \to 3 \to 6 — then since 7>67 > 6 and 66 has no right child, attach 77 as the right child of 66. Insertion is just a failed search plus one link.

Why balance matters

Every BST operation walks one root-to-leaf path, so the cost is O(h)O(h) where hh is the height. Balance determines hh:

hbalancedlog2nhworst=n1h_{\text{balanced}} \approx \log_2 n \qquad h_{\text{worst}} = n - 1

The worst case is easy to trigger: insert already-sorted data 1,2,3,4,51, 2, 3, 4, 5 into an empty BST and every node gets only a right child. The "tree" is now a linked list.

nn Balanced (comparisons) Degenerate (comparisons)
1,0001{,}000 about 1010 up to 1,0001{,}000
1,000,0001{,}000{,}000 about 2020 up to 1,000,0001{,}000{,}000

Self-balancing trees — AVL trees and red-black trees — fix this by performing local rotations after inserts and deletes, guaranteeing h=O(logn)h = O(\log n) no matter the input order. This is why standard library structures like C++ std::map and Java TreeMap are built on red-black trees.

Common mistakes

Validating a BST by checking only the children

Comparing each node to its immediate children misses violations deeper in the subtree. Correct validation passes a valid (min,max)(\min, \max) range down the recursion.

Assuming O(logn)O(\log n) without balance

The logarithmic bound is a property of balanced trees, not of BSTs in general. Sorted input on a plain BST gives O(n)O(n) behavior.

Expecting sorted output from any binary tree

Inorder traversal yields ascending order only when the BST property holds. On an arbitrary binary tree, inorder output is just one particular visiting sequence.

Ignoring the duplicates policy

Decide up front whether duplicates go left, go right, or are stored with a count. Mixing conventions in one tree breaks both search and the sorted-inorder guarantee.

Build one yourself

Insert 5,2,8,1,35, 2, 8, 1, 3 into an empty BST and draw the result, then write out all four traversals by hand. Afterwards, insert 1,2,3,4,51, 2, 3, 4, 5 in that order into a fresh tree and compare the shapes — seeing the degenerate chain appear once is worth more than rereading the definition.

Frequently Asked Questions

What is the difference between a binary tree and a binary search tree?
A binary tree only requires that each node has at most two children. A binary search tree adds an ordering rule: every key in a node’s left subtree is smaller than the node and every key in its right subtree is larger. That rule is what enables fast lookup, insertion, and deletion.
Why does inorder traversal of a BST give sorted output?
Inorder traversal visits the left subtree, then the node, then the right subtree. In a BST everything on the left is smaller and everything on the right is larger, so applying that rule recursively at every node means smaller keys are always printed before larger ones, producing ascending order.
What happens when a binary search tree becomes unbalanced?
Every operation walks a root-to-leaf path, so its cost equals the tree height. If inserts arrive in sorted order, each new node becomes a single chain and the height grows to n, making search, insert, and delete linear time instead of logarithmic, the same as scanning a list.
What is a balanced binary tree?
A balanced binary tree keeps the heights of left and right subtrees close at every node, so the overall height stays near log base 2 of n. Self-balancing variants such as AVL trees and red-black trees restore this property automatically with rotations after each insert or delete.

Need help with a problem?

Upload your question and get a verified, step-by-step solution in seconds.

Open GPAI Solver →