Data-structure invariants
Introduction / Definition
An invariant is a condition that must remain true whenever a data structure is valid. Operations must preserve the invariant after updates. Invariants make implementation correctness easier to reason about. They are especially important in search trees, heaps, and queues.
Syntax / Structure / Formula
Invariant: P(state) remains true after every valid operation.
Detailed Explanation
A binary search tree maintains an ordering relationship between each node and its subtrees. Insert and delete must preserve that ordering.
Example
Example: With root 50, 20 belongs on the left and 70 on the right.
Output / Result
The ordering invariant is preserved.
Important Points / Notes
- Invariants define valid states.
- Operations must preserve them.
Mini Practice Question
State the BST ordering invariant.