0% found this document useful (0 votes)
82 views36 pages

A Heap Is A Data Structure That Stores A Collection of Objects (With Keys), and Has The Following Properties

A heap is a complete binary tree data structure implemented as an array. Each node corresponds to an element in the array. A heap either stores elements in ascending or descending order, satisfying the heap property that a node is greater than or equal to (or less than or equal to) its children. Heaps are used to implement priority queues and for heap sort, which first builds a max (or min) heap from an array then removes elements in order.

Uploaded by

Vijay Trivedi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views36 pages

A Heap Is A Data Structure That Stores A Collection of Objects (With Keys), and Has The Following Properties

A heap is a complete binary tree data structure implemented as an array. Each node corresponds to an element in the array. A heap either stores elements in ascending or descending order, satisfying the heap property that a node is greater than or equal to (or less than or equal to) its children. Heaps are used to implement priority queues and for heap sort, which first builds a max (or min) heap from an array then removes elements in order.

Uploaded by

Vijay Trivedi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 36

HEAP

A heap is a data structure that stores a collection


of objects (with keys), and has the following
properties:
Complete Binary tree
Heap Order

It is implemented as an array where each node in


the tree corresponds to an element of the array.
HEAP
The binary heap data structures is an array that can be
viewed as a complete binary tree. Each node of the
binary tree corresponds to an element of the array. The
array is completely filled on all levels except possibly
lowest.
19

12 16

1 4 7

19 12 16 1 4 7

Array A
HEAP ORDER PROPERTY
For every node v, other than the root, the key stored in v
is greater or equal (smaller or equal for max heap) than
the key stored in the parent of v.

In this case the maximum value is stored in the root


DEFINITION
Max Heap
Store data in ascending order
Has property of
A[Parent(i)] ≥ A[i]
Min Heap
Store data in descending order
Has property of
A[Parent(i)] ≤ A[i]
MAX HEAP EXAMPLE
19

12 16

1 4 7

19 12 16 1 4 7

Array A
MIN HEAP EXAMPLE
1

4 16

7 12 19

1 4 16 7 12 19

Array A
INSERTION
 Algorithm
1. Add the new element to the next available position at the
lowest level
2. Restore the max-heap property if violated
 General strategy is percolate up (or bubble up): if the parent of
the element is smaller than the element, then interchange the
parent and child.

OR

Restore the min-heap property if violated


 General strategy is percolate up (or bubble up): if the parent of
the element is larger than the element, then interchange the
parent and child.
19 19

12 16 12 16

4 7 1 4 7 17
1

Insert 17
19

12 17
swap

1 4 7 16

Percolate up to maintain the heap


property
DELETION
Delete max
Copy the last number to the root ( overwrite the maximum
element stored there ).
Restore the max heap property by percolate down.

Delete min
Copy the last number to the root ( overwrite the minimum
element stored there ).
Restore the min heap property by percolate down.
HEAP SORT
A sorting algorithm that works by first organizing the data to
be sorted into a special type of binary tree called a heap
PROCEDURES ON HEAP
Build Heap
Heapify
Heap Sort
HEAP
The root of the tree A[1] and given index i of a node, the
indices of its parent, left child and right child can be
computed
 
PARENT (i)
        return floor(i/2)
LEFT (i)
        return 2i
RIGHT (i)
        return 2i + 1
HEAPIFY
 Heapify picks the largest child key and compare it to the parent key. If parent
key is larger than heapify quits, otherwise it swaps the parent key with the
largest child key. So that the parent is now becomes larger than its children.
void heapify(int arr[], int n, int i)
{
int largest = i;
int l = 2*i ;
int r = 2*i + 1;
if (l <= n && arr[l] > arr[largest]) // if left child is larger than root
largest = l;
if (r <= n && arr[r] > arr[largest]) // if right child is larger than //largest so far
largest = r;
if (largest != i) // if largest is not root
{
swap(arr[i], arr[largest]);
heapify(arr, n, largest); // recursively heapify the affected sub-tree
}
}
BUILD HEAP and HEAP SORT
 We can use the procedure 'Heapify' in a bottom-up fashion to
convert an array A[1 . . n] into a heap. The procedure
BUILD_HEAP goes through the remaining nodes of the tree and
runs 'Heapify' on each one. The bottom-up order of processing node
guarantees that the subtree rooted at children are heap before
'Heapify' is run at their parent.
 The heap sort algorithm starts by using procedure BUILD-HEAP to
build a heap on the input array A[1 . . n]. Since the maximum
element of the array stored at the root A[1], it can be put into its
correct final position by exchanging it with A[n] (the last element in
A). If we now discard node n from the heap than the remaining
elements can be made into heap. Note that the new element at the
root may violate the heap property. All that is needed to restore the
heap property
HEAP SORT ALGORITHM
void buildheap(int arr[], int n)
{
// build heap (rearrange array)
for (int i = n / 2 ; i >= 1; i--)
heapify(arr, n, i);
}
HeapSort(int arr[], int n)
{
buildheap(arr[], n);
// one by one extract an element from heap
for (int i=n; i>=1; i--)
{
// move current root to end
swap(arr[1], arr[i]);
// call max heapify on the reduced heap
heapify(arr, i-1, 1);
}
}
void printarray(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << "\n";
}

int main()
{
Int x,n,arr[n+1];
cout<<”enter number of elements in heap”;
cin>>n;
for(x=1;x<=n;x++)
cin>>arr[x];
heapSort(arr, n);
cout << "Sorted array is \n";
printarray(arr, n);
}
Example: Convert the following array to a heap

16 4 7 1 12 19

Picture the array as a complete binary tree:

16

4 7

1 12 19
16 16

4 7 4 19
swap

12 19 1 12 7
1

16 19
swap

12 19 12 16
swap

4 7 1 4 7
1
HEAP SORT
 The heapsort algorithm consists of two phases:
- build a heap from an arbitrary array
- use the heap to sort the data

 To sort the elements in the decreasing order, use a min heap


 To sort the elements in the increasing order, use a max heap

19

12 16

1 4 7
EXAMPLE OF HEAP SORT
Take out biggest
19

12 16
Move the last element
to the root

1 4 7

Sorted:
Array A

12 16 1 4 7 19
7
swap
HEAPIFY()
12 16

1 4

Sorted:
Array A

7 12 16 1 4 19
16

12 7

1 4

Sorted:
Array A

16 12 7 1 4 19
Take out biggest
16
Move the last element
to the root
12 7

1 4

Sorted:
Array A

12 7 1 4 16 19
4

12 7

Sorted:
Array A

4 12 7 1 16 19
swap 4

HEAPIFY()
12 7

Sorted:
Array A

4 12 7 1 16 19
12

4 7

Sorted:
Array A

12 4 7 1 16 19
Take out biggest
12
Move the last
element to the
root 4 7

Sorted:
Array A

4 7 1 12 16 19
1
swap

4 7

Sorted:
Array A

1 4 7 12 16 19
7

4 1

Sorted:
Array A

7 4 1 12 16 19
Take out biggest
7
Move the last
element to the
4 1 root

Sorted:
Array A

1 4 7 12 16 19
swap 1

HEAPIFY()
4

Sorted:
Array A

4 1 7 12 16 19
Take out biggest
Move the last 4
element to the
root
1

Sorted:
Array A

1 4 7 12 16 19
Take out biggest
1

Sorted:
Array A

1 4 7 12 16 19
Sorted:

1 4 7 12 16 19
TIME ANALYSIS
1)Build Heap Algorithm will run in O(n) time

2)Heap sort program combine Build Heap program and


Heapify, therefore it has the running time of O(n log n)
time.

3)Total time complexity: O(n log n)

You might also like