C Program to Find the Nearest Common Ancestor

This C Program to Find the Nearest Common Ancestor

Here is source code of the C Program to Find the Nearest Common Ancestor. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Find the Nearest Common Ancestor in the Given set 
  3.  * of Nodes 
  4.  */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. /*
  9.  * Structure of binary tree node
  10.  */
  11. struct btnode 
  12. {
  13.     int value;
  14.     struct btnode *l;
  15.     struct btnode *r;
  16. };
  17.  
  18. void createbinary();
  19. node* add(int val);
  20. int height(node *);
  21. int nearest_common_ancestor(node*,  int,  int);
  22. typedef struct btnode node;
  23. node *root = NULL, *ptr;
  24.  
  25. int  main()
  26. {
  27.     int c, n1, n2;
  28.  
  29.     createbinary();
  30.     printf("\nEnter nodes having common ancestor");
  31.     scanf("%d %d", &n1, &n2);
  32.     c = nearestcommonancestor(root, n1, n2);
  33.     if (c == -1)
  34.     {
  35.         printf("No common ancestor");
  36.     }
  37.     else
  38.     {
  39.         printf("The common ancestor is %d", c);
  40.     }
  41. }
  42. /*
  43.  * constructing the following binary tree
  44.  *     50
  45.  *     / \
  46.  *    20 30
  47.  *   / \ 
  48.  *  70 80
  49.  * / \     \
  50.  *10 40      60
  51.  */    
  52. void createbinary()
  53. {
  54.     root = add(50);
  55.     root->l = add(20);
  56.     root->r = add(30);
  57.     root->l->l = add(70);
  58.     root->l->r = add(80);
  59.     root->l->l->l = add(10);
  60.     root->l->l->r = add(40);
  61.     root->l->r->r = add(60);
  62. }
  63.  
  64. /*
  65.  * Add node to binary tree
  66.  */
  67. node* add(int val)
  68. {
  69.     ptr = (node*)malloc(sizeof(node));
  70.     if (ptr == NULL)
  71.     {
  72.         printf("Memory was not allocated");
  73.         return;
  74.     }
  75.     ptr->value = val;
  76.     ptr->l = NULL;
  77.     ptr->r = NULL;
  78.     return ptr;
  79. }
  80.  
  81. /*
  82.  * height of the binary tree
  83.  */
  84. int height(node *n)
  85. {
  86.     int lheight, rheight;
  87.  
  88.     if (n != NULL)
  89.     {
  90.         lheight = height(n->l);
  91.         rheight = height(n->r);
  92.         if (lheight > rheight)
  93.             return(lheight + 1);
  94.         else 
  95.             return(rheight + 1);
  96.     }
  97. }
  98.  
  99. /*
  100.  * Finds the nearest common ancestor
  101.  */
  102. int nearestcommonancestor(node *temp, int n1, int n2)
  103. {
  104.     int h, i, j, k;
  105.     node *prev;
  106.  
  107.     /*
  108.      * If any one the inputted node is root then no common ancestor
  109.      */
  110.     if (n1 == root->value || n2 == root->value)
  111.     {
  112.         return - 1;    
  113.     }
  114.     h = height(root);    
  115.     for (i = 1;i < h;i++)
  116.     {
  117.         if (temp->l->value == n1 || temp->r->value == n1 || temp ->l->value == n2 || temp->r->value == n2)
  118.         {
  119.             prev = temp;
  120.             for (j = 1, temp = root;j < h;j++)
  121.             {
  122.                 if (temp != NULL)
  123.                 {
  124.                     if (temp->r->value == n2 || temp->r->value == n1 || temp->l->value == n1 || temp->l->value == n2)
  125.                     {
  126.                         /*
  127.                          * If the parent of n1 and parent of n2 are same then the value of parent is returned
  128.                          */
  129.                         if (prev->value == temp->value)
  130.                             return prev->value;
  131.                         /*
  132.                          * otherwise from parents of two nodes is traversed upward if any node matches with other's node parent then that is 
  133.                          * considered as common ancestor
  134.                          */
  135.                         else
  136.                             for (k = 0, prev = temp;k < h;k++)
  137.                             {
  138.                                 if (temp->l->value == prev->l->value)
  139.                                     return temp->value;
  140.                                 else 
  141.                                     temp = temp->l;
  142.                             }
  143.                     }
  144.                 }
  145.                 temp = temp->l;
  146.             }
  147.         }
  148.         temp = temp->l;
  149.     }
  150. }

/*
 * Binary tree
 *     50
 *     / \
 *    20 30
 *   / \ 
 *  70 80
 * / \     \
 *10 40      60
 */    
$ gcc test14.c
$ a.out
Enter nodes having common ancestor 60 30
The common ancestor is 50
 
$ a.out
Enter nodes having common ancestor 10 40
The common ancestor is 70
 
$ a.out
Enter nodes having common ancestor 10 70
The common ancestor is 20
 
$ a.out
Enter nodes having common ancestor 20 50
No common ancestor

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
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.