Min and Max Element Searching in Binary Search Tree(BST)

  • Minimum element in the Binary Search Tree is the left most element.
  • Maximum element in the Binary Search Tree is the right most element.
  • See below Working Process.

Working Process for Min-Element Searching

  • Here we are traversing the left sub-tree and traverse till left most element of BST.
  • Left most element will be Minimum Element.
  • So 20 is the Min-Element of given BST.

Working Process for Max-Element Searching

  • Here we are traversing the right sub-tree and traverse till right most element of BST.
  • Right most element will be Maximum Element.
  • So 120 is the Max-Element of given BST.

Code Implementation for Min-Element Searching

void MinElementSearchInBST(BSTNode* root)
{
	if (root == NULL)
	{
		printf("Sorry Binary Search Tree is Empty...");
		return;
	}
	while (root->left!=NULL)
	{
		root = root->left;
	}
	printf("Minimum Element is : ",root->data);
}

Code Implementation for Max-Element Searching

void MaxElementSearchInBST(BSTNode* root)
{
	if (root == NULL)
	{
		printf("Sorry Binary Search Tree is Empty...");
		return;
	}
	while (root->right!=NULL)
	{
		root = root->right;
	}
	printf("Maximum Element is : ",root->data);
}

 Time & Space Complexity

  • For Min-Element Search :
    1 -: Time Complexity will be O(n).
    2 -: Space Complexity will be O(1).
  • For Max-Element Search :
    1 -: Time Complexity will be O(n).
    2 -: Space Complexity will be O(1).

Leave a Reply

Your email address will not be published. Required fields are marked *