11/05/08 Shaily Kabir,Dept. of CSE, DU 1
Heap
11/05/08 1
11/05/08 Shaily Kabir,Dept. of CSE, DU 2
Heap
• A heap is a complete binary tree except the bottom level adjusted to the left.
• The value of each node is greater than that of its two children. (Max Heap)
• The value of each node is less than that of its two children. (Min Heap)
• Height of the heap is log2n.
• Example
100
1621
14 19 17
100
1721
14 19 16
Figure: Not a Heap Figure: A Heap
11/05/08 Shaily Kabir,Dept. of CSE, DU 3
Heap Implementation
• We can use an array (due to the regular structure or completeness of binary tree).
• For a node N with location i, the following factors can be calculated.
1. Left child of N is in location (2 * i).
2. Right child of N is in location (2 * i + 1).
3. Parent of N is in location [i/2].
• Example
1 2 3 4 5 6 7 8 9
100 21 17 14 19 16
100
1721
14 19 16
Figure: Heap and Its Array Representation
11/05/08 Shaily Kabir,Dept. of CSE, DU 4
Heap Operations
(1) Insertion
(2) Delete-max or delete-min
(3) Make-heap (organize an arbitrary array as a heap)
Insertion
• Find the left-most open position and insert the new element NewElement.
• while ( NewElement > Its Parent ) exchange them.
• Example:
Figure: Item 70 is inserted.
97
88
55
48
70
97
88
55
70
48
97
88
70
55
48
11/05/08 Shaily Kabir,Dept. of CSE, DU 5
Algorithm: INSHEAP(Heap, N, Item)
1. Set N := N +1 and Ptr := N
2. Repeat steps 3 to 6 while Ptr > 1
3. Set Par := Ptr/2
4. If Item < Heap[Par], then
Set Heap[Ptr] := Item and Return.
1. Set Heap[Ptr] := Heap[Par]
2. Set Ptr := Par
3. Heap[1] := Item
4. Return.
Here,
Heap = Max heap
N = Total no. of items in the array
Item = New item to be inserted
The Complexity Analysis of Insert Operation
• Insertion takes O( log2n ) times.
• Reasons: 1) No. of operations = O( height of the heap)
2) Height of the heap: O ( log2 n )
11/05/08 Shaily Kabir,Dept. of CSE, DU 6
Delete-Max
• Replace the root node by the last node at bottom level maintaining completeness.
• While ( new root < its children ) exchange it with the greater of its children.
• Example:
50
40 23
18 30 20 21
21
40 23
18 30 20
40
21 23
18 30 20
40
30 23
18 21 20
Figure: Item 50 (root) is deleted.
11/05/08 Shaily Kabir,Dept. of CSE, DU 7
Algorithm: DELHEAP(Heap, N, Item)
1. Set Item := Heap[1] and Set Last:=Heap[N] and N:=N-1
2. Set Ptr := 1, Left := 2 and Right := 3
3. Repeat steps 5 to 6 while Right<=N
4. If Last >= Heap[Left] and Last >= Heap[Right] then
Set Tree[Ptr] := Last and Return.
5. If Heap[Right] <= Heap[Left] then
Set Heap[Ptr] := Heap[Left] and Ptr := Left.
Else Set Heap[Ptr] := Heap[Right] and Ptr := Right
6. Set Left := 2 * Ptr and Right := Left + 1
1. If Left = N and if Last < Heap[Left] then Set Heap[Ptr] := Heap[Left] and Ptr := Left.
2. Set Heap[Ptr] := Last
3. Return
11/05/08 Shaily Kabir,Dept. of CSE, DU 8
The Complexity Analysis of Delete Operation
• Deletion takes O( log2n ) times.
• Reasons: 1) No. of operations = O( height of the heap)
2) Height of the heap: O ( log2n )
11/05/08 Shaily Kabir,Dept. of CSE, DU 9
Variants of Heap
1. Binary Heap
2. Binomial Heap
3. Fibonacci Heap
4. Pairing Heap
5. Treap
6. Beap
Application of Heap
1. HeapSort
2. Priority Queue
3. Selection Algorithms
4. Graph Algorithms
11/05/08 Shaily Kabir,Dept. of CSE, DU 10
Heap Sort
• Suppose an array with N elements is given.
• The heap sort algorithm consists of the following two steps:
Step 1: Build a heap H out of the elements of A
Step 2: Repeatedly delete the root element of H
• For sorting in descending order, max heap must be created. For ascending order, min
heap must be created.
Algorithm: HeapSort(A, N)
1. Repeat for J=1 to N-1
Call INSHEAP(A, J, A[J+1])
2. Repeat while N>1
(a) Call DELHEAP(A, N, ITEM)
(b) Set A[N+1]:= ITEM
3. Exit
Here,
A = Max Heap
N = Total No. of Items in A
11/05/08 Shaily Kabir,Dept. of CSE, DU 11
Complexity of Heapsort
• Heap Sort takes O(nlog2n ) times.
Reasons:
1. BuildHeap from the initial array requires O(nlog2n).
2. Delete-Max n times requires n x O(log2n) = O(nlog2n).
11/05/08 Shaily Kabir,Dept. of CSE, DU 1211/05/08 Shaily Kabir,Dept. of CSE, DU 12
END!!!

More Related Content

PDF
Array data structure
PPTX
Stacks in DATA STRUCTURE
PPT
Stack application
PPTX
single linked list
PPTX
Queue Implementation Using Array & Linked List
PPTX
Infix to postfix conversion
PDF
linked lists in data structures
PPT
Array data structure
Stacks in DATA STRUCTURE
Stack application
single linked list
Queue Implementation Using Array & Linked List
Infix to postfix conversion
linked lists in data structures

What's hot (20)

PPT
Data Structures- Part5 recursion
PPT
Bubble sort
PPT
Asymptotic notation
PPTX
linked list using c
PPTX
Data structure by Digvijay
PPT
PPT
PPTX
Java Stack Data Structure.pptx
PPTX
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
PPTX
Prefix, Infix and Post-fix Notations
PPTX
Sparse matrix and its representation data structure
PPT
Infix to Postfix Conversion Using Stack
PPTX
Linked List - Insertion & Deletion
PPT
Queue Data Structure
PPTX
Graph traversals in Data Structures
PPT
PPTX
Doubly linked list
PPT
Heap tree
PPTX
Linked list
PDF
Trees, Binary Search Tree, AVL Tree in Data Structures
Data Structures- Part5 recursion
Bubble sort
Asymptotic notation
linked list using c
Data structure by Digvijay
Java Stack Data Structure.pptx
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Prefix, Infix and Post-fix Notations
Sparse matrix and its representation data structure
Infix to Postfix Conversion Using Stack
Linked List - Insertion & Deletion
Queue Data Structure
Graph traversals in Data Structures
Doubly linked list
Heap tree
Linked list
Trees, Binary Search Tree, AVL Tree in Data Structures
Ad

Viewers also liked (15)

PPTX
Fitcamp en
DOCX
Report of industrial training
PPTX
Fit Camp květen H-klub.cz
PPTX
Quality model ppt
PPT
17 linkedlist (1)
PPT
14 recursion
PPT
Game theory lecture
PPT
Unicity "Make Life Better" - ClearStart30 helps you understand about Detox & ...
PDF
DarkTeespringProfits FB_Masterclasseespringprofits fb masterclass
PPT
Ap for b.tech. (mechanical) Assignment Problem
PPT
Tp for b.tech. (mechanical)
PDF
Chapter 1. dpv a. lal revised
PDF
design and analysis of composite leaf spring for light weight vehicle
PPT
plastic_fabrication processes
Fitcamp en
Report of industrial training
Fit Camp květen H-klub.cz
Quality model ppt
17 linkedlist (1)
14 recursion
Game theory lecture
Unicity "Make Life Better" - ClearStart30 helps you understand about Detox & ...
DarkTeespringProfits FB_Masterclasseespringprofits fb masterclass
Ap for b.tech. (mechanical) Assignment Problem
Tp for b.tech. (mechanical)
Chapter 1. dpv a. lal revised
design and analysis of composite leaf spring for light weight vehicle
plastic_fabrication processes
Ad

Similar to Heap (20)

PPT
21. Heap_new.ppt
PPT
Unit III Heaps.ppt
PDF
PPTX
Heap Sort in Design and Analysis of algorithms
PPT
Analysis of Algorithms-Heapsort
PPT
5-heap.ppt
PPT
5-heap.ppt
PPT
M.E - Computer Science and Engineering-Data structure unit-2 heaps
PDF
Heap Hand note
PPTX
Heap Sort Algorithm
PDF
Heapsort quick sort
PPTX
Lecture 5_ Sorting and order statistics.pptx
PDF
Heap and heapsort
PPT
Heapsort ppt
PPT
Heapsort
PDF
Heaps
PPT
PPT 2.7 Heaps is a topic from dsa trees.ppt
PPTX
Algorithms - "heap sort"
PPTX
Ch2 Part III-Advanced Sorting algorithms.pptx
21. Heap_new.ppt
Unit III Heaps.ppt
Heap Sort in Design and Analysis of algorithms
Analysis of Algorithms-Heapsort
5-heap.ppt
5-heap.ppt
M.E - Computer Science and Engineering-Data structure unit-2 heaps
Heap Hand note
Heap Sort Algorithm
Heapsort quick sort
Lecture 5_ Sorting and order statistics.pptx
Heap and heapsort
Heapsort ppt
Heapsort
Heaps
PPT 2.7 Heaps is a topic from dsa trees.ppt
Algorithms - "heap sort"
Ch2 Part III-Advanced Sorting algorithms.pptx

Recently uploaded (20)

PDF
LS-6-Digital-Literacy (1) K12 CURRICULUM .pdf
PPTX
DATA STRCUTURE LABORATORY -BCSL305(PRG1)
PPT
UNIT-I Machine Learning Essentials for 2nd years
PPTX
chapter 1.pptx dotnet technology introduction
PPTX
MAD Unit - 3 User Interface and Data Management (Diploma IT)
DOCX
ENVIRONMENTAL PROTECTION AND MANAGEMENT (18CVL756)
DOCX
An investigation of the use of recycled crumb rubber as a partial replacement...
PPTX
INTERNET OF THINGS - EMBEDDED SYSTEMS AND INTERNET OF THINGS
PDF
Project_Mgmt_Institute_-Marc Marc Marc .pdf
PPTX
BBOC407 BIOLOGY FOR ENGINEERS (CS) - MODULE 1 PART 1.pptx
PDF
Mechanics of materials week 2 rajeshwari
PPTX
Agentic Artificial Intelligence (Agentic AI).pptx
PDF
electrical machines course file-anna university
PDF
Principles of operation, construction, theory, advantages and disadvantages, ...
PDF
Engineering Solutions for Ethical Dilemmas in Healthcare (www.kiu.ac.ug)
PPTX
SE unit 1.pptx aaahshdhajdviwhsiehebeiwheiebeiev
PDF
Research on ultrasonic sensor for TTU.pdf
PPTX
Solar energy pdf of gitam songa hemant k
PDF
Software defined netwoks is useful to learn NFV and virtual Lans
PPTX
WN UNIT-II CH4_MKaruna_BapatlaEngineeringCollege.pptx
LS-6-Digital-Literacy (1) K12 CURRICULUM .pdf
DATA STRCUTURE LABORATORY -BCSL305(PRG1)
UNIT-I Machine Learning Essentials for 2nd years
chapter 1.pptx dotnet technology introduction
MAD Unit - 3 User Interface and Data Management (Diploma IT)
ENVIRONMENTAL PROTECTION AND MANAGEMENT (18CVL756)
An investigation of the use of recycled crumb rubber as a partial replacement...
INTERNET OF THINGS - EMBEDDED SYSTEMS AND INTERNET OF THINGS
Project_Mgmt_Institute_-Marc Marc Marc .pdf
BBOC407 BIOLOGY FOR ENGINEERS (CS) - MODULE 1 PART 1.pptx
Mechanics of materials week 2 rajeshwari
Agentic Artificial Intelligence (Agentic AI).pptx
electrical machines course file-anna university
Principles of operation, construction, theory, advantages and disadvantages, ...
Engineering Solutions for Ethical Dilemmas in Healthcare (www.kiu.ac.ug)
SE unit 1.pptx aaahshdhajdviwhsiehebeiwheiebeiev
Research on ultrasonic sensor for TTU.pdf
Solar energy pdf of gitam songa hemant k
Software defined netwoks is useful to learn NFV and virtual Lans
WN UNIT-II CH4_MKaruna_BapatlaEngineeringCollege.pptx

Heap

  • 1. 11/05/08 Shaily Kabir,Dept. of CSE, DU 1 Heap 11/05/08 1
  • 2. 11/05/08 Shaily Kabir,Dept. of CSE, DU 2 Heap • A heap is a complete binary tree except the bottom level adjusted to the left. • The value of each node is greater than that of its two children. (Max Heap) • The value of each node is less than that of its two children. (Min Heap) • Height of the heap is log2n. • Example 100 1621 14 19 17 100 1721 14 19 16 Figure: Not a Heap Figure: A Heap
  • 3. 11/05/08 Shaily Kabir,Dept. of CSE, DU 3 Heap Implementation • We can use an array (due to the regular structure or completeness of binary tree). • For a node N with location i, the following factors can be calculated. 1. Left child of N is in location (2 * i). 2. Right child of N is in location (2 * i + 1). 3. Parent of N is in location [i/2]. • Example 1 2 3 4 5 6 7 8 9 100 21 17 14 19 16 100 1721 14 19 16 Figure: Heap and Its Array Representation
  • 4. 11/05/08 Shaily Kabir,Dept. of CSE, DU 4 Heap Operations (1) Insertion (2) Delete-max or delete-min (3) Make-heap (organize an arbitrary array as a heap) Insertion • Find the left-most open position and insert the new element NewElement. • while ( NewElement > Its Parent ) exchange them. • Example: Figure: Item 70 is inserted. 97 88 55 48 70 97 88 55 70 48 97 88 70 55 48
  • 5. 11/05/08 Shaily Kabir,Dept. of CSE, DU 5 Algorithm: INSHEAP(Heap, N, Item) 1. Set N := N +1 and Ptr := N 2. Repeat steps 3 to 6 while Ptr > 1 3. Set Par := Ptr/2 4. If Item < Heap[Par], then Set Heap[Ptr] := Item and Return. 1. Set Heap[Ptr] := Heap[Par] 2. Set Ptr := Par 3. Heap[1] := Item 4. Return. Here, Heap = Max heap N = Total no. of items in the array Item = New item to be inserted The Complexity Analysis of Insert Operation • Insertion takes O( log2n ) times. • Reasons: 1) No. of operations = O( height of the heap) 2) Height of the heap: O ( log2 n )
  • 6. 11/05/08 Shaily Kabir,Dept. of CSE, DU 6 Delete-Max • Replace the root node by the last node at bottom level maintaining completeness. • While ( new root < its children ) exchange it with the greater of its children. • Example: 50 40 23 18 30 20 21 21 40 23 18 30 20 40 21 23 18 30 20 40 30 23 18 21 20 Figure: Item 50 (root) is deleted.
  • 7. 11/05/08 Shaily Kabir,Dept. of CSE, DU 7 Algorithm: DELHEAP(Heap, N, Item) 1. Set Item := Heap[1] and Set Last:=Heap[N] and N:=N-1 2. Set Ptr := 1, Left := 2 and Right := 3 3. Repeat steps 5 to 6 while Right<=N 4. If Last >= Heap[Left] and Last >= Heap[Right] then Set Tree[Ptr] := Last and Return. 5. If Heap[Right] <= Heap[Left] then Set Heap[Ptr] := Heap[Left] and Ptr := Left. Else Set Heap[Ptr] := Heap[Right] and Ptr := Right 6. Set Left := 2 * Ptr and Right := Left + 1 1. If Left = N and if Last < Heap[Left] then Set Heap[Ptr] := Heap[Left] and Ptr := Left. 2. Set Heap[Ptr] := Last 3. Return
  • 8. 11/05/08 Shaily Kabir,Dept. of CSE, DU 8 The Complexity Analysis of Delete Operation • Deletion takes O( log2n ) times. • Reasons: 1) No. of operations = O( height of the heap) 2) Height of the heap: O ( log2n )
  • 9. 11/05/08 Shaily Kabir,Dept. of CSE, DU 9 Variants of Heap 1. Binary Heap 2. Binomial Heap 3. Fibonacci Heap 4. Pairing Heap 5. Treap 6. Beap Application of Heap 1. HeapSort 2. Priority Queue 3. Selection Algorithms 4. Graph Algorithms
  • 10. 11/05/08 Shaily Kabir,Dept. of CSE, DU 10 Heap Sort • Suppose an array with N elements is given. • The heap sort algorithm consists of the following two steps: Step 1: Build a heap H out of the elements of A Step 2: Repeatedly delete the root element of H • For sorting in descending order, max heap must be created. For ascending order, min heap must be created. Algorithm: HeapSort(A, N) 1. Repeat for J=1 to N-1 Call INSHEAP(A, J, A[J+1]) 2. Repeat while N>1 (a) Call DELHEAP(A, N, ITEM) (b) Set A[N+1]:= ITEM 3. Exit Here, A = Max Heap N = Total No. of Items in A
  • 11. 11/05/08 Shaily Kabir,Dept. of CSE, DU 11 Complexity of Heapsort • Heap Sort takes O(nlog2n ) times. Reasons: 1. BuildHeap from the initial array requires O(nlog2n). 2. Delete-Max n times requires n x O(log2n) = O(nlog2n).
  • 12. 11/05/08 Shaily Kabir,Dept. of CSE, DU 1211/05/08 Shaily Kabir,Dept. of CSE, DU 12 END!!!