Breadth-first traversal for top down tree

template<class BaseData>
inline void TDTree<BaseData>::breadthFirstTraversal(void(*processnode)(BaseData& value))
{
/*
std::queue< GtNode<BaseData>* > nodes2visit;

GtNode<BaseData> *item = GenTree<BaseData>::root;


if ( item )
{
nodes2visit.push( item );

while ( !nodes2visit.empty() )
{
item = nodes2visit.front();
nodes2visit.pop();
processnode( (item->info) );

if (item -> firstChild )
nodes2visit.push( item -> firstChild );
if (item -> sibling )
nodes2visit.push( item -> sibling );
}
}

*/
}

The comment on the breadth-first traversal is incorrect. It only works for Binary Tree but not Top-Down Tree.

I want these inputs ['H', 'i', ' ', 't', 'h', 'E', 'r', 'e'] into a breadth-first traversal when it called the function (Read every level from left to right). Any suggestions?
1
2
3
4
if (item -> firstChild )
   nodes2visit.push( item -> firstChild );
if (item -> sibling )
   nodes2visit.push( item -> sibling );
there you are assuming a binary tree
it should be something like
1
2
for(auto &c: item->children)
   nodes2visit.push(c);
where `children' is some kind of container that has all the children of the node.
(it would help to have any notion of how your tree is implemented)

> I want these inputs ['H', 'i', ' ', 't', 'h', 'E', 'r', 'e'] into a breadth-first traversal when it called the function
that's a list, not a tree
Oh, for 'H', 'i', ' ', 't', 'h', 'E', 'r', 'e', it should be read every level.
(Like 'H' has a first child 'i' and its sibling 't'.
'i' has a first child ' '.
't' has a first child 'h' and its siblings 'r' and 'e'.
'h' has a first child 'E')

> 't' has a first child 'h' and its siblings 'r' and 'e'.
¿how do you know that from the array?
(H (i ' ') (t (h e) r e))
Last edited on
Topic archived. No new replies allowed.