Skip to content

Commit e72c5e0

Browse files
committed
New line BFS
1 parent 4c4a1c7 commit e72c5e0

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

Diff for: Trees/Binary_Tree/BFS_newLines.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <iostream>
2+
#include <queue>
3+
#include "createDefaultTree.hpp"
4+
#include "basicTreeTemplate.h"
5+
using namespace std;
6+
7+
void BFSNL(BNode *root) {
8+
queue<BNode *> q;
9+
q.push(root);
10+
int ct = q.size();
11+
while (!q.empty()) {
12+
while (ct--) {
13+
BNode *now = q.front();
14+
cout << now->data << " ";
15+
q.pop();
16+
17+
if (now->left) q.push(now->left);
18+
if (now->right) q.push(now->right);
19+
}
20+
21+
ct = q.size();
22+
cout << endl;
23+
}
24+
}
25+
26+
int main() {
27+
BNode *root = createDefaultTree();
28+
29+
BFSNL(root);
30+
}
31+

Diff for: Trees/Binary_Tree/a.out

11.3 KB
Binary file not shown.

Diff for: Trees/Binary_Tree/basicTreeTemplate.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#pragma once
2+
13
#include <iostream>
24
#include <stack>
35
#include <queue>
@@ -72,4 +74,4 @@ void postOrderTraversalRecursive(BNode *root) {
7274
postOrderTraversalRecursive(root->right);
7375
cout << root->data << " ";
7476
return;
75-
}
77+
}

Diff for: Trees/Binary_Tree/createDefaultTree.hpp

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <iostream>
2+
#include "basicTreeTemplate.h"
3+
#include <queue>
4+
using namespace std;
5+
6+
BNode *createDefaultTree() {
7+
BNode *root = new BNode();
8+
root = createNode(1);
9+
root->left = createNode(2);
10+
root->right = createNode(3);
11+
root->left->left = createNode(4);
12+
root->left->right = createNode(5);
13+
root->right->left = createNode(6);
14+
root->right->right = createNode(7);
15+
16+
return root;
17+
}

0 commit comments

Comments
 (0)