Heaps Class - 2

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 187:-Heaps Class - 2

Certainly! Let's continue enhancing our Max Heap class by adding more functionalities such as building a heap from an array and checking if the heap is empty. Additionally, we'll implement a Min Heap class as well.

Here's the next part of the Heap class, including a Min Heap implementation:

pythonCopy code

class MaxHeap: def __init__(self): self.heap = [] # ... (previous code) def build_heap(self, arr): self.heap = arr for i in range(len(arr) // 2 - 1, -1, -1): self._heapify_down(i) def is_empty(self): return len(self.heap) == 0 class MinHeap: def __init__(self): self.heap = [] def insert(self, value): self.heap.append(value) self._heapify_up(len(self.heap) - 1) def _heapify_up(self, index): parent = (index - 1) // 2 while index > 0 and self.heap[index] < self.heap[parent]: self.heap[index], self.heap[parent] = self.heap[parent], self.heap[index] index = parent parent = (index - 1) // 2 def extract_min(self): if not self.heap: return None if len(self.heap) == 1: return self.heap.pop() min_value = self.heap[0] self.heap[0] = self.heap.pop() self._heapify_down(0) return min_value def _heapify_down(self, index): left_child = 2 * index + 1 right_child = 2 * index + 2 smallest = index if left_child < len(self.heap) and self.heap[left_child] < self.heap[smallest]: smallest = left_child if right_child < len(self.heap) and self.heap[right_child] < self.heap[smallest]: smallest = right_child if smallest != index: self.heap[index], self.heap[smallest] = self.heap[smallest], self.heap[index] self._heapify_down(smallest) # ... (build_heap and is_empty methods can be added similar to MaxHeap) # Example usage max_heap = MaxHeap() max_heap.build_heap([5, 10, 3, 8, 2]) print("Max Heap after building:", max_heap.heap) min_heap = MinHeap() min_heap.insert(5) min_heap.insert(10) min_heap.insert(3) min_heap.insert(8) min_heap.insert(2) print("Min Heap:", min_heap.heap) print("Extracted Min Value:", min_heap.extract_min()) print("Min Heap after extraction:", min_heap.heap)

In this part, we've added the following functionalities to the Heap class:

Build Heap: The build_heap method constructs a heap from a given array. It starts from the last non-leaf node and performs the _heapify_down operation for each node to establish the heap property.

Is Empty: The is_empty method checks whether the heap is empty or not.

We've also introduced a MinHeap class, which is similar to the MaxHeap class but maintains the min heap property where each node's value is less than or equal to the values of its children nodes.

These additions provide a more comprehensive Heap class, allowing you to work with both max heaps and min heaps and perform operations such as building heaps from arrays and checking for emptiness.

27. Heaps

Comments: 2

profile
@mk.info.work
17-Feb-2024, 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

profile
@na3744
23-Feb-2024, 02:52 AM

I bought this course, it worth it!

profile
@mk.info.work
15-Nov-2023, 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

profile
@sciaku1
11-Jan-2024, 03:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?