Left subtree < node < right subtree, so inorder traversal gives sorted order.
Search, insert, delete: O(h) — O(log n) if balanced, O(n) if skewed (e.g. inserting sorted data).
Delete a node with two children: replace it with its inorder successor (or predecessor).
Given only the preorder of a BST, the tree is unique (inorder is just the sorted order).
Left subtree < node < right subtree, isliye inorder traversal sorted order deta hai.
Search, insert, delete: O(h) — balanced ho toh O(log n), skewed ho toh O(n) (jaise sorted data insert karna).
Do children wala node delete karna: usko inorder successor (ya predecessor) se replace karo.
Sirf BST ka preorder diya ho toh bhi tree unique hai (inorder toh bas sorted order hai).
AVL treeAVL tree
A self-balancing BST where the balance factor (height of left − height of right) of every node is −1, 0 or +1. Rotations (LL, RR, LR, RL) restore balance after insert/delete.Ek self-balancing BST jisme har node ka balance factor (left height − right height) −1, 0 ya +1 hota hai. Insert/delete ke baad rotations (LL, RR, LR, RL) balance wapas laate hain.
Height h
Min nodes N(h)
0
1
1
2
2
4
3
7
4
12
TipN(h) = N(h − 1) + N(h − 2) + 1. All operations are O(log n).N(h) = N(h − 1) + N(h − 2) + 1. Saare operations O(log n).
Binary heapBinary heap
Complete binary tree stored in an array. Max-heap: parent ≥ children.
1-indexed: children of i are 2i and 2i + 1; parent is ⌊i / 2⌋.
Insert and delete-max: O(log n). Find max: O(1).
Build-heap (bottom-up heapify): O(n), not O(n log n).
Leaves are at indices ⌊n/2⌋ + 1 … n.
Complete binary tree jo array mein store hota hai. Max-heap: parent ≥ children.
1-indexed: i ke children 2i aur 2i + 1; parent ⌊i / 2⌋.
Common mistakeCommon galtiSearching an arbitrary element in a heap is O(n) — a heap is not a BST.Heap mein koi random element search karna O(n) hai — heap BST nahi hai.
Quick revision
A binary tree with n nodes has n + 1 null pointers.
Full binary tree: leaves = internal nodes + 1. In general, n₀ = n₂ + 1.
Max nodes at height h (root at height 0): 2^(h+1) − 1.
Inorder of a BST is sorted. Inorder + preorder (or postorder) gives a unique tree; pre + post does not.