HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppModerate

Building a parent/child tree relationship

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
parentbuildingchildtreerelationship

Problem

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 children;
};


I write a comparison function for a sort :

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


a function to get the parent:

item *getTopParent(const char src_data[][2], int len)
{
    for (int i = 0; i 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 value)
        {
            item* child = new item;
            child->value = src_data[i][1];
            parent->children.push_back(child);
            parentGotChildren = true;
        }
    }
    parent->children.sort(compareChild);

    return parentGotChildren;
}

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

    while (hasGotAChild && (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_da

Solution

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.

Context

StackExchange Code Review Q#69608, answer score: 13

Revisions (0)

No revisions yet.