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

What do we mean by a hidden argument in C++?

1 Answers  


What is data binding?

4 Answers   Ittiam Systems,


how much classes are used in c++

5 Answers  


How is data security provided in Object Oriented languages? ?

5 Answers  


if u write a class do u write Assignment operator and copy constructor

1 Answers   Siemens,






What is oops concept with example?

0 Answers  


WHEN A COPY CONSTER IS CALL ?

4 Answers  


I hv a same function name,arguments in both base class and dervied class, but the return type is different. Can we call this as a function overloading? Explain?

3 Answers  


String = "C++ is an object oriented programming language.An imp feature of oops is classes and objects".Write a pgm to count the repeated words from this scenario?

0 Answers   HCL,


write a short note on Overloading of Binary Operator?

2 Answers  


What is overriding vs overloading?

0 Answers  


Why do we need polymorphism in c#?

0 Answers  


Categories