C Program to Find Nearest Sibling of a Node in Tree

This C Program Finds the Nearest Sibling of a Node in a Tree.

Here is source code of the C Program to Find the Nearest Sibling of a Node in a Tree. 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 Sibling of a Node in a Tree 
  3.  */ 
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. struct btnode
  8. {
  9.     int value;
  10.     struct btnode *l;
  11.     struct btnode *r;
  12. }*root = NULL;
  13.  
  14. // Function Prototype
  15. void printout(struct btnode*);
  16. struct btnode* newnode(int);
  17. void find(struct btnode *, int);
  18. void find_sibling(struct btnode *, int);
  19.  
  20. void main()
  21. {
  22.     int data, res = 0;
  23.  
  24.     root = newnode(50);
  25.     root->l = newnode(20);
  26.     root->r = newnode(30);
  27.     root->l->l = newnode(70);
  28.     root->l->r = newnode(80);
  29.     root->l->r->r = newnode(60);
  30.     root->l->l->l = newnode(10);
  31.     root->l->l->r = newnode(40);
  32.     printf("tree elements are\n");
  33.     printout(root);
  34.     printf("\nenter element to find");
  35.     scanf("%d", &data);
  36.     if (root->value == data)
  37.         printf("--NO SIBLING FOR ROOT NODE--\n");
  38.     else
  39.     {
  40.         find(root,data);
  41.     }
  42. }
  43.  
  44. // Create a node
  45. struct btnode* newnode(int value)
  46. {
  47.     struct btnode* node = (struct btnode*)malloc(sizeof(struct btnode));
  48.     node->value = value;
  49.     node->l = NULL;
  50.     node->r = NULL;
  51.     return(node);
  52. }
  53.  
  54. // to display the tree
  55. void printout(struct btnode *tree)
  56. {
  57.     if (tree->l)
  58.         printout(tree->l);
  59.     printf("%d\t", tree->value);
  60.     if (tree->r)
  61.         printout(tree->r);
  62. }
  63.  
  64. // To find node in a tree
  65. void find(struct btnode *tree, int data)
  66. {
  67.     static int count = 0;
  68.     struct btnode *ptr = root;
  69.     int i;
  70.  
  71.     if (tree->l || tree->r)
  72.     {
  73.         if (tree->l->value == data)
  74.             find_sibling(tree, 1);
  75.         else if (tree->r->value == data)
  76.             find_sibling(tree, 0);
  77.         else
  78.         {
  79.             find(tree->l, data);
  80.             find(tree->r, data);
  81.         }
  82.     }
  83. }
  84.  
  85. // To find sibling of a node in a tree
  86. void find_sibling(struct btnode *tree,int side)
  87. {
  88.     if (side == 1 && tree->r != NULL)
  89.     {
  90.         printf("--------SIBLING FOUND---------\n");
  91.         printf("%d\n", tree->r->value);
  92.     }
  93.     else if (side == 0 && tree->l != NULL)
  94.     {
  95.         printf("--------SIBLING FOUND---------\n");
  96.         printf("%d\n", tree->l->value);
  97.     }
  98. }

$ gcc tree13.c
$ a.out
tree elements are
10      70      40      20      80      60      50      30
enter element to find 20
--------SIBLING FOUND---------
30
$ a.out
tree elements are
10      70      40      20      80      60      50      30
enter element to find 10
--------SIBLING FOUND---------
40
$ gcc tree13.c
$ a.out
tree elements are
10      70      40      20      80      60      50      30
enter element to find 30
--------SIBLING FOUND---------
20

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.