Trees & heaps formulas
Trees, binary search trees and binary heaps. 13 formulas for GATE CS, DA Data Structures and Algorithms, each with when to use it. Each is explained in the topic notes.
IAE Academy: Trees, binary search trees and binary heaps formulas (GATE CS, DA)
All 13 Trees & heaps formulas
MAXIMUM NODES AT A GIVEN LEVEL
Root at level 0. An upper bound for any binary tree, reached exactly when level is completely filled.
MAXIMUM NODES IN A TREE OF GIVEN HEIGHT
Reached exactly by a perfect binary tree of height (every level completely filled).
MINIMUM NODES IN A TREE OF GIVEN HEIGHT
Reached by any tree with exactly one node per level: left-skewed, right-skewed or zig-zag.
HEIGHT RANGE FOR N NODES
The lower bound follows from . A complete binary tree with nodes, and so every binary heap, has height exactly .
LEAVES VERSUS NODES WITH TWO CHILDREN (ANY BINARY TREE)
Holds for every non-empty binary tree, whatever the number of one-child nodes. Here = leaves and = nodes with two children. Proof: count edges two ways. With nodes there are edges, and each edge hangs below exactly one parent, so . Subtracting gives .
FULL (STRICT) BINARY TREE: LEAVES, INTERNAL NODES, TOTAL
In a full binary tree every node has 0 or 2 children, so . With internal nodes it has leaves and nodes in all (always odd). More generally, a full -ary tree (every node has 0 or children) with internal nodes has leaves.
BST HEIGHT FOR N KEYS
The lower bound holds when the tree is complete. The upper bound is a chain, and every operation then costs in the worst case. Sorted or reverse-sorted input gives a chain, but so do other orders, such as 1, 5, 2, 4, 3. Of the insertion orders of distinct keys, exactly give height . When all insertion orders are equally likely, the expected height is .
BALANCE FACTOR
An AVL tree needs at every node, with the height of an empty subtree taken as . A node whose BF becomes or after an update must be rebalanced by rotation.
MINIMUM NODES IN AN AVL TREE OF HEIGHT H
The sparsest AVL tree of height is a root whose two subtrees are sparsest AVL trees of heights and . This gives and in closed form , where are the Fibonacci numbers. Consequently an AVL tree with nodes has height , which is . The maximum number of nodes of height is , as for any binary tree.
HEIGHT, LEAVES AND INTERNAL NODES OF AN N-ELEMENT HEAP
In the 1-indexed layout the leaves are at indices to ; in the 0-indexed layout at to . In a max-heap with distinct keys the minimum is always at a leaf, since an internal node has a smaller child, and it can be at any leaf. Finding it therefore means scanning the leaves, which is .
BUILD-HEAP RUNNING TIME
Bottom-up build-heap calls sift-down (heapify) on every internal node, from the last one back to the root. An -element heap has at most nodes at height , and sifting one down costs . The total is , because . It is linear, NOT . Building by successive inserts instead costs in the worst case.
HEAP SORT
Build a max-heap in . Then times: swap the root with the last element of the heap, shrink the heap by 1 and sift down the new root, each costing . It sorts in place and is not stable. auxiliary space assumes an iterative sift-down; the recursive maxHeapify below uses stack space.
CATALAN NUMBER
The recurrence chooses the root's left subtree size ; the right subtree then has nodes. The first values are . is the number of binary tree shapes with nodes and the number of BSTs on distinct keys. It also counts the ways to fully parenthesise a product of factors, and the balanced bracket strings of length . If distinct labels may be placed freely in the nodes (no BST rule), the number of labelled binary trees is .
Where this was asked: 2018 (2), 2020 (4), 2022 (1), 2023 (2), 2024 (5), 2025 (5), 2026 (7). See the 26 previous-year questions on Trees & heaps. This shows what was asked, not what will be asked.