Search
 
SCRIPT & CODE EXAMPLE
 

CPP

max heap in c++

priority_queue <int> maxHeap; 
Comment

max heap insertion c++

#include<iostream>
using namespace std;
/*Heapify using Bottom up approach*/
void heapify(int a[],int n,int i)
{
	int parent;
	parent = (i-1)/2; //for finding the parent of child
	if(parent>=0)     //Check until index < 0
	{
		if(a[parent]<a[i])
		{
			swap(a[parent],a[i]);
			heapify(a,n,parent); //recursive heapify function
		}
	}
}
/*Inserting the New Element into the Heap*/
void insert(int a[],int &n,int val)
{
	/*Increase the Size of the Array by 1*/
	n=n+1;
	/*Insert the new element at the end of the Array*/
	a[n-1]=val;
	/*Heapify function*/
	heapify(a,n,n-1);
}
/*Printing the Heap*/
void print(int a[],int n)
{
	cout<<"
The Array Representation of Heap is
";
	for(int i=0;i<n;i++)
	{
		cout<<a[i]<<" ";
	}
}
/*Driver Function*/
int main()
{
	/*Initial Max Heap is */
	int a[100]={10,5,3,2,4};
	int n = 5;
	/*The Element to be insert is 15*/
	int val = 15;
	/*Printing the Array*/
	print(a,n);
	/*Insert Function*/
	insert(a,n,val); //here we are passing the 'n' value by reference
	/*Printing the Array*/
	print(a,n);
	return 0;
}
Comment

min heap insertion

Williams Algorithm: top downwhile not end of array, 	if heap is empty, 		place item at root; 	else, 		place item at bottom of heap; 		while (child > parent) 			swap(parent, child); 	go to next array element; end
Comment

min heap insertion

Williams Algorithm: top downwhile not end of array, 	if heap is empty, 		place item at root; 	else, 		place item at bottom of heap; 		while (child < parent) 			swap(parent, child); 	go to next array element; end
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ generic pointer 
Cpp :: int cpp 
Cpp :: 2d array of zeros c++ 
Cpp :: C++ program to print all possible substrings of a given string 
Cpp :: async multi thread 
Cpp :: Split a number and store it in vector 
Cpp :: evennumbers 1 to 100 
Cpp :: or in c++ 
Cpp :: qt file explorer 
Cpp :: Maximum sum of non consecutive elements 
Cpp :: operator overloading in c++ 
Cpp :: how to sort string array in c++ 
Cpp :: ex: cpp 
Cpp :: C++ Pointers to Structure 
Cpp :: memset c++ 
Cpp :: queue 
Cpp :: c++ visual studio 
Cpp :: c++ square and multiply algorithm 
Cpp :: c++ include difference between quotes and brackets 
Cpp :: how to get characters through their ascii value in c++ 
Cpp :: The five most significant revisions of the C++ standard are C++98 (1998), C++03 (2003) and C++11 (2011), C++14 (2014) and C++17 (2017) 
Cpp :: how to make c++ read strlen 
Cpp :: how to pronounce beaucoup 
Cpp :: identity 
Cpp :: find substring after character 
Cpp :: overloading templates in cpp 
Cpp :: multi variable assignment cpp 
Cpp :: cpp class access array member by different name 
Cpp :: how to modify set C++ 
Cpp :: Use of Scope Resolution operator for namespace 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =