You are on page 1of 19

Heap Data Structure

Instructor Name: Geetika Gautam


Heap
• A heap is a special tree-based data structure in which the tree is a
complete binary tree.
They are complete binary trees that have the following features:
• Every level is filled except the leaf nodes (nodes without children are
called leaves).
• Every node has a maximum of 2 children.
• All the nodes are as far left as possible, this means that every child is
to the left of his parent.
Height of a node: The number of edges starting at that node, and going
down to the furthest leaf.
Height of the heap: The maximum number of edges from the root to a
leaf
Heap Property
• Max-heap: in max heap the key present at the root node must be
greatest among the keys present at all it’s children.

• Min-heap: in min heap the key present at the root node must be
minimum among the keys present at all it’s children.
Operations in Heaps

The following are the essential operations you might use when implementing a heap data
structure:

heapify: rearranges the elements in the heap to maintain the heap property.

insert: adds an item to a heap while maintaining its heap property.

delete: removes an item in a heap.

extract: returns the value of an item and then deletes it from the heap.

isEmpty: boolean, returns true if boolean is empty and false if it has a node.

size: returns the size of the heap.


Max Heap Insertion
Step1 -Create a new node at the end of heap.
Step 2 − Assign new value to the node.
Step 3-Compare the value of this child node with its parent.
Step 4 − If value of parent is less than child, then swap them.
Step 5 − Repeat step 3 & 4 until Heap property holds.
Max heap Deletion
• Step 1-Remove root node.
• Step 2 − Move the last element of last level to root.
• Step 3 − Compare the value of this child node with its parent.
• Step 4 − If value of parent is less than child, then swap them.
• Step 5 − Repeat step 3 & 4 until Heap property holds.
Implementation of Heap

If we are looking at the i-th index in an array:


• It’s parent is at the floor (i-1)/2 index.
• It’s left child is at 2 * i + 1 index.
• It’s right child is at 2 *i + 2 index.
Example of Min Heap
3 1 6 5 2 4
Array:

Step 1 : Insert 3 in the root of empty heap

3
Next insert 1 at the bottom of the heap

Swap the child node (1) with the parent node (3) because 1 is less than 3
1

3
Insert 6 in the bottom of the heap, and since 6 is greater than 1, no swapping
needed
1

3 6
Next insert 5 to the bottom of the heap, and since 5 is greater than 3,no swapping
needed.
1

3 6

5
Next insert 2 to the bottom of the heap, and since 2 is less than 3,we need to
swap
1

2 6

5 3
Next insert 4 to the bottom of the heap, and since 4 is less than 6,need to swap

2 4

5 3 6
Example of Max Heap
3 1 6 5 2 4
Array:

Step 1 : Insert 3 in the root of empty heap

3
Next insert 1 at the bottom of the heap

Insert 6 in the bottom of the heap, and since 6 is greater than 3, swap it

1 3
Next insert 5 to the bottom of the heap, and since 5 is greater than 1,swap it

5 3

1
Next insert 2 to the bottom of the heap, and since 2 is less than 5,no swapping

5 3

1 2
Next insert 4 to the bottom of the heap, and since 4 is greater than 3,need to
swap
6

5 4

1 2 3
Exercise:
• Construct a max heap for the given array of elements-
1, 5, 6, 8, 12, 14, 16

You might also like