-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTraStack.C
More file actions
188 lines (186 loc) · 3.86 KB
/
TraStack.C
File metadata and controls
188 lines (186 loc) · 3.86 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
#include <stdio.h>
#include <stdlib.h>
#define bool int
struct tNode
{
int data;
struct tNode *left;
struct tNode *right;
};
struct sNode
{
struct tNode *t;
struct sNode *next;
};
void push(struct sNode **top_ref, struct tNode *t);
struct tNode *pop(struct sNode **top_ref);
bool isEmpty(struct sNode *top);
void postOrder(struct tNode *root)
{
if (root == NULL)
{
return;
}
struct sNode *s = NULL;
struct tNode *current = root;
struct tNode *u, *y;
bool done = 0;
push(&s, root);
do
{
while (current)
{
if (current->right)
{
push(&s, current->right);
}
push(&s, current);
current = current->left;
}
current = pop(&s);
u = pop(&s);
y = u;
push(&s, u);
if (current->right && current->right == y)
{
pop(&s);
push(&s, current);
current = current->right;
}
else
{
printf("%d ", current->data);
current = NULL;
}
} while (!isEmpty(s));
printf("%d", root->data);
}
void preOrder(struct tNode *root)
{
if (root == NULL)
{
return;
}
struct sNode *s = NULL;
struct tNode *current = root;
bool done = 0;
while (done != 1)
{
if (current != NULL)
{
printf("%d ", current->data);
if (current->right)
{
push(&s, current->right);
}
current = current->left;
}
else
{
if (!isEmpty(s))
{
current = pop(&s);
}
else
{
done = 1;
}
}
}
}
void inOrder(struct tNode *root)
{
struct tNode *current = root;
struct sNode *s = NULL;
bool done = 0;
while (!done)
{
if (current != NULL)
{
push(&s, current);
current = current->left;
}
else
{
if (!isEmpty(s))
{
current = pop(&s);
printf("%d ", current->data);
current = current->right;
}
else
done = 1;
}
}
}
/*struct tNode *peek(struct sNode *topref)
{
if (isEmpty(topref))
{
return NULL;
}
else
{
return;
}
}*/
void push(struct sNode **top_ref, struct tNode *t)
{
struct sNode *new_tNode =
(struct sNode *)malloc(sizeof(struct sNode));
if (new_tNode == NULL)
{
printf("Stack Overflow \n");
getchar();
exit(0);
}
new_tNode->t = t;
new_tNode->next = (*top_ref);
(*top_ref) = new_tNode;
}
bool isEmpty(struct sNode *top)
{
return (top == NULL) ? 1 : 0;
}
struct tNode *pop(struct sNode **top_ref)
{
struct tNode *res;
struct sNode *top;
if (isEmpty(*top_ref))
{
getchar();
exit(0);
}
else
{
top = *top_ref;
res = top->t;
*top_ref = top->next;
free(top);
return res;
}
}
struct tNode *newtNode(int data)
{
struct tNode *tNode = (struct tNode *)
malloc(sizeof(struct tNode));
tNode->data = data;
tNode->left = NULL;
tNode->right = NULL;
return (tNode);
}
int main()
{
struct tNode *root = newtNode(1);
root->left = newtNode(2);
root->right = newtNode(3);
root->left->left = newtNode(4);
root->left->right = newtNode(5);
printf("Inorder Sequence:\n");
inOrder(root);
printf("\nPreorder Sequence:\n");
preOrder(root);
printf("\nPostorder Sequence:\n");
postOrder(root);
return 0;
}