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

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.

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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.