-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1991.cpp
94 lines (80 loc) · 2.01 KB
/
1991.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <queue>
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl '\n'
using namespace std;
#define MALLOC(p, s) if(!((p) = (treePointer)malloc(s))) { \
fprintf(stderr, "Insufficient Memory"); \
exit(EXIT_FAILURE); \
}
typedef struct node* treePointer;
typedef struct node {
char data;
treePointer leftChild, rightChild;
} treeNode;
treePointer root = NULL;
void insert(char parent, char leftdata, char rightdata) {
treePointer newleftnode, newrightnode;
treePointer cur;
MALLOC(newleftnode, sizeof(*newleftnode));
MALLOC(newrightnode, sizeof(*newrightnode));
if(leftdata == '.') newleftnode = NULL;
else newleftnode->data = leftdata;
if(rightdata == '.') newrightnode = NULL;
else newrightnode->data = rightdata;
queue<treePointer>q;
q.push(root);
while(1) {
cur = q.front(); q.pop();
if(cur->data == parent) break;
if(cur) {
if(cur->leftChild) q.push(cur->leftChild);
if(cur->rightChild) q.push(cur->rightChild);
} else break;
}
cur->leftChild = newleftnode;
cur->rightChild = newrightnode;
}
void inorder(treePointer ptr) {
if (ptr) {
inorder(ptr->leftChild);
cout << ptr->data;
inorder(ptr->rightChild);
}
}
void postorder(treePointer ptr) {
if (ptr) {
postorder(ptr->leftChild);
postorder(ptr->rightChild);
cout << ptr->data;
}
}
void preorder(treePointer ptr) {
if (ptr) {
cout << ptr->data;
preorder(ptr->leftChild);
preorder(ptr->rightChild);
}
}
int main() {
fastio;
int n;
char parent, left, right;
cin >> n;
for(int i = 0; i < n; i++) {
cin >> parent >> left >> right;
if(root == NULL) {
MALLOC(root, sizeof(*root));
root->data = 'A';
root->leftChild = NULL; root->rightChild = NULL;
}
insert(parent, left, right);
}
preorder(root);
cout << endl;
inorder(root);
cout << endl;
postorder(root);
cout << endl;
return 0;
}