Concatenated list and tree

I need aid for this exercise. The function BuildT must use the following parameters nodeE* buildT(node* T, nodeE* C) and as a struct I must use the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
nodoE* conc(nodo* T, nodoE* C) {
    if (!C)  return new nodoE(T, 0);
    else
    {
        C->next = conc(T, C->next);
        return C;
    }
}
nodoE* buildT(nodo* T, nodoE* C) {
    if (!T) return C;
    else {
        buildT(T->left, C);
        buildT(T->right, C);
        nodoE* x = conc(T, C);
        return x;
    }
}

The delivery is this:
Given a binary tree T with nodes of the usual type, struct node{int info; node*left,*right;}; we want to
construct a concatenated C list of nodeE nodes as follows: struct nodeE{node*info; nodeE* next;};. The
list C must have as many nodes as there are nodes of T, and the nodes of C point to the nodes of T according to the postfix order of the nodes of T.
order, i.e. first the left subtree, then the right subtree and finally the root.

I solved it this way and in theory the error is that the first invocation does not pass the new C to the second.The conc function was made by me and is fully modifiable. The code I wrote is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
nodoE* conc(nodo* T, nodoE* C) {
    if (!C)  return new nodoE(T, 0);
    else
    {
        C->next = conc(T, C->next);
        return C;
    }
}
nodoE* buildT(nodo* T, nodoE* C) {
    if (!T) return C;
    else {
        buildT(T->left, C);
        buildT(T->right, C);
        nodoE* x = conc(T, C);
        return x;
    }
}


Last edited on
look at conc...
you pass in a tree and a list, T and C, as best as I can tell.
if c is not null,
you modify next. this seems incorrect, what if the list had 5 items in it at the time of the call?
I think you just want

else return(conc(T, C->next)); //maybe? or am I missing something?

Last edited on
1
2
3
4
nodoE* buildT(nodo* T, nodoE* C) {
    return T ? buildT(T->left, buildT(T->right, new nodoE(T, C)))
             : C;
}

Topic archived. No new replies allowed.