# Find minimum element in max heap

Here given code implementation process.

``````//C Program
//Find minimum element in max heap
#include <stdio.h>

void print_data(int heap[],int size)
{

for(int i = 0; i < size; i++)
{
printf(" %d ",heap[i] );
}
printf("\n");
}
int find_min(int heap[],int ans, int index,int size)
{
if((heap[index*2+1]) < ans )
{
return heap[index*2+1];
}
else if((heap[index*2+2]) < ans)
{
return heap[index*2+2];
}
return ans;

}
void min_element(int heap[],int size)
{
int min_node = heap[size/2];

for (int i = (size/2)-1; i >= 0; i--)
{
min_node = find_min(heap,min_node,i,size);
}
printf("  Min : %d\n",min_node );
}
int main()
{

/*
10
/   \
9     8
/ \   / \
7   4 3   1
/ \
6   2

*/
//max heap array elements
int heap[] = {10,  9,  8,  7,  4,  3,  1,  6,  2 };

//Get the size of array
int size = sizeof(heap)/sizeof(heap[0]);

print_data(heap,size);
min_element(heap,size);

return 0;
}```
```

#### Output

`````` 10  9  8  7  4  3  1  6  2
Min : 1``````
``````/*
C++ Program
Find minimum element in max heap
*/
#include<iostream>
using namespace std;

class MyHeap {
public:

//Display heap elements
void print_data(int heap[], int size) {
for (int i = 0; i < size; i++) {
cout << " " << heap[i];
}
cout << "\n";
}
int find_min(int heap[], int ans, int index, int size) {
if ((heap[index *2 + 1]) < ans) {
return heap[index *2 + 1];
} else
if ((heap[index *2 + 2]) < ans) {
return heap[index *2 + 2];
}
return ans;
}
void min_element(int heap[], int size) {
if (size <= 0) {
return;
}
int min_node = heap[size / 2];
for (int i = (size / 2) - 1; i >= 0; i--) {
min_node = this->find_min(heap, min_node, i, size);
}
cout << " Min : " << min_node << "\n";
}
};
int main() {
MyHeap obj = MyHeap();
int heap[] = {
10,
9,
8,
7,
4,
3,
1,
6,
2
};
//Get the size
int size = sizeof(heap) / sizeof(heap[0]);
obj.print_data(heap, size);
obj.min_element(heap, size);
return 0;
}```
```

#### Output

`````` 10 9 8 7 4 3 1 6 2
Min : 1``````
``````/*
Java Program
Find minimum element in max heap
*/
public class MyHeap {
//Display heap elements
public void print_data(int []heap,int size)
{

for(int i = 0; i < size; i++)
{
System.out.print("  "+heap[i] );
}
System.out.print("\n");
}
public int find_min(int []heap,int ans, int index,int size)
{
if((heap[index*2+1]) < ans )
{
return heap[index*2+1];
}
else if((heap[index*2+2]) < ans)
{
return heap[index*2+2];
}
return ans;

}
public void min_element(int []heap,int size)
{
if(size<=0)
{
return ;
}
int min_node = heap[size/2];

for (int i = (size/2)-1; i >= 0; i--)
{
min_node = find_min(heap,min_node,i,size);
}
System.out.print("  Min : "+min_node+"\n" );
}
public static void main(String[] args) {

MyHeap obj = new MyHeap();

/*
10
/   \
9     8
/ \   / \
7   4 3   1
/ \
6   2

*/
//max heap array elements
int []heap = {10,  9,  8,  7,  4,  3,  1,  6,  2 };

//Get the size
int size = heap.length;

obj.print_data(heap,size);

obj.min_element(heap,size);
}
}```
```

#### Output

`````` 10 9 8 7 4 3 1 6 2
Min : 1``````
``````/*
C# Program
Find minimum element in max heap
*/
using System;

public class MyHeap {
//Display heap elements
public void print_data(int[] heap, int size) {
for (int i = 0; i < size; i++) {
Console.Write(" " + heap[i]);
}
Console.Write("\n");
}
public int find_min(int[] heap, int ans, int index, int size) {
if ((heap[index * 2 + 1]) < ans) {
return heap[index * 2 + 1];
} else
if ((heap[index * 2 + 2]) < ans) {
return heap[index * 2 + 2];
}
return ans;
}
public void min_element(int[] heap, int size) {
if (size <= 0) {
return;
}
int min_node = heap[size / 2];
for (int i = (size / 2) - 1; i >= 0; i--) {
min_node = find_min(heap, min_node, i, size);
}
Console.Write(" Min : " + min_node + "\n");
}
public static void Main(String[] args) {
MyHeap obj = new MyHeap();
int[]
/*
10
/   \
9     8
/ \   / \
7   4 3   1
/ \
6   2

*/
//max heap array elements
heap = {
10,
9,
8,
7,
4,
3,
1,
6,
2
};
//Get the size
int size = heap.Length;
obj.print_data(heap, size);
obj.min_element(heap, size);
}
}```
```

#### Output

`````` 10 9 8 7 4 3 1 6 2
Min : 1``````
``````<?php
/*
Php Program
Find minimum element in max heap
*/
class MyHeap {
//Display heap elements

public 	function print_data(\$heap, \$size) {
for (\$i = 0; \$i < \$size; \$i++) {
echo(" ". \$heap[\$i]);
}
echo("\n");
}
public 	function find_min(\$heap, \$ans, \$index, \$size) {
if ((\$heap[\$index *2 + 1]) < \$ans) {
return \$heap[\$index *2 + 1];
} else
if ((\$heap[\$index *2 + 2]) < \$ans) {
return \$heap[\$index *2 + 2];
}
return \$ans;
}
public 	function min_element(\$heap, \$size) {
if (\$size <= 0) {
return;
}
\$min_node = \$heap[intval(\$size / 2)];
for (\$i = (intval(\$size / 2)) - 1; \$i >= 0; \$i--) {
\$min_node = \$this->find_min(\$heap, \$min_node, \$i, \$size);
}
echo(" Min : ". \$min_node ."\n");
}
}

function main() {
\$obj = new MyHeap();
/*
10
/   \
9     8
/ \   / \
7   4 3   1
/ \
6   2

*/
//max heap array elements
\$heap = array(10, 9, 8, 7, 4, 3, 1, 6, 2);
//Get the size
\$size = count(\$heap);
\$obj->print_data(\$heap, \$size);
\$obj->min_element(\$heap, \$size);

}
main();```
```

#### Output

`````` 10 9 8 7 4 3 1 6 2
Min : 1``````
``````/*
Node Js Program
Find minimum element in max heap
*/
class MyHeap {
//Display heap elements
print_data(heap, size) {
for (var i = 0; i < size; i++) {
process.stdout.write(" " + heap[i]);
}

process.stdout.write("\n");
}
find_min(heap, ans, index, size) {
if ((heap[index *2 + 1]) < ans) {
return heap[index *2 + 1];
} else
if ((heap[index *2 + 2]) < ans) {
return heap[index *2 + 2];
}

return ans;
}
min_element(heap, size) {
if (size <= 0) {
return;
}

var min_node = heap[parseInt(size / 2)];
for (var i = (parseInt(size / 2)) - 1; i >= 0; i--) {
min_node = this.find_min(heap, min_node, i, size);
}

process.stdout.write(" Min : " + min_node + "\n");
}
}

function main(args) {
var obj = new MyHeap();
/*
10
/   \
9     8
/ \   / \
7   4 3   1
/ \
6   2

*/
//max heap array elements
var heap = [10, 9, 8, 7, 4, 3, 1, 6, 2];
//Get the size
var size = heap.length;
obj.print_data(heap, size);
obj.min_element(heap, size);
}

main();```
```

#### Output

`````` 10 9 8 7 4 3 1 6 2
Min : 1``````
``````# Python 3 Program
# Find minimum element in max heap
class MyHeap :
# Display heap elements
def print_data(self, heap, size) :
i = 0
while (i < size) :
print(" ", heap[i], end = "")
i += 1

print("\n", end = "")

def find_min(self, heap, ans, index, size) :
if ((heap[index * 2 + 1]) < ans) :
return heap[index * 2 + 1]
elif ((heap[index * 2 + 2]) < ans) :
return heap[index * 2 + 2]

return ans

def min_element(self, heap, size) :
if (size <= 0) :
return

min_node = heap[int(size / 2)]
i = (int(size / 2)) - 1
while (i >= 0) :
min_node = self.find_min(heap, min_node, i, size)
i -= 1

print("  Min : ", min_node ,"\n", end = "")

def main() :
obj = MyHeap()

#                  10
#                /   \
#               9     8
#              / \   / \
#             7   4 3   1
#            / \
#           6   2
#
# max heap array elements
heap = [10, 9, 8, 7, 4, 3, 1, 6, 2]
# Get the size
size = len(heap)
obj.print_data(heap, size)
obj.min_element(heap, size)

if __name__ == "__main__":
main()```
```

#### Output

``````  10  9  8  7  4  3  1  6  2
Min :  1``````
``````# Ruby Program
# Find minimum element in max heap
class MyHeap
# Display heap elements
def print_data(heap, size)
i = 0
while (i < size)
print(" ", heap[i])
i += 1
end
print("\n")
end
def find_min(heap, ans, index, size)
if ((heap[index * 2 + 1]) < ans)
return heap[index * 2 + 1]
elsif ((heap[index * 2 + 2]) < ans)
return heap[index * 2 + 2]
end
return ans
end
def min_element(heap, size)
if (size <= 0)
return
end
min_node = heap[size / 2]
i = (size / 2) - 1
while (i >= 0)
min_node = self.find_min(heap, min_node, i, size)
i -= 1
end
print(" Min  : ", min_node ,"\n")
end
end
def main()
obj = MyHeap.new()

#                  10
#                /   \
#               9     8
#              / \   / \
#             7   4 3   1
#            / \
#           6   2
#
# max heap array elements
heap = [10, 9, 8, 7, 4, 3, 1, 6, 2]
# Get the size
size = heap.length
obj.print_data(heap, size)
obj.min_element(heap, size)
end
main()```
```

#### Output

`````` 10 9 8 7 4 3 1 6 2
Min  : 1
``````
``````/*
Scala Program
Find minimum element in max heap
*/
class MyHeap {
//Display heap elements
def print_data(heap: Array[Int], size: Int): Unit = {
var i: Int = 0;
while (i < size) {
print(" " + heap(i));
i += 1;
}
print("\n");
}
def find_min(heap: Array[Int], ans: Int, index: Int, size: Int): Int = {
if ((heap(index * 2 + 1)) < ans) {
return heap(index * 2 + 1);
} else
if ((heap(index * 2 + 2)) < ans) {
return heap(index * 2 + 2);
}
return ans;
}
def min_element(heap: Array[Int], size: Int): Unit = {
if (size <= 0) {
return;
}
var min_node: Int = heap((size / 2).toInt);
var i: Int = ((size / 2).toInt) - 1;
while (i >= 0) {
min_node = this.find_min(heap, min_node, i, size);
i -= 1;
}
print(" Min : " + min_node + "\n");
}
}
object Main {
def main(args: Array[String]): Unit = {
val obj: MyHeap = new MyHeap();

/*
10
/   \
9     8
/ \   / \
7   4 3   1
/ \
6   2

*/
//max heap array elements
val heap: Array[Int] = Array(10, 9, 8, 7, 4, 3, 1, 6, 2);

//Get the size
val size: Int = heap.length;
obj.print_data(heap, size);
obj.min_element(heap, size);
}
}```
```

#### Output

`````` 10 9 8 7 4 3 1 6 2
Min : 1``````
``````/*
Swift Program
Find minimum element in max heap
*/
class MyHeap {
//Display heap elements
func print_data(_ heap: [Int], _ size: Int) {
var i = 0;
while (i < size) {
print(" ", heap[i], terminator: "");
i += 1;
}
print("\n", terminator: "");
}
func find_min(_ heap: [Int], _ ans: Int, _ index: Int, _ size: Int) -> Int {
if ((heap[index * 2 + 1]) < ans) {
return heap[index * 2 + 1];
} else
if ((heap[index * 2 + 2]) < ans) {
return heap[index * 2 + 2];
}
return ans;
}
func min_element(_ heap: [Int], _ size: Int) {
if (size <= 0) {
return;
}
var min_node = heap[size / 2];
var i = (size / 2) - 1;
while (i >= 0) {
min_node = self.find_min(heap, min_node, i, size);
i -= 1;
}
print("  Min : ", min_node ,"\n", terminator: "");
}
}
func main() {
let obj = MyHeap();
/*
10
/   \
9     8
/ \   / \
7   4 3   1
/ \
6   2

*/
//max heap array elements
let heap = [10, 9, 8, 7, 4, 3, 1, 6, 2];
//Get the size
let size = heap.count;
obj.print_data(heap, size);
obj.min_element(heap, size);
}
main();```
```

#### Output

``````  10  9  8  7  4  3  1  6  2
Min :  1``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.