QUES-A binary tree of depth......i m lazy as u
ANS-
#include<iostream>
#include <exception>
using namespace std;
#include<string>
#include<stdlib.h>
bool ERR_Flag=false;
template<class T>
//Set this global error flag to true when there is no
//left/right child for a given node
class AC_BinaryTree
{
int no_Of_Ele;
T *ele;
int capacity;
public:
AC_BinaryTree();
bool isEmpty();
bool isFull();
void insert(T);
T leftChild(int pos);
T rightChild(int pos);
T parent(int pos);
};
template < class T >
AC_BinaryTree < T > :: AC_BinaryTree()
{
ele=new T[20];
no_Of_Ele=0;
capacity=20;
}
template < class T >
bool AC_BinaryTree < T > :: isEmpty()
{
return(!no_Of_Ele);
}
template < class T >
bool AC_BinaryTree < T > :: isFull()
{
return(no_Of_Ele==20);
}
template < class T >
void AC_BinaryTree < T > :: insert(T data)
{
if(no_Of_Ele < 20)
ele[no_Of_Ele++]=data;
else
cout<<"Tree full insertion cannot be made";
}
template < class T >
T AC_BinaryTree < T > :: leftChild(int pos)
{
if(2*pos+1 < no_Of_Ele)
{
ERR_Flag=false;
return(ele[2*pos+1]);
}
else
{
ERR_Flag=true;
cout<<"No left child";
}
return(ele[0]);
}
template < class T >
T AC_BinaryTree < T > :: rightChild(int pos)
{
if(2*pos+2 < no_Of_Ele)
{
ERR_Flag=false;
return(ele[2*pos+2]);
}
else
{
ERR_Flag=true;
cout<<"No right child";
}
return(ele[0]);
}
template < class T >
T AC_BinaryTree < T > :: parent(int pos)
{
return(ele[(pos-1)/2]);
}
//end
int main(){
int d_Choice, pos;
cin>>d_Choice;
int val;
string val1;
AC_BinaryTree<int> g;
int data;
AC_BinaryTree<string> g1;
string data1;
if(d_Choice == 1){
while(1){
int opt_Choice;
cin>>opt_Choice;
if(opt_Choice==1)
{
if(g.isEmpty())
cout<<"Empty"<<endl;
else
cout<<"Not empty"<<endl;
}
else if(opt_Choice==2)
{
if(g.isFull())
cout<<"Full"<<endl;
else
cout<<"Not full"<<endl;
}
else if(opt_Choice==3){
cin>>data;
g.insert(data);
}
else if(opt_Choice==4){
cin>>pos;
val = g.leftChild(pos);
if(!ERR_Flag)
cout<<val<<endl;
}
else if(opt_Choice==5){
cin>>pos;
val = g.rightChild(pos);
if(!ERR_Flag)
cout<<val<<endl;
}
else if(opt_Choice==6){
cin>>pos;
val = g.parent(pos);
cout<<val<<endl;
}
else if(opt_Choice==7)
exit(1);
}
}
if(d_Choice == 2){
while(1){
int opt_Choice;
cin>>opt_Choice;
if(opt_Choice==1)
{
if(g1.isEmpty())
cout<<"Empty"<<endl;
else
cout<<"Not empty"<<endl;
}
else if(opt_Choice==2){
if(g1.isFull())
cout<<"Full"<<endl;
else
cout<<"Not full"<<endl;
}
else if(opt_Choice==3){
cin>>data1;
g1.insert(data1);
}
else if(opt_Choice==4){
cin>>pos;
val1 = g1.leftChild(pos);
if(!ERR_Flag)
cout<<val1<<endl;
}
else if(opt_Choice==5){
cin>>pos;
val1 = g1.rightChild(pos);
if(!ERR_Flag)
cout<<val1<<endl;
}
else if(opt_Choice==6){
cin>>pos;
val1 = g1.parent(pos);
cout<<val1<<endl;
}
else if(opt_Choice==7)
exit(1);
}
}
}
ANS-
#include<iostream>
#include <exception>
using namespace std;
#include<string>
#include<stdlib.h>
bool ERR_Flag=false;
template<class T>
//Set this global error flag to true when there is no
//left/right child for a given node
class AC_BinaryTree
{
int no_Of_Ele;
T *ele;
int capacity;
public:
AC_BinaryTree();
bool isEmpty();
bool isFull();
void insert(T);
T leftChild(int pos);
T rightChild(int pos);
T parent(int pos);
};
//start
template < class T >
AC_BinaryTree < T > :: AC_BinaryTree()
{
ele=new T[20];
no_Of_Ele=0;
capacity=20;
}
template < class T >
bool AC_BinaryTree < T > :: isEmpty()
{
return(!no_Of_Ele);
}
template < class T >
bool AC_BinaryTree < T > :: isFull()
{
return(no_Of_Ele==20);
}
template < class T >
void AC_BinaryTree < T > :: insert(T data)
{
if(no_Of_Ele < 20)
ele[no_Of_Ele++]=data;
else
cout<<"Tree full insertion cannot be made";
}
template < class T >
T AC_BinaryTree < T > :: leftChild(int pos)
{
if(2*pos+1 < no_Of_Ele)
{
ERR_Flag=false;
return(ele[2*pos+1]);
}
else
{
ERR_Flag=true;
cout<<"No left child";
}
return(ele[0]);
}
template < class T >
T AC_BinaryTree < T > :: rightChild(int pos)
{
if(2*pos+2 < no_Of_Ele)
{
ERR_Flag=false;
return(ele[2*pos+2]);
}
else
{
ERR_Flag=true;
cout<<"No right child";
}
return(ele[0]);
}
template < class T >
T AC_BinaryTree < T > :: parent(int pos)
{
return(ele[(pos-1)/2]);
}
//end
int main(){int d_Choice, pos;
cin>>d_Choice;
int val;
string val1;
AC_BinaryTree<int> g;
int data;
AC_BinaryTree<string> g1;
string data1;
if(d_Choice == 1){
while(1){
int opt_Choice;
cin>>opt_Choice;
if(opt_Choice==1)
{
if(g.isEmpty())
cout<<"Empty"<<endl;
else
cout<<"Not empty"<<endl;
}
else if(opt_Choice==2)
{
if(g.isFull())
cout<<"Full"<<endl;
else
cout<<"Not full"<<endl;
}
else if(opt_Choice==3){
cin>>data;
g.insert(data);
}
else if(opt_Choice==4){
cin>>pos;
val = g.leftChild(pos);
if(!ERR_Flag)
cout<<val<<endl;
}
else if(opt_Choice==5){
cin>>pos;
val = g.rightChild(pos);
if(!ERR_Flag)
cout<<val<<endl;
}
else if(opt_Choice==6){
cin>>pos;
val = g.parent(pos);
cout<<val<<endl;
}
else if(opt_Choice==7)
exit(1);
}
}
if(d_Choice == 2){
while(1){
int opt_Choice;
cin>>opt_Choice;
if(opt_Choice==1)
{
if(g1.isEmpty())
cout<<"Empty"<<endl;
else
cout<<"Not empty"<<endl;
}
else if(opt_Choice==2){
if(g1.isFull())
cout<<"Full"<<endl;
else
cout<<"Not full"<<endl;
}
else if(opt_Choice==3){
cin>>data1;
g1.insert(data1);
}
else if(opt_Choice==4){
cin>>pos;
val1 = g1.leftChild(pos);
if(!ERR_Flag)
cout<<val1<<endl;
}
else if(opt_Choice==5){
cin>>pos;
val1 = g1.rightChild(pos);
if(!ERR_Flag)
cout<<val1<<endl;
}
else if(opt_Choice==6){
cin>>pos;
val1 = g1.parent(pos);
cout<<val1<<endl;
}
else if(opt_Choice==7)
exit(1);
}
}
}
This is clearly not going to work. You should test your codes.
ReplyDeleteActually it does work
DeleteTry typing correctly
Although it might not satisfy all test cases