Skip to main content

Find maximum node in binary search tree

Here given code implementation process.

//C Program 
//Find maximum node in binary search tree
#include <stdio.h>

#include <stdlib.h>
 //structure of Binary Search Tree node
struct Node
{
	int data;
	struct Node *left, *right;
};
//Adding a new node in binary search tree
void add_node(struct Node **root, int data)
{
	//Create a dynamic node of binary search tree 
	struct Node *new_node = (struct Node *) malloc(sizeof(struct Node));
	if (new_node != NULL)
	{
		//Set data and pointer values
		new_node->data = data;
		new_node->left = NULL; //Initially node left-pointer is NULL
		new_node->right = NULL; //Initially node right-pointer is NULL
		if ( *root == NULL)
		{
			//When adds a first node in binary tree
			*root = new_node;
		}
		else
		{
			struct Node *find = *root;
			//iterate binary tree and add new node to proper position
			while (find != NULL)
			{
				if (find->data > data)
				{
					if (find->left == NULL)
					{
						find->left = new_node;
						break;
					}
					else
					{ //visit left sub-tree
						find = find->left;
					}
				}
				else
				{
					if (find->right == NULL)
					{
						find->right = new_node;
						break;
					}
					else
					{
						//visit right sub-tree
						find = find->right;
					}
				}
			}
		}
	}
	else
	{
		printf("Memory Overflow\n");
		exit(0); //Termaxate program execution
	}
}
//Find and print maximum node of binary search tree
void max_node(struct Node *root)
{
	if (root == NULL)
	{
		printf("\n Empty Tree");
	}
	else
	{
		printf(" Maximum node is : ");
		struct Node *temp = root;
		//Find rightmost node from to root
		while (temp->right != NULL)
		{
			//Visit right node
			temp = temp->right;
		}
		//Display node value
		printf("%d\n", temp->data);
	}
}
int main()
{
	struct Node *root = NULL;
	//Create binary search tree
	/*
	       5
	     /   \
	    3     6
	   / \     \
	 -7   4     11
	   \       /  \
	    2     10   18
	    `          /
	              16                      

	*/
	add_node( & root, 5);
	add_node( & root, 3);
	add_node( & root, 6);
	add_node( & root, -7);
	add_node( & root, 4);
	add_node( & root, 11);
	add_node( & root, 18);
	add_node( & root, 2);
	add_node( & root, 10);
	add_node( & root, 16);
	max_node(root);
	return 0;
}

Output

 Maximum node is : 18
//Java program
//Find maximum node in binary search tree
//Binary Tree Node
class Node
{
	public int data;
	public Node left;
	public Node right;
	public Node(int data)
	{
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinarySearchTree
{
	public Node root;
	//Class constructors
	public BinarySearchTree()
	{
		root = null;
	}
	//insert a new node in BST
	public void add_node(int data)
	{
		//Create a new binary tree node
		Node new_node = new Node(data);
		if (new_node != null)
		{
			if (this.root == null)
			{
				//When adds a first node in binary tree
				this.root = new_node;
			}
			else
			{
				Node find = this.root;
				//add new node to proper position
				while (find != null)
				{
					if (find.data >= data)
					{
						if (find.left == null)
						{
							find.left = new_node;
							break;
						}
						else
						{
							//visit left sub-tree
							find = find.left;
						}
					}
					else
					{
						if (find.right == null)
						{
							find.right = new_node;
							break;
						}
						else
						{
							//visit right sub-tree
							find = find.right;
						}
					}
				}
			}
		}
		else
		{
			System.out.println("Memory Overflow");
		}
	}
	//Find and print maximum node of binary search tree
	public void max_node()
	{
		if (root == null)
		{
			System.out.print("\n Empty Tree");
		}
		else
		{
			System.out.print(" Maximum node is : ");
			Node temp = root;
			//Find rightmost node from to root
			while (temp.right != null)
			{
				//Visit right node
				temp = temp.right;
			}
			System.out.print(" " + temp.data + "\n");
		}
	}
	public static void main(String[] args)
	{
		BinarySearchTree obj = new BinarySearchTree();
		//Create binary search tree
		/*
		       5
		     /   \
		    3     6
		   / \     \
		 -7   4     11
		   \       /  \
		    2     10   18
		    `          /
		              16                      

		*/
		obj.add_node(5);
		obj.add_node(3);
		obj.add_node(6);
		obj.add_node(-7);
		obj.add_node(4);
		obj.add_node(11);
		obj.add_node(18);
		obj.add_node(2);
		obj.add_node(10);
		obj.add_node(16);
		obj.max_node();
	}
}

Output

 Maximum node is :  18
//Include header file
#include <iostream>

using namespace std;
//C++ program
//Find maximum node in binary search tree
//Binary Tree Node
class Node
{
	public: int data;
	Node * left;
	Node * right;
	Node(int data)
	{
		this->data = data;
		this->left = NULL;
		this->right = NULL;
	}
};
class BinarySearchTree
{
	public: Node * root;
	//Class constructors
	BinarySearchTree()
	{
		this->root = NULL;
	}
	//insert a new node in BST
	void add_node(int data)
	{
		//Create a new binary tree node
		Node * new_node = new Node(data);
		if (new_node != NULL)
		{
			if (this->root == NULL)
			{
				//When adds a first node in binary tree
				this->root = new_node;
			}
			else
			{
				Node * find = this->root;
				//add new node to proper position
				while (find != NULL)
				{
					if (find->data >= data)
					{
						if (find->left == NULL)
						{
							find->left = new_node;
							break;
						}
						else
						{
							//visit left sub-tree
							find = find->left;
						}
					}
					else
					{
						if (find->right == NULL)
						{
							find->right = new_node;
							break;
						}
						else
						{
							//visit right sub-tree
							find = find->right;
						}
					}
				}
			}
		}
		else
		{
			cout << "Memory Overflow";
		}
	}
	//Find and print maximum node of binary search tree
	void max_node()
	{
		if (this->root == NULL)
		{
			cout << "\n Empty Tree";
		}
		else
		{
			cout << " Maximum node is : ";
			Node * temp = this->root;
			//Find rightmost node from to root
			while (temp->right != NULL)
			{
				//Visit right node
				temp = temp->right;
			}
			cout << " " << temp->data << "\n";
		}
	}
};
int main()
{
	BinarySearchTree obj = BinarySearchTree();
	//Create binary search tree
	/*
			       5
			     /   \
			    3     6
			   / \     \
			 -7   4     11
			   \       /  \
			    2     10   18
			    `          /
			              16                      

			*/
	obj.add_node(5);
	obj.add_node(3);
	obj.add_node(6);
	obj.add_node(-7);
	obj.add_node(4);
	obj.add_node(11);
	obj.add_node(18);
	obj.add_node(2);
	obj.add_node(10);
	obj.add_node(16);
	obj.max_node();
	return 0;
}

Output

 Maximum node is :  18
//Include namespace system
using System;
//C# program
//Find maximum node in binary search tree
//Binary Tree Node
class Node
{
	public int data;
	public Node left;
	public Node right;
	public Node(int data)
	{
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinarySearchTree
{
	public Node root;
	//Class constructors
	public BinarySearchTree()
	{
		root = null;
	}
	//insert a new node in BST
	public void add_node(int data)
	{
		//Create a new binary tree node
		Node new_node = new Node(data);
		if (new_node != null)
		{
			if (this.root == null)
			{
				//When adds a first node in binary tree
				this.root = new_node;
			}
			else
			{
				Node find = this.root;
				//add new node to proper position
				while (find != null)
				{
					if (find.data >= data)
					{
						if (find.left == null)
						{
							find.left = new_node;
							break;
						}
						else
						{
							//visit left sub-tree
							find = find.left;
						}
					}
					else
					{
						if (find.right == null)
						{
							find.right = new_node;
							break;
						}
						else
						{
							//visit right sub-tree
							find = find.right;
						}
					}
				}
			}
		}
		else
		{
			Console.WriteLine("Memory Overflow");
		}
	}
	//Find and print maximum node of binary search tree
	public void max_node()
	{
		if (root == null)
		{
			Console.Write("\n Empty Tree");
		}
		else
		{
			Console.Write(" Maximum node is : ");
			Node temp = root;
			//Find rightmost node from to root
			while (temp.right != null)
			{
				//Visit right node
				temp = temp.right;
			}
			Console.Write(" " + temp.data + "\n");
		}
	}
	public static void Main(String[] args)
	{
		BinarySearchTree obj = new BinarySearchTree();
		//Create binary search tree
		/*
				       5
				     /   \
				    3     6
				   / \     \
				 -7   4     11
				   \       /  \
				    2     10   18
				    `          /
				              16                      

				*/
		obj.add_node(5);
		obj.add_node(3);
		obj.add_node(6);
		obj.add_node(-7);
		obj.add_node(4);
		obj.add_node(11);
		obj.add_node(18);
		obj.add_node(2);
		obj.add_node(10);
		obj.add_node(16);
		obj.max_node();
	}
}

Output

 Maximum node is :  18
<?php
//Php program
//Find maximum node in binary search tree
//Binary Tree Node
class Node
{
	public $data;
	public $left;
	public $right;

	function __construct($data)
	{
		$this->data = $data;
		$this->left = null;
		$this->right = null;
	}
}
class BinarySearchTree
{
	public $root;
	//Class constructors
	function __construct()
	{
		$this->root = null;
	}
	//insert a new node in BST
	public	function add_node($data)
	{
		//Create a new binary tree node
		$new_node = new Node($data);
		if ($new_node != null)
		{
			if ($this->root == null)
			{
				//When adds a first node in binary tree
				$this->root = $new_node;
			}
			else
			{
				$find = $this->root;
				//add new node to proper position
				while ($find != null)
				{
					if ($find->data >= $data)
					{
						if ($find->left == null)
						{
							$find->left = $new_node;
							break;
						}
						else
						{
							//visit left sub-tree
							$find = $find->left;
						}
					}
					else
					{
						if ($find->right == null)
						{
							$find->right = $new_node;
							break;
						}
						else
						{
							//visit right sub-tree
							$find = $find->right;
						}
					}
				}
			}
		}
		else
		{
			echo "Memory Overflow";
		}
	}
	//Find and print maximum node of binary search tree
	public	function max_node()
	{
		if ($this->root == null)
		{
			echo "\n Empty Tree";
		}
		else
		{
			echo " Maximum node is : ";
			$temp = $this->root;
			//Find rightmost node from to root
			while ($temp->right != null)
			{
				//Visit right node
				$temp = $temp->right;
			}
			echo " ". $temp->data ."\n";
		}
	}
}

function main()
{
	$obj = new BinarySearchTree();
	//Create binary search tree
	/*
			       5
			     /   \
			    3     6
			   / \     \
			 -7   4     11
			   \       /  \
			    2     10   18
			    `          /
			              16                      

			*/
	$obj->add_node(5);
	$obj->add_node(3);
	$obj->add_node(6);
	$obj->add_node(-7);
	$obj->add_node(4);
	$obj->add_node(11);
	$obj->add_node(18);
	$obj->add_node(2);
	$obj->add_node(10);
	$obj->add_node(16);
	$obj->max_node();
}
main();

Output

 Maximum node is :  18
//Node Js program
//Find maximum node in binary search tree
//Binary Tree Node
class Node
{
	constructor(data)
	{
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinarySearchTree
{
	//Class constructors
	constructor()
	{
		this.root = null;
	}
	//insert a new node in BST
	add_node(data)
	{
		//Create a new binary tree node
		var new_node = new Node(data);
		if (new_node != null)
		{
			if (this.root == null)
			{
				//When adds a first node in binary tree
				this.root = new_node;
			}
			else
			{
				var find = this.root;
				//add new node to proper position
				while (find != null)
				{
					if (find.data >= data)
					{
						if (find.left == null)
						{
							find.left = new_node;
							break;
						}
						else
						{
							//visit left sub-tree
							find = find.left;
						}
					}
					else
					{
						if (find.right == null)
						{
							find.right = new_node;
							break;
						}
						else
						{
							//visit right sub-tree
							find = find.right;
						}
					}
				}
			}
		}
		else
		{
			process.stdout.write("Memory Overflow");
		}
	}
	//Find and print maximum node of binary search tree
	max_node()
	{
		if (this.root == null)
		{
			process.stdout.write("\n Empty Tree");
		}
		else
		{
			process.stdout.write(" Maximum node is : ");
			var temp = this.root;
			//Find rightmost node from to root
			while (temp.right != null)
			{
				//Visit right node
				temp = temp.right;
			}
			process.stdout.write(" " + temp.data + "\n");
		}
	}
}

function main()
{
	var obj = new BinarySearchTree();
	//Create binary search tree
	/*
			       5
			     /   \
			    3     6
			   / \     \
			 -7   4     11
			   \       /  \
			    2     10   18
			    `          /
			              16                      

			*/
	obj.add_node(5);
	obj.add_node(3);
	obj.add_node(6);
	obj.add_node(-7);
	obj.add_node(4);
	obj.add_node(11);
	obj.add_node(18);
	obj.add_node(2);
	obj.add_node(10);
	obj.add_node(16);
	obj.max_node();
}
main();

Output

 Maximum node is :  18
# Python 3 program
# Find maximum node in binary search tree
# Binary Tree Node
class Node :
	
	def __init__(self, data) :
		self.data = data
		self.left = None
		self.right = None
	
class BinarySearchTree :
	
	# Class constructors
	def __init__(self) :
		self.root = None
	
	# insert a new node in BST
	def add_node(self, data) :
		# Create a new binary tree node
		new_node = Node(data)
		if (new_node != None) :
			if (self.root == None) :
				# When adds a first node in binary tree
				self.root = new_node
			else :
				find = self.root
				# add new node to proper position
				while (find != None) :
					if (find.data >= data) :
						if (find.left == None) :
							find.left = new_node
							break
						else :
							# visit left sub-tree
							find = find.left
						
					else :
						if (find.right == None) :
							find.right = new_node
							break
						else :
							# visit right sub-tree
							find = find.right
						
					
				
			
		else :
			print("Memory Overflow", end = "")
		
	
	# Find and print maximum node of binary search tree
	def max_node(self) :
		if (self.root == None) :
			print("\n Empty Tree", end = "")
		else :
			print(" Maximum node is : ", end = "")
			temp = self.root
			# Find rightmost node from to root
			while (temp.right != None) :
				# Visit right node
				temp = temp.right
			
			print(" ", temp.data ,"\n", end = "")
		
	

def main() :
	obj = BinarySearchTree()
	# Create binary search tree
	# 
	# 		       5
	# 		     /   \
	# 		    3     6
	# 		   / \     \
	# 		 -7   4     11
	# 		   \       /  \
	# 		    2     10   18
	# 		    `          /
	# 		              16                      
	# 		
	
	obj.add_node(5)
	obj.add_node(3)
	obj.add_node(6)
	obj.add_node(-7)
	obj.add_node(4)
	obj.add_node(11)
	obj.add_node(18)
	obj.add_node(2)
	obj.add_node(10)
	obj.add_node(16)
	obj.max_node()

if __name__ == "__main__": main()

Output

 Maximum node is :   18
# Ruby program
# Find maximum node in binary search tree
# Binary Tree Node
class Node 

	# Define the accessor and reader of class Node  
	attr_reader :data, :left, :right
	attr_accessor :data, :left, :right

	def initialize(data)
	
		self.data = data
		self.left = nil
		self.right = nil
	end
end
class BinarySearchTree 

	# Define the accessor and reader of class BinarySearchTree  
	attr_reader :root
	attr_accessor :root


	
	# Class constructors
	def initialize()
	
		@root = nil
	end
	# insert a new node in BST
	def add_node(data)
	
		# Create a new binary tree node
		new_node = Node.new(data)
		if (new_node != nil)
		
			if (self.root == nil)
			
				# When adds a first node in binary tree
				self.root = new_node
			else
			
				find = self.root
				# add new node to proper position
				while (find != nil)
				
					if (find.data >= data)
					
						if (find.left == nil)
						
							find.left = new_node
							break
						else
						
							# visit left sub-tree
							find = find.left
						end
					else
					
						if (find.right == nil)
						
							find.right = new_node
							break
						else
						
							# visit right sub-tree
							find = find.right
						end
					end
				end
			end
		else
		
			print("Memory Overflow")
		end
	end
	# Find and print maximum node of binary search tree
	def max_node()
	
		if (@root == nil)
		
			print("\n Empty Tree")
		else
		
			print(" Maximum node is : ")
			temp = @root
			# Find rightmost node from to root
			while (temp.right != nil)
			
				# Visit right node
				temp = temp.right
			end
			print(" ", temp.data ,"\n")
		end
	end
end
def main()

	obj = BinarySearchTree.new()
	# Create binary search tree
	# 
	# 		       5
	# 		     /   \
	# 		    3     6
	# 		   / \     \
	# 		 -7   4     11
	# 		   \       /  \
	# 		    2     10   18
	# 		    `          /
	# 		              16                      
	# 		
	
	obj.add_node(5)
	obj.add_node(3)
	obj.add_node(6)
	obj.add_node(-7)
	obj.add_node(4)
	obj.add_node(11)
	obj.add_node(18)
	obj.add_node(2)
	obj.add_node(10)
	obj.add_node(16)
	obj.max_node()
end
main()

Output

 Maximum node is :  18
//Scala program
//Find maximum node in binary search tree
//Binary Tree Node
class Node(var data: Int,
	var left: Node,
		var right: Node)
{
	def this(data: Int)
	{
		this(data, null, null);
	}
}
class BinarySearchTree(var root: Node)
{
	//Class constructors
	def this()
	{
		this(null);
	}
	//insert a new node in BST
	def add_node(data: Int): Unit = {
		//Create a new binary tree node
		var new_node: Node = new Node(data);
		if (new_node != null)
		{
			if (this.root == null)
			{
				//When adds a first node in binary tree
				this.root = new_node;
			}
			else
			{
				var find: Node = this.root;
				//add new node to proper position
				while (find != null)
				{
					if (find.data >= data)
					{
						if (find.left == null)
						{
							find.left = new_node;
							return;
						}
						else
						{
							//visit left sub-tree
							find = find.left;
						}
					}
					else
					{
						if (find.right == null)
						{
							find.right = new_node;
							return;
						}
						else
						{
							//visit right sub-tree
							find = find.right;
						}
					}
				}
			}
		}
		else
		{
			print("Memory Overflow");
		}
	}
	//Find and print maximum node of binary search tree
	def max_node(): Unit = {
		if (root == null)
		{
			print("\n Empty Tree");
		}
		else
		{
			print(" Maximum node is : ");
			var temp: Node = root;
			//Find rightmost node from to root
			while (temp.right != null)
			{
				//Visit right node
				temp = temp.right;
			}
			print(" " + temp.data + "\n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: BinarySearchTree = new BinarySearchTree();
		//Create binary search tree
		/*
				       5
				     /   \
				    3     6
				   / \     \
				 -7   4     11
				   \       /  \
				    2     10   18
				    `          /
				              16                      

				*/
		obj.add_node(5);
		obj.add_node(3);
		obj.add_node(6);
		obj.add_node(-7);
		obj.add_node(4);
		obj.add_node(11);
		obj.add_node(18);
		obj.add_node(2);
		obj.add_node(10);
		obj.add_node(16);
		obj.max_node();
	}
}

Output

 Maximum node is :  18
//Swift program
//Find maximum node in binary search tree
//Binary Tree Node
class Node
{
	var data: Int;
	var left: Node? ;
	var right: Node? ;
	init(_ data: Int)
	{
		self.data = data;
		self.left = nil;
		self.right = nil;
	}
}
class BinarySearchTree
{
	var root: Node? ;
	//Class constructors
	init()
	{
		self.root = nil;
	}
	//insert a new node in BST
	func add_node(_ data: Int)
	{
		//Create a new binary tree node
		let new_node: Node? = Node(data);
		if (new_node != nil)
		{
			if (self.root == nil)
			{
				//When adds a first node in binary tree
				self.root = new_node;
			}
			else
			{
				var find: Node? = self.root;
				//add new node to proper position
				while (find != nil)
				{
					if (find!.data >= data)
					{
						if (find!.left == nil)
						{
							find!.left = new_node;
							break;
						}
						else
						{
							//visit left sub-tree
							find = find!.left;
						}
					}
					else
					{
						if (find!.right == nil)
						{
							find!.right = new_node;
							break;
						}
						else
						{
							//visit right sub-tree
							find = find!.right;
						}
					}
				}
			}
		}
		else
		{
			print("Memory Overflow", terminator: "");
		}
	}
	//Find and print maximum node of binary search tree
	func max_node()
	{
		if (self.root == nil)
		{
			print("\n Empty Tree", terminator: "");
		}
		else
		{
			print(" Maximum node is : ", terminator: "");
			var temp: Node? = self.root;
			//Find rightmost node from to root
			while (temp!.right != nil)
			{
				//Visit right node
				temp = temp!.right;
			}
			print(" ", temp!.data ,"\n", terminator: "");
		}
	}
}
func main()
{
	let obj: BinarySearchTree = BinarySearchTree();
	//Create binary search tree
	/*
			       5
			     /   \
			    3     6
			   / \     \
			 -7   4     11
			   \       /  \
			    2     10   18
			    `          /
			              16                      

			*/
	obj.add_node(5);
	obj.add_node(3);
	obj.add_node(6);
	obj.add_node(-7);
	obj.add_node(4);
	obj.add_node(11);
	obj.add_node(18);
	obj.add_node(2);
	obj.add_node(10);
	obj.add_node(16);
	obj.max_node();
}
main();

Output

 Maximum node is :   18




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment