Tree top view problem doubt


tell me the problem in my code.

@Aryanomar00 hey aryan I made some changes in your code their was Issue in the class member variable was private.
corrected code. Now complete your code.

but there is no output shown .Please check.

@Aryanomar00 I have only removed the error in your code now it 's your task to complete the implementation.

i think the code is completed but no output so please tell me where i am lagging.

@Aryanomar00 hey aryan can you please share your implementation through cb.lk/ide this is the shortlink for coding blocks ide

I am not able to do please update my code .

please help me regarding my code.

@Aryanomar00 hey aryan the problem is that please use @ followed by my handle this will notify me that you have asked something and now problem with your code is that you have to build your tree level wise
node* buildTreeLevelWise(){

int d;
cin>>d;

node*root = new node(d);
queue<node*> q;
q.push(root);

while(!q.empty()){

	node*f = q.front();
	q.pop();
	int c1,c2;
	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 hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

why again theres a problem.wrong output.

@Aryanomar00
See this the code is well explained with comments

    #include<bits/stdc++.h>
    using namespace std;
    
    //Declared a Node for each tree
    class Node{
        public:
            int data;
            int index;
            Node* left;
            Node* right;
            Node(int d){
                data=d;
                left=NULL;
                right=NULL;
            }
    };
    
    //Function to print top view of a tree
    void topview(Node* root){
        if(root==NULL){
            return;
        }
        /*A queue is initialised to print the tree level wise in order to 
          print the top node at first.As the node at top level is vistied
          at first.
        */
        queue<Node*>q;
        map<int,int>m;
        int index=0;
        /*Initialise each node a index. So that the top most horizontal node
          is printed first*/
        root->index=0;
        q.push(root);
        while(q.size()){
            index=root->index;
            //m.count returns is there the index element present already
            if(m.count(index)==0){
                m[index]=root->data;
            }
            //We initalise the left of root index as index-1
            if(root->left){
                root->left->index=index-1;
                q.push(root->left);
            }
            //We initalise the right of root index as index+1
            if(root->right){
                root->right->index=index+1;
                q.push(root->right);
            }
            //Pop the top most node and explore the next node
            q.pop();
            //Initialise the current node as root
            root=q.front();
        }
         for(auto i=m.begin();i!=m.end();i++) 
        { 
            cout<<(i->second)<<" "; 
        } 
    }
    
    
    
    Node* createTree(Node* root){
        int d;
        cin>>d;
    
        root = new Node(d);
        queue<Node*> q;
        q.push(root);
    
        while(!q.empty()){
            
        	Node*f = q.front();
    	    q.pop();
    	    int c1,c2;
    	    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;
    }
     
    int main(){
        Node *root=NULL;
        root=createTree(root);
        topview(root);
        cout<<endl;
    }