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 is difference between abstraction and encapsulation?

0 Answers  


Write a program to reverse a string using recursive function?

0 Answers   TCS,


what are the different types of qualifier in java?

0 Answers   TCS,


What are the valid types of data that the main () can return in C/C++ language

3 Answers  


#include <stdio.h> #include <alloc.h> #include <stdlib.h> #include <conio.h> void insert(struct btreenode **, int); void inorder(struct btreenode *); struct btreenode { struct btreenode *leftchild; struct btreenode *rightchild; int data; }; main() { struct btreenode *bt; bt=(struct btreenode *)NULL; int req,i=1,num; clrscr(); printf("Enter number of nodes"); scanf("%d",&req); while(i<=req) { printf("Enter element"); scanf("%d",&num); insert(&bt,num); i++; } inorder(bt); } void insert(struct btreenode **sr, int num) { if(*sr==NULL) { *sr=(struct btreenode *)malloc (sizeof(struct btreenode)); (*sr)->leftchild=(struct btreenode *)NULL; (*sr)->rightchild=(struct btreenode *)NULL; (*sr)->data=num; return; } else { if(num < (*sr)->data) insert(&(*sr)->leftchild,num); else insert(&(*sr)->rightchild,num); } return; } void inorder(struct btreenode *sr) { if(sr!=(struct btreenode *)NULL) { inorder(sr->leftchild); printf("\n %d",sr->data); inorder(sr->rightchild); } else return; } please Modify the given program and add two methods for post order and pre order traversals.

0 Answers  






1. Strong name 2. how to prevent a class from being inherited 3. delegates 4. default modifier for interface 5. default modifier for class 6. base class for exception 7. diff bet trigger and view in sql 8. how to exchange values from one page to another page 9. can multiple catch block ll be executed at same time 10. can u store different data types in an array & array list 11. when we ll use trigger 12. try,catch,finally usage

2 Answers  


Write a program to find out the number of palindromes in a sentence.

1 Answers   TCS,


What is polymorphism in oops?

0 Answers  


how can we design a magic square in c++?or suggest me the basic idea of it.

3 Answers  


what is the difference between javap and jad utility

1 Answers   Wipro,


What is the important feature of inheritance?

0 Answers   BPL,


What is difference between new and malloc?

7 Answers   emc2,


Categories