Find maximum element in min heap

Here given code implementation process.

//C Program
//Find maximum element in min heap
#include <stdio.h>

//Display heap elements
void print_data(int heap[],int size)
{
  
  for(int i = 0; i < size; i++)
  {
    printf(" %d ",heap[i] );
  }
  printf("\n");
}

int find_max(int heap[],int ans, int root,int size)
{
  if((heap[root*2+1]) > ans )
  {
    //When get large value of left child node
    return heap[root*2+1];
  }
  else if((heap[root*2+2]) > ans)
  {
    //When get large value of right child node
    return heap[root*2+2];
  }
  return ans;
 
}
//Display max element in min heap
void max_element(int heap[],int size)
{
  //Assign initial max element
  int max_node = heap[size/2]; 

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

 {

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

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

  //Get thes size of array elements
  int size = sizeof(min_heap)/sizeof(min_heap[0]);

  print_data(min_heap,size);

  max_element(min_heap,size);

  return 0;
}

Output

 1  2  3  6  9  4  8  10  7
 Max : 10
/*
  C++ Program
  Find maximum element in min 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_max(int heap[], int ans, int root, int size) {
		if ((heap[root *2 + 1]) > ans) {
			return
			//When get large value of left child node
			heap[root *2 + 1];
		} else
		if ((heap[root *2 + 2]) > ans) {
			return
			//When get large value of right child node
			heap[root *2 + 2];
		}
		return ans;
	}
	//Display max element in min heap
	void max_element(int heap[], int size) {
		//Assign initial max element
		int max_node = heap[(size / 2)];
		for (int i = (size / 2) - 1; i >= 0; i--) {
			max_node = this->find_max(heap, max_node, i, size);
		}
		cout << " Max : " << max_node << "\n";
	}
};
int main() {
	MyHeap obj = MyHeap();
	/*
	             1
	           /   \
	          2     3
	         / \   / \ 
	        6   9 4   8
	       / \  
	      10  7 

	    */
	//min heap array elements
	int min_heap[] = {
		1,
		2,
		3,
		6,
		9,
		4,
		8,
		10,
		7
	};
	//Get the size
	int size = sizeof(min_heap) / sizeof(min_heap[0]);
	obj.print_data(min_heap, size);
	obj.max_element(min_heap, size);
	return 0;
}

Output

 1 2 3 6 9 4 8 10 7
 Max : 10
/*
  Java Program
  Find maximum element in min 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_max(int []heap,int ans, int root,int size)
  {
    if((heap[root*2+1]) > ans )
    {
      //When get large value of left child node
      return heap[root*2+1];
    }
    else if((heap[root*2+2]) > ans)
    {
      //When get large value of right child node
      return heap[root*2+2];
    }
    return ans;
   
  }
  //Display max element in min heap
  public void max_element(int []heap,int size)
  {
    //Assign initial max element
    int max_node = heap[(size/2)]; 

    for (int i = (size/2)-1; i >= 0; i--)
    {
      max_node = find_max(heap,max_node,i,size);
    }
    System.out.print(" Max : "+max_node+"\n" );
  }
  public static void main(String[] args) {
    
    MyHeap obj = new MyHeap();
    
    /*
             1
           /   \
          2     3
         / \   / \ 
        6   9 4   8
       / \  
      10  7 

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

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


    obj.print_data(min_heap,size);

    obj.max_element(min_heap,size);
  }
}

Output

 1 2 3 6 9 4 8 10 7
 Max : 10
/*
  C# Program
  Find maximum element in min 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_max(int[] heap, int ans, int root, int size) {
		if ((heap[root * 2 + 1]) > ans) {
			return heap[root * 2 + 1];
		} else
		if ((heap[root * 2 + 2]) > ans) {
			return heap[root * 2 + 2];
		}
		return ans;
	}
	//Display max element in min heap
	public void max_element(int[] heap, int size) {
		//Assign initial max element
		int max_node = heap[(size / 2)];
		for (int i = (size / 2) - 1; i >= 0; i--) {
			max_node = find_max(heap, max_node, i, size);
		}
		Console.Write(" Max : " + max_node + "\n");
	}
	public static void Main(String[] args) {
		MyHeap obj = new MyHeap();
		/*
		             1
		           /   \
		          2     3
		         / \   / \ 
		        6   9 4   8
		       / \  
		      10  7 

		    */
		//min heap array elements
		int []min_heap = {
			1,
			2,
			3,
			6,
			9,
			4,
			8,
			10,
			7
		};
		//Get the size
		int size = min_heap.Length;
		obj.print_data(min_heap, size);
		obj.max_element(min_heap, size);
	}
}

Output

 1 2 3 6 9 4 8 10 7
 Max : 10
<?php
/*
  Php Program
  Find maximum element in min 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_max($heap, $ans, $root, $size) {
		if (($heap[$root *2 + 1]) > $ans) {
			return $heap[$root *2 + 1];
		} else
		if (($heap[$root *2 + 2]) > $ans) {
			return $heap[$root *2 + 2];
		}
		return $ans;
	}
	//Display max element in min heap

	public 	function max_element($heap, $size) {
		//Assign initial max element
		$max_node = $heap[(intval($size / 2))];
		for ($i = (intval($size / 2)) - 1; $i >= 0; $i--) {
			$max_node = $this->find_max($heap, $max_node, $i, $size);
		}
		echo(" Max : ". $max_node ."\n");
	}
}

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

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

}
main();

Output

 1 2 3 6 9 4 8 10 7
 Max : 10
/*
  Node Js Program
  Find maximum element in min 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_max(heap, ans, root, size) {
		if ((heap[root *2 + 1]) > ans) {
			return heap[root *2 + 1];
		} else
		if ((heap[root *2 + 2]) > ans) {
			return heap[root *2 + 2];
		}

		return ans;
	}

	//Display max element in min heap
	max_element(heap, size) {
		//Assign initial max element
		var max_node = heap[(parseInt(size / 2))];
		for (var i = (parseInt(size / 2)) - 1; i >= 0; i--) {
			max_node = this.find_max(heap, max_node, i, size);
		}

		process.stdout.write(" Max : " + max_node + "\n");
	}
}

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

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

main();

Output

 1 2 3 6 9 4 8 10 7
 Max : 10
# Python 3 Program
# Find maximum element in min 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_max(self, heap, ans, root, size) :
		if ((heap[root * 2 + 1]) > ans) :
			return heap[root * 2 + 1]
		elif ((heap[root * 2 + 2]) > ans) :
			return heap[root * 2 + 2]
		
		return ans
	
	# Display max element in min heap
	def max_element(self, heap, size) :
		max_node = heap[(int(size / 2))]
		i = (int(size / 2)) - 1
		while (i >= 0) :
			max_node = self.find_max(heap, max_node, i, size)
			i -= 1
		
		print(" Max : ", max_node ,"\n", end = "")
	

def main() :
	obj = MyHeap()
	min_heap = [1, 2, 3, 6, 9, 4, 8, 10, 7]
	size = len(min_heap)
	obj.print_data(min_heap, size)
	obj.max_element(min_heap, size)


if __name__ == "__main__":
	main()

Output

  1  2  3  6  9  4  8  10  7
 Max :  10
# Ruby Program
# Find maximum element in min 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_max(heap, ans, root, size) 
		if ((heap[root * 2 + 1]) > ans) 
			return heap[root * 2 + 1]
		elsif ((heap[root * 2 + 2]) > ans) 
			return heap[root * 2 + 2]
		end
		return ans
	end
	 # Display max element in min heap
	def max_element(heap, size) 
		max_node = heap[(size / 2)]
		i = (size / 2) - 1
		while (i >= 0) 
			max_node = self.find_max(heap, max_node, i, size)
			i -= 1
		end
		print(" Max  :", max_node ,"\n")
	end
end
def main() 
	obj = MyHeap.new()
	min_heap = [1, 2, 3, 6, 9, 4, 8, 10, 7]
	size = min_heap.length
	obj.print_data(min_heap, size)
	obj.max_element(min_heap, size)
end
main()

Output

 1 2 3 6 9 4 8 10 7
 Max  :10
/*
  Scala Program
  Find maximum element in min 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_max(heap: Array[Int], ans: Int, root: Int, size: Int): Int = {
		if ((heap(root * 2 + 1)) > ans) {
			return heap(root * 2 + 1);
		} else
		if ((heap(root * 2 + 2)) > ans) {
			return heap(root * 2 + 2);
		}
		return ans;
	}
	//Display max element in min heap
	def max_element(heap: Array[Int], size: Int): Unit = {
		var max_node: Int = heap(((size / 2).toInt));
		var i: Int = ((size / 2).toInt) - 1;
		while (i >= 0) {
			max_node = this.find_max(heap, max_node, i, size);
			i -= 1;
		}
		print(" Max : " + max_node + "\n");
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		val obj: MyHeap = new MyHeap();
		val min_heap: Array[Int] = Array(1, 2, 3, 6, 9, 4, 8, 10, 7);
		val size: Int = min_heap.length;
		obj.print_data(min_heap, size);
		obj.max_element(min_heap, size);
	}
}

Output

 1 2 3 6 9 4 8 10 7
 Max : 10
/*
  Swift Program
  Find maximum element in min 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_max(_ heap: [Int], _ ans: Int, _ root: Int, _ size: Int) -> Int {
		if ((heap[root * 2 + 1]) > ans) {
			return heap[root * 2 + 1];
		} else
		if ((heap[root * 2 + 2]) > ans) {
			return heap[root * 2 + 2];
		}
		return ans;
	}
	//Display max element in min heap
	func max_element(_ heap: [Int], _ size: Int) {
		var max_node = heap[(size / 2)];
		var i = (size / 2) - 1;
		while (i >= 0) {
			max_node = self.find_max(heap, max_node, i, size);
			i -= 1;
		}
		print(" Max : ", max_node ,"\n", terminator: "");
	}
}
func main() {
	let obj = MyHeap();
	let min_heap = [1, 2, 3, 6, 9, 4, 8, 10, 7];
	let size = min_heap.count;
	obj.print_data(min_heap, size);
	obj.max_element(min_heap, size);
}
main();

Output

  1  2  3  6  9  4  8  10  7
 Max :  10


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.

New Comment







© 2021, kalkicode.com, All rights reserved