Write a function to find the depth of a binary tree.

Answer Posted / prakashkumarng

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
typedef struct linkedlist
{
int data;
struct linkedlist *left,*right;
}node;
node *makenode(int);
void setleft(node *,int);
void setright(node *,int);
void preorder(node *);
void postorder(node *);
void inorder(node *);
int depth(node *,int);
node *q,*k;
int l=0,d=1;
void main()
{
node *root,*p,*q;
int x,f;
clrscr();
printf("\nEnter the root :");
scanf("%d",&x);
root=makenode(x);
printf("\nEnter the data(-1 to stop) :");
scanf("%d",&x);
while(x!=-1)
{
p=root;
while(p!=NULL)
{
q=p;
if(x<p->data)
{
p=p->left;
}
else
{
p=p->right;
}
}
if(x<q->data)
{
setleft(q,x);
}
else
{
setright(q,x);
}
printf("\nEnter the data(-1 to stop) :");
scanf("%d",&x);
}
printf("\nPreorder traversal is \n");
preorder(root);
printf("\nPostorder traversal is \n");
postorder(root);
printf("\nInorder traversal is \n");
inorder(root);
printf("\nDepth of the tree is \n");
f=depth(root,l);
printf("%d",f+1);
getch();
}
node *makenode(int x)
{
node * temp;
temp=(node *)malloc(sizeof(node));
temp->data=x;
temp->left=NULL;
temp->right=NULL;
return(temp);
}
void setleft(node *p,int x)
{
node *temp;
if(p==NULL)
{
printf("Error");
}
temp=makenode(x);
p->left=temp;
}
void setright(node *p,int x)
{
node *temp;
if(p==NULL)
{
printf("Error");
}
temp=makenode(x);
p->right=temp;
}
void inorder(node *p)
{
if(p!=NULL)
{
inorder(p->left);
printf("%d\t",p->data);
inorder(p->right);
}
}
void postorder(node *p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
printf("%d\t",p->data);
}
}
void preorder(node *p)
{
if(p!=NULL)
{
printf("%d\t",p->data);
preorder(p->left);
preorder(p->right);
}
}
int depth(node *p,int l)
{
if(p!=NULL)
{
if(l>d)
d=l;
depth(p->left,l+1);
depth(p->right,l+1);
}
return d;
}

Is This Answer Correct ?    48 Yes 19 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Set up procedure for generating a wire frame display of a polyhedron with the hidden edges of the object drawn with dashed lines

2993


Given a spherical surface, write bump-mapping procedure to generate the bumpy surface of an orange

2827


how to programme using switch statements and fuctions, a programme that will output two even numbers, two odd numbers and two prime numbers of the users chioce.

2118


can you use proc sql to manpulate a data set or would u prefer to use proc report ? if so why ? make up an example and explain in detail

2293


create a C-code that will display the total fare of a passenger of a taxi if the driver press enter,the timer will stop. Every 10 counts is 2 pesos. Initial value is 25.00

6279






write a c program to input initial & final time in the format hh:mm and find the time intervel between them? Ex inputs are initial 06:30 final 00:05 and 23:22 final 22.30

2195


To Write a C program to remove the repeated characters in the entered expression or in entered characters(i.e) removing duplicates. String contains only lowercase characters ['a'-'z']

468


why do you use macros? Explain a situation where you had to incorporate macros in your proc report? use a simple instream data example with code ?

2222


What is full form of PEPSI

1830


What is data _null_? ,Explain with code when u need to use it in data step programming ?

2793


how to test pierrot divisor

2230


Write a program to model an exploding firecracker in the xy plane using a particle system

3661


Write a Program in 'C' To Insert a Unique Number Only. (Hint: Just Like a Primary Key Numbers In Database.) Please Some One Suggest Me a Better Solution for This question ??

1747


3) Int Matrix of certain size was given, We had few valu= es in it like this. =97=97=97=97=97=97=97=97=97=97=97 1 = | 4 | | 5 | &= nbsp; | 45 =97=97=97=97=97=97=97=97=97=97=97 &n= bsp; | 3 | 3 | 5 | = | 4 =97=97=97=97=97=97=97=97=97=97=97 34 |&nbs= p; 3 | 3 | | 12 | &= nbsp; =97=97=97=97=97=97=97=97=97=97=97 3 | &nbs= p; | 3 | 4 | = | 3 =97=97=97=97=97=97=97=97=97=97=97 3 | = ; | | | = ; 3 | =97=97=97=97=97=97=97=97=97=97=97 &= nbsp; | | 4 | = ; | 4 | 3 We w= ere supposed to move back all the spaces in it at the end. Note: = If implemented this prog using recursion, would get higher preference.

3297


Write a routine to implement the polymarker function

4350