Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to build a tree structure in C++ using std::map

Tags:

c++

tree

map

I am trying to write a tree sort of structure in C++. As in every tree there are branches and leaves. A branch can contain other branches as well as leaves. Now my implementation calls for each branch and leaf to have different functionalities. So for example. Take the tree structure

                                          Root                                             
                                |                          |                             
                             Branch1                     Branch2                     Branch3
      |                |                |
    Leaf1            Leaf2           Branch4

Now Each Leaf and branch has a different function to execute so Leaf1 will have a function called leaf1_func, Leaf2 will have leaf2_func, Branch4 has Branch4_func.

I was initially trying to implement composite design pattern. But that means I would have as many classes as leafs. But since I have tons of leaves and branches I would like to avoid creates more classes. I realize this is an unusual situation but was hoping somebody could help me in this regard. What would be the best way to implement this tree without creating too many classes.

i am using map STL container to store datas as well, i want to use this tree implementation to solve this in TSP problem.

#include <cstdlib>
#include <iostream>
#include <map>

using namespace std;

int n=4;
int min=1, max=10;




struct graph
{
int nodes;//total no. of nodes or vertices namely cities
std::map<std::pair<int,int>, int> graphMap;//an object that links a pair of vertices   
};


void directed_Graph(graph);

void directed_Graph(graph G)
{
//int n = G->nodes; //city count
int i, j;
for(i = 0; i <= n-1; i++)
{
    for(j = 0; j <= n-1; j++)
    {
        if(i!=j)
        {
            G.graphMap[std::make_pair(i,j)] = (rand()%10)+1;
            //cout<<G.graphMap[std::make_pair(i,j)]<<"\n";
        }

        else
        {
            G.graphMap[std::make_pair(i,j)] = 0;
        }

    }
}
}


int main(int argc, char** argv) 
{
graph g;
g.nodes = 4;

directed_Graph(g);

return 0;
}
like image 236
visanio_learner Avatar asked Nov 13 '22 05:11

visanio_learner


1 Answers

Different functions with the same signature do still have the same type. Even if the functions are completely unrelated, you can have a tree that stores random data by using void * (type erasure) and then typecast back to the known actual type after the leaf node is reached.

struct node {
    void (*fptr)(); // pointer to any function taking no args, no return
    void *data; // pointer to anything, need to cast before using
};

void f() { std::cout << "hello"; }
void g() { std::cout << "world"; }

node a = { f, f };
node b = { g, g };

a.fptr();
static_cast< void (*)() >( b.data )();

You could also use virtual methods with inheritance and store pointers to base class type in the tree. It depends on what the nodes really are.

None of this is really related to the fact it goes into a graph.

like image 63
Potatoswatter Avatar answered Dec 22 '22 04:12

Potatoswatter