I've got a working Binary Tree class, and a working breadth first traversal (using stl queue), but just need someone to help me implement a level feature.
for example, a tree with root 'a' and two children 'b' and 'c' on my program outputs a b c, where i would like it to output
a
b c
The basic idea: You count the number of items in the next level (variable "next") while you decrease the "current" level until you reach the end. Then you know that you are at a "level border" and the next level's count becomes current count and you start counting children again..
Well, what you could also do, is to insert some "end of level" marker (e.g. null pointer) into the queue every time you reached the last "end of line".
void Tree::breadthfirst(Tree *t) {
queue<Tree> q;
q.push(*t);
q.push(0);
while (q.size() > 1) { // one nullptr will always remain in the list. So test for size>1
*t=q.front();
q.pop();
if (!t) { cout << endl; q.push(0); continue; }
cout << t->RootData() << " ";
if (t->Left() != NULL)
q.push(*t->Left());
if (t->Right() != NULL)
q.push(*t->Right());
}
}