Find the node with minimum value in a Binary Search Tree
PROBLEM :
Find the node with minimum value in a Binary Search Tree
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.
For the above tree, we start with 20, then we move left 8, we keep on moving to left until we see NULL. Since left of 4 is NULL, 4 is the node with minimum value.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/* Structure of a Binary Tree
struct Node
{
int data;
struct Node* left, *right;
}; */
/* Function to get the maximum width of a binary tree*/
tree* getMin_tree(tree *root)
{
tree* temp ;
if(root->left==NULL)
return root ;
temp=root->left ;
while(temp->left!=NULL)
temp=temp->left ;
return temp ;
}
---------------------------------------------------------------------------------
Find the node with minimum value in a Binary Search Tree
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.
For the above tree, we start with 20, then we move left 8, we keep on moving to left until we see NULL. Since left of 4 is NULL, 4 is the node with minimum value.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/* Structure of a Binary Tree
struct Node
{
int data;
struct Node* left, *right;
}; */
/* Function to get the maximum width of a binary tree*/
tree* getMin_tree(tree *root)
{
tree* temp ;
if(root->left==NULL)
return root ;
temp=root->left ;
while(temp->left!=NULL)
temp=temp->left ;
return temp ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment