본문 바로가기

알고리즘 문제풀이

[Heap]MaxHeap구성

반응형
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
#include <stdio.h>
 
#define NUM_DATA    (8)
#define MAX_DATA    (NUM_DATA)
#define MAX_HEAP    (MAX_DATA+1)
#define LAST_NODE    (MAX_DATA)
 
int heap[MAX_HEAP];
int lastnode = 0;
 
int Heap_Push_Maxheap(int *heap, int sizeint *lastnode, int d)
{
    int n,p,temp;
 
    if(*lastnode == sizereturn 0;
    
    heap[++(*lastnode)] = d;
 
    n = *lastnode; p = n/2;
 
    while(p>0)
    {
        if(heap[n] > heap[p])
        {
            temp = heap[n];
            heap[n] = heap[p];
            heap[p] = temp;
            n = p; p = n / 2;
        }
        else break;
    }
 
    return 1;
}
 
int Heap_Pop_Maxheap(int *heap, int *lastnode, int *d)
{
    int n,c,lc,rc,temp;
 
    if(*lastnode == 0return 0;
 
    *= heap[1];
    heap[1= heap[(*lastnode)--];
 
    n = 1; lc = 2; rc = 3;
    while(lc<=*lastnode)
    {
        if(*lastnode == lc) c = lc;
        else c = (heap[lc]>heap[rc])?(lc):(rc);
    
        if(heap[c]>heap[n])
        {
            temp = heap[c];
            heap[c] = heap[n];
            heap[n] = temp;
            n = c; lc = n*2; rc = lc+1;
        }
        else break;
    }
 
    return 1;
}
cs

" target="_blank" rel="noopener" data-mce-href="http://

 

">http://

 

 

반응형

'알고리즘 문제풀이' 카테고리의 다른 글

[Hash]Open Address 방식  (0) 2020.10.07
[Heap]Heap Sort  (0) 2020.10.07
[HEAP] minheap 구성  (0) 2020.10.07
SegmentTree[구간의 최대값 구하기]  (0) 2020.10.07
[BOJ.16985번] Maaaaaaaaaze  (2) 2019.03.15