ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
info       Did you received any Funny E-Mails from your Friends and like to share with rest of our friends? Yeah!! you can post that stuff   HERE
Google
 
Categories >> Code-Snippets >> Programming-Code >> C-Code
 
 
 
Question
Write a function to find the depth of a binary tree.
 Question Submitted By :: =-PKG-=
I also faced this Question!!     Rank Answer Posted By  
 
Answer
#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;
}
 
2
Prakashkumarng
 
View All Answers
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com