0% found this document useful (0 votes)
139 views7 pages

M. Santosh Kumar Datastructures Using C++

The document discusses heaps and their implementation using arrays. It defines a max heap as a complete binary tree where each node's key is greater than or equal to its children's keys. It provides algorithms for heap operations like insertion, deletion, and building a heap from a list or binary tree. Heapsort is also summarized, which works by building a max heap from an array and then extracting elements in sorted order.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views7 pages

M. Santosh Kumar Datastructures Using C++

The document discusses heaps and their implementation using arrays. It defines a max heap as a complete binary tree where each node's key is greater than or equal to its children's keys. It provides algorithms for heap operations like insertion, deletion, and building a heap from a list or binary tree. Heapsort is also summarized, which works by building a max heap from an array and then extracting elements in sorted order.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

M.

SANTOSH KUMAR DATASTRUCTURES USING C++


Introduction:
1. A heap is a binary tree (complete), each level of the tree is completely filled, except the bottom level
where it is filled from left to right.
2. It satisfies the heap order property, i.e, the key value of each node is greater than or equal to the key value
of its children or
The key value of each node is lesser than or equal to the key value of its children.
Min Heap:
1. The heap value of each node is lesser than or equal to the key value of its children
Max Heap:
1. A max heap is where the key value of a node is greater than or equal to the key value of its children.
Note:
Whenever the term “heap” is used it indicate the max heap.
Implementation of heap:
1. To implement heaps using arrays, it is an easy task be simply number the nodes in the heap from top to
th th
bottom, from left to right and store the i node in the i location.
2. The root node is stored at index zero, left node at 1 and right at 2 and so on….
3. To find the parent node index from a particular node (i) is (i-1)/2
4. To find left node index from the node (i) 2i + 1
5. To find right node index from the node (i) 2i + 2
Heap ADT:
1. A heap is a complete binary tree, which satisfies the max heap property, that the key value of each node is
greater than or equal to the key value of its children.
ADT
ADT Heap
1. Create ( )  Heap
2. Insert (Heap data)  Heap
3. Delete Maxval (Heap)  Heap
4. ReHeapDown (Heap,Child)  Heap
5. ReHeapUp (Heap,root)  Heap
6. End
Operations on Heap:
Create ( ):
 Creates an empty tree to which the root points
Insert ( ):

1
M. SANTOSH KUMAR DATASTRUCTURES USING C++
 Inserts an element into the heap
Delete ( ):
 Deletes maximum value from the heap
ReHeapUp ():
 Re builds the heap tree when the insert function is applied
ReHeapDown ( ):
 Re builds the heap tree when the delete function is applied
ReHeapUp:
1. The following is the complete binary tree and following max heap property.
Example:
 To insert the node in the above complete binary tree:
 The above tree violates the max heap property because 36 greater than 23, reorganization is required.
 The above tree violates the max heap property because 36 is greater than 32, reorganization is required
 In a heap tree while insertion operation the value of newnode sometimes greater than its parent.
 In such situation, the new ode is lifted level by level by comparing its value with the parent (also root),
eventually placed in the correct position.
ReHeapDown:
 Delete the maximum value (root), bring the last node 21 and make it root.
 In the above the root value 21 is lesser than the right child 43, reorganization required.
 In the above the root value 21 is less than right child 41, reorganization required.
 When the complete binary tree satisfies the heap order property except in the root position, we implement
the ReHeapDown operation.
 The ReHeapDown operation occurs when the root element is deleted, provides to disjoint heap tree. To
correct the position of the heap tree the last node of the tree becomes the root, it violates the heap property.
 To restore the heap the root element is compared with the element at the next level and positioned in the
correct place using ReHeapDown.
I Constructing Heap from list
1. The unsorted keys are taken sequentially one at a time and added into a heap. The size of heap grows with
the addition of each key.
th
2. The i element is added into and exiting heap of size (i – 1) and a heap of size is is obtained.

3. The node is placed in the array (heap). If Ki is greater than the value of Ki-1 exchange is required, this
process will continues until it is in correct position.
Unsorted List: 8, 20, 9, 4, 15, 10, 7, 22, 3, 12

2
M. SANTOSH KUMAR DATASTRUCTURES USING C++

3
M. SANTOSH KUMAR DATASTRUCTURES USING C++

II Constructing Heap from Binary Tree


1. In this the heap is constructed from the given array after constructing the binary tree.
a) Organise the entire collection of data elements as a binary tree stored in an array indexed from 0 to n-1.
b) Divide the binary tree into two parts, the top part in which the data elements are in their original order and
the bottom part in which the data elements are in their heap order, where each node is in higher order than its
children if any
c) Start the bottom part which contain only leaf nodes (it is in heap order).
d) Move the last node from the top part to the bottom part compare its order with its children, and swap its
location with is highest order child if its order is lower than any child repeat the comparison and swapping to
ensure the bottom part is in heap order again with this new node.
e) Repeat step d until the top part is empty at its time the bottom part becomes a complete heap tree.

4
M. SANTOSH KUMAR DATASTRUCTURES USING C++

Array after constructing heap tree is as follows


Index 0 1 2 3 4 5 6 7 8 9 10 11
Array 99 88 90 60 77 66 40 33 55 22 44 11

Heap Sort:
5
M. SANTOSH KUMAR DATASTRUCTURES USING C++
1. The heap sort follows the following steps:
a) Build the heap tree
b) Start delete heap operation and store each deleted element at the end of the heap array.
c) The order of the elements will be opposite to that in the heap tree. Hence, if we want the elements to be
sorted in ascending order, we need to build the heap tree in descending order
i) When building the heap tree, a part of the array will be considered as the heap and the remaining part will
be the original array
ii) When sorting, a part of the array will be the heap and the remaining part will be the sorted array
 Consider the following example with the given list
13, 17, 11, 6, 15, 8
Heap tree:

Delete the top element 17

Store 17 at the end of the array, the element 8 is adjusted in the heap swap 17, 8. Now the following is the
resulted array (heap + sorted array)

The resulting tree is after ReHeapDown as follows

6
M. SANTOSH KUMAR DATASTRUCTURES USING C++

Delete the top element 15

Store 15 at the end of the array, the element 8 is adjusted in the heap swap 15, 8. Now the following is
resulted array (heap + sorted array)

Delete the top element 13


Delete the top element 11
Delete the top element 8
Delete the top element 6

You might also like