C Program to Print All Paths from Root to Leaf in a Tree

This C Program Prints all the Paths from the Root to the Leaf in a Tree.

Here is source code of the C program to Print only Nodes in Left SubTree. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Print all the Paths from the Root to the Leaf 
  3.  * in a Tree 
  4.  */
  5. #include<stdio.h>
  6. #include<stdlib.h>
  7.  
  8. struct node
  9. {
  10.    int data;
  11.    struct node* left;
  12.    struct node* right;
  13. };
  14.  
  15. void print_paths_recur(struct node* node, int path[], int path_len);
  16. void print_array(int ints[], int len);
  17.  
  18. /*Function to store all the paths from the root node to all leaf nodes in  a array*/
  19.  
  20. void print_paths(struct node* node) 
  21. {
  22.   int path[1000];
  23.   print_paths_recur(node, path, 0);
  24. }
  25.  
  26. /*Function which helps the print_path to recursively print all the nodes*/ 
  27. void print_paths_recur(struct node* node, int path[], int path_len) 
  28. {
  29.   if (node == NULL) 
  30.     return; 
  31.  
  32.   path[path_len] = node->data;
  33.   path_len++;
  34.  
  35.   if (node->left == NULL && node->right == NULL) 
  36.   {
  37.     print_array(path, path_len);
  38.   }
  39.   else
  40.   {
  41.     print_paths_recur(node->left, path, path_len);    //recursively calls the left node of the tree
  42.     print_paths_recur(node->right, path, path_len);    //recursively calls the right node of the tree
  43.   }
  44. }
  45.  
  46. /*Function to print all the paths */
  47. void print_array(int ints[], int len) 
  48. {
  49.   int i;
  50.   for (i = 0; i < len; i++) 
  51.   {
  52.     printf("%d -> ", ints[i]);
  53.   }
  54.   printf("\n");
  55. }    
  56.  
  57. struct node* newnode(int data)
  58. {
  59.   struct node* node = (struct node*) malloc(sizeof(struct node));
  60.   node->data = data;
  61.   node->left = NULL;
  62.   node->right = NULL;
  63.  
  64.   return(node);
  65. }
  66.  
  67. int main()
  68. { 
  69.    /*
  70.     The input tree is as shown below
  71.                 40
  72.                 / \
  73.             20        60
  74.             / \       \
  75.         10        30      80
  76.                           \
  77.                             90
  78.   */ 
  79.   struct node *root = newnode(40);
  80.   root->left = newnode(20);
  81.   root->right = newnode(60);
  82.   root->left->left = newnode(10);
  83.   root->left->right = newnode(30);
  84.   root->right->right = newnode(80);
  85.   root->right->right->right = newnode(90);
  86.   print_paths(root);
  87.   return 0;
  88. }

$ cc tree20.c
$ a.out
40 -> 20 -> 10 ->
40 -> 20 -> 30 ->
40 -> 60 -> 80 -> 90 ->

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.