Write a program to get the binary tree.

Answers were Sorted based on User's Feedback



Write a program to get the binary tree...

Answer / r.r.bharti

Simple way:

public int FindDepthOfTree(RBNode n)
{
if (n == null) return 0;
return Math.Max(FindDepthOfTree(n.LeftNode),
FindDepthOfTree(n.RightNode)) + 1;
}

Is This Answer Correct ?    1 Yes 0 No

Write a program to get the binary tree...

Answer / vamshi koduri

#include<iostream>
using namespace std;
typedef struct node
{
int d;
struct node *l,*r;
}node;
class bt
{
public:
int h,lev;
node *root;
node *create();
int height(node *);
void printlevel(node *,int lev);
void givenlevel();
void inorder(node *);
void preorder(node *);
void postorder(node *);
}a;
node *bt::create()
{
struct node *n;
n=new node;
int x;
cout<<"enter element
";
cin>>x;
if(x==-1)
return NULL;
n->d=x;
n->l=n->r=NULL;
cout<<"enter left "<<n->d<<ends;
n->l=create();
cout<<"enter right "<<n->d<<ends;
n->r=create();
return n;
}
void bt::inorder(node *p)
{
if(p!=NULL)
{
inorder(p->l);
cout<<p->d<<ends;
inorder(p->r);
}
}
void bt::preorder(node *q)
{
if(q!=NULL)
{
cout<<q->d<<ends;
preorder(q->l);
preorder(q->r);
}
}
void bt::postorder(node *q)
{
if(q!=NULL)
{
postorder(q->l);
postorder(q->r);
cout<<q->d<<ends;
}
}
int main()
{
a.root=a.create();
cout<<"levelorder: ";
a.givenlevel();
cout<<endl;
cout<<"inorder : ";
a.inorder(a.root);
cout<<endl;
cout<<"preorder : ";
a.preorder(a.root);
cout<<endl;
cout<<"postorder : ";
a.postorder(a.root);
cout<<endl<<"height: ";
cout<<a.height(a.root);
return 0;
}
int bt::height(node *p)
{
if(p==NULL)
return 0;
int l=height(p->l);
int r=height(p->r);
if(l>r)
h=1+l;
else
h=1+r;
return h;
}
void bt::givenlevel()
{
int p=height(root);
for(int i=1;i<=h;i++)
printlevel(root,i);
}
void bt::printlevel(node *p,int lev)
{
if(p==NULL)
return;
if(lev==1)
cout<<p->d<<ends;
if(lev>1)
{
printlevel(p->l,lev-1);
printlevel(p->r,lev-1);
}
}

Is This Answer Correct ?    0 Yes 0 No

Write a program to get the binary tree...

Answer / vijayan

#include<iostream.h>

Is This Answer Correct ?    4 Yes 21 No

Post New Answer

More OOPS Interview Questions

Plese get me a perfect C++ program for railway/airway reservation with all details.

0 Answers   ITM,


How to hide the base class functionality in Inheritance?

0 Answers   Viscus Infotech,


What are the different forms of polymorphism??

8 Answers   Mantaq, NUC, PCS,


What does <> mean pseudocode?

0 Answers  


What does oop mean in snapchat?

0 Answers  






How to handle exception in c++, For example in a functions i am assigning memory to some variables and also in next instructions in am dividing one variable also. If this functions generates a error while allocating memory to those variable and also while dividing the variable if i divide by zero then catch block how it will identify that this error has cone from perticular instruction

0 Answers  


What is object-oriented programming? Webopedia definition

0 Answers  


They started with the brief introduction followed by few basic C++ questions on polumorphism, inheritance and then virtual functions. What is polymorphims? How you will access polymorphic functions in C? How virtual function mechanism works?

0 Answers  


Prepare me a program for the animation of train

0 Answers  


What is memory leak and memory corruption?

1 Answers   TCS,


what is the difference between virtual function and destoctor?

1 Answers  


Can anyone please explain runtime polymorphism with a real time example??at what ciscumstances we go for it??

1 Answers  


Categories