-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp4.c
261 lines (212 loc) · 5.65 KB
/
p4.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//////////////////////////////////////////////////////////////////////////////////
/* CE1007/CZ1007 Data Structures
2016/17 S1
Author and Lab Group: Chee Jun Yuan Glenn FSP7
Program name: FSP7_Chee Jun Yuan Glenn
Date: 10 November 2016
Purpose: Implementing the required functions for Assignment 1 (Question 4)*/
//////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
//////////////////////////////////////////////////////////////////////////////////
typedef struct _bstnode {
int item;
struct _bstnode *left;
struct _bstnode *right;
} BSTNode; // You should not change the definition of BSTNode
typedef struct _stackNode {
BSTNode *item;
struct _stackNode *next;
}StackNode; // You should not change the definition of StackNode
typedef struct _stack
{
StackNode *top;
}Stack; // You should not change the definition of Stack
///////////////////////// function prototypes ////////////////////////////////////
// This is for question 4. You should not change the prototypes of these functions
void insertBSTNode(BSTNode **node, int value);
void printPostOrderIterative(BSTNode *root);
// You may use the following functions or you may write your own
void push(Stack *stack, BSTNode *node);
BSTNode * pop(Stack *s);
BSTNode * peek(Stack *s);
int isEmpty(Stack *s);
void removeAll(BSTNode **node);
///////////////////////////// main() /////////////////////////////////////////////
int main()
{
int c, i;
//Initialize the Binary Search Tree as an empty Binary Search Tree
BSTNode *root;
c = 1;
root = NULL;
printf("1:Insert an integer into the binary search tree;\n");
printf("2:Print the post-order traversal of the binary search tree;\n");
printf("0:Quit;\n");
while (c != 0)
{
printf("Please input your choice(1/2/0): ");
scanf("%d", &c);
switch (c)
{
case 1:
printf("Input an integer that you want to insert into the Binary Search Tree: ");
scanf("%d", &i);
insertBSTNode(&root, i);
break;
case 2:
printf("The resulting post-order traversal of the binary search tree is: ");
printPostOrderIterative(root); // You need to code this function
printf("\n");
break;
case 0:
removeAll(&root);
break;
default:
printf("Choice unknown;\n");
break;
}
}
return 0;
}
//////////////////////////////////////////////////////////////////////////////////
void insertBSTNode(BSTNode **node, int value) {
if (*node == NULL)
{
*node = malloc(sizeof(BSTNode));
if (*node != NULL) {
(*node)->item = value;
(*node)->left = NULL;
(*node)->right = NULL;
}
}
else
{
if (value < (*node)->item)
{
insertBSTNode(&((*node)->left), value);
}
else if (value >(*node)->item)
{
insertBSTNode(&((*node)->right), value);
}
else
return;
}
}
//////////////////////////////////////////////////////////////////////////////////
void printPostOrderIterative(BSTNode *root) {
/* add your code here */
///Postorder Traversal with a Stack (Iterative)
//if root is EMPTY, don't do anything
if (root == NULL || root[0].item == NULL) {
return; //break out of function
}
//Initialise two stacks
Stack *firstStack, *secondStack;
firstStack = malloc(sizeof(Stack));
secondStack = malloc(sizeof(Stack));
firstStack[0].top = NULL;
secondStack[0].top = NULL;
//if Stacks are NOT empty
if (!isEmpty(firstStack)) {
//removeAll(firstStack); //empty stack
}
if (!isEmpty(secondStack)) {
//removeAll(secondStack); //empty stack
}
//push root onto the first Stack
push(firstStack, root);
//initialise BSTNode *tempNode to traverse BinarySearchTree
BSTNode *tempNode;
//loop while firstStack is NOT empty
while (!isEmpty(firstStack) && firstStack != NULL) {
//pop a BSTNode from firstStack into currentNode
BSTNode *tempNode = pop(firstStack);
//push currentNode into secondStack
push(secondStack, tempNode);
//if currentNode has a leftNode
if (tempNode[0].left != NULL) {
push(firstStack, tempNode[0].left); //push currentNode.leftNode to firstStack
}
//if currentNode has a rightNode
if (tempNode[0].right != NULL) {
push(firstStack, tempNode[0].right); //push currentNode.rightNode to firstStack
}
}
//loop while secondStack is NOT empty
while (!isEmpty(secondStack) && secondStack != NULL) {
printf("%d ", pop(secondStack)[0].item); //process (print) the values of secondStack
}
//////////////////////////////////////////////////////////////////////////////////
///Postorder Traversal without a Stack (Recursive)
/*//if root is EMPTY, don't do anything
if (root == NULL || root[0].item == NULL) {
return; //break out of function
}
//POST-order, so left -> right -> process
printPostOrderIterative(root[0].left); //left
printPostOrderIterative(root[0].right); //right
printf("%d ", root[0].item); //process*/
}
//////////////////////////////////////////////////////////////////////////////////
void push(Stack *stack, BSTNode *node)
{
StackNode *temp;
temp = malloc(sizeof(StackNode));
if (temp == NULL)
return;
temp->item = node;
if (stack->top == NULL)
{
stack->top = temp;
temp->next = NULL;
}
else
{
temp->next = stack->top;
stack->top = temp;
}
}
BSTNode* pop(Stack * s)
{
StackNode *temp, *t;
BSTNode * ptr;
ptr = NULL;
t = s->top;
if (t != NULL)
{
temp = t->next;
ptr = t->item;
s->top = temp;
free(t);
t = NULL;
}
return ptr;
}
BSTNode* peek(Stack * s)
{
StackNode *temp;
temp = s->top;
if (temp != NULL)
return temp->item;
else
return NULL;
}
int isEmpty(Stack *s)
{
if (s->top == NULL)
return 1;
else
return 0;
}
void removeAll(BSTNode **node)
{
if (*node != NULL)
{
removeAll(&((*node)->left));
removeAll(&((*node)->right));
free(*node);
*node = NULL;
}
}