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.NN+1 [Increase N by 1 to create space for new node]
3.HEAP[N]ITEM [Add new ITEM after the last node]
4.PTRN [last node where ITEM is stored in now made the current node]
5.PARENTFLOOR(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)TEMPHEAP[PTR]
b)HEAP[PTR]HEAP[PARENT]
c)HEAP[PARENT]TEMP
8.PTRPARENT [Parents become child]
9.PARENTfloor[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]
ITEMHEAP[1]
2. [Bring last node to root node position]
HEAPHEAP[N]
3. NN-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) PTR1 [ Initialize PTR to index of ROOT]
(b) FLAGFALSE [Initially root node is not at correct position]
2. Repeat Steps 3 to 6 while FLAG=FALSE
3. PARPTR [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
PTR2*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
PTR2*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]
FLAGTRUE [Flag is initialized so as to exit from the step 2 loop]
Else [Interchange parent with greater of its children]
a) TEMPHEAP [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.ENDN [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)TEMPA[1]
b)A[1]A[END]
c)A[END]TEMP
5.ENDEND-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