Check out example codes for "gfg top view of tree". It will help you in understanding the concepts better.

Code Example 1

/* This is not the entire code. It's just the function which implements 
   bottom view. You need to write required code. */

// Obj class is used to store node with it's distance from parent.
class Obj
        Node *root;
        int dis; // distance from parent node. distance of root node will be 0.

        Obj(Node *node, int dist)
            root = node;
            dis = dist;

void topView(Node *root)
    queue<Obj*> q;
    q.push(new Obj(root, 0));
    map<int,int> m;

        Obj *ob = q.front();
      	/* insert node of unique distance from parent node. ignore repitation 
           of distance. */
        if(m.find(ob->dis) == m.end())
            m[ob->dis] = ob->root->data;

        if(ob->root->left != NULL)
            q.push(new Obj(ob->root->left, ob->dis-1)); 
        if(ob->root->right != NULL)
            q.push(new Obj(ob->root->right, ob->dis+1));

  	// printing nodes.
    for(auto it=m.begin(); it!=m.end(); it++)
        cout << it->second << "\t";

    cout << endl;

Learn ReactJs, React Native from