×

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

Detect and Remove Loop in a Linked List

Create a function called detectAndRemovetheLoop() that verifies whether a given Linked List has a loop, eliminates the loop if it does, and returns true if it does. It returns false...

6 minutes read.

Convert Binary Tree into a Threaded Binary Tree

Implementation /*Writing a C++ program that will help us change the binary tree into a threaded binary tree and help us transform. */ #include <bits/stdc++.h> using namespace std; /*Creating the structure of a node...

11 minutes read.

Delete N nodes after M nodes of a linked list

Delete N nodes after M nodes of a linked list In this problem, we have given a linked list and two integers M and N. We need to traverse the linked...

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

About Data Structures

What exactly are data structures? A data structure is a type of storage that is used to organise and store data. It is a method of organising data on a computer...

5 minutes read.

2-3 Trees and Basic Operations on them

2-3 Trees, like any other AVL trees or B-trees, are just a type of Height Balanced Tree. 2-3 Trees are the B-trees of order 3. Like every other B-tree, the...

4 minutes read.

Horizontal and Vertical Scaling

Being a software engineer, you would have designed a website or application and deployed it on any server. Imagine that the developed application starts getting popular, and many users engage...

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.

Trie data structure

Trie data structure The term “trie” comes from the word “retrieval” which means getting information. The trie data structure is a sorted extension of tree-based data structure. The trie data structure...

5 minutes read.

B Tree in Data Structure

Data management is called database management. A data model is a system that stores, manages, and optimizes computer resources. Data processing is not just about data storage. Almost every app...

9 minutes read.

Stack vs Queue: Data Structure

 Difference Between Stack and Queue What is Stack? The LIFO principle applies on insertion and deletion operations of the stack which means last inserted element to the stack will remove first....

3 minutes read.

Linear vs Non-Linear: Data Structure

What is Linear Data Structure? The data structure is said to be linear if the data elements are arranged linearly or we can say sequentially. In the linear data structure, the...

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

Linked List Representation of Binary Tree

As we all know, a binary tree has a maximum of two children and helps us manage the info correctly. The word binary itself represents its meaning; we know that...

4 minutes read.

Splay Tree

Splay Tree A splay tree is a self-balanced or self-adjusted binary search tree. We can say, Splay Tree is used in some cases where some elements or data are accessed more...

8 minutes read.

Intersection Point in Y Shaped Linked Lists in Java

Intersection Point in Y Shaped Linked Lists in Java In this article, we are going to see how to find the intersection point in a Y-shaped linked list. Method 1: We need to...

4 minutes read.

What is a 2-3 Tree in Data Structure?

Tree Data structure The information about the tree is self-explanatory. Trees are ordered and, therefore, not linear. But they are actually designed differently. Tree A node-based data model that represents and...

5 minutes read.

Traversal of binary tree

Traversal of binary tree: A node is visited only once in the traversal of the binary tree. There are three main types of traversal methods in the binary tree. In-order traversalPre-order...

3 minutes read.

Properties of Binary Tree

Trees are maybe of the most significant datum structures. They are used to store and figure out data. A binarytree is a tree data structure made from nodes, all of which has...

3 minutes read.

Implementation of Queue

Implementation of queue: We can implement the queue through the array and linked list. An array is the easiest way to implement the queue. When a queue is created with the...

7 minutes read.