-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_levels.cpp
70 lines (60 loc) · 1.82 KB
/
print_levels.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* BREADTH-FIRST PRINT (LEVEL ORDER TRAVERSAL)
*
* This program extends an assumed BinaryTree implementation (from "binary_tree.h") by adding
* functionality to print the tree in a breadth-first (level order) manner.
*
* The algorithm uses a queue to traverse the tree level by level:
* - Start from the root node and push it into the queue.
* - While the queue is not empty, remove the front node, print its value, and enqueue its children.
*
* ASCII Illustration:
* 10
* / \
* 5 12
* / \
* 11 16
*
* Example Output:
* For the tree built by inserting {10, 5, 12, 11, 16}, the printed output will be:
* 10 5 12 11 16
*
* Explanation:
* The nodes are printed level by level from left to right.
*/
#include "binary_tree.h"
#include <iostream>
#include <queue>
class TreeWithBreadthFirstPrint : public BinaryTree {
public:
TreeWithBreadthFirstPrint() : BinaryTree() {}
// Prints the tree in a breadth-first order.
void print() const {
if (!root) {
std::cout << "Tree is empty." << std::endl;
return;
}
std::queue<const Node *> nodesToPrint;
nodesToPrint.push(root.get());
while (!nodesToPrint.empty()) {
const auto *currentNode = nodesToPrint.front();
nodesToPrint.pop();
std::cout << currentNode->value << ' ';
if (currentNode->left) {
nodesToPrint.push(currentNode->left.get());
}
if (currentNode->right) {
nodesToPrint.push(currentNode->right.get());
}
}
std::cout << std::endl;
}
};
int main() {
TreeWithBreadthFirstPrint tree;
for (const auto &value : {10, 5, 12, 11, 16}) {
tree.add(value);
}
tree.print();
return 0;
}