C Program to Print All Nodes of Nth Level in Single Line

This C Program prints all the elements of Nth level in single line.

Here is source code of the C Program to print all the elements of Nth level in single line. 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 Elements of Nth Level in Single Line 
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. /*
  8.  * structure of node
  9.  */
  10. struct btnode 
  11. {
  12.     int value;
  13.     struct btnode *l;
  14.     struct btnode *r;
  15. };
  16.  
  17. void createbinary();
  18. node* add(int val);
  19. int height(node *);
  20. void printlevel(node *, int, int);
  21. void print();
  22.  
  23. typedef struct btnode node;
  24. node *root = NULL, *ptr;
  25.  
  26. int  main()
  27. {
  28.     int c;
  29.  
  30.     createbinary();
  31.     print();
  32. }
  33. /*
  34.  * constructing the following binary tree
  35.  *     50
  36.  *     / \
  37.  *    20 30
  38.  *   / \ 
  39.  *  70 80
  40.  * / \     \
  41.  *10 40      60
  42.  */    
  43. void createbinary()
  44. {
  45.     root = add(50);
  46.     root->l = add(20);
  47.     root->r = add(30);
  48.     root->l->l = add(70);
  49.     root->l->r = add(80);
  50.     root->l->l->l = add(10);
  51.     root->l->l->r = add(40);
  52.     root->l->r->r = add(60);
  53. }
  54.  
  55. /*
  56.  * Adding node to binary tree
  57.  */
  58. node* add(int val)
  59. {
  60.     ptr = (node*)malloc(sizeof(node));
  61.     if (ptr == NULL)
  62.     {
  63.         printf("Memory was not allocated");
  64.         return;
  65.     }
  66.     ptr->value = val;
  67.     ptr->l = NULL;
  68.     ptr->r = NULL;
  69.     return ptr;
  70. }
  71.  
  72. /*
  73.  * Prints all the nodes of all levels of the binary tree
  74.  */
  75. void print()
  76. {
  77.     int h, i;
  78.  
  79.     h = height(root);
  80.     for (i = 0;i < h;i++)
  81.     {
  82.         printf("\nLEVEL %d  :", i);
  83.         printlevel(root, i, 0);
  84.         printf("\n");
  85.     }
  86. }
  87. /*
  88.  *Prints the nodes of a particular level
  89.  */
  90. void printlevel(node *n, int desired, int current)
  91. {
  92.     if (n)
  93.     {
  94.         if (desired == current)
  95.             printf("%d\t", n->value);
  96.         else
  97.         {
  98.             printlevel(n->l, desired, current + 1);
  99.             printlevel(n->r, desired, current + 1);
  100.         }
  101.     }
  102. }
  103.  
  104. /*
  105.  * Height of the binary tree
  106.  */
  107. int height(node *n)
  108. {
  109.     int lheight, rheight;
  110.     if (n != NULL)
  111.     {
  112.         lheight = height(n->l);
  113.         rheight = height(n->r);
  114.         if (lheight > rheight)
  115.             return(lheight+1);
  116.         else 
  117.             return(rheight+1);
  118.     }
  119. }

/*
 * Binary tree
 *     50
 *     / \
 *    20 30
 *   / \ 
 *  70 80
 * / \     \
 *10 40      60
 */    
$ gcc tes26.c
$ a.out
 
LEVEL 0  :50
 
LEVEL 1  :20    30
 
LEVEL 2  :70    80
 
LEVEL 3  :10    40      60

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.