Building Heap in O(n) šŸŽÆ

beginner
13 min

Building Heap in O(n) šŸŽÆ

Welcome to our deep dive into building a Heap in O(n)! This lesson is designed for both beginners and intermediates, so let's get started! šŸš€

Understanding Heaps šŸ“

A Heap is a specialized tree-based data structure that satisfies the heap property, which ensures that the parent node is either larger (in Max Heap) or smaller (in Min Heap) than its children. Heaps are incredibly useful for implementing efficient priority queues and sorting algorithms.

Max Heap and Min Heap šŸ’”

  • Max Heap: In a Max Heap, the root node (parent node at index 0) always holds the maximum value.
  • Min Heap: In a Min Heap, the root node always holds the minimum value.

Building a Max Heap in O(n) šŸ“

The process of building a Max Heap from an arbitrary array involves shifting elements upwards until the heap property is satisfied. Here's how:

  1. Initialize the current index (i) as the last non-leaf node (i.e., the last index where the parent exists, which is len(arr) // 2 - 1).
  2. While i > 0 and the parent (i // 2) has a larger value than either of its children (2 * i + 1 and 2 * i + 2), swap the parent and the larger child, then update the current index i to the index of the swapped child.
  3. Repeat step 2 with the updated i until the heap property is satisfied.
  4. Repeat the process for every node from the last non-leaf node to the first node (root).

Here's a practical example of building a Max Heap using Python:

python
def build_max_heap(arr): n = len(arr) for i in range(n // 2 - 1, -1, -1): heapify_up(arr, n, i) def heapify_up(arr, n, i): while i > 0 and arr[i] > arr[i // 2]: arr[i], arr[i // 2] = arr[i // 2], arr[i] i = i // 2

šŸ’” Pro Tip: You can use the heapify_up function recursively to build the Max Heap more efficiently.

Practical Application šŸ“

Max Heaps are widely used in areas like job scheduling, network routing, and various sorting and searching algorithms. Let's consider a simple example of job scheduling:

Suppose we have a list of jobs with their deadlines and profits. The goal is to schedule jobs in such a way that the total profit is maximized and no job overlaps with another. By building a Max Heap with jobs sorted by their deadlines (earliest first), we can schedule jobs optimally.

python
jobs = [(1, 20), (2, 10), (3, 40), (4, 30), (5, 5), (6, 60)] def compare(job1, job2): return job1[0] < job2[0] jobs.sort(key=lambda x: x[0], reverse=False) # Sort jobs by their deadlines. build_max_heap(jobs) # Build a Max Heap. # Implement job scheduling using Max Heap. scheduled_jobs = [] for job in jobs: while scheduled_jobs and scheduled_jobs[-1][0] > job[0]: complete_job = scheduled_jobs.pop() scheduled_jobs.append(job) print("Scheduled Jobs:", scheduled_jobs)

Quiz Time šŸŽÆ

Quick Quiz
Question 1 of 1

What is the property that a Heap satisfies?

Quick Quiz
Question 1 of 1

What does a Max Heap prioritize?

Building Heaps is a fundamental concept for understanding and implementing data structures and algorithms, and it's an essential tool for many real-world problems. Keep learning, and happy coding! šŸŽ‰