-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTreeUse.cpp
More file actions
50 lines (40 loc) · 1.08 KB
/
TreeUse.cpp
File metadata and controls
50 lines (40 loc) · 1.08 KB
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
#include <iostream>
#include "../TreeNode.h"
using namespace std;
TreeNode<int>* takeInput() {
int rootData;
cout << "Enter data:";
cin >> rootData;
auto root = new TreeNode<int>(rootData);
int n;
cout << "Enter num of children of " << rootData << ":";
cin >> n;
for (int i = 0; i < n; i++) {
auto child = takeInput();
root->children.push_back(child);
}
return root;
}
void printTree(TreeNode<int>* root) {
if (root == nullptr) {
return;
}
cout << root->data << ":";
for (int i = 0; i < root->children.size(); i++) {
cout << root->children.at(i)->data << ",";
}
cout << endl;
for (int i = 0; i < root->children.size(); i++) {
printTree(root->children.at(i));
}
}
int main() {
/*TreeNode<int> *root = new TreeNode<int>(1);
TreeNode<int> *node1 = new TreeNode<int>(2);
TreeNode<int> *node2 = new TreeNode<int>(3);
root->children.push_back(node1);
root->children.push_back(node2);*/
auto root = takeInput();
printTree(root);
// TODO: Delete the tree
}