// print the value
cout << node->value << " ";
// enqueue left child to queue
if(node->left != NULL) {
q.push(node->left);
}
// enqueue right child to queue
if(node->right != NULL) {
q.push(node->right);
I want to go to new line after every level
Assistant
To print each level of a binary tree on a new line during a level-order (breadth-first) traversal, you can modify your existing code by keeping track of the number of nodes at each level. Here's how you can achieve this:
Use a loop that processes nodes level by level.
For each level, determine the number of nodes (levelSize) at that level.
Process exactly levelSize nodes, ensuring that all nodes at the current level are printed on the same line.
After processing each level, print a newline character to move to the next line.
Here's the modified code with comments explaining each step:
cpp
#include <iostream>
#include <queue>
// Assuming BinaryTreeNode is defined as follows:
struct BinaryTreeNode {
int value;
BinaryTreeNode* left;
BinaryTreeNode* right;
BinaryTreeNode(int val) : value(val), left(NULL), right(NULL) {}
};
void printLevelOrder(BinaryTreeNode* root) {
if (root == NULL) {
return;
}
std::queue<BinaryTreeNode*> q;
q.push(root);
while (!q.empty()) {
int levelSize = q.size(); // Number of nodes at the current level
for (int i = 0; i < levelSize; ++i) {
BinaryTreeNode* node = q.front();
q.pop();
// Print the current node's value
std::cout << node->value << " ";
// Enqueue left child if it exists
if (node->left != NULL) {
q.push(node->left);
}
// Enqueue right child if it exists
if (node->right != NULL) {
q.push(node->right);
}
}
// After printing all nodes at the current level, print a newline
std::cout << std::endl;
}
}
int main() {
// Example usage:
/*
1
/ \
2 3
/ \ \
4 5 6
*/
BinaryTreeNode* root = new BinaryTreeNode(1);
root->left = new BinaryTreeNode(2);
root->right = new BinaryTreeNode(3);
root->left->left = new BinaryTreeNode(4);
root->left->right = new BinaryTreeNode(5);
root->right->right = new BinaryTreeNode(6);
std::cout << "Level Order Traversal:" << std::endl;
printLevelOrder(root);
// Clean up memory (optional here for demonstration purposes)
// In practice, consider using smart pointers to manage memory automatically.
delete root->right->right;
delete root->left->right;
delete root->left->left;
delete root->right;
delete root->left;
delete root;
return 0;
}
Explanation of the Changes:
Tracking Level Size:
Before entering the for-loop, levelSize is set to the current size of the queue. This represents the number of nodes at the current level.
The for-loop runs exactly levelSize times, ensuring that all nodes at the current level are processed.
Printing Nodes:
Each node's value is printed followed by a space.
After all nodes at the current level are printed, a newline character is printed to move to the next line.
Enqueuing Child Nodes:
As before, the left and right children of each node are enqueued if they exist. These children constitute the next level.
Sample Output:
For the given example tree, the output will be:
code
Level Order Traversal:
1
2 3
4 5 6
Each level of the tree is printed on a separate line, as desired.
Alternative Approach Using a Sentinel (NULL Marker):
Another method to achieve level separation is by using a sentinel (e.g., NULL) to mark the end of a level. Here's how you can implement it:
cpp
void printLevelOrderWithSentinel(BinaryTreeNode* root) {
if (root == NULL) return;
std::queue<BinaryTreeNode*> q;
q.push(root);
q.push(NULL); // Sentinel to mark the end of the first level
while (!q.empty()) {
BinaryTreeNode* node = q.front();
q.pop();
if (node == NULL) {
std::cout << std::endl; // End of current level
if (!q.empty()) {
q.push(NULL); // Add sentinel for the next level
}
} else {
std::cout << node->value << " ";
if (node->left != NULL) {
q.push(node->left);
}
if (node->right != NULL) {
q.push(node->right);
}
}
}
}
Explanation:
A NULL pointer is used as a sentinel to indicate the end of a level.
When a NULL is encountered in the queue, it signifies that all nodes at the current level have been processed, so a newline is printed.
If the queue isn't empty after popping a NULL, another NULL is pushed to mark the end of the next level.
Conclusion:
Both methods effectively separate the levels in a level-order traversal. The first method using levelSize is generally more efficient since it avoids the need for sentinel markers and additional checks. Choose the approach that best fits your coding style and requirements.