Heap

A heap is a special tree-based data structure where the tree is a complete binary tree. This means that every level of the tree is completely filled except possibly the last level.

Types of Heaps

  1. Max Heap: The value of any parent node in a max heap is greater than or equal to the value of its children.
  2. Min Heap: The value of any parent node in a min heap is less than or equal to the value of its children.

Operations on Heaps

  1. Insert: Adds an element to the heap and maintains the heap property.
  2. Delete: Removes the root element (maximum or minimum) from the heap and restores the heap property.
  3. Find-Max/Find-Min: Returns the maximum/minimum element in the heap.
  4. Increase/Decrease-Key: Increases/decreases the value of a specific key in the heap, maintaining the heap property.