# AVL Trees, Binary Heaps & Hashing Probe Complexity
Exam: GATE Computer Science & Information Technology (GATE CS)
Subject: Programming & Data Structures
Canonical URL: https://gateandnet.com/exams/gate-cs/programming-and-ds/avl-trees-heaps-and-hashing

> Minimum nodes in AVL tree of height h, O(n) Build-Heap proof, and LL/RR/LR/RL rotation rules.

## Binary Heap Index Formulas (0-based vs 1-based)

| Convention | Root | Left Child of $i$ | Right Child of $i$ | Parent of $i$ |
| :--- | :--- | :--- | :--- | :--- |
| **1-Based Array** | $1$ | $2i$ | $2i + 1$ | $\lfloor i / 2 \rfloor$ |
| **0-Based Array** | $0$ | $2i + 1$ | $2i + 2$ | $\lfloor (i - 1) / 2 \rfloor$ |

> [!TIP]
> **Finding Maximum in a Min-Heap:** In a Min-Heap of $n$ distinct elements, the **maximum element MUST reside in one of the $\lceil n/2 \rceil$ leaf nodes**, so finding it requires $\Theta(n)$ comparisons in the worst case!

## AVL Rotations Summary

- **Single Rotation (1 rotation):** Triggered by outer insertion **LL** (Right Rotate) or **RR** (Left Rotate).
- **Double Rotation (2 rotations):** Triggered by zig-zag inner insertion **LR** (Left then Right) or **RL** (Right then Left).
- In **AVL insertion**, fixing the *lowest* imbalanced ancestor restores balance to the entire tree ($\mathcal{O}(1)$ rotations!). In **AVL deletion**, rotations may propagate up to the root ($\mathcal{O}(\log n)$ rotations).


## Solved Practice Questions & PYQs

### [GATE CS DSA | MCQ]
What is the tight worst-case time complexity of building a **Binary Max-Heap** bottom-up (`Build-Max-Heap`) from an unsorted array of $n$ elements?
**Correct Answer:** B
**Solution:** In bottom-up `Build-Heap` (Floyd’s algorithm), nodes at height $h$ number $\lceil n / 2^{h+1} \rceil$ and each `Heapify` costs $\mathcal{O}(h)$. Summing $\sum_{h=0}^{\log n} \frac{n}{2^{h+1}} h = n \sum \frac{h}{2^{h+1}} \le n \cdot 1 = \mathbf{\Theta(n)}$.

### [GATE CS DSA | MCQ]
What is the **minimum number of nodes** in an height-balanced **AVL tree** of height $h = 4$ (where a single root node has height $0$)?
**Correct Answer:** B
**Solution:** Minimum nodes in an AVL tree obey $N(h) = N(h-1) + N(h-2) + 1$ with $N(0)=1,\; N(1)=2$: $N(2) = 4$, $N(3) = 7$, and $N(4) = 7 + 4 + 1 = \mathbf{12}$.

### [UGC NET / GATE DSA | MCQ]
Which open-addressing collision resolution technique eliminates **both primary and secondary clustering**?
**Correct Answer:** C
**Solution:** Linear probing suffers from **primary clustering**; quadratic probing eliminates primary clustering but suffers from **secondary clustering** (keys hashing to the same home slot follow identical probe sequences). **Double Hashing** $h(k,i) = (h_1(k) + i \cdot h_2(k)) \bmod m$ makes both initial slot and stride key-dependent!
