Inserting nodes into a tree

Steven Schveighoffer schveiguy at yahoo.com
Fri Feb 11 11:33:04 PST 2011


On Fri, 11 Feb 2011 14:28:47 -0500, %u <no at spam.com> wrote:

> Please pardon my complete lack of knowledge. Please provide some
> suggestions/pointers so that I can improve myself.
>
> Given a table containing three values (ie, myName, myId, parentId),
> how does one insert those values into a tree such that the
> parent/child relationship defined in the table is maintained?
> Basically I'm given a file containing parents and children in no
> particular order. I would like to print all children of a given
> parent regardless of where they fall on the tree.
>
> Note, this is not a school related question, simply a retard trying
> to learn how to do things more efficiently.

With a many-to-one relationship of parent to child, I'd suggest each tree  
node having a array of child node pointers:

struct Node
{
    int id;
    string myName;
    Node *parent; // only needed if you want to go up the tree.
    Node *[] children;
}

-Steve


More information about the Digitalmars-d-learn mailing list