×

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

Rearrange a linked list into alternate fashion first and the last element

Rearrange a linked list into alternate fashion first and the last element This article will explain how to rearrange the linked list into alternate fashion first and the last element. Here,...

3 minutes read.

Deque in Data Structure

Deque A deque referred as “Double-Ended Queue”, is a linear collection of data items same like queue data structure. deque has two ends, front end and rear end, deque is the...

27 minutes read.

Merge two sorted linked lists

Merge two sorted linked lists In this article, we are going to learn how to merge two linked lists. Here we have given two linked lists that are sorted in increasing...

7 minutes read.

Sorting Algorithms

Sorting: In the data structure, sorting is the process by which you arrange the data in a logical order. This logical order can also be an ascending order or a...

7 minutes read.

How to get Better in Data Structures and Algorithms?

Introduction Data structures and algorithms are fundamental computer science concepts that store, organize, and process data efficiently. By understanding different data structures and algorithms and using them effectively, you can become...

19 minutes read.

Circular Linked List

Circular Linked List A circular linked list where all nodes are connected to their next node and last node is connected to the starting node or we can say all nodes...

5 minutes read.

Number of visible boxes putting one inside another

You have given one array, which consists of values which represent the sizes of different boxes. We can put one box inside another if the size of the outside box...

3 minutes read.

Does Overloading Work with Inheritance

This is a question that occasionally comes to many programmers. Who are curious to know more now has a complete explanation and a solution through this tutorial! Inheritance: The functions of...

3 minutes read.

Given a Perfect Binary Tree, Reverse Alternate Levels

Implementation //writing a program in C++ language to see how to approach it. #include <bits/stdc++.h> using namespace std; // creating a tree node. struct Nod { char ky; struct Nod *Lft, *Rt; }; // creating a new utility function...

9 minutes read.

Tree vs Graph: Data Structure

Difference Between Tree and Graph What is Tree? A tree is a non-linear data structure and finite collection of elements called node. A tree, in which the data items are arranged in...

3 minutes read.

Sorting Algorithms in Data Structures

A sorting algorithm is used to organize the elements of an array or list. Sorting an array, for example. Unsorted array 572941 Sorted array 124579 We're sorting the array in ascending order right now. This procedure...

4 minutes read.

A Full Binary Tree with n Nodes

Implementation // Writing the implementation of the above approach in C++ #include <bits/stdc++.h> using namespace std; // We are creating a class that will create a node and its left and right children.  struct __nod...

12 minutes read.

Box Stacking Problem

Stacking of boxes depending on their base You have been given n different boxes. These boxes will have different heights, widths, and depths. You have to stack all these boxes in...

4 minutes read.

Function to Delete a Leaf Node from a Binary Tree

Implementation // We are writing a C++ code to eliminate all the leaves from the given value.  #include <bits/stdc++.h> using namespace std; // creating a new binary tree node struct __nod { int record; struct __nod *Lft,...

4 minutes read.

Extended Binary Tree

An extended binary tree is a binary tree in which all the NILL subtrees present mainly in the original trees are exchanged with the special nodes that are primarily known...

3 minutes read.

Operations on 2D-Arrays

Two Dimensional Array Operations Adding Elements to Two-D Arrays We must put data in both rows and columns when inserting items in 2-D Arrays. As a result, we employ the idea of...

10 minutes read.

Primitive Data Structure in C

The data structure is a logical or mathematical model for organizing and structuring the main memory or elements. We can classify the data structures in two ways one is primitive, and...

10 minutes read.

DFS (Depth-first search) Algorithm: Data Structure

What is DFS (Depth-first search)? The depth first search is a graph traversal algorithm. The idea behind this algorithm is backtracking and it is a kind of recursive algorithm. In the...

3 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.

Common Operations on various Data Structures

Data structures are ways to organise data in computer memory for quick and effective use. The storage of data uses a variety of data-structures. It is also possible to define...

7 minutes read.