C Program to Check if Binary Tree is Subtree of Another Tree

This is a C Program program to check whether a binary tree is subtree of another tree. Given two binary trees, check if the first tree is subtree of the second one. A subtree of a tree T is a tree S consisting of a node in T and all of its descendants in T. The subtree corresponding to the root node is the entire tree; the subtree corresponding to any other node is called a proper subtree.

Here is source code of the C Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /* A binary tree node has data, left child and right child */
  5. struct node {
  6.     int data;
  7.     struct node* left;
  8.     struct node* right;
  9. };
  10.  
  11. /* A utility function to check whether trees with roots as root1 and
  12.  root2 are identical or not */
  13. bool areIdentical(struct node * root1, struct node *root2) {
  14.     /* base cases */
  15.     if (root1 == NULL && root2 == NULL)
  16.         return true;
  17.  
  18.     if (root1 == NULL || root2 == NULL)
  19.         return false;
  20.  
  21.     /* Check if the data of both roots is same and data of left and right
  22.      subtrees are also same */
  23.     return (root1->data == root2->data
  24.             && areIdentical(root1->left, root2->left) && areIdentical(
  25.             root1->right, root2->right));
  26. }
  27.  
  28. /* This function returns true if S is a subtree of T, otherwise false */
  29. bool isSubtree(struct node *T, struct node *S) {
  30.     /* base cases */
  31.     if (S == NULL)
  32.         return true;
  33.  
  34.     if (T == NULL)
  35.         return false;
  36.  
  37.     /* Check the tree with root as current node */
  38.     if (areIdentical(T, S))
  39.         return true;
  40.  
  41.     /* If the tree with root as current node doesn't match then
  42.      try left and right subtrees one by one */
  43.     return isSubtree(T->left, S) || isSubtree(T->right, S);
  44. }
  45.  
  46. /* Helper function that allocates a new node with the given data
  47.  and NULL left and right pointers. */
  48. struct node* newNode(int data) {
  49.     struct node* node = (struct node*) malloc(sizeof(struct node));
  50.     node->data = data;
  51.     node->left = NULL;
  52.     node->right = NULL;
  53.     return (node);
  54. }
  55.  
  56. /* Driver program to test above function */
  57. int main() {
  58.     /* Construct the following tree
  59.        26
  60.      /   \
  61.     10     3
  62.   /    \     \
  63.  4      6      3
  64.   \
  65.    30
  66.      */
  67.     struct node *T = newNode(26);
  68.     T->right = newNode(3);
  69.     T->right->right = newNode(3);
  70.     T->left = newNode(10);
  71.     T->left->left = newNode(4);
  72.     T->left->left->right = newNode(30);
  73.     T->left->right = newNode(6);
  74.  
  75.     /* Construct the following tree
  76.        10
  77.      /    \
  78.     4      6
  79.      \
  80.       30
  81.      */
  82.     struct node *S = newNode(10);
  83.     S->right = newNode(6);
  84.     S->left = newNode(4);
  85.     S->left->right = newNode(30);
  86.  
  87.     if (isSubtree(T, S))
  88.         printf("Tree S is subtree of tree T");
  89.     else
  90.         printf("Tree S is not a subtree of tree T");
  91.  
  92.     getchar();
  93.     return 0;
  94. }

Output:

$ gcc SubTree.c
$ ./a.out
 
Tree S is subtree of tree T

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

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

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.