Heaps Class - 1

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 186:-Heaps Class - 1

Sure! Let's start by implementing a basic Max Heap class in Python. A max heap is a binary tree where the value of each node is greater than or equal to the values of its children nodes. It has the property that the maximum value is always at the root.

Here's a simple implementation of a Max Heap class:

pythonCopy code

class MaxHeap: 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_max(self): if not self.heap: return None if len(self.heap) == 1: return self.heap.pop() max_value = self.heap[0] self.heap[0] = self.heap.pop() self._heapify_down(0) return max_value def _heapify_down(self, index): left_child = 2 * index + 1 right_child = 2 * index + 2 largest = index if left_child < len(self.heap) and self.heap[left_child] > self.heap[largest]: largest = left_child if right_child < len(self.heap) and self.heap[right_child] > self.heap[largest]: largest = right_child if largest != index: self.heap[index], self.heap[largest] = self.heap[largest], self.heap[index] self._heapify_down(largest) # Example usage max_heap = MaxHeap() max_heap.insert(5) max_heap.insert(10) max_heap.insert(3) max_heap.insert(8) max_heap.insert(2) print("Max Heap:", max_heap.heap) print("Extracted Max Value:", max_heap.extract_max()) print("Max Heap after extraction:", max_heap.heap)

In this implementation, we've defined a MaxHeap class with methods to insert elements and extract the maximum value from the heap. The _heapify_up and _heapify_down methods are used to maintain the max heap property after insertion and extraction operations.

The insert method adds an element to the heap and then performs the _heapify_up operation to ensure the max heap property is maintained.

The extract_max method removes and returns the maximum value from the heap, replacing the root with the last element and then performing the _heapify_down operation to restore the max heap property.

This is a basic implementation of a Max Heap class. You can further extend it to include more functionalities like deleting specific elements, merging heaps, and building heaps from an array of elements.

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?