Graph

Hello.
I've got a problem here...
So yea, this time i have to make a "tree" and compare few things in it. The problem is, its not a binary tree. Each parent can have N amount of child. So...
Basicly, i have to make something like this.
http://prntscr.com/9nck53

and the task is to make sure that children value sum must be equal to parents value. Like if you check out the picture, 1st knots value is 150. Its children values are 30 and 120. 30+120 = 150 so that's correct.

Here's how i'm trying to implement this:

http://prntscr.com/9ncjts

And this is how it looks:
http://prntscr.com/9ncjws

Basicly in horizontal list im saving the knots. And then just adding elements to it verticaly which will point to the knots children knots.

This seems complicated for me so i was wondering if anyone has any idea how to implement this the "easy way". Once again only libs i'm allowed to use are iostream and fstream.

The input file looks like this:
2// Represents how many roots the tree has.
1 150//Adding the 1st root.
2 300//adding the 2nd root.
1 11 30//Adding to 1st root a knot with ID 11 and value 30.
1 12 120
11 111 10// Adding to the knot with ID 11 children with id 111 and value 10.
11 112 20
12 121 30
12 122 40
12 123 50


And this is how far ive come
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  #include <fstream>
#include <iostream>
using namespace std;

struct link// Here's the problem. 
{
    int LinkID;
    link* lower;
    node* next;// <------------- this !!
    link(int ID){LinkID = ID; lower = NULL; next = NULL;};
};
struct node
{
    int KnotMinistry;
    int KnotID;
    int KnotValue;
    node *next;
    link *lower;
    node(int ministry, int ID, int value) {KnotMinistry = ministry; KnotID = ID; KnotValue = value; next = NULL; lower = NULL;};
};
class List
{
    node *first;
    node *last;
    link *lowest;
    public:
        List(){first = NULL;};


    void AddKnot(int ministry, int ID, int value)//Adds knots. 
    {
        node *n = new node(ministry, ID, value);
        if(first == NULL)
        {
            first = last = n;
        }
        else
        {
            last->next = n;
            last = n;
        }
    }
    void LinkKnots(int Parent, int Knot)//Adds the vertical nodes and points them to children knots. 
    {
        link *k = new link(Knot);
        for(node *n = first; n!=NULL; n=n->next)
        {
            if(n->KnotID == Parent)
            {
                if(n->lower == NULL)
                {
                    n->lower = k;
                    lowest = k;
                    for(node *g = first; g!=NULL; g=g->next)
                    {
                        if(g->KnotID == Knot)
                        {
                            k-> next = g;//<------ the problem. 
                            break;
                        }
                    }
                }
                else
                {
                    lowest ->lower = k;
                    lowest = k;
                    for(node *g = first; g!=NULL; g=g->next)
                    {
                        if(g->KnotID == Knot)
                        {
                            k-> next = g;
                            break;
                        }
                    }
                }
            }
        }
    }
    void PrintList()
    {
        for(node *n = first; n!=NULL; n=n->next)
        {
            cout << n->KnotMinistry <<" "<<n->KnotID<<" "<< n->KnotValue<< endl;
        }
    }




};

int main()
{
    int ministrijas = 0;
    int Knot = 0;
    int Budget = 0;
    int Ministry = 0;
    int Parent = 0;
    ifstream fin;
    ofstream fout;
    List h;
    fin.open("budget.i1");
    fout.open("budget.o1");
    fin >> ministrijas;
    for(int i=0; i < ministrijas; i++)
    {
        fin >> Knot >> Budget;
        h.AddKnot(Ministry, Knot, Budget );
    }
    while(fin>>Parent>>Knot>>Budget)
    {
        h.AddKnot(Parent, Knot, Budget);
        h.LinkKnots(Parent, Knot);

    }
    h.PrintList();
}


The thing is, i have to point both ways. From node to Link and from Link to node. The problem is, it wont let me to. If i define the Link structure before the node structure, I obvi can't use the node inside the Link. The same thing if i swap them around. both the vertical and horizontal node points to 2 other nodes but the nodes has to be different as they hold different information, so i dont think i could merge Node with Link....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct node;

struct link// Here's the problem. 
{
    int LinkID;
    link* lower;
    node* next;// <------------- this !!
    link(int ID){LinkID = ID; lower = NULL; next = NULL;};
};

struct node
{
    int KnotMinistry;
    int KnotID;
    int KnotValue;
    node *next;
    link *lower;
    node(int ministry, int ID, int value) {KnotMinistry = ministry; KnotID = ID; KnotValue = value; next = NULL; lower = NULL;};
};
Topic archived. No new replies allowed.