Skip to content
GATEiit

GATE CSE · Programming and Data Structures

Trees, binary search trees and binary heaps

Binary tree properties and counting, traversals and tree reconstruction, binary search tree insert, search and delete, and binary heaps in arrays, with the node counts, traversal orders and heap traces GATE asks.

Before you start

  • Recursion →

    Traversals, BST insert and delete, and height calculations are all written recursively.

  • Arrays and linked structures →

    A heap lives in an array; a BST is a linked structure of nodes with two pointers.

  • Logarithms base 2

    Heights of complete and balanced trees are ⌊log⁡2n⌋.

This topic comes before: Algorithm design techniques: greedy, dynamic programming and divide-and-conquer, Context-free grammars and push-down automata, File organization and indexing (e.g. B and B+ trees), Graph traversals, minimum spanning trees and shortest paths, Intermediate code generation, Searching, sorting and hashing

Concept notes

1. Binary trees: terms and counting results

A binary tree is empty or a root with a left and a right subtree. The depth of a node is the number of edges from the root; the height of a tree is the largest depth (a single node has height 0). Check which convention the question uses; some count levels from 1.

  • A full (strict) binary tree: every node has 0 or 2 children.
  • A complete binary tree: every level is full except possibly the last, which is filled from the left.
  • A perfect binary tree: all levels full.

Key results for a binary tree with n nodes:

  • It has n−1 edges and n+1 NULL child pointers.
  • If n0, n1, n2 are the numbers of nodes with 0, 1, 2 children, then n0=n2+1 (from counting edges: n1+2n2=n−1).
  • Level d has at most 2d nodes; a tree of height h has between h+1 and 2h+1−1 nodes.
  • The minimum height for n nodes is ⌊log⁡2n⌋; a complete binary tree reaches it.
  • A full binary tree with i internal nodes has i+1 leaves and 2i+1 nodes.

Counting shapes. The number of distinct binary tree shapes with n nodes is the Catalan number Cn=1n+1(2nn) (1, 2, 5, 14, 42). The number of BSTs on n distinct keys is also Cn, because each shape can be filled with sorted keys in exactly one way.

2. Traversals and rebuilding a tree

Traversal Order Note
Preorder root, left, right first element is the root
Inorder left, root, right sorted order for a BST
Postorder left, right, root last element is the root
Level order level by level, left to right uses a queue (BFS)

The three depth-first traversals take O(n) time and O(h) stack space.

Rebuilding. Inorder together with preorder (or postorder) fixes a binary tree uniquely: take the root from preorder (or postorder), find it in the inorder sequence, and the elements to its left and right form the two subtrees. Recurse.

Preorder with postorder does not fix a tree in general: a node with one child gives the same pair whether the child is on the left or right. It is unique for a full binary tree.

For a BST, the preorder (or postorder) alone fixes the tree, because the inorder is just the sorted keys.

Quick checks: in preorder the root comes first; in postorder last; the leftmost node of the tree is first in inorder.

3. Binary search trees

A binary search tree (BST) keeps, at every node, all keys in the left subtree smaller and all keys in the right subtree larger. So an inorder traversal lists the keys in sorted order.

  • Search and insert follow one root-to-leaf path: compare, go left or right. A new key always becomes a leaf.
  • Delete a node x: (1) leaf: remove it; (2) one child: link the child to x's parent; (3) two children: replace x's key by its inorder successor (the minimum of the right subtree) and delete that successor, which has no left child. Using the inorder predecessor is also correct; GATE usually says which.
  • Minimum: keep going left. Maximum: keep going right.

All these operations take O(h) time. The height h lies between ⌊log⁡2n⌋ and n−1. Inserting keys in sorted order gives a chain of height n−1, so the worst case is O(n). Balanced trees (such as AVL or red-black trees) keep h=O(log⁡n).

To check whether a sequence can be the preorder of a BST, rebuild it: each key must fall within the (low, high) range allowed by its ancestors.

5030702040608035
BST after inserting 50, 30, 70, 20, 40, 60, 80, 35 in that order (height 3)

4. Binary heaps

A binary max-heap is a complete binary tree in which every node's key is at least its children's keys (a min-heap uses at most). Being complete, it is stored in an array with no pointers:

  • 1-based: children of i are 2i and 2i+1, parent is ⌊i/2⌋.
  • 0-based: children are 2i+1 and 2i+2, parent is ⌊(i−1)/2⌋.
  • The leaves are positions ⌊n/2⌋+1 to n (1-based). The height is ⌊log⁡2n⌋.
Operation Time How
Find max O(1) the root
Insert O(log⁡n) add at the end, swap up while bigger than parent
Delete max O(log⁡n) move the last element to the root, swap down with the larger child
Build heap (bottom-up) O(n) sift down nodes ⌊n/2⌋ down to 1
n inserts one by one O(nlog⁡n)
Find min in a max-heap Θ(n) it is one of the ⌈n/2⌉ leaves
Search an arbitrary key O(n) heap order does not guide the search

Heapsort builds a max-heap, then repeatedly swaps the root with the last element and sifts down: O(nlog⁡n), in place, not stable.

Building by repeated insertion and by bottom-up build-heap can give different arrays from the same input; read which method the question uses.

403015102012345401302153104205
Max-heap after inserting 10, 20, 15, 30, 40 one by one, with its array (1-based indices)

Formulas to remember

Leaves and degree-2 nodes
n0=n2+1
NULL pointers in a binary tree2n pointer fields, n−1 of them used by edges
n+1
Nodes in a tree of height hHeight counted in edges, root at depth 0
h+1≤n≤2h+1−1
Binary tree shapes / BSTs on n keys
Cn=1n+1(2nn)
Heap index arithmetic (1-based)
parent(i)=⌊i/2⌋,left(i)=2i,right(i)=2i+1
Leaves of a heap (1-based)
⌊n/2⌋+1, …, n
Height of a heap or complete tree
⌊log⁡2n⌋

Common mistakes

  • Mixing up height conventions: counting nodes instead of edges changes 2h+1−1 into 2h−1.
  • Deleting a two-child BST node by moving a whole subtree up instead of copying the inorder successor (or predecessor) into it.
  • Assuming preorder and postorder together fix a binary tree; they do not unless the tree is full.
  • Giving the same array for one-by-one insertion and bottom-up build-heap; the two methods can produce different heaps.
  • Saying build-heap is O(nlog⁡n); the bottom-up method is O(n).
  • Using 0-based index formulas (children 2i+1, 2i+2) on a 1-based array, or the reverse.

Worked examples

Try each step yourself, then reveal the working.

Example 1: Building and deleting in a BST

numerical

Insert 50, 30, 70, 20, 40, 60, 80, 35 into an empty BST. Give the height, the preorder and the postorder. Then delete 30 using the inorder successor and give the new preorder.

  1. Step 1: Insert each key by comparing from the root.

    Try it on paper first.

Example 2: Rebuilding a tree from preorder and inorder

numerical

A binary tree has preorder A B D E G C F and inorder D B G E A C F. Find its postorder.

  1. Step 1: Find the root and split the inorder.

    Try it on paper first.

Example 3: Heap insertions and a delete-max

numerical

Insert 10, 20, 15, 30, 40 one by one into an empty max-heap stored in an array. Give the array, then the array after one delete-max.

  1. Step 1: Insert 10, 20, 15.

    Try it on paper first.

Example 4: Counting nodes from degrees

numerical

A binary tree has 10 nodes with two children and 5 nodes with one child. How many leaves, nodes and NULL pointers does it have?

  1. Step 1: Use the leaf rule.

    Try it on paper first.

Previous year questions

30 official GATE CSE questions on this topic, newest first.

Practise these 30 PYQs →

Topic test

10 questions: up to 4 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.

Mark as mastered

Loading your progress…

My revision notes

Notes written by the GATEiit team against the official GATE CSE syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.