SlideShare une entreprise Scribd logo
1  sur  19
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

Contenu connexe

Tendances (20)

Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Heaps
HeapsHeaps
Heaps
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
single linked list
single linked listsingle linked list
single linked list
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Binary tree
Binary treeBinary tree
Binary tree
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
 
Double Linked List (Algorithm)
Double Linked List (Algorithm)Double Linked List (Algorithm)
Double Linked List (Algorithm)
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
 
B and B+ tree
B and B+ treeB and B+ tree
B and B+ tree
 
Heaps
HeapsHeaps
Heaps
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
Doubly linked list (animated)
Doubly linked list (animated)Doubly linked list (animated)
Doubly linked list (animated)
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Linked List
Linked ListLinked List
Linked List
 

En vedette (10)

Hash Functions
Hash FunctionsHash Functions
Hash Functions
 
Binomial queues
Binomial queuesBinomial queues
Binomial queues
 
Binomial heap (a concept of Data Structure)
Binomial heap (a concept of Data Structure)Binomial heap (a concept of Data Structure)
Binomial heap (a concept of Data Structure)
 
Threaded binarytree&heapsort
Threaded binarytree&heapsortThreaded binarytree&heapsort
Threaded binarytree&heapsort
 
Avl trees
Avl treesAvl trees
Avl trees
 
Heaps
HeapsHeaps
Heaps
 
Lecture8 data structure(graph)
Lecture8 data structure(graph)Lecture8 data structure(graph)
Lecture8 data structure(graph)
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
Threaded Binary Tree
Threaded Binary TreeThreaded Binary Tree
Threaded Binary Tree
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 

Similaire à Heap tree

Data structures and algorithms lab10
Data structures and algorithms lab10Data structures and algorithms lab10
Data structures and algorithms lab10Bianca Teşilă
 
heap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithmsheap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithmsssuser7319f8
 
Quick and Heap Sort with examples
Quick and Heap Sort with examplesQuick and Heap Sort with examples
Quick and Heap Sort with examplesBst Ali
 
Sienna 7 heaps
Sienna 7 heapsSienna 7 heaps
Sienna 7 heapschidabdu
 
05 heap 20161110_jintaeks
05 heap 20161110_jintaeks05 heap 20161110_jintaeks
05 heap 20161110_jintaeksJinTaek Seo
 
Heap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithmsHeap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithmssamairaakram
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).pptAmitShou
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3infanciaj
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.pptLegesseSamuel
 
Heaps and tries power point this is an educational material
Heaps and tries power point this is an educational materialHeaps and tries power point this is an educational material
Heaps and tries power point this is an educational materialAymericTaylor
 
Heap Sort 1053.pptx
Heap Sort 1053.pptxHeap Sort 1053.pptx
Heap Sort 1053.pptxZainiXh
 
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdfSorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdfArjunSingh81957
 

Similaire à Heap tree (20)

Unit III Heaps.ppt
Unit III Heaps.pptUnit III Heaps.ppt
Unit III Heaps.ppt
 
Data structures and algorithms lab10
Data structures and algorithms lab10Data structures and algorithms lab10
Data structures and algorithms lab10
 
Heaps & priority queues
Heaps & priority queuesHeaps & priority queues
Heaps & priority queues
 
heap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithmsheap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithms
 
Heap sort
Heap sortHeap sort
Heap sort
 
Quick and Heap Sort with examples
Quick and Heap Sort with examplesQuick and Heap Sort with examples
Quick and Heap Sort with examples
 
Sienna 7 heaps
Sienna 7 heapsSienna 7 heaps
Sienna 7 heaps
 
05 heap 20161110_jintaeks
05 heap 20161110_jintaeks05 heap 20161110_jintaeks
05 heap 20161110_jintaeks
 
Heap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithmsHeap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithms
 
heapsort
 heapsort heapsort
heapsort
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
 
Heap sort
Heap sortHeap sort
Heap sort
 
HeapSort
HeapSortHeapSort
HeapSort
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
Heaps and tries power point this is an educational material
Heaps and tries power point this is an educational materialHeaps and tries power point this is an educational material
Heaps and tries power point this is an educational material
 
Heap Sort 1053.pptx
Heap Sort 1053.pptxHeap Sort 1053.pptx
Heap Sort 1053.pptx
 
Trees
TreesTrees
Trees
 
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdfSorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
 

Plus de Shankar Bishnoi

Plus de Shankar Bishnoi (6)

Avl trees 2
Avl trees 2Avl trees 2
Avl trees 2
 
Avl tree
Avl treeAvl tree
Avl tree
 
trees
trees trees
trees
 
binary search tree
binary search treebinary search tree
binary search tree
 
binary tree
binary treebinary tree
binary tree
 
tree in Data Structures
tree in Data Structurestree in Data Structures
tree in Data Structures
 

Dernier

THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxJanEmmanBrigoli
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsRommel Regala
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxruthvilladarez
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 

Dernier (20)

THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptx
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World Politics
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 

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