forked from priyankchheda/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfsTraversal.cpp
61 lines (55 loc) · 1.26 KB
/
bfsTraversal.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
/**
* LevelOrder traversal of a tree is breadth first traversal for the tree
*
* Example:
*
* F
* / \
* B G
* / \ \
* A D I
*
* Level Order Traversal := F -> B -> G -> A -> D -> I
*/
#include <iostream>
#include <queue>
#include "BST/BST.hpp"
void levelOrder(const BST& tree, std::vector<int>& output);
/* Main Operational Function */
int main()
{
BST tree;
int original[] = { 9, 5, 3, 10, 6, 1, 7, 4 };
for (int i : original)
tree.insert(i);
std::vector<int> output;
levelOrder(tree, output);
for (int o : output)
std::cout << o << " ";
std::cout << "\n";
return 0;
}
/**
* Breadth First Traversal (Level Order Traversal)
* @param tree bst tree object
* @param output output of level order traversal is stored in vector
*/
void levelOrder(const BST& tree, std::vector<int>& output)
{
Node* root = tree.getRoot();
if (root == nullptr)
return;
std::queue<Node*> q;
q.push(root);
while(!q.empty()) {
Node* node = q.front();
output.push_back(node->data);
if (node->left != nullptr) {
q.push(node->left);
}
if (node->right != nullptr) {
q.push(node->right);
}
q.pop();
}
}