void add_to_heap(node_heap *root, int data) { if (data < root->data) { if (root->l == NULL) { node_heap *temp_node_heap = (node_heap*)malloc(sizeof(node_heap)); temp_node_heap->data = data; temp_node_heap->l = 0; temp_node_heap->r = 0; root->l = temp_node_heap; } else add_to_heap(root->l, data); } else { if (root->r == NULL) { node_heap *temp_node_heap = (node_heap*)malloc(sizeof(node_heap)); temp_node_heap->data = data; temp_node_heap->l = 0; temp_node_heap->r = 0; root->r = temp_node_heap; } else add_to_heap(root->r, data); } }
int main() { n = sizeof(arr)/sizeof(arr[0]); create_heap(); print(); int x; x = eliminate(); printf("%d\n", x); print(); x = eliminate(); printf("%d\n", x); print(); add_to_heap(); print(); return(0); };
/* * main function */ void main(){ int i = 0; /* * allocate an array of type struct data */ struct data *array = NULL; array = (struct data *)malloc(sizeof(struct data) * MAX_ELEM); if(array == NULL){ printf("Failed to allocate memory\n"); return; } /* * initialize the input array to hold '\0' values */ memset(array, '\0', sizeof(array)); struct data temp; /* * generate some random integer values as input to the tree */ srand(time(NULL)); for(i = 0; i < MAX_ELEM; i++){ temp.key = rand() % 100; add_to_heap(array, &temp, i); } /* display the heap */ printf("Initial max heap represented as an array\n"); print_the_heap(array); /* call heapsort function */ array = heapsort(array, MAX_ELEM); /* display the heap */ printf("Sorted array\n"); print_the_heap(array); }
void node_to_heap(int inode, float cost, int prev_node, int prev_edge, float backward_path_cost, float R_upstream) { /* Puts an rr_node on the heap, if the new cost given is lower than the * * current path_cost to this channel segment. The index of its predecessor * * is stored to make traceback easy. The index of the edge used to get * * from its predecessor to it is also stored to make timing analysis, etc. * * easy. The backward_path_cost and R_upstream values are used only by the * * timing-driven router -- the breadth-first router ignores them. */ struct s_heap *hptr; if(cost >= rr_node_route_inf[inode].path_cost) return; hptr = alloc_heap_data(); hptr->index = inode; hptr->cost = cost; hptr->u.prev_node = prev_node; hptr->prev_edge = prev_edge; hptr->backward_path_cost = backward_path_cost; hptr->R_upstream = R_upstream; add_to_heap(hptr); }
void test_two_heap_entry() { printf("Testing two heap entry... "); create_heap(); add_to_heap(7); add_to_heap(3); print_heap(); int max = peek_heap(); if (max != 7) { printf("ERROR: Expected 7, but got %d\n", max); } else { printf("Pass\n"); } destroy_heap(); }
int sel_start_timer(sel_timer_t *timer, struct timeval *timeout) { if (timer->in_heap) return EBUSY; timer->timeout = *timeout; add_to_heap(&(timer->sel->timer_top), &(timer->sel->timer_last), timer); timer->in_heap = 1; return 0; }
void test_many_heap_entry() { printf("Testing many heap entry... "); create_heap(); for (int i = 100; i > 0; i -= 1) { add_to_heap(i); } print_heap(); int max = peek_heap(); if (max != 100) { printf("ERROR: Expected 100, but got %d\n", max); } else { printf("Pass\n"); } destroy_heap(); }