HEAPS
HEAP TREE 
• A heap tree is a complete binary tree in which data 
values stored in any node is greater than or equal 
to the value of seach of its children(if any) 
• There is only restriction between parent and its 
children and not within the left or right subtree as 
in case of binary search trees. 
• The value stored in the root node of a heap tree is 
always the largest value in the tree.Such a heap 
tree is called a max-heap.
• We can also choose to orient a heap tree in such a 
way that data values stored in any node is less than 
or equal to the value of that node’s children. 
• Such a tree is known as min-heap. In min-heap, the 
value stored in the root is guaranteed to be the 
smallest.
ARRAY REPRESENTATION OF HEAP 
TREE 
• When representing heap tree as a 1D array,we store the 
elements in a level traversal order i.e. first node of level 0 are 
stored from left to right order followed by nodes at level 1 
from left to right order and so on. 
• In this,the root node of the heap tree is stored at index 1of the 
array and its two children are at index position 2 and 3. 
• So for any node stored at index position (i) is given by └i/2┘ 
• The location of its left and right child are given 2i and 2i+1 
respectively.
OPERATION ON HEAP TREE
INSERTION 
• Inserting an item into a heap tree is relatively straight forward. 
• We begin the insertion by first placing the node containing the 
given item immediately after the last node in the tree. 
• It is done so that the new tree still remains a complete binary 
tree.Although it staisfies the strucutral property but may violate 
the ordering property of heap tree. 
• If so,the new node is bubbled up the tree by exchanging the 
child and parent node that are out of order.We repeat the 
process until either new node reaches its correct location or 
root of heap is reached. 
• This operation that repairs the heap tree strucutre so that the 
new node is bubbled up to its correct position is the reheap up 
operation
• INSERT_HEAP(HEAP,N,ITEM)-Given a array HEAP 
which currently contains N nodes of the heap .The 
MAXSIZE variable represents the total number of 
nodes of the heap tree that HEAP array can contain . 
The local PTR stores the current position (i.e. index) 
of ITEM in the HEAP . The local variable PARENT 
stores the position (i.e. index) parent of ITEM. This 
algorithm inserts a given ITEM into a heap tree
1.[full heap?] 
If N>MAXSIZE then Write(“heap overflow”) 
Return 
[end of IF structure] 
2.NN+1 [Increase N by 1 to create space for new node] 
3.HEAP[N]ITEM [Add new ITEM after the last node] 
4.PTRN [last node where ITEM is stored in now made the current node] 
5.PARENTFLOOR(PTR/2) [Find parent of ITEM] 
6.[Continue until root is reached or ITEM reaches to its correct location] 
Repeat steps 7 to 9 while parent>=1 and HEAP[PTR]>HEAP[PARENT]
7.[Interchange HEAP[PTR] and HEAP[PARENT] 
a)TEMPHEAP[PTR] 
b)HEAP[PTR]HEAP[PARENT] 
c)HEAP[PARENT]TEMP 
8.PTRPARENT [Parents become child] 
9.PARENTfloor[PARENT/2] [Parent of parent is now new parent] 
[end of step 6 loop] 
10.Return.
DELETION 
• The root node is deleted from the heap 
• After deleting the root we are left with two 
subtrees witout a root. 
• So we must reconstruct the tree by moving the 
data from the last node to the root node. 
• And then replace the root node with its child node 
and swap with large child and repeat until the node 
reaches its correct location.
Deletion In Heaps 
DELETE_HEAP(HEAP,N): Given a array HEAP which 
currently contain N nodes. This algo deletes the root 
node and reconstruct the heap tree with remaining 
elements. The deleted root node’s value is stored in 
local variable ITEM which is finally returned.
1. [Read the root node and store into ITEM] 
ITEMHEAP[1] 
2. [Bring last node to root node position] 
HEAPHEAP[N] 
3. NN-1 [Size of heap is decreased by 1] 
4. Call SIFT_DOWN(HEAP,N) 
5. Return (ITEM)
Sift_Down 
SIFT_DOWN (HEAP,LAST):Given an array HEAP which 
currently contains N nodes of heap tree. The LAST 
variable contains the index of the last node of the heap 
tree. The variable PTR stores the index of the current 
node under consideration. The variable PAR contains 
the index of the parent node . The boolean variable 
FLAG is used to keep track whether the root node 
reaches to the correct location or to the leaf node. This 
algo sift down (reestablishes the heap) the root node to 
its correct position in the heap.
1. (a) PTR1 [ Initialize PTR to index of ROOT] 
(b) FLAGFALSE [Initially root node is not at correct position] 
2. Repeat Steps 3 to 6 while FLAG=FALSE 
3. PARPTR [PAR contains index of parent of current node] 
[PTR will contain the index of greater child of PAR if exchange is needed] 
4.If (2*PAR)<=LAST [Whether PAR has left child ] and 
HEAP[2*PAR]>HEAP [PTR] [Whether left child is greater than parent] then 
PTR2*PAR [PTR contains the index of left child] 
[End of If structure] 
5. If (2* PAR)<LAST [ Whether PAR has right child] and 
HEAP[2* PAR+1]> HEAP [PTR] [Whether right child is greater ] then 
PTR2*PAR+1 [PTR contains index of right child] 
[End of if structure]
6. If PTR=PAR then [No interchange required, node reaches its correct position] 
FLAGTRUE [Flag is initialized so as to exit from the step 2 loop] 
Else [Interchange parent with greater of its children] 
a) TEMPHEAP [PAR] 
b) HAEP[PAR]HEAP[PTR] 
c) HEAP[PTR]TEMP 
[End of If structure] 
[End of Step 2 loop] 
7. Return
HEAP SORT 
• The most common application of heap tree is the 
heapsort. 
• Heap sort begins by constructing a heap tree from 
an array of elements to be sorted. 
• Once we build the heap tree ,we then exchange the 
root element (i.e. the first element which has the 
largest value)of the heap with the last element in 
the array. 
• This exchange results in the largest element being 
placed at the correct position in the resulting 
sorted array.
• But now the rest of the element doesnot staisfy the 
ordering property of the heap so in order to get the 
next greatest element ,we again reconstruct the 
heap using the remaining unsorted elements and 
then exchange the root element with the last 
element of the resulting heap 
• This process continues until the entire list has been 
sorted.
HEAPSORT(A,N)-Given an array A containing N 
element . The algorithm sorts the elements 
of array using heapsort. 
1.[Building a heap by inserting element one by one using INSERT-HEAP 
algorithm] 
Repeat for I=1 to N 
Call INSERT_HEAP(A,I-1,A[I]) 
[End of loop] 
2.ENDN [End keep track of index of last node] 
3.Repeat steps 4 to 6 while END>0
4.[Exchange root and last node] 
a)TEMPA[1] 
b)A[1]A[END] 
c)A[END]TEMP 
5.ENDEND-1 [ Size is decreased by 1 as root reaches its correct 
position] 
6.Call SIFT_DOWN(A,END) 
[end of step 4 loop] 
7.Exit

More Related Content

PPTX
heap Sort Algorithm
PPT
PPT
Lec 17 heap data structure
PPT
SEARCHING AND SORTING ALGORITHMS
PPTX
Data structure - Graph
PDF
Python Variable Types, List, Tuple, Dictionary
PPTX
Presentation on queue
PPTX
Data structure by Digvijay
heap Sort Algorithm
Lec 17 heap data structure
SEARCHING AND SORTING ALGORITHMS
Data structure - Graph
Python Variable Types, List, Tuple, Dictionary
Presentation on queue
Data structure by Digvijay

What's hot (20)

PPSX
Data Structure (Queue)
PPT
PPTX
Binary Search Tree in Data Structure
PPTX
Graph traversals in Data Structures
PPT
Queue data structure
PPSX
Data Structure (Stack)
PPTX
Searching
PPT
PPTX
Tree in data structure
PPTX
Queue in Data Structure
PPT
PDF
linked lists in data structures
PPT
Heap sort
PDF
Heaps
PPTX
Sorting Algorithms
PPTX
Sparse matrix and its representation data structure
PPTX
Queue Implementation Using Array & Linked List
PPTX
Quick sort-Data Structure
PPTX
PPTX
Heap tree
Data Structure (Queue)
Binary Search Tree in Data Structure
Graph traversals in Data Structures
Queue data structure
Data Structure (Stack)
Searching
Tree in data structure
Queue in Data Structure
linked lists in data structures
Heap sort
Heaps
Sorting Algorithms
Sparse matrix and its representation data structure
Queue Implementation Using Array & Linked List
Quick sort-Data Structure
Heap tree
Ad

Viewers also liked (9)

PPT
Hash Functions
PDF
Binomial queues
PPTX
Binomial heap (a concept of Data Structure)
PDF
Threaded binarytree&heapsort
PPT
Avl trees
PPTX
Threaded Binary Tree
PPTX
Graph in data structure
Hash Functions
Binomial queues
Binomial heap (a concept of Data Structure)
Threaded binarytree&heapsort
Avl trees
Threaded Binary Tree
Graph in data structure
Ad

Similar to Heap tree (20)

PPTX
Heap sort
PPT
Unit III Heaps.ppt
PPT
PPT
PPT
3.7 heap sort
PPT
3-heapsort gajajag jahajahab jabajanan jabajan
PPTX
heapsort
PPT
Heapsort
PPT
Heapsortokkay
PPT
Analysis of Algorithms-Heapsort
PPTX
Heaps and tries power point this is an educational material
PDF
Heap Hand note
PPTX
heapsort_bydinesh
PDF
Heap and heapsort
PPTX
05 heap 20161110_jintaeks
PPT
Unit 4 tree
PPTX
Data structures and algorithms lab10
PPT
Heapsort
PPT
Heapsort
Heap sort
Unit III Heaps.ppt
3.7 heap sort
3-heapsort gajajag jahajahab jabajanan jabajan
heapsort
Heapsort
Heapsortokkay
Analysis of Algorithms-Heapsort
Heaps and tries power point this is an educational material
Heap Hand note
heapsort_bydinesh
Heap and heapsort
05 heap 20161110_jintaeks
Unit 4 tree
Data structures and algorithms lab10
Heapsort
Heapsort

Recently uploaded (20)

PPTX
ACFE CERTIFICATION TRAINING ON LAW.pptx
PDF
Journal of Dental Science - UDMY (2021).pdf
PPTX
Diploma pharmaceutics notes..helps diploma students
PDF
Journal of Dental Science - UDMY (2022).pdf
PPTX
Reproductive system-Human anatomy and physiology
PPTX
UNIT_2-__LIPIDS[1].pptx.................
PPTX
Power Point PR B.Inggris 12 Ed. 2019.pptx
PDF
Farming Based Livelihood Systems English Notes
PDF
FYJC - Chemistry textbook - standard 11.
PDF
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
PPTX
Thinking Routines and Learning Engagements.pptx
PPTX
Cite It Right: A Compact Illustration of APA 7th Edition.pptx
PDF
Hospital Case Study .architecture design
PPTX
PLASMA AND ITS CONSTITUENTS 123.pptx
PDF
Horaris_Grups_25-26_Definitiu_15_07_25.pdf
PDF
The TKT Course. Modules 1, 2, 3.for self study
PDF
0520_Scheme_of_Work_(for_examination_from_2021).pdf
PPTX
Case Study on mbsa education to learn ok
PDF
PUBH1000 - Module 6: Global Health Tute Slides
PPTX
pharmaceutics-1unit-1-221214121936-550b56aa.pptx
ACFE CERTIFICATION TRAINING ON LAW.pptx
Journal of Dental Science - UDMY (2021).pdf
Diploma pharmaceutics notes..helps diploma students
Journal of Dental Science - UDMY (2022).pdf
Reproductive system-Human anatomy and physiology
UNIT_2-__LIPIDS[1].pptx.................
Power Point PR B.Inggris 12 Ed. 2019.pptx
Farming Based Livelihood Systems English Notes
FYJC - Chemistry textbook - standard 11.
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
Thinking Routines and Learning Engagements.pptx
Cite It Right: A Compact Illustration of APA 7th Edition.pptx
Hospital Case Study .architecture design
PLASMA AND ITS CONSTITUENTS 123.pptx
Horaris_Grups_25-26_Definitiu_15_07_25.pdf
The TKT Course. Modules 1, 2, 3.for self study
0520_Scheme_of_Work_(for_examination_from_2021).pdf
Case Study on mbsa education to learn ok
PUBH1000 - Module 6: Global Health Tute Slides
pharmaceutics-1unit-1-221214121936-550b56aa.pptx

Heap tree

  • 2. HEAP TREE • A heap tree is a complete binary tree in which data values stored in any node is greater than or equal to the value of seach of its children(if any) • There is only restriction between parent and its children and not within the left or right subtree as in case of binary search trees. • The value stored in the root node of a heap tree is always the largest value in the tree.Such a heap tree is called a max-heap.
  • 3. • We can also choose to orient a heap tree in such a way that data values stored in any node is less than or equal to the value of that node’s children. • Such a tree is known as min-heap. In min-heap, the value stored in the root is guaranteed to be the smallest.
  • 4. ARRAY REPRESENTATION OF HEAP TREE • When representing heap tree as a 1D array,we store the elements in a level traversal order i.e. first node of level 0 are stored from left to right order followed by nodes at level 1 from left to right order and so on. • In this,the root node of the heap tree is stored at index 1of the array and its two children are at index position 2 and 3. • So for any node stored at index position (i) is given by └i/2┘ • The location of its left and right child are given 2i and 2i+1 respectively.
  • 6. INSERTION • Inserting an item into a heap tree is relatively straight forward. • We begin the insertion by first placing the node containing the given item immediately after the last node in the tree. • It is done so that the new tree still remains a complete binary tree.Although it staisfies the strucutral property but may violate the ordering property of heap tree. • If so,the new node is bubbled up the tree by exchanging the child and parent node that are out of order.We repeat the process until either new node reaches its correct location or root of heap is reached. • This operation that repairs the heap tree strucutre so that the new node is bubbled up to its correct position is the reheap up operation
  • 7. • INSERT_HEAP(HEAP,N,ITEM)-Given a array HEAP which currently contains N nodes of the heap .The MAXSIZE variable represents the total number of nodes of the heap tree that HEAP array can contain . The local PTR stores the current position (i.e. index) of ITEM in the HEAP . The local variable PARENT stores the position (i.e. index) parent of ITEM. This algorithm inserts a given ITEM into a heap tree
  • 8. 1.[full heap?] If N>MAXSIZE then Write(“heap overflow”) Return [end of IF structure] 2.NN+1 [Increase N by 1 to create space for new node] 3.HEAP[N]ITEM [Add new ITEM after the last node] 4.PTRN [last node where ITEM is stored in now made the current node] 5.PARENTFLOOR(PTR/2) [Find parent of ITEM] 6.[Continue until root is reached or ITEM reaches to its correct location] Repeat steps 7 to 9 while parent>=1 and HEAP[PTR]>HEAP[PARENT]
  • 9. 7.[Interchange HEAP[PTR] and HEAP[PARENT] a)TEMPHEAP[PTR] b)HEAP[PTR]HEAP[PARENT] c)HEAP[PARENT]TEMP 8.PTRPARENT [Parents become child] 9.PARENTfloor[PARENT/2] [Parent of parent is now new parent] [end of step 6 loop] 10.Return.
  • 10. DELETION • The root node is deleted from the heap • After deleting the root we are left with two subtrees witout a root. • So we must reconstruct the tree by moving the data from the last node to the root node. • And then replace the root node with its child node and swap with large child and repeat until the node reaches its correct location.
  • 11. Deletion In Heaps DELETE_HEAP(HEAP,N): Given a array HEAP which currently contain N nodes. This algo deletes the root node and reconstruct the heap tree with remaining elements. The deleted root node’s value is stored in local variable ITEM which is finally returned.
  • 12. 1. [Read the root node and store into ITEM] ITEMHEAP[1] 2. [Bring last node to root node position] HEAPHEAP[N] 3. NN-1 [Size of heap is decreased by 1] 4. Call SIFT_DOWN(HEAP,N) 5. Return (ITEM)
  • 13. Sift_Down SIFT_DOWN (HEAP,LAST):Given an array HEAP which currently contains N nodes of heap tree. The LAST variable contains the index of the last node of the heap tree. The variable PTR stores the index of the current node under consideration. The variable PAR contains the index of the parent node . The boolean variable FLAG is used to keep track whether the root node reaches to the correct location or to the leaf node. This algo sift down (reestablishes the heap) the root node to its correct position in the heap.
  • 14. 1. (a) PTR1 [ Initialize PTR to index of ROOT] (b) FLAGFALSE [Initially root node is not at correct position] 2. Repeat Steps 3 to 6 while FLAG=FALSE 3. PARPTR [PAR contains index of parent of current node] [PTR will contain the index of greater child of PAR if exchange is needed] 4.If (2*PAR)<=LAST [Whether PAR has left child ] and HEAP[2*PAR]>HEAP [PTR] [Whether left child is greater than parent] then PTR2*PAR [PTR contains the index of left child] [End of If structure] 5. If (2* PAR)<LAST [ Whether PAR has right child] and HEAP[2* PAR+1]> HEAP [PTR] [Whether right child is greater ] then PTR2*PAR+1 [PTR contains index of right child] [End of if structure]
  • 15. 6. If PTR=PAR then [No interchange required, node reaches its correct position] FLAGTRUE [Flag is initialized so as to exit from the step 2 loop] Else [Interchange parent with greater of its children] a) TEMPHEAP [PAR] b) HAEP[PAR]HEAP[PTR] c) HEAP[PTR]TEMP [End of If structure] [End of Step 2 loop] 7. Return
  • 16. HEAP SORT • The most common application of heap tree is the heapsort. • Heap sort begins by constructing a heap tree from an array of elements to be sorted. • Once we build the heap tree ,we then exchange the root element (i.e. the first element which has the largest value)of the heap with the last element in the array. • This exchange results in the largest element being placed at the correct position in the resulting sorted array.
  • 17. • But now the rest of the element doesnot staisfy the ordering property of the heap so in order to get the next greatest element ,we again reconstruct the heap using the remaining unsorted elements and then exchange the root element with the last element of the resulting heap • This process continues until the entire list has been sorted.
  • 18. HEAPSORT(A,N)-Given an array A containing N element . The algorithm sorts the elements of array using heapsort. 1.[Building a heap by inserting element one by one using INSERT-HEAP algorithm] Repeat for I=1 to N Call INSERT_HEAP(A,I-1,A[I]) [End of loop] 2.ENDN [End keep track of index of last node] 3.Repeat steps 4 to 6 while END>0
  • 19. 4.[Exchange root and last node] a)TEMPA[1] b)A[1]A[END] c)A[END]TEMP 5.ENDEND-1 [ Size is decreased by 1 as root reaches its correct position] 6.Call SIFT_DOWN(A,END) [end of step 4 loop] 7.Exit