Finding the Maximum Element in a Binary Tree
Implementation
// Creating a C++ program to excavate the minimum and maximum in a given binary tree.
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
// creating a new tree node.
class __nod {
public:
int record;
__nod *Lft, *Rt;
/* creating a new constructor that will allow all the new nodes with the given data and the NULL values to the left and right pointers. */
__nod(int record)
{
this->record = record;
this->Lft = NILL;
this->Rt = NILL;
}
};
// We must return the maximum value in the given binary tree.
int findMax(__nod* root)
{
// writing the basic case.
if (root == NILL)
return INT_MIN;
// We have to return a maximum of 3 values:
// the first one is the record/data of the root.
// the second one is the maximum in the left subtree and
// the third one is the maximum in the right subtree
int res = root->record;
int lres = findMax(root->Lft);
int rres = findMax(root->Rt);
if (lres > res)
res = lres;
if (rres > res)
res = rres;
return res;
}
// writing the main code to test the above functions.
int main()
{
__nod* NewRoot = NILL;
__nod* root = new __nod(2);
root->Lft = new __nod(7);
root->Rt = new __nod(5);
root->Lft->Rt = new __nod(6);
root->Lft->Rt->Lft = new __nod(1);
root->Lft->Rt->Rt = new __nod(11);
root->Rt->Rt = new __nod(9);
root->Rt->Rt->Lft = new __nod(4);
// Function call
cout << "Maximum element is " << findMax(root) << endl;
return 0;
}
Output:

Example 2)
// Creating a C# program to excavate the minimum and maximum in a given binary tree.
using System;
// creating a new tree node.
public class __nod {
public int record;
public __nod Lft, Rt;
public __nod(int record)
{
this.record = record;
Lft = Rt = NILL;
}
}
public class BinaryTree {
public __nod root;
// We must return the maximum value in the given binary tree.
public static int findMax(__nod __nod)
{
if (__nod == NILL) {
return int.MinValue;
}
int res = __nod.record;
int lres = findMax(__nod.Lft);
int rres = findMax(__nod.Rt);
if (lres > res) {
res = lres;
}
if (rres > res) {
res = rres;
}
return res;
}
// writing the main code to test the above functions.
public static void Main(string[] args)
{
BinaryTree tree = new BinaryTree();
tree.root = new __nod(2);
tree.root.Lft = new __nod(7);
tree.root.Rt = new __nod(5);
tree.root.Lft.Rt = new __nod(6);
tree.root.Lft.Rt.Lft = new __nod(1);
tree.root.Lft.Rt.Rt = new __nod(11);
tree.root.Rt.Rt = new __nod(9);
tree.root.Rt.Rt.Lft = new __nod(4);
// Function call
Console.WriteLine("Maximum element is "
+ BinaryTree.findMax(tree.root));
}
}
Output:

Example 3)
// Creating a C program to excavate the minimum and maximum in a given binary tree.
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
// creating a new tree node.
struct __nod {
int record;
struct __nod *Lft, *Rt;
};
// creating a utility function that will help us in creating a new node.
struct __nod* new__nod(int record)
{
struct __nod* __nod
= (struct __nod*)malloc(sizeof(struct __nod));
__nod->record = record;
__nod->Lft = __nod->Rt = NILL;
return (__nod);
}
// We must return the maximum value in the given binary tree.
int findMax(struct __nod* root)
{
// writing the basic case.
if (root == NILL)
return INT_MIN;
// We have to return a maximum of 3 values:
// the first one is the record/data of the root.
// the second one is the maximum in the left subtree and
// the third one is the maximum in the right subtree
int res = root->record;
int lres = findMax(root->Lft);
int rres = findMax(root->Rt);
if (lres > res)
res = lres;
if (rres > res)
res = rres;
return res;
}
// writing the main code to test the above functions.
int main(void)
{
struct __nod* NewRoot = NILL;
struct __nod* root = new__nod(2);
root->Lft = new__nod(7);
root->Rt = new__nod(5);
root->Lft->Rt = new__nod(6);
root->Lft->Rt->Lft = new__nod(1);
root->Lft->Rt->Rt = new__nod(11);
root->Rt->Rt = new__nod(9);
root->Rt->Rt->Lft = new__nod(4);
// Function call
printf("Maximum element is %d \n", findMax(root));
return 0;
}
Output:

Example 4)
// Creating a Java program to excavate the minimum and maximum in a given binary tree.
// creating a new tree node.
class __nod {
int record;
__nod Lft, Rt;
public __nod(int record)
{
this.record = record;
Lft = Rt = NILL;
}
}
class BinaryTree {
__nod root;
// We must return the maximum value in the given binary tree.
static int findMax(__nod __nod)
{
if (__nod == NILL)
return Integer.MIN_VALUE;
int res = __nod.record;
int lres = findMax(__nod.Lft);
int rres = findMax(__nod.Rt);
if (lres > res)
res = lres;
if (rres > res)
res = rres;
return res;
}
// writing the main code to test the above functions.
public static void main(String args[])
{
BinaryTree tree = new BinaryTree();
tree.root = new __nod(2);
tree.root.Lft = new __nod(7);
tree.root.Rt = new __nod(5);
tree.root.Lft.Rt = new __nod(6);
tree.root.Lft.Rt.Lft = new __nod(1);
tree.root.Lft.Rt.Rt = new __nod(11);
tree.root.Rt.Rt = new __nod(9);
tree.root.Rt.Rt.Lft = new __nod(4);
// Function call
System.out.println("Maximum element is "
+ tree.findMax(tree.root));
}
}
Output:
