0% found this document useful (0 votes)
0 views

Data Structure 8

The document contains a C program that implements a binary search tree (BST) with functions to count leaf nodes, find the smallest and largest nodes, and calculate the height of the tree. It includes functions for creating nodes, inserting values, and traversing the tree. The main function demonstrates these capabilities by inserting a set of values and printing the results.

Uploaded by

andhaleomkar1
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Data Structure 8

The document contains a C program that implements a binary search tree (BST) with functions to count leaf nodes, find the smallest and largest nodes, and calculate the height of the tree. It includes functions for creating nodes, inserting values, and traversing the tree. The main function demonstrates these capabilities by inserting a set of values and printing the results.

Uploaded by

andhaleomkar1
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

//* count Number of leaf nodes, find the biggest and

smallest and height of the tree_Diksha


Gundap_19_CSD*//

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

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

struct Node* createNode(int value) {


struct Node* newNode = (struct
Node*)malloc(sizeof(struct Node)); newNode->data =
value;
newNode->left = newNode->right =
NULL; return newNode;
}

struct Node* insert(struct Node* root, int


value) { if (root == NULL)
return createNode(value);

if (value < root->data)


root->left = insert(root->left,
value); else if (value > root->data)
root->right = insert(root->right, value);

return root;
}

int countLeafNodes(struct Node*


root) { if (root == NULL) return
0;
if (root->left == NULL && root->right == NULL) return 1;
return countLeafNodes(root->left) + countLeafNodes(root->right);
}

struct Node* findMin(struct Node*


root) { while (root->left != NULL)
root = root->left;
return root;
}

struct Node* findMax(struct Node*


root) { while (root->right != NULL)
root = root->right;
return root;
}

int treeHeight(struct Node* root) {


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

int main() {
struct Node* root = NULL;
int values[] = {50, 30, 70, 20, 40, 60, 80};
int n = sizeof(values) / sizeof(values[0]);

// Insert nodes into the


BST for (int i = 0; i < n;
i++) {
root = insert(root, values[i]);
}

printf("Number of Leaf Nodes: %d\n", countLeafNodes(root));

// Find and print the smallest and largest nodes


printf("Smallest Node: %d\n", findMin(root)->data);
printf("Largest Node: %d\n", findMax(root)->data);

// Compute and print the height of the tree


printf("Height of the Tree: %d\n",
treeHeight(root));

return 0;
}

You might also like