Monday, 1 November 2021

Finding minimum and maximum value in Binary Search Tree(BST)

In Binary Search Tree, we can find maximum by traversing right pointers until we reach the rightmost node. But in Binary Tree, we must visit every node to figure out maximum. So the idea is to traverse the given tree and for every node return maximum of 3 values.  

 
1.Node’s data.
2.Maximum in node’s left subtree.
3.Maximum in node’s right subtree.

Find the node with minimum value in BST

This is quite simple. Just traverse the node from root to left recursively until left is NULL. The node whose left is NULL is the node with minimum value.

public BSTNode minimum(BSTNode P)
{
  BSTNode min=null;
  While(p!=null)
p=  p.left;
}
return min;
}

Find the node with maximum value in BST

This is quite simple. Just traverse the node from root to rigt recursively until right is NULL. The node whose right is NULL is the node with maximum value.

public BSTNode maximum(BSTNode P)
{
  BSTNode max=null;
  While(p!=null)
p=  p.right;
}
return max;
}

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.

Find Us On Facebook

python tutorial

More

C Programming

More

Java Tutorial

More

Data Structures

More

MS Office

More

Database Management

More
Top