-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP122.cpp
126 lines (111 loc) · 2.46 KB
/
P122.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <stdio.h>
#include <vector>
/*
Binary tree: Sequence of (n,s): n: value, s: L/R path
*/
//typedef unsigned int Node;
struct Node {
unsigned int value;
Node *L;
Node *R;
};
#define TREE_SIZE 256
bool checkTree(Node const * const node) {
if(node == NULL)
return true;
if(node->value == 0)
return false;
if(!checkTree(node->L))
return false;
if(!checkTree(node->R))
return false;
return true;
}
void printTree(Node * node) {
std::vector<Node* > lv;
lv.push_back(node);
while(!lv.empty()) {
// Print all in lv
std::vector<Node* > nxt;
for(std::vector<Node*>::iterator it = lv.begin(); it != lv.end(); ++it) {
Node *n = *it;
if(n != node)
std::cout << " ";
std::cout << n->value;
if(n->L != NULL)
nxt.push_back(n->L);
if(n->R != NULL)
nxt.push_back(n->R);
}
lv = nxt;
}
std::cout << std::endl;
}
int main() {
Node tree[TREE_SIZE];
while(true) {
// Reset tree:
for(unsigned int i = 0; i < TREE_SIZE; ++i) {
tree[i].value = 0;
tree[i].L = NULL;
tree[i].R = NULL;
}
unsigned int treeI = 1; // next node to construct.
// Read from input and build tree
unsigned int value;
char path[256];
bool error = false;
// Read/construct loop:
while(true) {
// Read:
std::cin >> path;
if(std::cin.eof())
return 0;
if(path[1] == ')') {
break; // case ()
}
// Build node:
// Compute value:
value = path[1]-'0';
unsigned int i = 2;
while(path[i] != ',') {
value = value*10+(path[i]-'0');
++i;
}
++i;
Node *node = &tree[0];
// Compute path:
while(path[i] != ')') {
if(path[i] == 'R') {
if(node->R == NULL) {
node->R = &tree[treeI++];
}
node = node->R;
}
else {
if(node->L == NULL) {
node->L = &tree[treeI++];
}
node = node->L;
}
++i;
}
// Set value at current location:
if(node->value != 0) {
error = true;
//std::cerr << "Doubly specified node " << path << " value: " << value << std::endl;
}
//std::cerr << "Added node " << path << " value: " << value << std::endl;
node->value = value; // normal case
} // end read/construct.
//std::cerr << "Done reading" << std::endl;
// Check tree:
Node *node = &tree[0];
if(error || !checkTree(node)) {
std::cout << "not complete" << std::endl;
continue;
}
printTree(node);
}
}