This C program calculates the height of a binary tree, defined as the number of edges in the longest path from the root node to the farthest leaf node.

Program Explanation

The program is structured as follows:

  • Node Structure: Defines the structure of a binary tree node.
  • Function to Create a New Node: Allocates a new node with given data.
  • Function to Calculate Height: Recursively calculates the height of the binary tree.
  • Main Function: Creates a sample binary tree and calculates its height.

Program Code

// Include necessary headers
#include <stdio.h>
#include <stdlib.h>

// Define the structure for tree nodes
typedef struct node {
    int data;
    struct node* left;
    struct node* right;
} Node;

// Function to create a new tree node
Node* newNode(int data) {
    Node* temp = (Node*)malloc(sizeof(Node));
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}

// Function to calculate the height of the binary tree
int height(Node* root) {
    if (root == NULL)
        return 0; // The height of an empty tree is 0
    else {
        // Compute the height of each subtree
        int leftHeight = height(root->left);
        int rightHeight = height(root->right);
        // Return the larger one
        return 1 + (leftHeight > rightHeight ? leftHeight : rightHeight);
    }
}

// Main function to demonstrate the tree height calculation
int main() {
    Node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);
    root->right->right = newNode(6);

    printf("Height of the tree is %d\\n", height(root));
    return 0;
}
    

 

Key Components of the Program:

  • Node Structure: Defines each node in the binary tree containing data and pointers to left and right children.
  • newNode Function: Helps to allocate and initialize a new node with specified data.
  • height Function: Recursively calculates the height of the tree. It compares the heights of the left and right subtrees and returns the greater of the two, plus one to account for the current node.
  • Main Function: Constructs a sample binary tree, calculates its height using the height function, and prints the result.

This program structure offers a clear and organized approach to managing binary tree operations, with a focus on determining the maximum depth or height of the tree effectively.

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)