Beispiel #1
0
void post_order(struct heap *add_post, int i) {
    if (L_CHILD(i) < add_post->size)
        post_order(add_post, L_CHILD(i));
    if (R_CHILD(i) < add_post->size)
        post_order(add_post, R_CHILD(i));
    printf("%d, ", add_post->ptr_node[i].data);
}
Beispiel #2
0
void in_order(struct heap *add_in, int i) {
    if (L_CHILD(i) < add_in->size)
        in_order(add_in, L_CHILD(i));
    printf("%d, ", add_in->ptr_node[i].data);
    if (R_CHILD(i) < add_in->size)
        in_order(add_in, R_CHILD(i));
}
Beispiel #3
0
void pre_order(struct heap *add_pre, int i) {
    printf("%d, ", add_pre->ptr_node[i].data);
    if (L_CHILD(i) < add_pre->size)
        pre_order(add_pre, L_CHILD(i));
    if (R_CHILD(i) < add_pre->size)
        pre_order(add_pre, R_CHILD(i));
}
Beispiel #4
0
void keep_heap_property(HEAP heap, INFO_OF_NODE info, int p, int rear)
{
    int pos = p; 

    if(L_CHILD(p) < rear && 
       info[heap[L_CHILD(p)]].path_len < info[heap[pos]].path_len) {
    
        pos = L_CHILD(p); 
    }

    if(R_CHILD(p) < rear && 
       info[heap[R_CHILD(p)]].path_len < info[heap[pos]].path_len) {
        
        pos = R_CHILD(p); 

    }

    if(pos != p) {
        
        int tmp = heap[p]; 
        heap[p] = heap[pos]; 
        heap[pos] = tmp; 

        keep_heap_property(heap, info, pos, rear); 
    
    }

    info[heap[p]].heap_pos = p; 
    

}
Beispiel #5
0
void keep_heap_property(HEAP heap[], NODE_TAG node_tag, int heap_pos, int rear) 
{
    int mark = heap_pos;
    int vertex = heap[heap_pos].vertex;

    if(L_CHILD(heap_pos) < rear && 
       heap[mark].key > heap[L_CHILD(heap_pos)].key) {
        
        mark = L_CHILD(heap_pos); 
    }

    if(R_CHILD(heap_pos) < rear && 
       heap[mark].key > heap[R_CHILD(heap_pos)].key) {
        
        mark = R_CHILD(heap_pos); 
    }
   
    if(mark != heap_pos) {
        
        HEAP tmp = heap[heap_pos]; 
        heap[heap_pos] = heap[mark]; 
        heap[mark] = tmp;

        keep_heap_property(heap, node_tag, mark, rear); 
    }

    node_tag[heap[heap_pos].vertex].heap_pos = heap_pos; 

    
}
Beispiel #6
0
void heapify(struct heap *add_fy, int i) {
    int largest;
    
    if (L_CHILD(i) < add_fy->size && add_fy->ptr_node[L_CHILD(i)].data > add_fy->ptr_node[i].data)
        largest = L_CHILD(i);
    else
        largest = i;
    if (R_CHILD(i) < add_fy->size && add_fy->ptr_node[R_CHILD(i)].data > add_fy->ptr_node[largest].data)   
        largest = R_CHILD(i);
        
    if (largest != i) {
        swap(&(add_fy->ptr_node[i]), &(add_fy->ptr_node[largest]));
        heapify(add_fy, largest);
    }
}