×

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:

Finding the Maximum Element in a Binary Tree

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:

Finding the Maximum Element in a Binary Tree

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:

Finding the Maximum Element in a Binary Tree

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:

Finding the Maximum Element in a Binary Tree

Related Topics

B+ Tree in Data Structure

A B-Tree extension called B+ Tree, which enables effective search, insertion, and deletion operations. Both Records and keys can be stored in internal and leaf nodes in a B tree. Contrarily,...

4 minutes read.

Strictly binary tree in Data Structures?

What is a strictly Binary Tree in Data Structures? There are various kinds of binary trees that we know exist in data structures, and they all have their purposes. In this...

4 minutes read.

Application of 2D array - Sparse Matrix

2D Arrays Application - Sparse Matrix A matrix is a two-dimensional data item consisting of m rows and n columns, with a total of m x n values. A sparse matrix...

7 minutes read.

Reverse a Linked List in groups of given size

Reverse a Linked List in groups of given size This article will explain how to reverse a linked list in groups of given size. Here we have given a linked list...

2 minutes read.

Find the fractional (n/kth) node in the linked list

Find the fractional (n/kth) node in the linked list In this problem, we have given a singly linked list and a number k. Here we need to find the (n/k)th element...

2 minutes read.

Merge Sort

Merge Sort is one of the most widely used sorting algorithms, and it is based on the Divide and Conquer principle. A problem is subdivided into multiple sub-problems in this method....

8 minutes read.

FIFO approach

FIFO is first in first out approach. It is done for the list of elements in data structures where first element will be deleted after another element ia added to it Here,...

6 minutes read.

Breadth First Search

Breadth First Search Breadth first search is a graph traversing algorithm. In this, we start traversing from the source node or any selected node and traverse the graph layer by layer....

6 minutes read.

Binary tree insertion

As we all know, a binary tree has a maximum of two children and helps us manage the info correctly. Here the name of the tree itself portrays the mechanism...

4 minutes read.

Red-black Tree in Data Structures?

A type of binary tree which is known as the Red-Black tree, is a specialized and unique tree. What is the urgency or, to be more precise, the necessity of...

10 minutes read.

What Is Graph Data Structure

A graph is generally a set of vertices and edges or border that is mainly used to join these vertices. A graph is basically pictured as a cyclic tree in...

7 minutes read.

Program to calculate the area of the circumcircle of an equilateral triangle

You have given one value which represents the side of the equilateral triangle. You have to find out the area of the circumcircle. Let’s take an example - For the above...

3 minutes read.

Bookshop management system using file handling in C++

We see different software in every hospitals or library to manage their database. It is very important to store organization’s data. So we use this software. Now we are going...

5 minutes read.

Difference between B-tree and Binary Tree

What is B-TREE? The nodes of B-tree are sorted during in-order traversal, and it is called self-balancing tree. A node in a B-tree can have more than two offspring, in contrast...

3 minutes read.

Buffer overflow attack with examples

You have undoubtedly faced the term buffer overflow in your programming journey. Many times it occurs when we try to run a piece of code with user input, but it...

4 minutes read.

Spanning Tree

Spanning Tree: The spanning tree is a subset of the graph. It is a non-cyclic graph. If any node in the spanning tree is truncated, the entire graph fails. There are...

10 minutes read.

Serialize and Deserialize Binary Trees

In order to save a tree in a file that can later be restored, serialisation is used. The tree's structure must be preserved. Deserialization involves reading a tree from a...

4 minutes read.

Applications of trees in data structures

Data structures Storage used to organize and store data is known as the data structure. It is a method of managing computerized data to translate or retrieve it more efficiently. A...

7 minutes read.

Array vs Linked List: Data Structure

Data structure: Difference Between Array and Linked List What is Array? An array is a linear data structure that can store similar data items for further processing. The similar data items...

3 minutes read.

Find out the area between two concentric circles

You have given two values of the radius of two circles. You have to find out the area between these two circles. Let's take an example - For the above diagram,...

3 minutes read.