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
- Compare the new element with the root first.
- If the element is less than the current node, move to its left child.
- If the element is greater than the current node, move to its right child.
- Follow the flow down the tree, comparing at every node, until an empty position (null child) is reached.
- Insert the new element at that empty position as a leaf.
Final BST after insertions
Drawing the diagram…
Shows the binary search tree resulting from inserting the twelve elements in the given order.
Drawing the diagram…
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.
| Step | Element | Comparisons made | Position where inserted |
|---|---|---|---|
| 1 | 50 | — (first element) | Becomes the root |
| 2 | 15 | 15 < 50 | Left child of 50 |
| 3 | 62 | 62 > 50 | Right child of 50 |
| 4 | 5 | 5 < 50, 5 < 15 | Left child of 15 |
| 5 | 20 | 20 < 50, 20 > 15 | Right child of 15 |
| 6 | 58 | 58 > 50, 58 < 62 | Left child of 62 |
| 7 | 91 | 91 > 50, 91 > 62 | Right child of 62 |
| 8 | 3 | 3 < 50, 3 < 15, 3 < 5 | Left child of 5 |
| 9 | 8 | 8 < 50, 8 < 15, 8 > 5 | Right child of 5 |
| 10 | 37 | 37 < 50, 37 > 15, 37 > 20 | Right child of 20 |
| 11 | 60 | 60 > 50, 60 < 62, 60 > 58 | Right child of 58 |
| 12 | 24 | 24 < 50, 24 > 15, 24 > 20, 24 < 37 | Left 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 20 = 37
- Left of 15 = 5
- Right of 50 = 62
- Left of 62 = 58
- Right of 58 = 60
- Right of 62 = 91
- Left of 62 = 58
- Left of 50 = 15
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: 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 such orders.
The minimum possible height is floor().
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