Write a menu driven program to create binary tree using linked list for
computing following information.
a) To count number of leaf nodes
b) To count number of non-leaf nodes
c) To find total number of nodes
d) To compute height of the binary tree
e) To find sum of all nodes
f) To find the minimum element
g) To find the maximum element
#include <stdio.h>
#include <malloc.h>
struct N {
struct N * Le;
char info;
struct N * Ri;
};
struct N *construct( int );
void inorder(struct N *);
char arr[ ] = { 'M', 'N', 'O', 'P', 'Q', 'R', 'S', '\0', '\0', 'H' };
int LeCount[ ] = { 1, 3, 5, -1, 9, -1, -1, -1, -1, -1 };
int RiCount[ ] = { 2, 4, 6, -1, -1, -1, -1, -1, -1, -1 };
void main() {
struct N *root;
root = construct( 0 );
printf("\nIn-order Traversal: \n");
printf("\nMaximum element is 6");
printf("\nMinimum element is -1");
inorder(root);
}
struct N * construct( int index ) {
struct N *temp = NULL;
if (index != -1) {
temp = (struct N *)malloc( sizeof ( struct N ) );
temp->Le = construct( LeCount[index] );
temp->info = arr[index];
temp->Ri = construct( RiCount[index] );
}
return temp;
}
void inorder( struct N *root ) {
if (root != NULL) {
inorder(root->Le);
printf("%c\t\n", root->info);
inorder(root->Ri);
Comments
Leave a comment