-
Notifications
You must be signed in to change notification settings - Fork 4
/
bt_bfs.cpp
75 lines (68 loc) · 1.36 KB
/
bt_bfs.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
71
72
73
74
75
/**
* Author: Skylar Payne
* Date: January 6, 2015
* Print out each level of a binary tree on its own line.
**/
#include <iostream>
#include <queue>
#include <stdlib.h>
class Node {
public:
char value;
Node* left;
Node* right;
int height = 0;
Node(char v, Node* l = nullptr, Node* r = nullptr) : value(v), left(l), right(r) { }
};
void print_tree(Node* root) {
std::queue<Node*> q;
q.push(root);
int height = 0;
while(!q.empty()) {
Node* c = q.front();
q.pop();
if(c->height > height) {
std::cout << std::endl;
height = c->height;
}
std::cout << c->value << " ";
if(c->left != nullptr) {
c->left->height = c->height + 1;
q.push(c->left);
}
if(c->right != nullptr) {
c->right->height = c->height + 1;
q.push(c->right);
}
}
std::cout << std::endl;
}
void destroy(Node* root) {
if(root == nullptr) {
return;
}
destroy(root->left);
destroy(root->right);
delete root;
}
int main(int argc, char** argv) {
if(argc < 2) {
std::cout << "Please input a list of characters" << std::endl;
return -1;
}
int i = 1;
std::queue<Node*> q;
Node* root = new Node(argv[i++][0]);
q.push(root);
while(i < argc) {
Node* c = q.front();
q.pop();
c->left = new Node(argv[i++][0]);
c->right = (i < argc ? new Node(argv[i++][0]) : nullptr);
q.push(c->left);
q.push(c->right);
}
print_tree(root);
destroy(root);
return 0;
}