Social Icons

banner image

heap in data structure

Example of a complete binary max-heap with node keys being integers from 1 to 100
In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: If A is a parent node of B then the key of node A is ordered with respect to the key of node B with the same ordering applying across the heap. Heaps can be classified further as either a "max heap" or a "min heap". In a max heap, the keys of parent nodes are always greater than or equal to those of the children and the highest key is in the root node. In a min heap, the keys of parent nodes are less than or equal to those of the children and the lowest key is in the root node. Heaps are crucial in several efficient graph algorithms such as Dijkstra's algorithm, and in the sorting algorithm heapsort. A common implementation of a heap is the binary heap, in which the tree is a complete binary tree (see figure).
In a heap, the highest (or lowest) priority element is always stored at the root, hence the name heap. A heap is not a sorted structure and can be regarded as partially ordered. As visible from the Heap-diagram, there is no particular relationship among nodes on any given level, even among the siblings. When a heap is a complete binary tree, it has a smallest possible height—a heap with N nodes always has log N height. A heap is a useful data structure when you need to remove the object with the highest (or lowest) priority.
Note that, as shown in the graphic, there is no implied ordering between siblings or cousins and no implied sequence for an in-order traversal (as there would be in, e.g., a binary search tree). The heap relation mentioned above applies only between nodes and their parents, grandparents, etc. The maximum number of children each node can have depends on the type of heap, but in many types it is at most two, which is known as a binary heap.
The heap is one maximally efficient implementation of an abstract data type called a priority queue, and in fact priority queues are often referred to as "heaps", regardless of how they may be implemented. Note that despite the similarity of the name "heap" to "stack" and "queue", the latter two are abstract data types, while a heap is a specific data structure, and "priority queue" is the proper term for the abstract data type.[citation needed]
heap data structure should not be confused with the heap which is a common name for the pool of memory from which dynamically allocated memory is allocated. The term was originally used only for the data structure.
//HEAP
#include<iostream>
#include<conio.h>
using namespace std;
int a[10],i,n;

int makeheap(int n)
{
int val,k,par;
for(i=1;i<n;i++)
{
val=a[i];
k=i;
par=(k-1)/2;
while(k>0&&a[par]<val)
{
a[k]=a[par];
k=par;
par=(k-1)/2;
a[k]=val;
}
}
}

int heapsort()
{
int temp;
for(i=9;i>0;i--)
{
temp=a[i];
a[i]=a[0];
a[0]=temp;
makeheap(i);
}
}

int main()
{
cout<<"Enter the values\n";
for(i=1;i<10;i++)
{
cin>>a[i];
}
makeheap(10);
cout<<"After makeheap\n";
for(i=1;i<10;i++)
{
cout<<a[i]<<endl;
}
heapsort();
cout<<"After heapsort\n";
for(i=1;i<10;i++)
{
cout<<a[i]<<endl;
}
}
heap in data structure heap in data structure Reviewed by Shobhit Goel on November 07, 2015 Rating: 5

No comments:

Airtel Hackaton 2017

Powered by Blogger.