-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.cpp
51 lines (45 loc) · 1022 Bytes
/
display.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
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
class Node{
public:
int data;
vector<Node*> children;
Node(int data){
this-> data = data;
}
};
Node* input(){
stack<Node*> *stk = new stack<Node*>;
int element, root_data, size;
cin >> size >> root_data;
Node *root = new Node(root_data);
stk-> push(root);
while(!stk-> empty()){
cin >> element;
if(element == -1){
stk-> pop();
}
else{
Node *new_node = new Node(element);
stk->top()-> children.push_back(new_node);
stk-> push(new_node);
}
}
return root;
}
void display(Node *root){
cout << root-> data << " ==> {";
for(int i = 0 ;i < root-> children.size(); i++){
cout << root-> children[i]->data << ',';
}
cout << ".}" << '\n';
for(int i = 0; i < root-> children.size(); i++){
display(root-> children[i]);
}
}
int main(){
Node* root = input();
display(root);
}