1. Gyanastra
  2. Data Structures
  3. Binary Search Trees
  4. Insertion of Elements into a Binary Search Tree
Free lesson, no account needed 6 min read 14 practice questions after it
Asked in past papers19 times
  • GATE CSE202520202017201620152008200620052004200319971996
  • ISRO20172009
  • UGC NET20182017201620142008

Data Structures · Binary Search Trees

Insertion of Elements into a Binary Search Tree

Overview

A Binary Search Tree (BST) stores keys so that every left descendant is smaller and every right descendant is larger than its node. Insertion walks down the tree by comparisons and places each new element at the first empty position, so the ordering property is preserved.

Key concepts

  • Binary Search Tree (BST): a binary tree with the ordering property — for any node, all elements in its left subtree are less than the node, and all elements in its right subtree are greater than the node.
  • Distinct keys: a BST assumes nodes are not repeated. It stores keys, and each key points to some record; two records are not supposed to have the same key, so the numbers are always distinct.
  • Binary search tree: a binary tree with distinct keys — all elements less than the node in the left subtree, all elements greater than the node in the right subtree.
  • Duplicate compromise: if numbers can be the same, all numbers less than or equal to the node go into the left subtree, and all numbers greater than the node go into the right subtree.

How it works

  1. Compare the new element with the root first.
  2. If the element is less than the current node, move to its left child.
  3. If the element is greater than the current node, move to its right child.
  4. Follow the flow down the tree, comparing at every node, until an empty position (null child) is reached.
  5. Insert the new element at that empty position as a leaf.

Final BST after insertions

Shows the binary search tree resulting from inserting the twelve elements in the given order.

Figure. Inserting 7 into this tree: 7 is less than 8 so go left; 7 is more than 3 so go right; 7 is more than 6 so go right; that place is empty, so 7 becomes the right child of 6 (shown in crimson). Each comparison removes one half of the remaining tree.

Worked examples

GATE 1996 question: insert all the given elements into the binary search tree.

Elements in insertion order: 50, 15, 62, 5, 20, 58, 91, 3, 8, 37, 60, 24.

StepElementComparisons madePosition where inserted
150— (first element)Becomes the root
21515 < 50Left child of 50
36262 > 50Right child of 50
455 < 50, 5 < 15Left child of 15
52020 < 50, 20 > 15Right child of 15
65858 > 50, 58 < 62Left child of 62
79191 > 50, 91 > 62Right child of 62
833 < 50, 3 < 15, 3 < 5Left child of 5
988 < 50, 8 < 15, 8 > 5Right child of 5
103737 < 50, 37 > 15, 37 > 20Right child of 20
116060 > 50, 60 < 62, 60 > 58Right child of 58
122424 < 50, 24 > 15, 24 > 20, 24 < 37Left child of 37

Final tree (described in words):

  • Root = 50
    • Left of 50 = 15
      • Left of 15 = 5
        • Left of 5 = 3
        • Right of 5 = 8
      • Right of 15 = 20
        • Right of 20 = 37
          • Left of 37 = 24
    • Right of 50 = 62
      • Left of 62 = 58
        • Right of 58 = 60
      • Right of 62 = 91

Revision summary

  • Always compare with the root first, then follow the flow down the tree.
  • Smaller element → go left; larger element → go right.
  • Every inserted element becomes a leaf at the empty position reached by the comparisons.
  • The first element inserted becomes the root of the tree.
  • A BST assumes distinct keys (strict BST); if duplicates are allowed, use the compromise: ≤\le goes left, >> goes right.

Previous-year exam insights

  • The inorder traversal (left, root, right) of a BST lists its keys in increasing sorted order; therefore, for keys 1 through n, the inorder sequence is exactly 1, 2, ..., n.
    Asked inGATE 2003
  • A preorder sequence is valid for a BST if the first value is the root, the remaining values split into a contiguous prefix of values smaller than the root and a suffix of values greater than the root, and each part is recursively valid; the same split reconstructs the BST, and postorder is postorder(left) + postorder(right) + root.
    Asked inGATE 2020GATE 2017GATE 1997
  • The first inserted key becomes the root; for distinct keys, the number of nodes in the root's left subtree equals the number of inserted keys smaller than the root, and the number in the right subtree equals the number of inserted keys greater than the root.
    Asked inGATE 2005UGC NET 2018GATE 1996
  • Height is the number of edges on the longest root-to-leaf path, with a single node having height 0; the depth of an inserted node equals the number of comparisons/edges from the root, and the deepest node is at the lowest level.
    Asked inGATE 2025GATE 2015GATE 2004GATE 2016
  • For n distinct keys, the maximum BST height is n-1; insertion orders that produce this skewed height are exactly those in which each new key is either the current minimum or current maximum of the remaining set, giving 2n−12^{n-1} such orders.
    The minimum possible height is floor(log⁡2n\log_{2} n).
    Asked inGATE 2016
  • In a max-heap, insertion is not done like BST insertion: place the new key at the next available position in level order, then repeatedly swap it with its parent while it is larger than the parent.
    Asked inGATE 2004
Pages 1–2 of 2