#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#define MAXSIZE 10
typedef struct {
int heapAry[MAXSIZE];
int last;
int size;
int maxSize;
}HEAP;
HEAP* heapCreate (int maxSize){
HEAP *heap = (HEAP*)malloc (sizeof (HEAP));
if(!heap) return NULL;
heap->last = -1; //the index (works as top in stack)
heap->size = 0;
//wtf?
heap->maxSize = (int) pow(2, ceil(log2(maxSize))) - 1;
//force heap size to power of 2-1
int array[maxSize];
//allocate the array[maxSize] times size of void*
return heap;
}
void reheapUp(HEAP* heap, int childLoc){
int parent = 0; // start a traverse from the parent
int hold = 0;
//if child is root node, right goes to return
if(childLoc){
//if the child is not null == not root node
parent = (childLoc - 1) / 2;
if(heap->heapAry[childLoc] > heap->heapAry[parent]){
//if compare returns 1 or true,
//it means child is bigger than parent
//child is greater than parent == swap
hold = heap->heapAry[parent];
heap->heapAry[parent] = heap->heapAry[childLoc];
heap->heapAry[childLoc] = hold;
reheapUp(heap, parent);
//now the child which was bigger than parent is in parent
//we compare new parent to compare its parent to find the order
//in the heap by calling this function again
}
//if c-p is in right order, return
}
return;
}
//
//void reheapDown(HEAP* heap, int root){
// int hold = NULL;
// int leftData = NULL;
// int rightData = NULL;
// int largeLoc = 0;
// int last = 0;
//
// last = heap->last;
//
// if((root*2 + 1) <= last){//left subtree there is at least one child
// leftData = heap->heapAry[root * 2 + 1];
//
//
// if((root * 2 + 2) <= last){
// rightData = heap->heapAry[root * 2 + 2];
// }
//
// else {
// rightData = NULL;
// }
//
// if(!rightData ||
// leftData > rightData){
// //qhwn leftdata is bigger than right data
// largeLoc = root * 2 + 1;
// }
//
// else{
// //if there is right data or right data is bigger than left one
// largeLoc = root * 2 + 2;
// }
//
// if(heap->heapAry[root] < heap->heapAry[largeLoc]){
// //if largeLoc is bigger than parent, need to swap down
// hold = heap->heapAry[root];
// heap->heapAry[root] = heap->heapAry[largeLoc];
// heap->heapAry[largeLoc] = hold;
// reheapDown(heap, largeLoc);
//
// }
// }
// return;
//}
bool heapInsert(HEAP* heap, int dataPtr){
if(heap->size >= heap->maxSize){
//the most recent one's index is out of boundary
return false;
}
++(heap->last);
++(heap->size);
heap->heapAry[heap->last] = dataPtr;
// printf("just added %d and saved is \n", dataPtr,
// heap->heapAry[heap->last]);
reheapUp(heap, heap->last);
return true;
}
//
//bool heapDelete(HEAP* heap, int* dataOutPtr){
// if(heap->size == 0) return false;
//
// *dataOutPtr = heap->heapAry[0];
// heap->heapAry[0] = heap->heapAry[heap->last];
// (heap->last)--;
// (heap->size)--;
// reheapDown(heap,0);
// return true;
// //nothing to delete, return
//}
void Print(HEAP* heap){
for(int i = 0; i < MAXSIZE - 1; i++){
printf("%d", heap->heapAry[i]);
if(heap->heapAry[i] != 35) printf(", ");
else printf("\n");
}
}
int main(){
int arr[10] = {10,12, 89,236, 758, 45,21,96,35};
HEAP* myheap = heapCreate(MAXSIZE);
for(int i = 0; i < MAXSIZE; i++){
heapInsert(myheap, arr[i]);
}
printf("Contents of the Max-heap:\n");
Print(myheap);
return 0;
}
/*Not everything works and failed to use void ** since I didnt know how to implement it using type casting but i passed the assignment so whatever!!*/
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#define MAXSIZE 10
typedef struct {
int heapAry[MAXSIZE];
int last;
int size;
int maxSize;
}HEAP;
HEAP* heapCreate (int maxSize){
HEAP *heap = (HEAP*)malloc (sizeof (HEAP));
if(!heap) return NULL;
heap->last = -1; //the index (works as top in stack)
heap->size = 0;
//wtf?
heap->maxSize = (int) pow(2, ceil(log2(maxSize))) - 1;
//force heap size to power of 2-1
int array[maxSize];
//allocate the array[maxSize] times size of void*
return heap;
}
void reheapUp(HEAP* heap, int childLoc){
int parent = 0; // start a traverse from the parent
int hold = 0;
//if child is root node, right goes to return
if(childLoc){
//if the child is not null == not root node
parent = (childLoc - 1) / 2;
if(heap->heapAry[childLoc] > heap->heapAry[parent]){
//if compare returns 1 or true,
//it means child is bigger than parent
//child is greater than parent == swap
hold = heap->heapAry[parent];
heap->heapAry[parent] = heap->heapAry[childLoc];
heap->heapAry[childLoc] = hold;
reheapUp(heap, parent);
//now the child which was bigger than parent is in parent
//we compare new parent to compare its parent to find the order
//in the heap by calling this function again
}
//if c-p is in right order, return
}
return;
}
//
//void reheapDown(HEAP* heap, int root){
// int hold = NULL;
// int leftData = NULL;
// int rightData = NULL;
// int largeLoc = 0;
// int last = 0;
//
// last = heap->last;
//
// if((root*2 + 1) <= last){//left subtree there is at least one child
// leftData = heap->heapAry[root * 2 + 1];
//
//
// if((root * 2 + 2) <= last){
// rightData = heap->heapAry[root * 2 + 2];
// }
//
// else {
// rightData = NULL;
// }
//
// if(!rightData ||
// leftData > rightData){
// //qhwn leftdata is bigger than right data
// largeLoc = root * 2 + 1;
// }
//
// else{
// //if there is right data or right data is bigger than left one
// largeLoc = root * 2 + 2;
// }
//
// if(heap->heapAry[root] < heap->heapAry[largeLoc]){
// //if largeLoc is bigger than parent, need to swap down
// hold = heap->heapAry[root];
// heap->heapAry[root] = heap->heapAry[largeLoc];
// heap->heapAry[largeLoc] = hold;
// reheapDown(heap, largeLoc);
//
// }
// }
// return;
//}
bool heapInsert(HEAP* heap, int dataPtr){
if(heap->size >= heap->maxSize){
//the most recent one's index is out of boundary
return false;
}
++(heap->last);
++(heap->size);
heap->heapAry[heap->last] = dataPtr;
// printf("just added %d and saved is \n", dataPtr,
// heap->heapAry[heap->last]);
reheapUp(heap, heap->last);
return true;
}
//
//bool heapDelete(HEAP* heap, int* dataOutPtr){
// if(heap->size == 0) return false;
//
// *dataOutPtr = heap->heapAry[0];
// heap->heapAry[0] = heap->heapAry[heap->last];
// (heap->last)--;
// (heap->size)--;
// reheapDown(heap,0);
// return true;
// //nothing to delete, return
//}
void Print(HEAP* heap){
for(int i = 0; i < MAXSIZE - 1; i++){
printf("%d", heap->heapAry[i]);
if(heap->heapAry[i] != 35) printf(", ");
else printf("\n");
}
}
int main(){
int arr[10] = {10,12, 89,236, 758, 45,21,96,35};
HEAP* myheap = heapCreate(MAXSIZE);
for(int i = 0; i < MAXSIZE; i++){
heapInsert(myheap, arr[i]);
}
printf("Contents of the Max-heap:\n");
Print(myheap);
return 0;
}
/*Not everything works and failed to use void ** since I didnt know how to implement it using type casting but i passed the assignment so whatever!!*/
댓글
댓글 쓰기