M. Santosh Kumar Datastructures Using C++
M. Santosh Kumar Datastructures Using C++
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++
4
M. SANTOSH KUMAR DATASTRUCTURES USING C++
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:
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)
6
M. SANTOSH KUMAR DATASTRUCTURES USING C++
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)