×

Finding the Minimum and Maximum Value of a Binary Tree

Implementation

// Writing a C++ program that will help us find out the maximum and the minimum in a binary tree. 
#include <bits/stdc++.h>
#include <iostream>
using namespace std;


// creating a new class 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 find__Max(__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 result = root->record;
	int lresult = find__Max(root->Lft);
	int rresult = find__Max(root->Rt);
	if (lresult > result)
		result = lresult;
	if (rresult > result)
		result = rresult;
	return result;
}


// 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 " << find__Max(root) << endl;


	return 0;
}

Output:

Finding the Minimum and Maximum Value of a Binary Tree

Example 2)

// Writing a C++ program that will help us find out the maximum and the minimum in a binary tree. 
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>


// creating a new class tree node.
struct __nod {
	int record;
	struct __nod *Lft, *Rt;
};


// creating a utility function to create 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 have to return the maximum value in the given binary tree. 
int find__Max(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 result = root->record;
	int lresult = find__Max(root->Lft);
	int rresult = find__Max(root->Rt);
	if (lresult > result)
		result = lresult;
	if (rresult > result)
		result = rresult;
	return result;
}
// 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", find__Max(root));


	return 0;
}

Output:

Finding the Minimum and Maximum Value of a Binary Tree

Example 3)

// Writing a Java program that will help us find out the maximum and the minimum in a binary tree. 


// creating a new class 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 find__Max(__nod __nod)
	{
		if (__nod == NILL)
			return Integer.MIN_VALUE;


		int result = __nod.record;
		int lresult = find__Max(__nod.Lft);
		int rresult = find__Max(__nod.Rt);


		if (lresult > result)
			result = lresult;
		if (rresult > result)
			result = rresult;
		return result;
	}
// 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.find__Max(tree.root));
	}
}

Output:

Finding the Minimum and Maximum Value of a Binary Tree

Example 4)

# Writing a Python program that will help us find out the maximum and the minimum in a binary tree. 
# Creating a new class tree node.
class new__nod:
	def __init__(self, record):
		self.record = record
		self.Lft = self.Rt = None
# We have to return the maximum value in the given binary tree. 


def find__Max(root):
# Writing the basic case.


	if (root == None):
		return float('-inf')
# 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


	result = root.record
	lresult = find__Max(root.Lft)
	rresult = find__Max(root.Rt)
	if (lresult > result):
		result = lresult
	if (rresult > result):
		result = rresult
	return result




# Writing the main code to test the above functions.
if __name__ == '__main__':
	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
	print("Maximum element is",
		find__Max(root))

Output:

Finding the Minimum and Maximum Value of a Binary Tree

Example 5)

// Writing a C# program will help us find out the maximum and the minimum in a binary tree. 
using System;


// creating a new class 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 find__Max(__nod __nod)
	{
		if (__nod == NILL) {
			return int.MinValue;
		}


		int result = __nod.record;
		int lresult = find__Max(__nod.Lft);
		int rresult = find__Max(__nod.Rt);


		if (lresult > result) {
			result = lresult;
		}
		if (rresult > result) {
			result = rresult;
		}
		return result;
	}


// 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.find__Max(tree.root));
	}
}

Output:

Finding the Minimum and Maximum Value of a Binary Tree

Example 6)

<script>


// Writing a Javascript program that will help us find out the maximum and the minimum in a binary tree. 
	let root;
	
	class __nod
	{
		constructor(record) {
		this.Lft = NILL;
		this.Rt = NILL;
		this.record = record;
		}
	}


// We have to return the maximum value in the given binary tree. 


	function find__Max(__nod)
	{
		if (__nod == NILL)
			return Number.MIN_VALUE;


		let result = __nod.record;
		let lresult = find__Max(__nod.Lft);
		let rresult = find__Max(__nod.Rt);


		if (lresult > result)
			result = lresult;
		if (rresult > result)
			result = rresult;
		return result;
	}
	
	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
	document.write("Maximum element is "
					+ find__Max(root));
	
</script>

Output:

Finding the Minimum and Maximum Value of 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.

Timsort

TimSort Time Complexity Timsort is a sorting algorithm that is quite efficient for real-world data. Timsort is created in 2001 by Tim Peters for the python programming language. Timsort is a...

3 minutes read.

Flatten Binary Tree to a linked list

Implementation In this section, we will see the implementation of the binary Tree and its conversion into linked lists. let us proceed: - // Writing a C++ program that will convert a...

4 minutes read.

Deletion Operation from A B Tree

This article will show the deletion operation through the b tree in C++ programming language. Implementation #include <iostream> using namespace std; class B_TreeNod {   int *kys;   int m;   BTreeNod **C;   int j;   bool leaf;  ...

5 minutes read.

Linked List Data Structure

Linked list in DS: The linked list is a non-primitive and linear data structure. It is a list of a particular type of data element that is connected to each...

3 minutes read.

Graph Data Structure

A graph is a non-primitive and non-linear data structure. It is a group of (V, E) where V is a set of vertexes, and E is a set of edge....

3 minutes read.

Finding Rank in a Binary Search Tree

Implementation // writing a C++ program to find out the rank and element in the program.  #include <bits/stdc++.h> using namespace std; struct __nod { int record; __nod *Lft, *Rt; int LftSize; }; __nod* new__nod(int record) { __nod *temp = new __nod; temp->record...

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

Find the nth node from the end of a Linked List

Find the nth node from the end of a Linked List In this problem, we have given a singly linked list and a number 'n,' and we need to find the...

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.

Complete Binary tree

In this article, we will discuss the complete binary tree. But before start discussing the complete binary tree, we should first see a brief description of a binary tree. What is...

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

Perfect Binary Tree

Complete binary trees are an important and general topic in the concept – of tree data structures. Before discussing a complete binary tree, we need to know the concept of...

4 minutes read.

Singly Linked list

Singly Linked list A singly linked list is a kind of linked list which is unidirectional. If we talk about singly linked list, then we can say it can be traversed...

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

Function to Create a Copy of Binary Search Tree

Implementation // creating a new hashmap in the language C++ that will help us clone a binary tree with arbitrary pointers.  #include<iostream> #include<unordered_map> using namespace std; /* A given binary tree has a record, a...

9 minutes read.

Linear Queue VS Circular Queue

What is Queue? A queue is one of the important linear data structures extensively used in various computer applications. It is based on the FIFO (First In First Out) principle. It...

9 minutes read.

Big O Notations

What is Big O Notation, and why is it important? "Big O notation is a mathematical notation that depicts a function's limiting behaviour when the input tends towards a certain value...

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

Create a binary search tree

Implementation In this section of the article, we will see the usage and mechanism of how we will create a given binary tree. Let's observe these in more depth and then...

7 minutes read.