diff --git a/Exercise 2 - Heap (Week 3)/Ex2.pdf b/Exercise 2 - Heap (Week 3)/Ex2.pdf new file mode 100644 index 0000000..adda9e3 Binary files /dev/null and b/Exercise 2 - Heap (Week 3)/Ex2.pdf differ diff --git a/Exercise 2 - Heap (Week 3)/Ex2.txt b/Exercise 2 - Heap (Week 3)/Ex2.txt new file mode 100644 index 0000000..7cad601 --- /dev/null +++ b/Exercise 2 - Heap (Week 3)/Ex2.txt @@ -0,0 +1,97 @@ +177 +284 +870 +319 +47 +312 +14 +885 +662 +467 +955 +269 +719 +169 +434 +306 +300 +253 +65 +373 +37 +99 +132 +27 +30 +835 +242 +221 +957 +38 +582 +837 +177 +126 +308 +963 +808 +382 +633 +489 +426 +406 +159 +579 +899 +47 +784 +668 +88 +359 +163 +688 +544 +415 +581 +796 +903 +856 +285 +948 +803 +679 +567 +911 +38 +491 +451 +263 +473 +48 +818 +378 +729 +446 +218 +534 +130 +673 +269 +113 +940 +644 +494 +275 +31 +832 +658 +932 +210 +502 +783 +248 +914 +283 +753 +739 +43 diff --git a/Exercise 2 - Heap (Week 3)/Ex2_Prof_Solution.pdf b/Exercise 2 - Heap (Week 3)/Ex2_Prof_Solution.pdf new file mode 100644 index 0000000..5e395a1 Binary files /dev/null and b/Exercise 2 - Heap (Week 3)/Ex2_Prof_Solution.pdf differ diff --git a/Exercise 2 - Heap (Week 3)/ex2.c b/Exercise 2 - Heap (Week 3)/ex2.c new file mode 100644 index 0000000..fc4a030 --- /dev/null +++ b/Exercise 2 - Heap (Week 3)/ex2.c @@ -0,0 +1,195 @@ +/* License: AGPLv3 or later. https://www.gnu.org/licenses/licenses.html + * + * Exercise 2 - Implementing a heap (Week 3) + * Name: Manish + * Student Login: ***** + * + * Compile as: + * $ gcc -std=c11 -Wall -o ex2 ex2.c + */ + +// assert used only for testing heap property +// #include +#include +#include +#include + +int heap[100]; +int size = 0; + +void makeheap(); +void shiftdown(int index); +// void shiftup(int index); +// void swap(int i, int j); +// int min(int i, int j); +// void test_heap(); +// int is_bigger_or_equal(int bigger, int smaller); + +int main(void) +{ + printf("Enter file name: "); + char filename[257]; + // Assuming filename/file path won't be longer than 256 characters + scanf("%256s", filename); + + FILE* file = fopen(filename, "r"); + if (!file) + { + perror(filename); + exit(EXIT_FAILURE); + } + + // Trailing whitespace to consume all subsequent ' ', \t, \n, \r + while (fscanf(file, " %d ", &heap[size]) != EOF) + { + size++; + } + + fclose(file); + + makeheap(); + + // test_heap(); + + // int upto = min(5, size); + // Assuming at least 5 integers will be read (else segfault!) + for (int i = 0; i < 5; i++) + { + printf("%d ", heap[i]); + } + printf("\n"); + + return 0; +} + +void makeheap() +{ + // Shiftdown Method + int shiftdowns_required = (size / 2)-1; + for (int i = shiftdowns_required; i >= 0; i--) + { + shiftdown(i); + } + + // Shiftup method + /*for (int i = 0; i < size; i++) + { + shiftup(i); + }*/ +} + +void shiftdown(int index) +{ + int child; + int temp; // used for swapping + // Loop instead of recursion for efficiency + while (1) + { + child = (index * 2) + 1; + // Reached end of tree + if (child >= size) + { + return; + } + // Has only one child + if (child + 1 >= size) + { + } + // Has both children + else + { + // Pick the bigger child + if (heap[child] < heap[child + 1]) + { + child = child + 1; + } + } + if (heap[index] < heap[child]) + { + // swap + temp = heap[index]; + heap[index] = heap[child]; + heap[child] = temp; + + index = child; + } + else + { + return; + } + } +} +/* +void shiftup(int index) +{ + int parent; + int temp; // used for swapping + while (1) + { + if (index == 0) + { + return; + } + parent = (index - 1) / 2; + if (parent >= 0) + { + if (heap[parent] < heap[index]) + { + // swap + temp = heap[index]; + heap[index] = heap[parent]; + heap[parent] = temp; + + index = parent; + } + else + { + return; + } + } + else + { + return; + } + } +}*/ +/* +inline void swap(int i, int j) +{ + int temp = heap[i]; + heap[i] = heap[j]; + heap[j] = temp; +}*/ +/* +inline int min(int i, int j) +{ + if (i < j) + { + return i; + } + return j; +}*/ +/* +void test_heap() +{ + int checks_required = (size/2)+1; + int child; + for (int i=0; i < checks_required; i++) + { + // Left child + child = (i*2)+1; + assert(is_bigger_or_equal(i, child)); + // Right child + assert(is_bigger_or_equal(i, ++child)); + } +} + +inline int is_bigger_or_equal(int bigger, int smaller) +{ + if (bigger < size && smaller < size) + { + return (heap[bigger] >= heap[smaller]); + } + return 1; +} +*/ diff --git a/Exercise 2 - Heap (Week 3)/makefile b/Exercise 2 - Heap (Week 3)/makefile new file mode 100644 index 0000000..05f3cf9 --- /dev/null +++ b/Exercise 2 - Heap (Week 3)/makefile @@ -0,0 +1,2 @@ +ex2: ex2.c + gcc -Wall -std=c11 -o ex2 ex2.c