Building a parent/child tree relationship I have a problem: build a tree relationship parent/child like this one:



Can you review this piece of code please since I am convinced it could be improved?

struct item {
    char value;
    std::list&lt;item*&gt; children;
};


I write a comparison function for a sort :

bool compareChild(const item *first, const item *second)
{
    return ( first-&gt;value &lt; second-&gt;value );
}


a function to get the parent:

item *getTopParent(const char src_data[][2], int len)
{
    for (int i = 0; i &lt; len; ++i)
    {
        if (src_data[i][0] == '\0')
        {
            item* head = new item;
            head-&gt;value = src_data[i][1];
            return head;
        }
    }
    return NULL;
}


Two functions to push the children:

bool PushChildren(const char src_data[][2], int len, item *parent)
{
    bool parentGotChildren = false;

    for (int i = 0; i &lt; len; ++i)
    {
        if (src_data[i][0] == parent-&gt;value)
        {
            item* child = new item;
            child-&gt;value = src_data[i][1];
            parent-&gt;children.push_back(child);
            parentGotChildren = true;
        }
    }
    parent-&gt;children.sort(compareChild);

    return parentGotChildren;
}

bool PushChildrenforAParent(const char src_data[][2], item *parent, int len)
{
    bool hasGotAChild = true;
    std::list&lt;item*&gt;::iterator itchild = parent-&gt;children.begin();
    std::list&lt;item*&gt;::iterator childLast = parent-&gt;children.end();

    while (hasGotAChild &amp;&amp; (itchild != childLast))
    {
        hasGotAChild = PushChildren( src_data, len, *itchild);
        if (hasGotAChild)
        {
            hasGotAChild = PushChildrenforAParent( src_data, *itchild, len);
        }
        ++itchild;
    } 
    return hasGotAChild;
}


A function to initialise the "tree":

item* initialize_tree(const char src_data[][2], int len)
{
    bool hasTopParentGotAChild = false;
    //initialisation of the head
    item *head = getTopParent( src_data, len);

    if (head != NULL)
    {
        hasTopParentGotAChild = PushChildren( src_data, len, head);

        if (head != NULL)
        {
            if (hasTopParentGotAChild)
            {
                PushChildrenforAParent(src_data, head, len);
            }
            return head;
        }
        else 
        {
            return NULL;
        }
    }
    return NULL;
}
Some quick comments:


Your code style is all over the place. You have three different styles for function names: initialize_tree, PushChildren and getTopParent, pick one and stick to it. I would recommend the last one.
Your code looks more like C and not C++ with what I presume to be free functions.
item is an inappropriate name, a more typical name is Node.
You really should push the functionality to modify the nodes, or tree in general onto the item type and turn it into a class using member functions (methods).
You are allocating memory with new but never freeing it with delete thus you are leaking memory like it's 1995.
You are handling raw pointers, this is not recommended. Prefer to use the std containers or smart pointers for managing memory.
If you feel that you need to explain something to the reader, that's usually a hint that your code needs better structure. hint
std::list is slow, prefer to use std::vector on modern CPUs. Vector is \$\mathcal{O}(n)\$ insertion and removal at the front while list is \$\mathcal{O}(1)\$. However the cost of poor locality of reference on the list means that the cross-over point when the list becomes faster than the vector for just about anything at all is when you have about 100-1000 elements (depending on size) and are hitting the worst case for vector. Which I doubt you won't reach in your program. So unless you have a very specific reason to use std::list prefer a std::vector.