Not Yet Answered # difference between min heap and max heap...

Ravalon 62 Discussion Starter cezarjont Ravalon 62 Narue 5,707 Narue 5,707 Hi I'm having a problem implementing a mini shopping cart drop down in the header to show the user all the products they have in their shopping cart. It seems the only solution for this is Ajax, and I've looked all over and can't find anything that I could possibly ...

0

is min heap different from max heap...?

Yes. :) A min heap uses ascending priority where the smallest item is the first to be popped from the heap. A max heap uses descending priority where the largest item is the first to be popped. You can read more about heaps here. Oh, the C++ heap functions in <algorithm> assume a max heap. :)

```
#include <algorithm>
#include <cstdlib>
#include <iostream>
int main()
{
using namespace std;
int heap[10];
for ( int i = 0; i < 10; i++ )
heap[i] = rand() % 100;
// Build a max heap
make_heap( heap, heap + 10 );
// Prove that it's a max heap
for ( int n = 10; n > 0; n-- ) {
for ( int i = 0; i < n; i++ )
cout << heap[i] << ' ';
pop_heap( heap, heap + n );
cout << '\n';
}
return 0;
}
```

You can turn it into a min heap with some clever functional magic. ;)

```
#include <algorithm>
#include <functional>
#include <cstdlib>
#include <iostream>
int main()
{
using namespace std;
int heap[10];
for ( int i = 0; i < 10; i++ )
heap[i] = rand() % 100;
// Build a max heap
make_heap( heap, heap + 10, greater<int>() );
// Prove that it's a max heap
for ( int n = 10; n > 0; n-- ) {
for ( int i = 0; i < n; i++ )
cout << heap[i] << ' ';
pop_heap( heap, heap + n, greater<int>() );
cout << '\n';
}
return 0;
}
```

0

thnx... but i can't really understand it... i only know turbo c... not c++...

2

thnx... but i can't really understand it... i only know turbo c... not c++...

But I don't wanna write a heap in C just for an example. :cry: Okay okay, here is a simple example in C for a max heap. :)

```
#include <stdlib.h>
#define CHILD(node) ((node) * 2 + 1)
static void heapify( int a[], int i, int n )
{
/* Heapify an unknown number of subtrees */
while ( CHILD( i ) < n ) {
int child = CHILD( i );
/* Pick the larger of the two children */
if ( child + 1 < n && a[child] < a[child + 1] )
++child;
/* Move the largest node to the root */
if ( a[i] < a[child] ) {
int temp = a[i];
a[i] = a[child];
a[child] = temp;
}
/* Move to the next node */
++i;
}
}
void make_heap( int a[], int n )
{
int i = n / 2;
while ( i-- > 0 )
heapify( a, i, n );
}
void pop_heap( int heap[], int n )
{
int temp = heap[0];
heap[0] = heap[n];
heap[n] = temp;
heapify( heap, 0, n );
}
int main( void )
{
int heap[10];
int i;
int n;
for ( i = 0; i < 10; i++ )
heap[i] = rand() % 100;
/* Build a max heap */
make_heap( heap, 10 );
/* Prove that it's a max heap */
for ( n = 10; n >= 0; n-- ) {
printf( "%d ", heap[0] );
pop_heap( heap, n );
}
putchar( '\n' );
return 0;
}
```

And a min heap.

```
#include <stdlib.h>
#define CHILD(node) ((node) * 2 + 1)
static void heapify( int a[], int i, int n )
{
/* Heapify an unknown number of subtrees */
while ( CHILD( i ) < n ) {
int child = CHILD( i );
/* Pick the smaller of the two children */
if ( child + 1 < n && a[child] > a[child + 1] )
++child;
/* Move the smallest node to the root */
if ( a[i] > a[child] ) {
int temp = a[i];
a[i] = a[child];
a[child] = temp;
}
/* Move to the next node */
++i;
}
}
void make_heap( int a[], int n )
{
int i = n / 2;
while ( i-- > 0 )
heapify( a, i, n );
}
void pop_heap( int heap[], int n )
{
int temp = heap[0];
heap[0] = heap[n];
heap[n] = temp;
heapify( heap, 0, n );
}
int main( void )
{
int heap[10];
int i;
int n;
for ( i = 0; i < 10; i++ )
heap[i] = rand() % 100;
/* Build a min heap */
make_heap( heap, 10 );
/* Prove that it's a min heap */
for ( n = 10; n >= 0; n-- ) {
printf( "%d ", heap[0] );
pop_heap( heap, n );
}
putchar( '\n' );
return 0;
}
```

0

**>is there any difference between the construction of max heap and min heap??**

The only difference is the ordering of your data. In a min heap, the smallest elements have priority, and in a max heap the largest elements have priority.

0

**>can you please explain with example **

What's wrong with the examples that were already given in this thread?

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Hello All ...

Iam Getting An Error With try to excecute the stored procedure .

I have Have Sql database , the stored procedure like so :

```
USE [MPRS]
GO
/****** Object: StoredProcedure [dbo].[Search_Licenses_By_Number] Script Date: 26-Nov-16 8:06:52 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE ...
```

Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"

Here is the sample output that I ...