From 11d7eacd2afe8f62d61ba74fc866c9097f93fb28 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 30 Oct 2019 17:28:42 +0530 Subject: [PATCH] implementation of max heap in c++ --- Data Structures/Heap/CPP/max_heap.cpp | 50 +++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Data Structures/Heap/CPP/max_heap.cpp diff --git a/Data Structures/Heap/CPP/max_heap.cpp b/Data Structures/Heap/CPP/max_heap.cpp new file mode 100644 index 00000000..a4db5453 --- /dev/null +++ b/Data Structures/Heap/CPP/max_heap.cpp @@ -0,0 +1,50 @@ + +//BUILDING MAX HEAP USING UPADJUSTMENT + +#include +using namespace std; +int main() +{ + int h[20]; + int n=0,largest=1; + int val,i=1,j=1,parent,temp; + int ans=0; + do + { + cout<<"\nEnter the node:"; + cin>>val; + + //UPADJUSTMENT: sorting while adding + + h[i]=val; + j=i; //current node + parent=j/2; //parent node of the current node + while(parent>=1) + { + if(h[parent]> ans; + }while(ans==1); + n=i-1; + h[0]=n; + + //Displaying the max heap. + + int k=1; + cout<<"The heap is as: "; + for(k=1;k<=n;k++) + { + cout<<" "<