why the below where i have made the queue class myself is not working fine.the output showing only the nodes in the right subtree.why what is the mistake?
#include
//#include
using namespace std;
//implementing templated queue
template
struct queue{
t *arr;
int front;
int rear;
int cs;
int ms;
queue(int ds=10){
ms=ds;
arr=new t[ds];
front=0;
rear=ms-1;
cs=0;
}
bool empty(){
return cs==0;
}
bool full(){
return cs==ms;
}
void enqueue(t data){
if(!full()){
rear=(rear +1)%ms;
arr[front]=data;
cs++;}
}
void dequeue(){
if(!empty()){
front=(front+1)%ms;
cs–;
}
}
int getcs(){
return cs;
}
t getfront(){
return arr[front];
}
void display(){
for(int i=front;i<=rear;i++)
cout<<arr[i]<<" ";
cout<<endl;
}
};
struct node{
int data;
node *left;
node *right;
node(int data){
this->data=data;
left=NULL;
right=NULL;
}
};
node *buildtree(){
int data;
cin>>data;
if(data==-1)
return NULL;
node *root=new node(data);
root->left=buildtree();
root->right=buildtree();
return root;
}
void printpost(node *root){
if(root==NULL)
return;
printpost(root->left);
printpost(root->right);
cout<data<<" ";
}
void bfs(node *root){
//1.declare a queue and push the root node to the queue
queue<node *> q;
//q.enqueue(root);
q.enqueue(root);
//2.print and pop the front of the bqueue
while(!q.empty()){
node * no=q.getfront();
//cout<<no->data<<" ";
//q.dequeue();
q.dequeue();
cout<<no->data<<" ";
//3.add the children of the popped node into the queue
if(no->left)
q.enqueue(no->left);
if(no->right)
q.enqueue(no->right);
}
}
int main(){
node *root=buildtree();
printpost(root);//post order traversal
cout<<endl;
bfs(root);//level order traversal
return 0;
}