Segmentation Fault

Ambuj,… There are few errors in your code… Firstly your code is not producing any sort of output… So Firstly I would suggest you to change the buildtree function firstly… as it is given in the question that you need to build the tree using queue… So use the approach as :

node* build_tree()
{
int data;
queue<node*> q;
cin>>data;
node* root=new node(data);
q.push(root);
int c1,c2;

while(!q.empty())
{
    node *f=q.front();
    q.pop();
    cin>>c1>>c2;
    if(c1!=-1)
    {
        f->left=new node(c1);
        q.push(f->left);
    }
    if(c2!=-1)
    {
        f->right=new node(c2);
        q.push(f->right);
    }
}
return root;

}

I applied this buildtree() function in my code but the code is not producing any output. Could you please help me with this.

Pls share the edited code… With this buildtree function so that I could make changes in your code…

Hello @yuktimutreja01
Please refer for the edited code: -

Ambuj, the approach you are trying to use is not correct… You need to use the approach of horizontal distance in your code to print the top view as,
void topview(node *root)
{
if(root==NULL)
{
return;
}
queue<node *>q;
map<int,int> m;
int hd=0;
root->hd=hd;
q.push(root);
while(q.size())
{
hd=root->hd;
if(m.count(hd)==0)
{
m[hd]=root->data;
}
if(root->left)
{
root->left->hd=hd-1;
q.push(root->left);
}
if(root->right)
{
root->right->hd=hd+1;
q.push(root->right);
}
q.pop();
root=q.front();
}
for(auto i=m.begin(); i!=m.end();i++)
{
cout<second<<" ";
}
}

This approach is well explained in the tutorial link as : https://youtu.be/LtO96Ici8kc