Below is a binary tree with elements:
The tree, 'Tree' and its node 'Node' are represented as:
struct Node {
std::string key;
Node* left;
Node* right;
Node(std::string& k)
:key(k),left(NULL),right(NULL) {}
};
struct Tree {
Tree():root(NULL){}
void traversal(Node* n);
// .... more methods......
Node* root;
};
Below is the implementation of the traversal function:
void Tree::traversal(Node* n)
{
queue<Node*> q;
q.push(n);
while (! q.empty())
{
Node* v = q.front();
std::cout << v->key << " - ";
if (v->left != NULL)
q.push(v->left);
if (v->right != NULL)
q.push(v->right);
q.pop();
}
}
What is the output of this code?