SlideShare a Scribd company logo
2
Most read
3
Most read
insertion,
deletion,
sorting
2
Heap
 A max tree is a tree in which the key value in
each node is no smaller than the key values in
its children. A max heap is a complete binary
tree that is also a max tree.
 A min tree is a tree in which the key value in
each node is no larger than the key values in
its children. A min heap is a complete binary
tree that is also a min tree.
 Operations on heaps
 creation of an empty heap
 insertion of a new element into the heap;
 deletion of the largest element from the heap
Figure : Sample max heaps
[4]
14
12 7
810 6
9
6 3
5
30
25
[1]
[2] [3]
[5] [6]
[1]
[2] [3]
[4]
[1]
[2]
Property:
The root of max heap (min heap) contains
the largest (smallest).
2
7 4
810 6
10
20 83
50
11
21
[1]
[2] [3]
[5] [6]
[1]
[2] [3]
[4]
[1]
[2]
[4]
Figure : Sample min heaps
5
Example of Insertion to Max Heap
20
15 2
14 10
initial location of new node
21
15 20
14 10 2
insert 21 into heap
20
15 5
14 10 2
insert 5 into heap
Insertion into a Max Heap
INSHEAP(TREE, 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<= TREE[PAR], then:
Set TREE[PTR]=ITEM, and Return.
[End of If structure]
5. Set TREE[PTR]=TREE[PAR]
6. Set PTR=PAR.
[End of step 2]
7. Set TREE[1]=ITEM
8. Return.
7
Example of Deletion from Max Heap
20
remove
15 2
14 10
10
15 2
14
15
14 2
10
Deletion from a Max Heap
DELHEAP(TREE, N, ITEM)
1. Set ITEM= TREE[1].
2. Set LAST= TREE[N] and N= N-1.
3. Set PTR=1, LEFT=2 and RIGHT=3.
4. Repeat Steps 5 to 7 while RIGHT<=N:
5. If LAST>=TREE[LEFT] and LAST>= TREE[RIGHT], then:
Set TREE[PTR]=LAST and Return.
[End of If structure.]
1. If TREE[RIGHT]<= TREE[LEFT], then:
Set TREE[PTR]= TREE[LEFT] and PTR=LEFT.
Else:
Set TREE[PTR]= TREE[RIGHT] and PTR=RIGHT.
[End of If structure.]
1. Set LEFT=2*PTR and RIGHT=LEFT+1.
[End of Step 4 loop.]
8. If LEFT=N and if LAST < TREE[LEFT], TREE[PTR]= TREE[LEFT]
then: Set PTR=LEFT.
9. Set TREE[PTR]= LAST.
10. Return.
HeapSort
HEAPSORT(A, N)
1. Repeat for J=1 to N-1:
Call INSHEAP(A, J, A[J+1]).
[End of loop.]
1. Repeat while N>1:
a) Call DELHEAP(A, N, ITEM).
b) A[N+1]=ITEM.
[End of loop.]
1. Exit.
Data Structure and Algorithms Heaps and Trees

More Related Content

PDF
Arrays in python
PPTX
PPT
PDF
Heap and heapsort
PPTX
3 searching algorithms in Java
PPTX
Arrays In C++
PDF
Php array
Arrays in python
Heap and heapsort
3 searching algorithms in Java
Arrays In C++
Php array

What's hot (20)

PPTX
Presentation on the topic selection sort
PDF
Python list
PPTX
Row major and column major in 2 d
PPTX
6. static keyword
PPTX
Hashing in datastructure
PPTX
Datastructures in python
PPTX
Sparse matrix and its representation data structure
PPTX
Analysis of Algorithm (Bubblesort and Quicksort)
PPTX
Greedy Algorithm - Knapsack Problem
PPT
Selection sort
PDF
linear search and binary search
PPT
Tree and Binary Search tree
PPTX
Heap sort
PDF
Python programming : List and tuples
PPTX
PPTX
The Stack And Recursion
PPT
Unit 4 external sorting
PDF
Python programming : Arrays
PPTX
Tree in data structure
Presentation on the topic selection sort
Python list
Row major and column major in 2 d
6. static keyword
Hashing in datastructure
Datastructures in python
Sparse matrix and its representation data structure
Analysis of Algorithm (Bubblesort and Quicksort)
Greedy Algorithm - Knapsack Problem
Selection sort
linear search and binary search
Tree and Binary Search tree
Heap sort
Python programming : List and tuples
The Stack And Recursion
Unit 4 external sorting
Python programming : Arrays
Tree in data structure
Ad

Similar to Data Structure and Algorithms Heaps and Trees (20)

PPT
21. Heap_new.ppt
PPTX
Algorithms - "heap sort"
PDF
Heap Tree.pdf
PPTX
Binary Heap Tree, Data Structure
PPTX
Data structures trees and graphs - Heap Tree.pptx
PPTX
Heaps and tries power point this is an educational material
PPT
Analysis of Algorithms-Heapsort
PDF
Heap Hand note
PPT
M.E - Computer Science and Engineering-Data structure unit-2 heaps
PPT
5-heap.ppt
PPT
5-heap.ppt
PPT
Unit III Heaps.ppt
PPTX
Array implementation & Construction of Heap
PDF
Heapsort quick sort
PPTX
Heaps & its operation -Max Heap, Min Heap
PPTX
Lecture 07 - HeapSort.pptx
PPTX
heap Sort Algorithm
PPTX
Heap_data_structures_in_data_steruc.pptx
PPTX
Weak 14 Heaps - Update.pptx uohogckkjjjkj
PPTX
Heap Sort Algorithm
21. Heap_new.ppt
Algorithms - "heap sort"
Heap Tree.pdf
Binary Heap Tree, Data Structure
Data structures trees and graphs - Heap Tree.pptx
Heaps and tries power point this is an educational material
Analysis of Algorithms-Heapsort
Heap Hand note
M.E - Computer Science and Engineering-Data structure unit-2 heaps
5-heap.ppt
5-heap.ppt
Unit III Heaps.ppt
Array implementation & Construction of Heap
Heapsort quick sort
Heaps & its operation -Max Heap, Min Heap
Lecture 07 - HeapSort.pptx
heap Sort Algorithm
Heap_data_structures_in_data_steruc.pptx
Weak 14 Heaps - Update.pptx uohogckkjjjkj
Heap Sort Algorithm
Ad

More from ManishPrajapati78 (15)

PPT
Data Structure and Algorithms Binary Search Tree
PPT
Data Structure and Algorithms Binary Tree
PPT
Data Structure and Algorithms Queues
PPTX
Data Structure and Algorithms Merge Sort
PPTX
Data Structure and Algorithms The Tower of Hanoi
PPT
Data Structure and Algorithms Stacks
PPT
Data Structure and Algorithms Linked List
PPT
Data Structure and Algorithms Sorting
PPT
Data Structure and Algorithms Arrays
PPT
Data Structure and Algorithms
PPT
Data Structure and Algorithms Hashing
PPTX
Data Structure and Algorithms Graph Traversal
PPT
Data Structure and Algorithms Graphs
PPT
Data Structure and Algorithms Huffman Coding Algorithm
PPT
Data Structure and Algorithms AVL Trees
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Queues
Data Structure and Algorithms Merge Sort
Data Structure and Algorithms The Tower of Hanoi
Data Structure and Algorithms Stacks
Data Structure and Algorithms Linked List
Data Structure and Algorithms Sorting
Data Structure and Algorithms Arrays
Data Structure and Algorithms
Data Structure and Algorithms Hashing
Data Structure and Algorithms Graph Traversal
Data Structure and Algorithms Graphs
Data Structure and Algorithms Huffman Coding Algorithm
Data Structure and Algorithms AVL Trees

Recently uploaded (20)

PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
AI in Product Development-omnex systems
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
medical staffing services at VALiNTRY
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Transform Your Business with a Software ERP System
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Upgrade and Innovation Strategies for SAP ERP Customers
AI in Product Development-omnex systems
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
VVF-Customer-Presentation2025-Ver1.9.pptx
Reimagine Home Health with the Power of Agentic AI​
Odoo POS Development Services by CandidRoot Solutions
medical staffing services at VALiNTRY
How to Migrate SBCGlobal Email to Yahoo Easily
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Which alternative to Crystal Reports is best for small or large businesses.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Transform Your Business with a Software ERP System
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
wealthsignaloriginal-com-DS-text-... (1).pdf
PTS Company Brochure 2025 (1).pdf.......
Odoo Companies in India – Driving Business Transformation.pdf
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Wondershare Filmora 15 Crack With Activation Key [2025

Data Structure and Algorithms Heaps and Trees

  • 2. 2 Heap  A max tree is a tree in which the key value in each node is no smaller than the key values in its children. A max heap is a complete binary tree that is also a max tree.  A min tree is a tree in which the key value in each node is no larger than the key values in its children. A min heap is a complete binary tree that is also a min tree.  Operations on heaps  creation of an empty heap  insertion of a new element into the heap;  deletion of the largest element from the heap
  • 3. Figure : Sample max heaps [4] 14 12 7 810 6 9 6 3 5 30 25 [1] [2] [3] [5] [6] [1] [2] [3] [4] [1] [2] Property: The root of max heap (min heap) contains the largest (smallest).
  • 4. 2 7 4 810 6 10 20 83 50 11 21 [1] [2] [3] [5] [6] [1] [2] [3] [4] [1] [2] [4] Figure : Sample min heaps
  • 5. 5 Example of Insertion to Max Heap 20 15 2 14 10 initial location of new node 21 15 20 14 10 2 insert 21 into heap 20 15 5 14 10 2 insert 5 into heap
  • 6. Insertion into a Max Heap INSHEAP(TREE, 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<= TREE[PAR], then: Set TREE[PTR]=ITEM, and Return. [End of If structure] 5. Set TREE[PTR]=TREE[PAR] 6. Set PTR=PAR. [End of step 2] 7. Set TREE[1]=ITEM 8. Return.
  • 7. 7 Example of Deletion from Max Heap 20 remove 15 2 14 10 10 15 2 14 15 14 2 10
  • 8. Deletion from a Max Heap DELHEAP(TREE, N, ITEM) 1. Set ITEM= TREE[1]. 2. Set LAST= TREE[N] and N= N-1. 3. Set PTR=1, LEFT=2 and RIGHT=3. 4. Repeat Steps 5 to 7 while RIGHT<=N: 5. If LAST>=TREE[LEFT] and LAST>= TREE[RIGHT], then: Set TREE[PTR]=LAST and Return. [End of If structure.] 1. If TREE[RIGHT]<= TREE[LEFT], then: Set TREE[PTR]= TREE[LEFT] and PTR=LEFT. Else: Set TREE[PTR]= TREE[RIGHT] and PTR=RIGHT. [End of If structure.] 1. Set LEFT=2*PTR and RIGHT=LEFT+1. [End of Step 4 loop.] 8. If LEFT=N and if LAST < TREE[LEFT], TREE[PTR]= TREE[LEFT] then: Set PTR=LEFT. 9. Set TREE[PTR]= LAST. 10. Return.
  • 9. HeapSort HEAPSORT(A, N) 1. Repeat for J=1 to N-1: Call INSHEAP(A, J, A[J+1]). [End of loop.] 1. Repeat while N>1: a) Call DELHEAP(A, N, ITEM). b) A[N+1]=ITEM. [End of loop.] 1. Exit.