C Program to Find Minimum and Maximum Element in Binary Search Tree

This is a C Program for finding the smallest and the largest value in a Binary Search Tree.

Problem Description

We have to write a C program to find the smallest and the largest value in a Binary Search Tree.

Expected Input and Output

Case 1. Balanced Tree:When the weight is equal on both the sides of root.

                    25
                  /    \  
                 17     35   
                / \     / \ 
              13  19   27 55

Output:
Inorder traversal of tree 1 : 13 17 19 25 27 35 55
Largest value is 55
Smallest value is 13

Case 2. Right Skewed Tree:When the nodes at every level just have a right child.

                    1   
                     \
                      2    
                       \    
                        3 
                         \
                          4
                           \
                            5

Output:
Inorder traversal of tree 2 : 1 2 3 4 5
Largest value is 5
Smallest value is 1

advertisement
advertisement

Case 3. Tree having just one node

                    15

Output:
Inorder traversal of tree 3 : 15
Largest value is 15
Smallest value is 15

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Problem Solution

1. We can easily find the smallest and the largest value in a BST using it’s properties and inorder traversal.
2. Inorder traversal of a BST gives a sequence of nodes arranged in ascending order of their values because in a BST the left child is always smaller and the right child is always greater than it’s parent.
3. So for the smallest value we need to find the extreme left or the leftmost node in leftsubtree and for the largest value we need to find the extreme right or the rightmost node in rightsubtree.

Program/Source Code

Here is source code of the C Program for finding the smallest and the largest node in a Binary Search Tree. The program is successfully compiled and tested using Codeblocks gnu/gcc compiler on windows 10. The program output is also shown below.

  1. /*
  2.  * C Program for finding the smallest
  3.  * and the largest node in a Binary Search Tree
  4.  */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. struct node
  8. {
  9.     int info;
  10.     struct node *left, *right;
  11. };
  12. struct node *createnode(int key)
  13. {
  14.     struct node *newnode = (struct node*)malloc(sizeof(struct node));
  15.     newnode->info = key;
  16.     newnode->left = NULL;
  17.     newnode->right = NULL;
  18.     return(newnode);
  19. }
  20. void inorder(struct node *root)
  21. {
  22.     if(root != NULL)
  23.     {
  24.         inorder(root->left);
  25.         printf(" %d ",root->info);
  26.         inorder(root->right);
  27.     }
  28. }
  29. void smallest(struct node *root)
  30. {
  31.     while(root != NULL && root->left != NULL)
  32.     {
  33.         root = root->left;
  34.     }
  35.     printf("\nSmallest value is %d\n", root->info);
  36. }
  37. void largest(struct node *root)
  38. {
  39.     while (root != NULL && root->right != NULL)
  40.     {
  41.         root = root->right;
  42.     }
  43.     printf("\nLargest value is %d", root->info);
  44. }
  45. /*
  46.  * Main Function
  47.  */
  48. int main()
  49. {
  50.     /* Creating first Tree. */
  51.     struct node *newnode = createnode(25);
  52.     newnode->left = createnode(17);
  53.     newnode->right = createnode(35);
  54.     newnode->left->left = createnode(13);
  55.     newnode->left->right = createnode(19);
  56.     newnode->right->left = createnode(27);
  57.     newnode->right->right = createnode(55);
  58.     /* Sample Tree 1:
  59.      *               25
  60.      *             /    \
  61.      *            17     35
  62.      *           / \     / \
  63.      *         13  19   27 55
  64.      */
  65.     printf("Inorder traversal of tree 1 :");
  66.     inorder(newnode);
  67.     largest(newnode);
  68.     smallest(newnode);
  69.  
  70.     /* Creating second Tree. */
  71.     struct node *node = createnode(1);
  72.     node->right = createnode(2);
  73.     node->right->right = createnode(3);
  74.     node->right->right->right = createnode(4);
  75.     node->right->right->right->right = createnode(5);
  76.     /* Sample Tree 2:   Right Skewed Tree (Unbalanced).
  77.      *               1
  78.      *                \
  79.      *                 2
  80.      *                  \
  81.      *                   3
  82.      *                    \
  83.      *                     4
  84.      *                      \
  85.      *                       5
  86.      */
  87.     printf("\nInorder traversal of tree 2 :");
  88.     inorder(node);
  89.     largest(node);
  90.     smallest(node);
  91.  
  92.     /* Creating third Tree. */
  93.     struct node *root = createnode(15);
  94.     /* Sample Tree 3- Tree having just one root node.
  95.      *              15
  96.      */
  97.     printf("\nInorder traversal of tree 3 :");
  98.     inorder(root);
  99.     largest(root);
  100.     smallest(root);
  101.     return 0;
  102. }
Program Explanation

Program contains four important functions.

advertisement

1. createnode(key);
This function helps to create a new node by allocating it a memory dynamically. It has just one parameter which is “key” which assigns value to the node thereby creating a fresh node having left and right child as “NULL”.

2. inorder(struct node *root);
This function helps to traverse the whole tree. First we recursively traverse the left subtree, then print the value of the node and then we recursively traverse the right subtree.

3. largest(struct node *root)
(a). Inorder traversal of a BST gives a sequence of nodes arranged in increasing order of their values because in BST the left child of a node is always smaller and the right child of a node is always greater than it’s parent.
(b). So the largest node in a BST will be the last node in the right subtree.
(c). That is what we are doing in largest() function, we are making the root = root->right so that we can reach the extreme right node or the last node in a right subtree.
(d). The last node in a right subtree will not have any child, therefore the while condition is going to terminate as root->right will become = NULL.

4. smallest(struct node *root)
(a). The smallest node in a Binary Search Tree will be the leftmost node in leftsubtree, so we keep on traversing the leftsubtree tree till we reach the leftmost node (root = root->left), and when we encounter the leftmost node we print it’s value and terminate the loop as root->left will become NULL.

advertisement
Runtime Test Cases
Inorder traversal of tree 1 : 13  17  19  25  27  35  55
Largest value is 55
Smallest value is 13
 
Inorder traversal of tree 2 : 1  2  3  4  5
Largest value is 5
Smallest value is 1
 
Inorder traversal of tree 3 : 15
Largest value is 15
Smallest value is 15

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.