C Program to Print Height and Depth of Binary Tree

This C Program print height and depth of given binary tree.

Here is source code of the C Program to print height and depth of given binary 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 Print Height and Depth of given Binary Tree
  3.  *                                        50
  4.  *                                       /  \
  5.  *                                      20   30
  6.  *                                     /  \
  7.  *                                    70  80
  8.  *                                   / \    \
  9.  *                                  10  40   60                                
  10.  */    
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14. struct btnode {
  15.     int value;
  16.     struct btnode *l;
  17.     struct btnode *r;
  18. };
  19. struct btnode *root;
  20. typedef struct btnode bt;
  21. bt *new,*ptr,*ptr1,*ptr2;
  22.  
  23. bt* create()
  24. {
  25.     new = (bt *)malloc(sizeof(bt));
  26.     new->l = NULL;
  27.     new->r = NULL;
  28.     return new;
  29. }
  30.  
  31. void construct_binary_tree()
  32. {
  33.     root = create();
  34.     root->value = 50;
  35.  
  36.     ptr = create();
  37.     root->l = ptr;
  38.     ptr->value = 20;
  39.  
  40.     ptr1 = create();
  41.     ptr->l = ptr1;
  42.     ptr1->value = 70;
  43.  
  44.     ptr2 = create();
  45.     ptr1->l = ptr2;
  46.     ptr2->value = 10;
  47.  
  48.     ptr2 = create();
  49.     ptr1->r = ptr2;
  50.     ptr2->value = 40;
  51.  
  52.     ptr1 = create();
  53.     ptr->r = ptr1;
  54.     ptr1->value = 80;
  55.  
  56.     ptr2 = create();
  57.     ptr1->r = ptr2;
  58.     ptr2->value = 60;
  59.  
  60.     ptr = create();
  61.     root->r = ptr;
  62.     ptr->value = 30;
  63. }
  64.  
  65. void main()
  66. {
  67.     int depth1 = 0, depth2 = 0;
  68.  
  69.     construct_binary_tree();
  70.     ptr = root;
  71.     while (ptr->l != NULL || ptr->r != NULL)
  72.     {
  73.         depth1++;
  74.         if (ptr->l == NULL)
  75.             ptr = ptr->r;
  76.         else
  77.             ptr = ptr->l;
  78.     }
  79.     ptr = root;    
  80.     while (ptr->l != NULL || ptr->r != NULL)
  81.     {
  82.         depth2++;
  83.         if (ptr->r == NULL)
  84.             ptr = ptr->l;
  85.         else
  86.             ptr = ptr->r;
  87.     }
  88. /*
  89.  *DEPTH IS CONSIDERED FROM LEVEL-0 ALSO HEIGHT IS CONSIDERED AS NUMBER OF EDGES
  90.  */
  91.     if (depth1 > depth2)
  92.         printf("height of the tree is %d\ndepth of the tree is %d",depth1,depth1);
  93.     else    
  94.         printf("height of the tree is %d\ndepth of the tree is %d",depth2,depth2);
  95. }

$ cc tree18.c
$ a.out
height of the tree is 3
depth of the tree is 3

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.