-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabstract_machines.h
319 lines (263 loc) · 5.69 KB
/
abstract_machines.h
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct tree {
int val;
struct tree* fg;
struct tree* fd;
bool visited;
}tree;
typedef struct qc {
tree* node;
struct qc* next;
}qc;
typedef struct queue {
qc* head;
qc* tail;
}queue;
typedef struct stack{
qc* top;
}stack;
// Function headers
//tree abstract machine
void create_node(tree** node);
void ass_fg(tree* parent, tree* fg);
void ass_fd(tree* parent, tree* fd);
void ass_val(tree* node, int val);
void ass_visited(tree* node, bool vis);
tree* fg(tree* node);
tree* fd(tree* node);
int value(tree* node);
bool visited(tree* node);
void free_tree(tree* root);
//BST basic opertations
tree* insert(tree* node, int val);
bool leaf(tree* node);
tree* delete(tree* root, int val);
bool search_recursive(tree* node, int val);
void print_tree(tree* root, int space);
//list abstract machine
void allocate_qc(qc** p);
tree* value_qc(qc* p);
qc* next_qc(qc* p);
void ass_val_qc(qc* p, tree* v);
void ass_adr_qc(qc* p, qc* q);
//queue abstract machine
void create_queue(queue* q);
bool empty_queue(queue q);
void enqueue(queue* q, tree* val);
void dequeue(queue* q, tree** val);
void print_queue(queue q);
//stack abstract machine
void print_stack(stack s);
void create_stack(stack* s);
bool empty_stack(stack s);
void push(stack* s, tree* val);
void pop(stack* s, tree** val);
//tree
//----------------------------------------------------------------
void create_node(tree** node) {
*node=(tree *)malloc(sizeof(tree));
}
void ass_fg(tree* parent,tree* fg) {
parent->fg=fg;
}
void ass_fd(tree* parent,tree* fd) {
parent->fd=fd;
}
void ass_val(tree* node,int val) {
node->val=val;
}
void ass_visited(tree* node,bool vis) {
node->visited=vis;
}
tree* fg(tree* node) {
return node->fg;
}
tree* fd(tree* node) {
return node->fd;
}
int value(tree* node) {
return node->val;
}
bool visited(tree* node) {
return node->visited;
}
void free_tree(tree* root) {
if (root !=NULL) {
free_tree(fg(root));
free_tree(fd(root));
free(root);
}
}
//--------------------------------------------------------
//BST
//--------------------------------------------------------
tree* insert(tree* node,int val) {
if (node==NULL) {
tree* newNode;
create_node(&newNode);
ass_val(newNode,val);
ass_fd(newNode,NULL);
ass_fg(newNode,NULL);
ass_visited(newNode,false);
return newNode;
} else if (value(node)<val) {
ass_fd(node,insert(fd(node),val));
} else {
ass_fg(node,insert(fg(node),val));
}
return node;
}
bool leaf(tree* node) {
return fg(node)==NULL && fd(node)==NULL;
}
tree* delete(tree* root,int val) {
if (root==NULL) {
return root;
}
if (val<value(root)) {
ass_fg(root,delete(fg(root),val));
} else if (val>value(root)) {
ass_fd(root,delete(fd(root),val));
} else {
if (leaf(root)) {
free(root);
root=NULL;
} else if (fg(root)==NULL) {
tree* temp=root;
root=fd(root);
free(temp);
} else if (fd(root)==NULL) {
tree* temp=root;
root=fg(root);
free(temp);
} else {
tree* p=fd(root);
while (fg(p)!=NULL) {
p=fg(p);
}
ass_val(root,value(p));
ass_fd(root,delete(fd(root),value(p)));
}
}
return root;
}
bool search_recursive(tree* node,int val) {
if (node==NULL) {
return false;
} else if(value(node)==val){
return true;
} else {
if (value(node)<val) {
return search_recursive(fd(node),val);
} else {
return search_recursive(fg(node),val);
}
}
}
void print_tree(tree *root, int space)
{
if (root == NULL)
{
return;
}
space += 10;
print_tree(fd(root), space);
printf("\n");
for (int i = 10; i < space; i++)
{
printf(" ");
}
printf("%d\n", value(root));
print_tree(fg(root), space);
}
//---------------------------------------------------------------------
//queue
//---------------------------------------------------------------------
void allocate_qc(qc** p) {
*p=(qc*)malloc(sizeof(qc));
}
tree* value_qc(qc* p) { //give the value of a cell
return p->node;
}
qc* next_qc(qc* p) { //give the next cell pointer
return p->next;
}
void ass_val_qc(qc*p,tree* v) { //assigne a value to the cell
p->node=v;
}
void ass_adr_qc(qc* p,qc* q) { //assigne a next value to the cell
p->next=q;
}
void create_queue(queue* q) {
q->head=NULL;
}
bool empty_queue(queue q) {
return q.head==NULL;
}
void enqueue(queue* q,tree* val) {
qc* new_cell;
allocate_qc(&new_cell);
ass_val_qc(new_cell,val);
ass_adr_qc(new_cell,NULL);
if (empty_queue(*q)) {
q->head=new_cell;
} else {
ass_adr_qc(q->tail,new_cell);
}
q->tail=new_cell;
}
void dequeue(queue* q,tree** val) {
if(!empty_queue(*q)) {
qc* temp=q->head;
*val=value_qc(temp);
q->head=next_qc(q->head);
free(temp);
} else {
printf("underflow\n");
*val=NULL;
}
}
//---------------------------------------------------------------
//stack
//---------------------------------------------------------------
void print_stack(stack s) {
qc* p=s.top;
while (p!=NULL) {
printf("%2d |",value_qc(p));
p=next_qc(p);
}
printf("\n");
}
void create_stack(stack* s) {
s->top=NULL;
}
bool empty_stack(stack s) {
return s.top==NULL;
}
void push(stack* s,tree* val) {
qc* new_cell;
allocate_qc(&new_cell);
ass_val_qc(new_cell,val);
ass_adr_qc(new_cell,s->top);
s->top=new_cell;
}
void pop(stack* s,tree** val) {
if(!empty_stack(*s)) {
*val=value_qc(s->top);
qc* temp=s->top;
s->top=next_qc(s->top);
free(temp);
} else {
printf("underflow\n");
}
}
void print_queue(queue q) {
tree* node;
while (!empty_queue(q)) {
dequeue(&q,&node);
printf("%2d |",value(node));
}
printf("\n");
}