Dec 30, 2008

Given an n-ary tree represented as a list of items, containing a node and its parent, construct the n-nary tree from it

Given an n-ary tree represented as a list of items, containing a node and its parent, construct the n-nary tree from it

for(node* it=list->head;it!=NULL;it=it->next)
{
if(it->parent!=NULL)
it->parent->addToChildrenList(it);
}

or
Hash the nodes based on their parent pointer values. Then iterate through the list and for each node, get the hash value of its own pointer. All the nodes that are in the list present in that hash slot are its children

No comments:

Post a Comment