Develop a nonrecursive algorithm in psuedocode to find the s
Develop a nonrecursive algorithm in psuedocode to \"find the smallest node in a binary search tree\"
Solution
Hello
You have to initialise the root value to a temporary variable. and while temp->left is not null i.e., it doesnt reach the end, you have to keep iterating forth by using temp=temp->left and when its null, that means thats the minimum value (it must be remembered that in BST, the left most child is the minimum most value and the right most child is the maximum most value. So we use that concept here). So when temp->left hits null, we print the value present there.
Please find below a program for the above concept.
#include <stdio.h>
 #include<stdlib.h>
 
 struct node
 {
 int data;
 struct node* lef;
 struct node* rit;
 };
struct node* nNode(int data)
 {
 struct node* node = (struct node*)
 malloc(sizeof(struct node));
 node->data = data;
 node->lef = NULL;
 node->rit = NULL;
 
 return(node);
 }
struct node* ins(struct node* node, int data)
 {
if (node == NULL)
 return(nNode(data));
 else
 {
   
 if (data <= node->data)
 node->lef = ins(node->lef, data);
 else
 node->rit = ins(node->rit, data);
 
 
 return node;
 }
 }
int minVal(struct node* node) {
 struct node* curr = node;
while (curr->left != NULL) {
 curr = curr->left;
 }
 return(curr->data);
 }
 
 
 int main()
 {
 struct node* root = NULL;
 root = ins(root, 4);
 ins(root, 2);
 ins(root, 1);
 ins(root, 3);
 ins(root, 6);
 ins(root, 5);
 
 printf(\"\  Minimum value in BST is %d\", minVal(root));
 return 0;
 }


