-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree-orders-iterative_1a1.c
148 lines (131 loc) · 4.56 KB
/
tree-orders-iterative_1a1.c
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
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int read_1a1(int *n, int **key, int **left, int **right) {
int i;
scanf("%d", n); // n is same as &*n or *&n here.
*key = (int *)calloc(*n, sizeof(int));
if (*key == NULL) {
fprintf(stderr, "calloc failed\n");
return(-1);
}
*left = (int *)calloc(*n, sizeof(int));
if (*left == NULL) {
fprintf(stderr, "calloc failed\n");
return(-1);
}
*right = (int *)calloc(*n, sizeof(int));
if (*right == NULL) {
fprintf(stderr, "calloc failed\n");
return(-1);
}
for (i = 0; i < *n; i++) {
scanf("%d%d%d", &(*key)[i], &(*left)[i], &(*right)[i]); // it's probably a little faster to use local pointers (keyLocal, leftLocal, rightLocal - like in tree-orders-iterative_1a2.c) because of this for loop, because of many dereferencing operations we have here
}
return 0;
}
int * inOrder_1a1(int n, int *key, int *left, int *right) {
int * result;
result = (int *)calloc(n, sizeof(int));
int resultSize = 0;
int * stack; // stack contains indices of vertices
stack = (int *)calloc(n, sizeof(int));
int size = 0; // current stack size
int current = 0; // an index - 0 is root
while (1) {
while (current > -1) {
stack[size++] = current;
current = left[current];
}
if (size) {
current = stack[--size];
result[resultSize++] = key[current]; // visit()
current = right[current];
}
else
return result;
}
}
int * preOrder_1a1(int n, int * key, int * left, int * right) {
int * result;
result = (int *)calloc(n, sizeof(int));
int resultSize = 0;
int * stack; // stack contains indices of vertices
stack = calloc(n, sizeof(int));
int size = 0; // current stack size
stack[size++] = 0; // stack contains indices of vertices - 0 is root
int current;
while (size) {
current = stack[--size]; // an index
result[resultSize++] = key[current]; // visit()
if (right[current] != -1) {
stack[size++] = right[current];
}
if (left[current] != -1) {
stack[size++] = left[current];
}
}
return result;
}
int * postOrder_1a1(int n, int * key, int * left, int * right) {
int * result = (int *)calloc(n, sizeof(int));
int resultSize = 0;
int * stack = (int *)calloc(n, sizeof(int)); // stack contains indices of vertices
int size = 0; // current stack size
char * boolStack = (char *)calloc(n, sizeof(char)); // For each element on the node stack, a corresponding value is stored on the bool stack. If this value is true, then we need to pop and visit the node on next encounter.
int boolSize = 0;
char alreadyEncountered; // boolean
int current = 0; // an index - 0 is root
while (current > -1) {
stack[size++] = current;
boolStack[boolSize++] = 0; // false
current = left[current];
}
while (size) {
current = stack[size - 1];
alreadyEncountered = boolStack[boolSize - 1];
if (alreadyEncountered) {
result[resultSize++] = key[current]; // visit()
size--;
boolSize--;
}
else {
boolSize--;
boolStack[boolSize++] = 1; // true
current = right[current];
while (current > -1) {
stack[size++] = current;
boolStack[boolSize++] = 0; // false
current = left[current];
}
}
}
return result;
}
void print_1a1(int n, int * array) {
int i;
for (i = 0; i < n; i++) {
if (i > 0) {
printf(" ");
}
printf("%d", array[i]);
}
printf("\n");
}
/* main_1a1() */
int main() {
int n, *key, *left, *right, *result;
read_1a1(&n, &key, &left, &right);
result = inOrder_1a1(n, key, left, right);
print_1a1(n, result);
result = preOrder_1a1(n, key, left, right);
print_1a1(n, result);
result = postOrder_1a1(n, key, left, right);
print_1a1(n, result);
free(result);
free(key);
free(left);
free(right);
scanf("%d", &n);
return 0;
}