-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.cpp
More file actions
202 lines (129 loc) · 3.39 KB
/
Tree.cpp
File metadata and controls
202 lines (129 loc) · 3.39 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <strings.h>
#include "Tree.h"
Node* node_ctor(int data, Node* left, Node* right) {
Node* node = (Node*)calloc(1, sizeof(Node));
node->data = data;
node->left = left;
node->right = right;
return node;
}
int node_dtor(Node* node) {
if (node->left != 0 && node->right != 0) {
node_dtor(node->left);
node_dtor(node->right);
}
node->left = 0;
node->right = 0;
node->data = TREE_POISON;
free(node);
return 0;
}
void print_node_pre(Node* node, FILE* output) {
if (node == 0) {
fprintf(output, "nil ");
return;
}
fprintf(output, "( ");
fprintf(output, "%d ", node->data);
print_node_pre(node->left, output);
print_node_pre(node->right, output);
fprintf(output, ") ");
}
void print_node_post(Node* node, FILE* output) {
if (node == 0) {
fprintf(output, "nil ");
return;
}
fprintf(output, "( ");
print_node_post(node->left, output);
print_node_post(node->right, output);
fprintf(output, "%d ", node->data);
fprintf(output, ") ");
}
void print_node_in(Node* node, FILE* output) {
if (node == 0) {
fprintf(output, "nil ");
return;
}
fprintf(output, "( ");
print_node_in(node->left, output);
fprintf(output, "%d ", node->data);
print_node_in(node->right, output);
fprintf(output, ") ");
}
Tree* tree_ctor(Node* root, size_t size) {
assert(root);
Tree* tree = (Tree*)calloc(1, sizeof(Tree));
tree->root = root;
tree->size = size;
return tree;
}
int tree_dtor(Tree* tree) {
node_dtor(tree->root);
tree->root = 0;
tree->size = 0;
free(tree);
return 0;
}
int add_node(Tree* tree, Node* node, int data) {
if (data <= node->data) {
if (node->left != 0)
add_node(tree, node->left, data);
else {
Node* new_node = node_ctor(data, 0, 0);
node->left = new_node;
}
}
else {
if (node->right != 0)
add_node(tree, node->right, data);
else {
Node* new_node = node_ctor(data, 0, 0);
node->right = new_node;
}
}
return 0;
}
Node* read_node(FILE* file) {
int number = 0;
char current[MAX_LINE_LEN] = "";
Node* node = (Node*)calloc(1, sizeof(Node));
fscanf(file, "%d", &number);
printf("%d\n", number);
node->data = number;
fscanf(file, "%s", current);
if (strcmp("(", current) == 0) {
printf("open %s\n", current);
node->left = read_node(file);
}
else if (strcmp("nil", current) == 0) {
printf("nils %s\n", current);
node->left = 0;
}
else
printf("Syntax error!\n");
fscanf(file, "%s", current);
if (strcmp("(", current) == 0) {
printf("open %s\n", current);
node->right = read_node(file);
}
else if (strcmp("nil", current) == 0) {
printf("nil %s\n", current);
node->right = 0;
}
else
printf("Syntax error!\n");
fscanf(file, "%s", current); //çàêðûâàþùàÿ ñêîáêà
printf("data: %d\n", node->data);
return node;
}
Node* read_data(FILE* file) {
char current[MAX_LINE_LEN] = "";
fscanf(file, "%s", current); //îòêðûâàþùàÿ ñêîáêà
printf("open %s\n", current);
Node* new_node = read_node(file);
return new_node;
}