-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDYN_PAMAT.cpp
354 lines (317 loc) · 7.95 KB
/
DYN_PAMAT.cpp
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include <stdio.h>
#include <math.h>
//#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <stdarg.h>
#define nl printf("\n")
#define TREESORT_METHOD // BUBBLESORT_METHOD
struct set {
int length;
int array[]; // flexible member, has to be at the end: https://en.wikipedia.org/wiki/Flexible_array_member
// if needed to expand the struct, add new elements before "int array[];"
};
void set_delete(set* arr){
free(arr);
/* Since the flexible array member is placed at the end of the structure and is allocated with the same malloc call,
it will be automatically freed along with the rest of the structure.*/
}
//----- start of treesort part
struct Node {
int key;
struct Node* left;
struct Node* right;
};
void tree_delete(Node* root){
if (root == NULL) {
return;
}
tree_delete(root->left);
tree_delete(root->right);
free(root);
}
struct Node* newNode(int item){
struct Node* ptr = (struct Node*) malloc(sizeof(struct Node)); //has constant size
if(ptr == NULL){
return NULL;
}
ptr->key = item;
ptr->left = NULL;
ptr->right = NULL;
return ptr;
}
bool Qsmaller(int item1, int item2){
//this function can be changed to compare and sort not only numbers
// Q stands for Question
if(item2 > item1){
return true;
}
return false;
}
void tree_to_set(Node* root, set* arr, int &i){
// convert the sorted result into set
/* By using a reference parameter (int &i),
any changes made to the i variable inside the function
will be reflected outside the function as well, on the other scopes of the recursion tree as well.
*/
if (root != NULL){ //if tree is failed to be created then nothing is done to arr
tree_to_set(root->left, arr, i);
arr->array[i] = root->key;
i++;
tree_to_set(root->right, arr, i);
}
return;
}
Node* insert_branch(Node* root, int item){
if(root == NULL){
return newNode(item); //if newnode failed, will pass NULL forward automatically
}
if( Qsmaller(item, root->key) ){
root->left = insert_branch(root->left, item);
} else {
root->right = insert_branch(root->right, item);
}
return root;
}
set* tree_sort(set* arr){ //return the same pointer
Node* root = NULL;
root = insert_branch(root, arr->array[0]);
for( int i=1; i < arr->length; i++){
root = insert_branch(root,arr->array[i]);
}
int j = 0;
tree_to_set(root, arr, j);
tree_delete(root);
return arr;
}
//-----end of treesort part
//-----start of set functions
set* rand_set(int length, int maxstep){ //constructor of sorted random set
int value = 0;
set* ptr = (set*) malloc(sizeof(int)*(length+1));
if(ptr == NULL){
return NULL;
}
(ptr->length) = length;
for(int i=0; i<length;i++){
value += 1+rand()%maxstep;
(ptr->array)[i] = value;
}
return ptr;
};
set* create_set(int length, ...){
va_list valist;
va_start(valist, length);
set* ptr = (set*) malloc(sizeof(int)*(length+1));
if(ptr == NULL){
return NULL;
}
(ptr->length) = length;
for(int i=0; i<length;i++){
(ptr->array)[i] = va_arg(valist, int);
}
va_end(valist);
return ptr;
}
void print_set(set* ptr){
if(ptr == NULL){
printf("\nSomething failed and ptr contains NULL.\n");
} else {
printf("\n 0x%.8X with the size %d \n", ptr, ptr->length);
for(int i=0; i < (ptr->length); i++){
printf("%d ", (ptr->array)[i]);
}
nl;
}
return;
}
bool inside(int num, set* ptr){
if(ptr == NULL){
return false;
}
for (int i = 0; i<ptr->length; i++){
if(num==ptr->array[i]){
return true;
}
}
return false;
}
bool inside_sorted(int num, set* ptr){
if(ptr == NULL){
return false;
}
for (int i = 0; i<ptr->length; i++){
if(num == ptr->array[i]){
return true;
} else if (num < ptr->array[i]) {
return false;
}
}
return false;
}
set* intersect(set* ptr1, set* ptr2){ //pointer to a new set
//O(n^2)
int minlen = (ptr1->length) < (ptr2->length) ? (ptr1->length) : (ptr2->length);
set* newptr = (set*) malloc(sizeof(int)*(minlen+1)); //might allocate bigger than needed, but a minimal safe size: intersection cannot be smaller than the first set
if(newptr == NULL){
return NULL;
}
newptr->length = 0;
for (int i = 0; i<ptr1->length; i++){
if(inside(ptr1->array[i], ptr2)){
newptr->array[newptr->length] = ptr1->array[i]; //because length is used like growing index
newptr->length++;
}
}
return newptr;
}
set* intersect_sorted(set* ptr1, set*ptr2){ //pointer to a new set
// O(n+m)
int minlen = (ptr1->length) < (ptr2->length) ? (ptr1->length) : (ptr2->length);
set* newptr = (set*) malloc(sizeof(int)*(minlen+1));
if(newptr == NULL){
return NULL;
}
int index1 = 0;
int index2 = 0;
newptr->length = 0;
while( index1 < ptr1->length && index2 < ptr2->length ){
if( ptr1->array[index1] < ptr2->array[index2]){
index1++;
} else if( ptr1->array[index1] > ptr2->array[index2]){
index2++;
} else {
newptr->array[newptr->length] = ptr2->array[index2];
index1++;
index2++;
newptr->length++;
}
}
return newptr;
}
set* union_sorted(set* ptr1, set*ptr2){ //pointer to a new set
// O(n+m)
int maxlen = ptr1->length + ptr2->length ;
set* newptr = (set*) malloc(sizeof(int)*(maxlen+1));
if(newptr == NULL){
return NULL;
}
int index1 = 0;
int index2 = 0;
newptr->length = 0;
while( index1 < ptr1->length && index2 < ptr2->length ){
if( ptr1->array[index1] < ptr2->array[index2]){
newptr->array[newptr->length] = ptr1->array[index1];
index1++;
} else if( ptr1->array[index1] > ptr2->array[index2]){
newptr->array[newptr->length] = ptr2->array[index2];
index2++;
} else {
newptr->array[newptr->length] = ptr2->array[index2];
index1++;
index2++;
}
newptr->length++;
}
while(index1 < ptr1->length){
newptr->array[newptr->length] = ptr1->array[index1];
newptr->length++;
index1++;
}
while(index2 < ptr2->length){
newptr->array[newptr->length] = ptr2->array[index2];
newptr->length++;
index2++;
}
return newptr;
}
set* bubble_sort(set* ptr){ //return the same pointer
//O(n^2)
int helper;
for(int i=0; i < ptr->length-1; i++){
for(int j = 0; j < ptr->length-i-1; j++){
if(ptr->array[j] > ptr->array[j+1]){
helper = ptr->array[j];
ptr->array[j] = ptr->array[j+1];
ptr->array[j+1] = helper;
}
}
}
return ptr;
}
set* copy_set(set* ptr){
// O(n)
set* newptr = (set*) malloc(sizeof(int)*(ptr->length+1));
if(newptr == NULL){
return NULL;
}
newptr->length = ptr->length;
for(int i=0; i < newptr->length; i++){
newptr->array[i] = ptr->array[i];
}
return newptr;
}
set* shrink_sorted(set* ptr){ // O(n), return new ptr
int recent=ptr->array[0];
set* newptr = (set*) malloc(sizeof(int)*(ptr->length+1));
if(newptr == NULL){
return NULL;
}
newptr->array[0] = recent;
newptr->length = 1;
for(int i=1; i<ptr->length; i++){
if( ptr->array[i] != recent){
recent = ptr->array[i];
newptr->array[newptr->length] = ptr->array[i];
newptr->length++;
}
}
return newptr;
}
set* shrink2(set* ptr){ //return pointer to a new object
//O(n^2)
set* newptr = (set*) malloc(sizeof(int)*(ptr->length+1));
if(newptr == NULL){
return NULL;
}
newptr->length = 0;
for(int i=0; i<ptr->length; i++){
if( !(inside(ptr->array[i], newptr)) ){
newptr->array[newptr->length] = ptr->array[i];
newptr->length++;
}
}
return newptr;
}
int main(){
srand(time(NULL));
printf("\nRandom set a:");
set* a = rand_set(10,3);
print_set(a);
printf("\nRandom set b:");
set* b = rand_set(10,3);
print_set(b);
printf("\nIntersection of a and b:");
set* c = intersect_sorted(a,b);
print_set(c); free(c);
printf("\nUnion of a and b:");
c = union_sorted(a,b);
print_set(c); free(c);
printf("\n Handcrafted set e:");
set* e = create_set(11, 2,3,2,12,5,5,2,4,5,8,-6);
print_set(e);
#ifdef BUBBLESORT_METHOD
printf("\n Bubble sorted set e:");
e = bubble_sort(e);
print_set(e);
#endif
#ifdef TREESORT_METHOD
printf("\n Tree sorted set e:");
e = tree_sort(e);
print_set(e);
#endif
printf("\n New set c equals shrunk set e:");
c = shrink_sorted(e);
print_set(c);
printf("\n Now that 'cleaned set' c can be used with set functions, because it is sorted and shrunk");
}