Agenda
What Is Heap Data Structure in C?
Types of Heap Data Structure in C
Heap Insertion Operation
Heap Deletion Operation
What Is Heap Data Structure in C?
What Is Heap Data Structure in C?
Heap
Heap
Heap is a free pool or memory storage space to store data dynamically. When
program variables are allocated dynamically it is stored in the heap memory storage.
Dynamic variable
What Is Heap Data Structure in C?
In c programming, Heap is a complete tree-based data structure following a specific order. A
heap consists of root node/ parent node and child node
7
5 6
2 3
Root node / parent node
Child node
7 5 6 2 3
Array Representation
Heap Representation
0 1 2 3 4
0
1 2
6
4 5
3
Types of Heap Data Structure in C
Types of Heap Data Structure in C
Max heap
The element of the root node must be greater than or equal to the
elements of its children's nodes
Max heap example
7
5 6
2 3
Types of Heap Data Structure in C
Min heap
The element of the root node must be less than or equal to the elements
of its children's nodes
Min heap example
2
3 5
6 7
Types of Heap Data Structure in C
Max heap implementation
7
2 6
5 3
• Let the root node index be i which is the current element and set
the current element i to be the largest element.
• The left child index is given in terms of 2i+1 and the right child
index is 2i+2
• If the left child node or right child node element is larger than the
current element, then swap the elements and set it as the largest
element.
• Repeat the process until the tree is heapified.
2
5
0
1 2
3 4
Heap Insertion Operation
Insert one new element into the heap
Heap Insertion Operation
8
5 6
2 3
Let’s Insert element 7 into the heap
8
5 6
2 3 7
Heapify the tree. By swapping the elements
Heap Insertion Operation
Finally, an element is added to the heap
8
5 6
2 3 7
8
5 7
2 3 6
Heap Deletion Operation
Heap Deletion Operation
8
5 6
2 3 7
0
2
1
3 4 5
6
8
5 6
2 3 7
2
1
3 4 5
6
0
Delete one element from the heap
To delete element 5, swap the
element with the last node
element 7
8
7 6
2 3 5
2
1
3 4 5
3
6
0
Finally eliminate the
element from the heap
Heap Data Structure Tutorial

Heap Data Structure Tutorial

  • 2.
    Agenda What Is HeapData Structure in C? Types of Heap Data Structure in C Heap Insertion Operation Heap Deletion Operation
  • 3.
    What Is HeapData Structure in C?
  • 4.
    What Is HeapData Structure in C? Heap Heap Heap is a free pool or memory storage space to store data dynamically. When program variables are allocated dynamically it is stored in the heap memory storage. Dynamic variable
  • 5.
    What Is HeapData Structure in C? In c programming, Heap is a complete tree-based data structure following a specific order. A heap consists of root node/ parent node and child node 7 5 6 2 3 Root node / parent node Child node 7 5 6 2 3 Array Representation Heap Representation 0 1 2 3 4 0 1 2 6 4 5 3
  • 6.
    Types of HeapData Structure in C
  • 7.
    Types of HeapData Structure in C Max heap The element of the root node must be greater than or equal to the elements of its children's nodes Max heap example 7 5 6 2 3
  • 8.
    Types of HeapData Structure in C Min heap The element of the root node must be less than or equal to the elements of its children's nodes Min heap example 2 3 5 6 7
  • 9.
    Types of HeapData Structure in C Max heap implementation 7 2 6 5 3 • Let the root node index be i which is the current element and set the current element i to be the largest element. • The left child index is given in terms of 2i+1 and the right child index is 2i+2 • If the left child node or right child node element is larger than the current element, then swap the elements and set it as the largest element. • Repeat the process until the tree is heapified. 2 5 0 1 2 3 4
  • 10.
  • 11.
    Insert one newelement into the heap Heap Insertion Operation 8 5 6 2 3 Let’s Insert element 7 into the heap 8 5 6 2 3 7
  • 12.
    Heapify the tree.By swapping the elements Heap Insertion Operation Finally, an element is added to the heap 8 5 6 2 3 7 8 5 7 2 3 6
  • 13.
  • 14.
    Heap Deletion Operation 8 56 2 3 7 0 2 1 3 4 5 6 8 5 6 2 3 7 2 1 3 4 5 6 0 Delete one element from the heap To delete element 5, swap the element with the last node element 7 8 7 6 2 3 5 2 1 3 4 5 3 6 0 Finally eliminate the element from the heap