/******************************************************************************
Trees Concepts ! Height of a tree and other traversals as discussed in class
@Mukesh Mann dated 13-04-2020
*******************************************************************************/
/******************************************************************************
*******************************************************************************/
#include <stdio.h>
typedef struct node
{
int data;
struct node *left;
struct node *right;
}Node;
// Lets try to creat a root node first
Node * create()
{
/* Sample Tree -
5
/ \
10 15
/
20
*/
int x;
Node *newnode=(Node*)malloc(sizeof(Node));
printf("Enter the data for the nodes(-1 for no data)");
scanf("%d", &x);
if(x==-1)
{
//printf("You have not entered any data");
return NULL;
}
newnode->data=x;
printf("Enter the left child data rooted at %d \n",newnode->data);
newnode->left=create();
printf("Enter the right child data rooted at %d\n",newnode->data);
newnode->right=create();
return (newnode);
}
void traverse(Node *t)
{
if(!t==NULL)
{
printf("Left child data is %d ",t->data);
//printf("Left child data is %d",t->left->data);
//printf("right child data is %d",t->left->data);
traverse(t->left);
t=t->right;
}
if(!t==NULL)
{
printf("Right child data is %d\n",t->data);
traverse(t->right);
}
}
int H(Node *t)
{
int l; int r; int max;
if(!t)
{
return 0;
}
else if( !(t->left) && !(t->right) )
{
return 0;
}
else
{
l=H(t->left);
r=H(t->right);
int p= l>r?l:r;
printf("Height is %d\n ",p);
return(1+p);
}
}
int main()
{
int height;
Node *root=create();
printf("Root child is %d\n ",root->data);
traverse(root);
height= H(root);
printf(" The height is %d " , height);
Inorder(t);
printf(" is the order traversal \n\n" );
return 0;
}
Trees Concepts ! Height of a tree and other traversals as discussed in class
@Mukesh Mann dated 13-04-2020
*******************************************************************************/
/******************************************************************************
*******************************************************************************/
#include <stdio.h>
typedef struct node
{
int data;
struct node *left;
struct node *right;
}Node;
// Lets try to creat a root node first
Node * create()
{
/* Sample Tree -
5
/ \
10 15
/
20
*/
int x;
Node *newnode=(Node*)malloc(sizeof(Node));
printf("Enter the data for the nodes(-1 for no data)");
scanf("%d", &x);
if(x==-1)
{
//printf("You have not entered any data");
return NULL;
}
newnode->data=x;
printf("Enter the left child data rooted at %d \n",newnode->data);
newnode->left=create();
printf("Enter the right child data rooted at %d\n",newnode->data);
newnode->right=create();
return (newnode);
}
void traverse(Node *t)
{
if(!t==NULL)
{
printf("Left child data is %d ",t->data);
//printf("Left child data is %d",t->left->data);
//printf("right child data is %d",t->left->data);
traverse(t->left);
t=t->right;
}
if(!t==NULL)
{
printf("Right child data is %d\n",t->data);
traverse(t->right);
}
}
int H(Node *t)
{
int l; int r; int max;
if(!t)
{
return 0;
}
else if( !(t->left) && !(t->right) )
{
return 0;
}
else
{
l=H(t->left);
r=H(t->right);
int p= l>r?l:r;
printf("Height is %d\n ",p);
return(1+p);
}
}
int main()
{
int height;
Node *root=create();
printf("Root child is %d\n ",root->data);
traverse(root);
height= H(root);
printf(" The height is %d " , height);
Inorder(t);
printf(" is the order traversal \n\n" );
return 0;
}