0% found this document useful (0 votes)
12 views3 pages

Height - Leaves Others

Uploaded by

witob23385
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Height - Leaves Others

Uploaded by

witob23385
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Program to implement functions to find the minimum node, height of

the binary tree, count the number of leaves, search for a key in the
binary tree.

#include <stdio.h>
#include <stdlib.h>

// Define a Node structure


typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;

// Function to create a new node


Node* createNode(int val) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Memory allocation failed!\n");
return NULL;
}
newNode->data = val;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}

// Function to calculate the height of the binary tree


int getHeight(Node* root) {
if (root == NULL) {
return 0; // Height of an empty tree is 0
}
int leftHeight = getHeight(root->left);
int rightHeight = getHeight(root->right);
return 1 + (leftHeight > rightHeight ? leftHeight :
rightHeight);
}

// Function to count the number of leaves in the binary tree


int countLeaves(Node* root) {
if (root == NULL) {
return 0; // No leaves in an empty tree
}
if (root->left == NULL && root->right == NULL) {
return 1; // This is a leaf node
}
return countLeaves(root->left) + countLeaves(root->right);
}

// Function to search for a key in the binary tree


Node* search(Node* root, int key) {
if (root == NULL || root->data == key) {
return root; // Return the node if found or NULL if not
found
}
if (key < root->data) {
return search(root->left, key);
} else {
return search(root->right, key);
}
}

// Function to find the minimum value in the binary tree


Node* findMin(Node* root) {
if (root == NULL) {
return NULL; // Tree is empty
}
while (root->left != NULL) {
root = root->left;
}
return root;
}

// Example usage
int main() {
// Create a simple binary tree
Node* root = createNode(20);
root->left = createNode(10);
root->right = createNode(30);
root->left->left = createNode(5);
root->left->right = createNode(15);
root->right->left = createNode(25);
root->right->right = createNode(35);

// Searching for a key


int key = 15;
Node* result = search(root, key);
if (result != NULL) {
printf("Found key: %d\n", result->data);
} else {
printf("Key not found!\n");
}

// Finding the minimum value


Node* minNode = findMin(root);
if (minNode != NULL) {
printf("Minimum value in the tree: %d\n", minNode->data);
} else {
printf("The tree is empty!\n");
}
// Calculating the height of the tree
int height = getHeight(root);
printf("Height of the tree: %d\n", height);

// Counting the number of leaf nodes


int leafCount = countLeaves(root);
printf("Number of leaves in the tree: %d\n", leafCount);

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy