Find the node with maximum value in a Binary Search Tree using recursion
Last Updated :
25 May, 2022
Given a Binary Search Tree, the task is to find the node with maximum value.
Examples:
Input:
Output: 22
Approach: Just traverse the node from root to right recursively until right is NULL. The node whose right is NULL is the node with the maximum value.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct node {
int data;
struct node* left;
struct node* right;
};
struct node* newNode( int data)
{
struct node* node = ( struct node*)
malloc ( sizeof ( struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
struct node* insert( struct node* node, int data)
{
if (node == NULL)
return (newNode(data));
else {
if (data <= node->data)
node->left = insert(node->left, data);
else
node->right = insert(node->right, data);
return node;
}
}
int maxValue( struct node* node)
{
if (node->right == NULL)
return node->data;
return maxValue(node->right);
}
int main()
{
struct node* root = NULL;
root = insert(root, 4);
insert(root, 2);
insert(root, 1);
insert(root, 3);
insert(root, 6);
insert(root, 5);
cout << maxValue(root);
return 0;
}
|
Java
class GFG
{
static class node
{
int data;
node left;
node right;
};
static node newNode( int data)
{
node node = new node();
node.data = data;
node.left = null ;
node.right = null ;
return (node);
}
static node insert(node node, int data)
{
if (node == null )
return (newNode(data));
else
{
if (data <= node.data)
node.left = insert(node.left, data);
else
node.right = insert(node.right, data);
return node;
}
}
static int maxValue(node node)
{
if (node.right == null )
return node.data;
return maxValue(node.right);
}
public static void main(String args[])
{
node root = null ;
root = insert(root, 4 );
root = insert(root, 2 );
root = insert(root, 1 );
root = insert(root, 3 );
root = insert(root, 6 );
root = insert(root, 5 );
System.out.println(maxValue(root));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def newNode(data):
node = Node( 0 )
node.data = data
node.left = None
node.right = None
return (node)
def insert(node,data):
if (node = = None ):
return (newNode(data))
else :
if (data < = node.data):
node.left = insert(node.left, data)
else :
node.right = insert(node.right, data)
return node
def maxValue(node):
if (node.right = = None ):
return node.data
return maxValue(node.right)
if __name__ = = '__main__' :
root = None
root = insert(root, 4 )
root = insert(root, 2 )
root = insert(root, 1 )
root = insert(root, 3 )
root = insert(root, 6 )
root = insert(root, 5 )
print (maxValue(root))
|
C#
using System;
class GFG
{
public class node
{
public int data;
public node left;
public node right;
};
static node newNode( int data)
{
node node = new node();
node.data = data;
node.left = null ;
node.right = null ;
return (node);
}
static node insert(node node, int data)
{
if (node == null )
return (newNode(data));
else
{
if (data <= node.data)
node.left = insert(node.left, data);
else
node.right = insert(node.right, data);
return node;
}
}
static int maxValue(node node)
{
if (node.right == null )
return node.data;
return maxValue(node.right);
}
public static void Main(String []args)
{
node root = null ;
root = insert(root, 4);
root = insert(root, 2);
root = insert(root, 1);
root = insert(root, 3);
root = insert(root, 6);
root = insert(root, 5);
Console.WriteLine(maxValue(root));
}
}
|
Javascript
<script>
class node
{
constructor(data)
{
this .left = null ;
this .right = null ;
this .data = data;
}
}
function newNode(data)
{
let Node = new node(data);
return (Node);
}
function insert(Node, data)
{
if (Node == null )
return (newNode(data));
else
{
if (data <= Node.data)
Node.left = insert(Node.left, data);
else
Node.right = insert(Node.right, data);
return Node;
}
}
function maxValue(Node)
{
if (Node.right == null )
return Node.data;
return maxValue(Node.right);
}
let root = null ;
root = insert(root, 4);
root = insert(root, 2);
root = insert(root, 1);
root = insert(root, 3);
root = insert(root, 6);
root = insert(root, 5);
document.write(maxValue(root));
</script>
|
Output:
6
Time Complexity: O(n), worst case happens for right skewed trees.
Auxiliary Space: O(n), maximum number of stack frames that could be present in memory is ‘n’
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...