1. Gyanastra
  2. Data Structures
  3. Binary Search Trees
  4. Need for Binary Search Trees and Search Complexity
Free lesson, no account needed 4 min read 13 practice questions after it
Asked in past papers22 times
  • GATE CSE20212020201920172014201320082007200620042003
  • ISRO2017201620112009
  • UGC NET2020201620142012201020082006

Data Structures · Binary Search Trees

Need for Binary Search Trees and Search Complexity

Overview

A binary search tree (BST) is a binary tree in which elements are inserted in a proper order so that searching becomes easier. An ordinary binary tree gives no direction for a search and can cost O(n)O(n), while a balanced BST reduces search to O(log⁡n)O(\log n).

Key concepts

  • Binary search tree (BST): a special kind of binary tree in which elements are inserted in a proper order so that searching for an element becomes easier.
    The trees that help search for an element are called binary search trees; they have a special property that is very useful while searching.
  • General binary tree search problem: there is no particular direction to go, so you may end up searching everywhere and might have to go through every node.
  • Balanced binary search tree: a BST that has been balanced; it gives good performance, with search time of order log⁡n\log n.

How it works

  1. In a normal (general) binary tree, searching for an element may require visiting every node, so the time complexity is of order nn.
  2. Visiting every node costs order nn regardless of traversal: preorder, postorder or inorder all take O(n)O(n).
  3. Insert elements in proper order to get a BST, which makes searching easier.
  4. Balance the BST to obtain a balanced binary search tree, whose search time is O(log⁡n)O(\log n).

Path: ordinary binary tree → search is O(n)O(n); insert elements in proper order to get a BST → search becomes easier; balance the BST → search becomes O(log⁡n)O(\log n).

Growth chart: Need for Binary Search Trees and Search Complexity
Figure. Need for Binary Search Trees and Search Complexity

Formulas

  • T(n)=O(n)T(n) = O(n) — search in a general binary tree, because in the worst case every node must be visited (preorder, postorder or inorder all take O(n)O(n)).
  • T(n)=O(log⁡n)T(n) = O(\log n) — search in a balanced binary search tree, achieved by balancing the BST.
  • Here nn = number of nodes in the tree.

Comparison

Tree typeSearch time complexityReason
General binary treeO(n)O(n)No direction for search; may have to visit every node (any traversal is O(n)O(n))
Balanced binary search treeO(log⁡n)O(\log n)Elements stored in proper order and tree is balanced, so search follows a directed path

Revision summary

  • A BST is a special binary tree where elements are inserted in a proper order to make searching easier.
  • A general binary tree gives no particular direction for search, so you may have to search everywhere → O(n)O(n).
  • Any traversal (preorder, postorder, inorder) of a general binary tree takes O(n)O(n) because every node is visited.
  • Balancing a BST gives search time of order log⁡n\log n; such trees are called balanced binary search trees.
  • Shortcut: In the GATE exam, small questions are generally asked on this topic — insert these elements into the binary search tree, delete this element, or what is the inorder/preorder of the tree.

Previous-year exam insights

  • In a BST, search, insertion and deletion each take Θ(h)\Theta(h) time (h = height);
    worst case Θ(n)\Theta(n) for a skewed tree, Θ(log⁡n)\Theta(\log n) for a balanced/AVL tree.
    Asked inGATE 2013GATE 2008
  • The minimum possible height of a BST on n nodes is ⌊log⁡2n\log_{2} n⌋ and the maximum is n−1 (height of a single node = 0).
    Asked inGATE 2017
  • A BST is uniquely determined by its preorder or postorder sequence together with the BST property (inorder is the sorted order); reconstruction from preorder/postorder takes Θ(n)\Theta(n) time.
    Asked inGATE 2013GATE 2008
  • Inorder traversal of a BST yields the keys in sorted (ascending) order; the minimum key is the leftmost node and the maximum key is the rightmost node.
    Asked inGATE 2013GATE 2021
  • Reporting all keys in a range [a,b] in a balanced BST costs Θ(log⁡n+k)\Theta(\log n + k) where k is the number of reported keys (O(log⁡n)O(\log n) to locate the range boundaries plus O(k)O(k) to output).
    Asked inGATE 2020GATE 2014
  • A max-heap (or min-heap) does not satisfy the BST ordering, so converting a heap to a BST requires sorting, i.e. Ω(nlog⁡n)\Omega(n \log n) time, whereas building a heap from a BST takes Θ(n)\Theta(n) time.
    Asked inGATE 2019
Pages 1–2 of 2