C Program to Find Maximum Value in Tree using Inorder Traversal

This is a C Program for finding the largest value in a Binary Search Tree using Inorder traversal.

Problem Description

We have to write a C program to find the largest value in a Binary Search Tree using Inorder traversal.

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

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

advertisement
advertisement

Case 3. Tree having just one node

                    15

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

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

1. In binary search tree, the left child of a node is always smaller than it’s parent while the right child is always greater than it’s parent.
2. In Inorder traversal we traverse the left subtree then we print the values of the nodes and then we traverse the right subtree.
3. As mentioned above that for a Binary Search Tree, the left child of a node is always smaller and right child is always greater than it’s parent, if we traverse a tree using inorder traversal, we are going to get a sequence of nodes arranged in ascending order of their values.
4. So the largest value of a BST using inorder traversal will be the value of the last node traversed, because the output of inorder traversal gives nodes arranged in ascending order if the tree is a Binary Search Tree(BST).

Program/Source Code

Here is source code of the C Program for finding the largest node in a Binary Search Tree using Inorder traversal . 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 largest node
  3.  * in a Binary Search Tree using  Inorder Traversal 
  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 largest(struct node *root)
  30. {
  31.     while (root != NULL && root->right != NULL)
  32.     {
  33.         root = root->right;
  34.     }
  35.     printf("\nLargest value is %d\n", root->info);
  36. }
  37. /*
  38.  * Main Function
  39.  */
  40. int main()
  41. {
  42.     /* Creating first Tree. */
  43.     struct node *newnode = createnode(25);
  44.     newnode->left = createnode(17);
  45.     newnode->right = createnode(35);
  46.     newnode->left->left = createnode(13);
  47.     newnode->left->right = createnode(19);
  48.     newnode->right->left = createnode(27);
  49.     newnode->right->right = createnode(55);
  50.     /* Sample Tree 1:
  51.      *               25
  52.      *             /    \
  53.      *            17     35
  54.      *           / \     / \
  55.      *         13  19   27 55
  56.      */
  57.     printf("Inorder traversal of tree 1 :");
  58.     inorder(newnode);
  59.     largest(newnode);
  60.  
  61.     /* Creating second Tree. */
  62.     struct node *node = createnode(1);
  63.     node->right = createnode(2);
  64.     node->right->right = createnode(3);
  65.     node->right->right->right = createnode(4);
  66.     node->right->right->right->right = createnode(5);
  67.     /* Sample Tree 2:   Right Skewed Tree (Unbalanced).
  68.      *               1
  69.      *                \
  70.      *                 2
  71.      *                  \
  72.      *                   3
  73.      *                    \
  74.      *                     4
  75.      *                      \
  76.      *                       5
  77.      */
  78.     printf("\nInorder traversal of tree 2 :");
  79.     inorder(node);
  80.     largest(node);
  81.  
  82.     /* Creating third Tree. */
  83.     struct node *root = createnode(15);
  84.     /* Sample Tree 3- Tree having just one root node.
  85.      *              15
  86.      */
  87.     printf("\nInorder traversal of tree 3 :");
  88.     inorder(root);
  89.     largest(root);
  90.     return 0;
  91. }
Program Explanation

Program contains three 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.

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

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

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.

If you find any mistake above, kindly email to [email protected]

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.