Heap

ํž™(heap)์€ ์ตœ๋Œ“๊ฐ’ ๋ฐ ์ตœ์†Ÿ๊ฐ’์„ ์ฐพ์•„๋‚ด๋Š” ์—ฐ์‚ฐ์„ ๋น ๋ฅด๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ๊ณ ์•ˆ๋œ ์™„์ „์ด์ง„ํŠธ๋ฆฌ(Complete binary tree)๋ฅผ ๊ธฐ๋ณธ์œผ๋กœ ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ(tree-based structure)์ด๋‹ค. (์œ„ํ‚ค๋ฐฑ๊ณผ )

์‘์šฉ

  1. Graph Algorithm : Dijkstra, Prim's Minimum Spanning Tree(ํ”„๋ฆผ ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ)

๊ทธ๋ฆฌ๊ณ  ๋‹ค์–‘ํ•œ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š”๋ฐ์—์„œ ์‚ฌ์šฉ๋œ๋‹ค.

๊ทœ์น™

์ตœ์†Œ ํž™(min heap)

๋ถ€๋ชจ๋…ธ๋“œ์˜ ํ‚ค๊ฐ’์ด ์ž์‹๋…ธ๋“œ์˜ ํ‚ค๊ฐ’๋ณด๋‹ค ํ•ญ์ƒ ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. key(๋ถ€๋ชจ๋…ธ๋“œ) < key(์ž์‹๋…ธ๋“œ)

์ตœ๋Œ€ ํž™(max heap)

๋ถ€๋ชจ๋…ธ๋“œ์˜ ํ‚ค๊ฐ’์ด ์ž์‹๋…ธ๋“œ์˜ ํ‚ค๊ฐ’๋ณด๋‹ค ํ•ญ์ƒ ํฌ๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. key(๋ถ€๋ชจ๋…ธ๋“œ) > key(์ž์‹๋…ธ๋“œ)

๋”ฐ๋ผ์„œ ๋ฃจํŠธ๋…ธ๋“œ์—๋Š” ํ•ญ์ƒ ๊ฐ€์žฅ ํฐ ๊ฐ’์ด๋‚˜ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์ด ์ €์žฅ๋˜์–ด ์žˆ๊ธฐ๋•Œ๋ฌธ์— ์ตœ๋Œ€๊ฐ’ ๋˜๋Š” ์ตœ์†Ÿ๊ฐ’์„ O(1)๋งŒ์— ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํ‚ค๊ฐ’์€ ์˜ค๋กœ์ง€ ๋ถ€๋ชจ ์ž์‹๋…ธ๋“œ๊ฐ„์—๋งŒ ์„ฑ๋ฆฝํ•œ๋‹ค.

Height/Depth

n๊ฐœ์˜ ๋…ธ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” heap์˜ ๋†’์ด๋Š” O(logn)์ด๋‹ค. ํž™์€ ์™„์ „์ด์ง„ํŠธ๋ฆฌ์ด๋ฏ€๋กœ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๋กœ ๊นŠ์ด์™€ ๋†’์ด๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค.

๋งˆ์ง€๋ง‰ level์„ ์ œ์™ธํ•˜๊ณ ๋Š” ๊ฐ ๋ ˆ๋ฒจ i์— 2^i๊ฐœ์˜ ๋…ธ๋“œ๊ฐ€ ์กด์žฌํ•œ๋‹ค.

๊ตฌํ˜„

๋ฐฐ์—ด

ํž™์€ ์™„์ „์ด์ง„ํŠธ๋ฆฌ์ด๋ฏ€๋กœ ๊ฐ ๋…ธ๋“œ์— ๋ฒˆํ˜ธ๋ฅผ ๋ถ™์ผ ์ˆ˜ ์žˆ๋‹ค. ๋ฐฐ์—ด์€ ์™„์ „์ด์ง„ํŠธ๋ฆฌ๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ์— ๋งค์šฐ ํšจ์œจ์ ์ด๋‹ค.

๋ฐฐ์—ด๋กœ ํ‘œํ˜„ํ•จ์œผ๋กœ์จ ๋ถ€๋ชจ๋…ธ๋“œ์™€ ์ž์‹๋…ธ๋“œ๋ฅผ ์ฐพ๊ธฐ๊ฐ€ ์‰ฌ์›Œ์ง„๋‹ค.

  • ์™ผ์ชฝ ์ž์‹๋…ธ๋“œ ์ธ๋ฑ์Šค : 2*(๋ถ€๋ชจ๋…ธ๋“œ ์ธ๋ฑ์Šค)

  • ์˜ค๋ฅธ์ชฝ ์ž์‹๋…ธ๋“œ 2*(๋ถ€๋ชจ๋…ธ๋“œ ์ธ๋ฑ์Šค)+1

  • ๋…ธ๋“œ x์˜ ๋ถ€๋ชจ ๋…ธ๋“œ ์ธ๋ฑ์Šค๋Š” (์ž์‹๋…ธ๋“œ ์ธ๋ฑ์Šค)/2๋กœ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

์ฃผ์˜์‚ฌํ•ญ index๋Š” 1๋ถ€ํ„ฐ ์‹œ์ž‘!ํ•œ๋‹ค๋Š” ์ ์„ ์ฃผ์˜ํ•ด์•ผํ•œ๋‹ค.

์‚ฝ์ž…(upheap)

  1. ๊ฐ€์žฅ ๋์˜ ์ž๋ฆฌ์— ๋…ธ๋“œ๋ฅผ ์‚ฝ์ž…ํ•œ๋‹ค.

  2. ๊ทธ ๋…ธ๋“œ์™€ ๋ถ€๋ชจ ๋…ธ๋“œ๋ฅผ ์„œ๋กœ ๋น„๊ตํ•œ๋‹ค.

  3. ๊ทœ์น™(์ตœ์†Œํž™, ์ตœ๋Œ€ํž™)์— ๋งž์œผ๋ฉด ๊ทธ๋Œ€๋กœ ๋‘๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๋ถ€๋ชจ์™€ ๊ตํ™˜ํ•œ๋‹ค.

  4. ๊ทœ์น™์— ๋งž์„ ๋•Œ๊นŒ์ง€ 1~3๋ฒˆ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

์‹œ๊ฐ„๋ณต์žก๋„

  • O(logn)

์‚ญ์ œ(downheap)

์ตœ๋Œ“๊ฐ’ ํ˜น์€ ์ตœ์†Ÿ๊ฐ’์ด ์ €์žฅ๋œ ๋ฃจํŠธ ๋…ธ๋“œ๋งŒ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค.

  1. ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.

  2. ๋ฃจํŠธ ์ž๋ฆฌ์— ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋ฅผ ์‚ฝ์ž…ํ•œ๋‹ค.

  3. ์˜ฌ๋ผ๊ฐ„ ๋…ธ๋“œ์™€ ๊ทธ์˜ ์ž์‹ ๋…ธ๋“œ(๋“ค)์™€ ๋น„๊ตํ•œ๋‹ค.

  4. ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜๋ฉด ๊ทธ๋Œ€๋กœ ๋‘๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ž์‹๊ณผ ๊ตํ™˜ํ•œ๋‹ค.

  5. ์ตœ๋Œ€ ํž™ 1. ๋ถ€๋ชจ๋ณด๋‹ค ๋” ํฐ ์ž์‹์ด ์—†์œผ๋ฉด ๊ตํ™˜ํ•˜์ง€ ์•Š๊ณ  ๋๋‚ธ๋‹ค. 2. ๋ถ€๋ชจ๋ณด๋‹ค ๋” ํฐ ์ž์‹์ด ํ•˜๋‚˜๋งŒ ์žˆ์œผ๋ฉด ๊ทธ ์ž์‹ํ•˜๊ณ  ๊ตํ™˜ํ•˜๋ฉด ๋œ๋‹ค. 3. ๋ถ€๋ชจ๋ณด๋‹ค ๋” ํฐ ์ž์‹์ด ๋‘˜ ์žˆ์œผ๋ฉด ์ž์‹๋“ค ์ค‘ ํฐ ๊ฐ’๊ณผ ๊ตํ™˜ํ•œ๋‹ค.

  6. ์ตœ์†Œ ํž™ 1. ๋ถ€๋ชจ๋ณด๋‹ค ๋” ์ž‘์€ ์ž์‹์ด ์—†์œผ๋ฉด ๊ตํ™˜ํ•˜์ง€ ์•Š๊ณ  ๋๋‚ธ๋‹ค. 2. ๋ถ€๋ชจ๋ณด๋‹ค ๋” ์ž‘์€ ์ž์‹์ด ํ•˜๋‚˜๋งŒ ์žˆ์œผ๋ฉด ๊ทธ ์ž์‹ํ•˜๊ณ  ๊ตํ™˜ํ•˜๋ฉด ๋œ๋‹ค. 3. ๋ถ€๋ชจ๋ณด๋‹ค ๋” ์ž‘์€ ์ž์‹์ด ๋‘˜ ์žˆ์œผ๋ฉด ์ž์‹๋“ค ์ค‘ ์ž‘์€ ๊ฐ’๊ณผ ๊ตํ™˜ํ•œ๋‹ค.

  7. ์กฐ๊ฑด์„ ๋งŒ์กฑํ•  ๋•Œ๊นŒ์ง€ 4์˜ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

์‹œ๊ฐ„๋ณต์žก๋„

  • O(logn)

๊ตฌํ˜„

์ตœ์†Œํž™

#include <stdio.h>
#define MAX 1000000

typedef struct{
    int heap[MAX];
    int hsize;
}Heap;

void init(Heap * h){
    h->hsize=0;
}

void insert(Heap * h, int key){
    int p; //ํ˜„์žฌ ์œ„์น˜ ์ €์žฅ

    // ๋งˆ์ง€๋ง‰ ์œ„์น˜์— ์ถ”๊ฐ€
    h->heap[++(h->hsize)]=key;

    // ์œ„์น˜ ์กฐ์ •
    p=h->hsize;
    while((h->heap[p/2])>key){
        h->heap[p] = h->heap[p/2];
        p/=2;
    }
    h->heap[p]=key;
}

int delete(Heap * h){
    int min,last,child,i;

    //๋ฃจํŠธ๋…ธ๋“œ๊ฐ’์ด ์ตœ์†Œ
    min = h->heap[1];
    last = h->heap[(h->hsize)--];

    for(i=1; i*2<=h->hsize; i=child){
        child = i*2;
        if(child!=h->hsize && h->heap[child+1]<h->heap[child])child++;
        if(last>h->heap[child])h->heap[i]=h->heap[child];
        else break;
    }
    h->heap[i]=last;
    return min;
}


int main(int argc, const char * argv[]) {

    Heap heap;
    int min;
    init(&heap);
    insert(&heap, 10);
    insert(&heap, 5);
    insert(&heap, 30);

    for(int i=1;i<=heap.hsize;i++){
        printf("%d ",heap.heap[i]);
    }
    min = delete(&heap);
    printf("\n%d \n",min);
    for(int i=1;i<=heap.hsize;i++){
        printf("%d ",heap.heap[i]);
    }
    min = delete(&heap);
    printf("\n%d \n",min);
    for(int i=1;i<=heap.hsize;i++){
        printf("%d ",heap.heap[i]);
    }


    return 0;
}

์ตœ๋Œ€ํž™

#include <stdio.h>
#define MAX 100

typedef struct{
    int heap[MAX];
    int hsize;
}Heap;

void init(Heap * h){
    h->hsize=0;
}

void insert_max(Heap * h, int key){
    int p; //ํ˜„์žฌ ์œ„์น˜ ์ €์žฅ

    p=++(h->hsize);
    // ๋งˆ์ง€๋ง‰ ์œ„์น˜์— ์ถ”๊ฐ€
    h->heap[p]=key;


    // ์œ„์น˜ ์กฐ์ •
    while((h->heap[p/2])<key){
        if(p==1)break;
        h->heap[p] = h->heap[p/2];
        p/=2;
    }
    h->heap[p]=key;
}
int delete_max(Heap * h){
    int max,last,child,i;

    //๋ฃจํŠธ๋…ธ๋“œ๊ฐ’์ด ์ตœ์†Œ
    max = h->heap[1];
    last = h->heap[(h->hsize)--];

    for(i=1; i*2<=h->hsize; i=child){
        child = i*2;
        if(child!=h->hsize && h->heap[child+1]>h->heap[child])child++;
        if(last<h->heap[child])h->heap[i]=h->heap[child];
        else break;
    }
    h->heap[i]=last;
    return max;
}


int main(int argc, const char * argv[]) {

    Heap heap;
    int max;
    init(&heap);
    insert_max(&heap, 10);
    insert_max(&heap, 5);
    insert_max(&heap, 30);

    for(int i=1;i<=heap.hsize;i++){
        printf("%d ",heap.heap[i]);
    }
    max = delete_max(&heap);
    printf("\n%d \n",max);
    for(int i=1;i<=heap.hsize;i++){
        printf("%d ",heap.heap[i]);
    }
    max = delete_max(&heap);
    printf("\n%d \n",max);
    for(int i=1;i<=heap.hsize;i++){
        printf("%d ",heap.heap[i]);
    }


    return 0;
}

Last updated