## Heap Types

beginner
23 min

Heap Properties (Complete Binary Tree)

Welcome to a deep dive into Heap Properties, a fundamental aspect of data structures that will help you understand and solve complex problems efficiently. Let's get started! šŸŽÆ

What is a Heap? šŸ“

A Heap is a specialized tree-based data structure that follows the Heap Property, ensuring that either the parent nodes are greater than or equal to the child nodes (Max Heap) or the parent nodes are less than or equal to the child nodes (Min Heap). This property makes Heaps extremely useful for implementing priority queues, graph algorithms, and other optimized data structures.

Heap Types

  • Max Heap: Parent nodes are greater than or equal to child nodes
  • Min Heap: Parent nodes are less than or equal to child nodes

Key Terms

  • Complete Binary Tree: A binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible
  • Heap Property: The property that ensures the parent nodes are either greater than or equal to the child nodes (Max Heap) or less than or equal to the child nodes (Min Heap)

Understanding the Heap Structure šŸ“

A Heap is essentially a Complete Binary Tree, meaning every level is fully populated, and nodes are placed as far left as possible.

Heap Structure

In this example, we have a Max Heap. Notice how the parent nodes have values larger than their child nodes. This is the key to understanding Heaps and their usefulness.

Building a Heap šŸ’”

Building a Heap involves creating a Complete Binary Tree and then repeatedly swapping the largest or smallest element with the last element until the Heap Property is satisfied. This process is called Heapification.

Heapification Algorithm (for Max Heap)

  1. Start at the last parent node (index parent = (n - 1) / 2, where n is the number of nodes)
  2. Compare the current node with its left and right child nodes
  3. If the current node is smaller than either child, swap it with the larger child and repeat the process with the swapped node's new position as the current position
  4. Repeat until the entire heap is built or reaches the root

Here's a simple Python example to build a Max Heap:

python
def heapify(arr, n, i): largest = i # Initialize largest as root l = 2 * i + 1 # Left child r = 2 * i + 2 # Right child # If left child is larger than root if l < n and arr[largest] < arr[l]: largest = l # If right child is larger than root if r < n and arr[largest] < arr[r]: largest = r # If largest is not root if largest != i: arr[i], arr[largest] = arr[largest], arr[i] # Swap heapify(arr, n, largest) # Recursively heapify the updated node def build_max_heap(arr): n = len(arr) for i in range(n // 2, -1, -1): heapify(arr, n, i)

Quiz

Quick Quiz
Question 1 of 1

What is a key property of Heaps?

Heap Operations šŸ“

Insert

  1. Add the new element to the end of the array
  2. Heapify the inserted element using the Heapification algorithm

Extract Max/Min

  1. Swap the first and last elements
  2. Heapify the root node with the last element

Example

Let's build a Max Heap and perform some operations:

python
arr = [0, 3, 10, 1, 5, 2] build_max_heap(arr) print("Max Heap: ", arr) # Insert a new element arr.append(8) heapify(arr, len(arr), len(arr) - 2) print("Max Heap after inserting 8: ", arr) # Extract the maximum element arr[0], arr[-1] = arr[-1], arr[0] heapify(arr, len(arr) - 1, 0) print("Max Heap after extracting the maximum element: ", arr)

That's it for Heap Properties! Remember, understanding Heaps is crucial for mastering data structures and algorithms. Happy learning! šŸ¤–šŸŽ‰

Quiz

Quick Quiz
Question 1 of 1

What is the time complexity of Heapification in the worst-case scenario?